Commit 000f6cda authored by Marco Descher's avatar Marco Descher
Browse files

[16303] Refactor IElexisEnvironmentService

parent fe24708c
Loading
Loading
Loading
Loading
+29 −0
Original line number Original line Diff line number Diff line
package ch.elexis.core.services.eenv;

import ch.elexis.core.eenv.IElexisEnvironmentService;
import ch.elexis.core.services.holder.ConfigServiceHolder;

public class ElexisEnvironmentService implements IElexisEnvironmentService {
	
	private String elexisEnvironmentHost;
	
	public ElexisEnvironmentService(String elexisEnvironmentHost){
		this.elexisEnvironmentHost = elexisEnvironmentHost;
	}
	
	@Override
	public String getVersion(){
		return "unused_unimplemented";
	}
	
	@Override
	public String getProperty(String key){
		return ConfigServiceHolder.get().get(key, null);
	}
	
	@Override
	public String getHostname(){
		return elexisEnvironmentHost;
	}
	
}
+79 −0
Original line number Original line Diff line number Diff line
package ch.elexis.core.services.eenv;

import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.constants.ElexisEnvironmentPropertyConstants;
import ch.elexis.core.constants.ElexisSystemPropertyConstants;
import ch.elexis.core.eenv.IElexisEnvironmentService;
import ch.elexis.core.services.IConfigService;

/**
 * Programmatically register the {@link ElexisEnvironmentService} if conditions are met. That is, 
 * an Elexis-Environment host value is passed.
 */
@Component
public class ElexisEnvironmentServiceActivator {
	
	private ServiceRegistration<IElexisEnvironmentService> serviceRegistration;
	
	private String elexisEnvironmentHost;
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Reference
	private IConfigService configService;
	
	@Activate
	public void activate(){
		// 1. try via system property
		elexisEnvironmentHost =
			System.getProperty(ElexisSystemPropertyConstants.EE_HOSTNAME);
		// 2. if empty fetch via environment variable
		if(StringUtils.isBlank(elexisEnvironmentHost)) {
			elexisEnvironmentHost = System.getenv(ElexisEnvironmentPropertyConstants.EE_HOSTNAME);
		}
		// 3. if empty fetch via config service
		if (StringUtils.isBlank(elexisEnvironmentHost)) {
			elexisEnvironmentHost =
				configService.get(IElexisEnvironmentService.CFG_EE_HOSTNAME, null);
		}
		
		if (StringUtils.isNotBlank(elexisEnvironmentHost)) {
	
			try {
				// activate the service
				ElexisEnvironmentService elexisEnvironmentService = new ElexisEnvironmentService(
					elexisEnvironmentHost);
				serviceRegistration =
					FrameworkUtil.getBundle(ElexisEnvironmentServiceActivator.class)
						.getBundleContext().registerService(IElexisEnvironmentService.class,
							elexisEnvironmentService, null);
				
				log.info("Bound to elexis-environment v{} on [{}]",
					elexisEnvironmentService.getVersion(), elexisEnvironmentService.getHostname());
				
			} catch (Exception e) {
				log.warn("Initializing elexis-environment failed", e);
			}
		} else {
			log.debug("No elexis-environment configured");
		}
		
	}
	
	@Deactivate
	public void deactivate(){
		if (serviceRegistration != null) {
			serviceRegistration.unregister();
		}
	}
	
}
+4 −0
Original line number Original line Diff line number Diff line
/**
 * Elexis-Environment specific services. 
 */
package ch.elexis.core.services.eenv;
 No newline at end of file
+6 −1
Original line number Original line Diff line number Diff line
@@ -18,4 +18,9 @@ public class ElexisEnvironmentPropertyConstants {
	public static String DB_PASSWORD = "DB_PASSWORD";
	public static String DB_PASSWORD = "DB_PASSWORD";
	
	
	public static String DB_JDBC_PARAMETER_STRING = "DB_JDBC_PARAMETER_STRING";
	public static String DB_JDBC_PARAMETER_STRING = "DB_JDBC_PARAMETER_STRING";
	
	/**
	 * Elexis-Environment: Entry hostname of the elexis environment
	 */
	public static String EE_HOSTNAME = "EE_HOSTNAME";
}
}
+6 −0
Original line number Original line Diff line number Diff line
@@ -32,4 +32,10 @@ public class ElexisSystemPropertyConstants {
	 * REST URL of the elexis server, e.g. http://localhost:8380/services"
	 * REST URL of the elexis server, e.g. http://localhost:8380/services"
	 */
	 */
	public static final String ELEXIS_SERVER_REST_INTERFACE_URL = "elexisServerUrl";
	public static final String ELEXIS_SERVER_REST_INTERFACE_URL = "elexisServerUrl";
	
	/**
	 * Elexis-Environment: Entry hostname of the elexis environment. If passed as system property,
	 * overrides both environment variable and Config stored value.
	 */
	public static final String EE_HOSTNAME = "EE_HOSTNAME";
}
}
Loading