Commit 000f6cda authored by Marco Descher's avatar Marco Descher 🏔

[16303] Refactor IElexisEnvironmentService

parent fe24708c
Pipeline #11289 passed with stages
in 4 minutes and 21 seconds
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;
}
}
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();
}
}
}
/**
* Elexis-Environment specific services.
*/
package ch.elexis.core.services.eenv;
\ No newline at end of file
...@@ -16,6 +16,11 @@ public class ElexisEnvironmentPropertyConstants { ...@@ -16,6 +16,11 @@ public class ElexisEnvironmentPropertyConstants {
public static String DB_TYPE = "DB_TYPE"; public static String DB_TYPE = "DB_TYPE";
public static String DB_USERNAME = "DB_USERNAME"; public static String DB_USERNAME = "DB_USERNAME";
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";
} }
...@@ -32,4 +32,10 @@ public class ElexisSystemPropertyConstants { ...@@ -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";
} }
...@@ -8,7 +8,7 @@ public interface IElexisEnvironmentService { ...@@ -8,7 +8,7 @@ public interface IElexisEnvironmentService {
public static final String CFG_EE_HOSTNAME = "EE_HOSTNAME"; public static final String CFG_EE_HOSTNAME = "EE_HOSTNAME";
/** /**
* @return * @return the entry hostname of the elexis-environment
*/ */
public String getHostname(); public String getHostname();
...@@ -19,7 +19,7 @@ public interface IElexisEnvironmentService { ...@@ -19,7 +19,7 @@ public interface IElexisEnvironmentService {
/** /**
* @param key * @param key
* @return a property provided by the elexis-environment * @return a property provided by the elexis-environment or <code>null</code> if not found
*/ */
public String getProperty(String key); public String getProperty(String key);
......
/** /**
* Interfaces for the elexis-environment * Elexis-Environment interfaces
*/ */
package ch.elexis.core.eenv; package ch.elexis.core.eenv;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment