Added External Configuration Option
diff --git a/Dockerfile b/Dockerfile index ff3b7d9..87c76f6 100644 --- a/Dockerfile +++ b/Dockerfile
@@ -2,6 +2,6 @@ FROM tomcat:9.0-jdk8-adoptopenjdk-openj9 MAINTAINER dimitrios.chalepakis@pta.de -COPY deploy/conf/context.xml /usr/local/tomcat/conf/context.xml +COPY deploy/conf/contextExternalConfig.xml /usr/local/tomcat/conf/context.xml COPY webapps/ /usr/local/tomcat/webapps/
diff --git a/deploy/conf/contextExternalConfig.xml b/deploy/conf/contextExternalConfig.xml new file mode 100644 index 0000000..41c3eb1 --- /dev/null +++ b/deploy/conf/contextExternalConfig.xml
@@ -0,0 +1,28 @@ +<?xml version='1.0' encoding='utf-8'?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- The contents of this file will be loaded for each web application --> + +<Context> + + <WatchedResource>WEB-INF/web.xml</WatchedResource> + + <Environment name="configurationPathBackend" value="/configPortal/backendConfig.json" type="java.lang.String"/> + <Environment name="configurationPathModules" value="/configPortal/moduleConfig.json" type="java.lang.String"/> + <Parameter name="OK_PORTAL_ENVIRONMENT" override="false" value="ExternalConfig"/> + +</Context>
diff --git a/pom.xml b/pom.xml index 9c27fd5..35635af 100644 --- a/pom.xml +++ b/pom.xml
@@ -228,7 +228,7 @@ </execution> <execution> <id>jacoco-site</id> - <phase>verify</phase> + <phase>test</phase> <goals> <goal>report</goal> </goals>
diff --git a/src/main/java/org/eclipse/openk/portal/common/BackendConfig.java b/src/main/java/org/eclipse/openk/portal/common/BackendConfig.java index 4ac8a11..df2d9f6 100644 --- a/src/main/java/org/eclipse/openk/portal/common/BackendConfig.java +++ b/src/main/java/org/eclipse/openk/portal/common/BackendConfig.java
@@ -39,7 +39,11 @@ private static String loadJsonConfig() { ResourceLoaderBase resourceLoaderBase = new ResourceLoaderBase(); - return resourceLoaderBase.loadStringFromResource(configFileName); + String jsonConfig = resourceLoaderBase.loadStringFromResource(configFileName); + if (jsonConfig == null || jsonConfig.isEmpty()) { + jsonConfig = resourceLoaderBase.loadFromPath(configFileName); + } + return jsonConfig; } public Integer getInternalSessionLengthMillis() { return internalSessionLengthMillis; }
diff --git a/src/main/java/org/eclipse/openk/portal/common/InitBackendConfig.java b/src/main/java/org/eclipse/openk/portal/common/InitBackendConfig.java index 61d7e63..1a41cea 100644 --- a/src/main/java/org/eclipse/openk/portal/common/InitBackendConfig.java +++ b/src/main/java/org/eclipse/openk/portal/common/InitBackendConfig.java
@@ -12,8 +12,12 @@ package org.eclipse.openk.portal.common; import org.apache.log4j.Logger; +import org.eclipse.openk.portal.common.util.ResourceLoaderBase; import org.eclipse.openk.portal.viewmodel.UserModule; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -22,6 +26,8 @@ private static final long serialVersionUID = -7882117179312471533L; private static final Logger LOGGER = Logger.getLogger(InitBackendConfig.class.getName()); + private String backendConfigFile; + private String moduleConfigFile; @Override public void init() throws ServletException { @@ -32,28 +38,28 @@ private void setConfigFiles(String environment ) { String env = (environment == null ? "Production": environment); - String backendConfigFile; - String moduleConfigFile; switch (env){ + case "ExternalConfig": + loadExternalConfig(); + break; case "DevLocal": - backendConfigFile="backendConfigDevLocal.json"; - moduleConfigFile="moduleConfigDevLocal.json"; + backendConfigFile ="backendConfigDevLocal.json"; + moduleConfigFile ="moduleConfigDevLocal.json"; break; case "DevServer": - backendConfigFile="backendConfigDevServer.json"; - moduleConfigFile="moduleConfigDevServer.json"; + backendConfigFile ="backendConfigDevServer.json"; + moduleConfigFile ="moduleConfigDevServer.json"; break; case "Custom": - backendConfigFile="backendConfigCustom.json"; - moduleConfigFile="moduleConfigCustom.json"; + backendConfigFile ="backendConfigCustom.json"; + moduleConfigFile ="moduleConfigCustom.json"; break; case "Docker": - backendConfigFile="backendConfigDocker.json"; - moduleConfigFile="moduleConfigDocker.json"; + backendConfigFile ="backendConfigDocker.json"; + moduleConfigFile ="moduleConfigDocker.json"; break; default: - backendConfigFile="backendConfigProduction.json"; - moduleConfigFile="moduleConfigProduction.json"; + setDefault(); } BackendConfig.setConfigFileName(backendConfigFile); @@ -62,4 +68,20 @@ LOGGER.info("Portal backendendenviroment is: " +environment+ ". Setting moduleConfig accordingly to: "+ moduleConfigFile); } + private void setDefault() { + backendConfigFile ="backendConfigProduction.json"; + moduleConfigFile ="moduleConfigProduction.json"; + } + + private void loadExternalConfig() { + Context ctx = null; + try { + ctx = new InitialContext(); + backendConfigFile = (String) ctx.lookup("java:comp/env/configurationPathBackend"); + moduleConfigFile = (String) ctx.lookup("java:comp/env/configurationPathModules"); + } catch (NamingException e) { + LOGGER.error("Error while loadExternalConfig", e); + } + } + }
diff --git a/src/main/java/org/eclipse/openk/portal/common/util/ResourceLoaderBase.java b/src/main/java/org/eclipse/openk/portal/common/util/ResourceLoaderBase.java index cb5d9d3..2a963c9 100644 --- a/src/main/java/org/eclipse/openk/portal/common/util/ResourceLoaderBase.java +++ b/src/main/java/org/eclipse/openk/portal/common/util/ResourceLoaderBase.java
@@ -1,21 +1,33 @@ /** -****************************************************************************** -* Copyright © 2017-2018 PTA GmbH. -* All rights reserved. This program and the accompanying materials -* are made available under the terms of the Eclipse Public License v1.0 -* which accompanies this distribution, and is available at -* -* http://www.eclipse.org/legal/epl-v10.html -* -****************************************************************************** -*/ + ****************************************************************************** + * Copyright © 2017-2018 PTA GmbH. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + * + ****************************************************************************** + */ package org.eclipse.openk.portal.common.util; +import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.commons.io.ByteOrderMark; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.BOMInputStream; +import org.apache.log4j.Logger; public class ResourceLoaderBase { + + private static final Logger LOGGER = Logger.getLogger(ResourceLoaderBase.class.getName()); + private String stream2String(InputStream is) { StringWriter writer = new StringWriter(); try { @@ -24,8 +36,22 @@ return ""; } return writer.toString(); + } + private String stream2String(InputStream is, String filename) { + StringWriter writer = new StringWriter(); + try (BOMInputStream bomInputStream = new BOMInputStream(is, false, + ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, + ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE)) { + IOUtils.copy(bomInputStream, writer, StandardCharsets.UTF_8.name()); + } catch (IOException e) { + LOGGER.error("Fehler in stream2String()", e); + return ""; + } + + LOGGER.debug("Datei erfolgreich eingelesen: " + filename); + return writer.toString(); } public String loadStringFromResource(String filename) { @@ -33,4 +59,17 @@ InputStream jsonstream = classLoader.getResourceAsStream(filename); return stream2String(jsonstream); } + + public String loadFromPath(String path) { + try { + Path paths = Paths.get(path); + LOGGER.debug("paths: " + path); + try (InputStream inputStream = Files.newInputStream(Paths.get(path))) { + return stream2String(inputStream, paths.getFileName().toString()); + } + } catch (IOException e) { + LOGGER.error("Fehler in loadFromPath", e); + return null; + } + } }
diff --git a/src/main/java/org/eclipse/openk/portal/viewmodel/UserModule.java b/src/main/java/org/eclipse/openk/portal/viewmodel/UserModule.java index 70c18d3..402676f 100644 --- a/src/main/java/org/eclipse/openk/portal/viewmodel/UserModule.java +++ b/src/main/java/org/eclipse/openk/portal/viewmodel/UserModule.java
@@ -38,7 +38,11 @@ private static String loadJsonConfig() { ResourceLoaderBase resourceLoaderBase = new ResourceLoaderBase(); - return resourceLoaderBase.loadStringFromResource(configFileName); + String jsonConfig = resourceLoaderBase.loadStringFromResource(configFileName); + if (jsonConfig == null || jsonConfig.isEmpty()) { + jsonConfig = resourceLoaderBase.loadFromPath(configFileName); + } + return jsonConfig; } public String getModuleName() { return name; }