package com.kuebler.osgi.ds.equinox;

import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;

/**
 *
 * Add additional information to augment a property for the application being debugged
 *
 */
@Component
public class EquinoxAppConfigurator {

	@Activate
	public void activate() throws IOException {
		ConfigurationAdmin configurationAdmin = _configAdmin.get();
		Configuration configuration = configurationAdmin.getConfiguration(EquinoxSCR.class.getName());
		Dictionary<String, Object> properties = new Hashtable<>();
		String application = System.getProperty("eclipse.application");
		String product = System.getProperty("eclipse.product");
		
		if(application != null) {
			properties.put("eclipse.application", application);
		}
		if(product != null) {
			properties.put("eclipse.product", product);
		}
		configuration.update(properties);
	}

	
	private AtomicReference<ConfigurationAdmin> _configAdmin = new AtomicReference<ConfigurationAdmin>();

	  /**
	   * OSGi bind method
	   * @param name
	   */
	  @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
	  public void bindConfigAdmin(ConfigurationAdmin configAdmin) {
	    _configAdmin.set(configAdmin);
	  }

	  /**
	   * OSGi unbind method
	   * @param name
	   */
	  public void unbindConfigAdmin(ConfigurationAdmin name) {
	    _configAdmin.compareAndSet(name, null);
	  }}

@@ -125,38 +125,8 @@
 e;%0D%0A
-import java.util.Properties;%0D%0A
 impo
@@ -1141,16 +1141,83 @@
 uct%22);%0D%0A
+%09%09String instanceArea = System.getProperty(%22osgi.instance.area%22);%0D%0A
 %09%09%0D%0A%09%09if
@@ -1380,16 +1380,107 @@
 ;%0D%0A%09%09%7D%0D%0A
+%09%09if(instanceArea != null) %7B%0D%0A%09%09%09properties.put(%22osgi.instance.area%22, instanceArea);%0D%0A%09%09%7D%0D%0A
 %09%09config

add additional variables