From 0483a9aeb5c57b37a598641582efdb21f671f7cc Mon Sep 17 00:00:00 2001 From: Daniel Laguna Date: Tue, 9 May 2017 17:57:13 -0600 Subject: [PATCH] =?UTF-8?q?#11481:=20Yet=20another=20change=20on=20the=20l?= =?UTF-8?q?ogic=20of=20the=20OSGIUtil=20to=20fit=20the=20Un=E2=80=A6=20(#1?= =?UTF-8?q?1540)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #11481: Yet another change on the logic of the OSGIUtil to fit the UnitTest * #11481: UnitTest added to OSGUtil for base directory --- dotCMS/build-aws-tests.sh | 3 ++ .../java/com/dotcms/osgi/OSGIUtilTest.java | 54 ++++++++++++++++--- .../it-dotmarketing-config.properties | 1 + .../java/com/dotmarketing/util/OSGIUtil.java | 49 ++++++++++++++++- 4 files changed, 99 insertions(+), 8 deletions(-) diff --git a/dotCMS/build-aws-tests.sh b/dotCMS/build-aws-tests.sh index b806cc201dfe..c36a15767536 100755 --- a/dotCMS/build-aws-tests.sh +++ b/dotCMS/build-aws-tests.sh @@ -85,6 +85,9 @@ TOOLBOX_MANAGER_PATH=$PWD/dotserver/tomcat/webapps/ROOT/WEB-INF/toolbox.xml echo " context.path.felix=$PWD/dotserver/tomcat/webapps/ROOT/WEB-INF/felix " >> core/dotCMS/src/integration-test/resources/it-dotmarketing-config.properties +echo " +felix.base.dir=$PWD/dotserver/tomcat/webapps/ROOT/WEB-INF/felix +" >> core/dotCMS/src/integration-test/resources/it-dotmarketing-config.properties # Create output directory diff --git a/dotCMS/src/integration-test/java/com/dotcms/osgi/OSGIUtilTest.java b/dotCMS/src/integration-test/java/com/dotcms/osgi/OSGIUtilTest.java index 4cc9278e7f75..ad803232bbfe 100644 --- a/dotCMS/src/integration-test/java/com/dotcms/osgi/OSGIUtilTest.java +++ b/dotCMS/src/integration-test/java/com/dotcms/osgi/OSGIUtilTest.java @@ -2,6 +2,7 @@ import com.dotcms.util.IntegrationTestInitService; import com.dotmarketing.util.Config; +import com.dotmarketing.util.Logger; import com.dotmarketing.util.OSGIUtil; import org.apache.commons.io.FileUtils; @@ -60,11 +61,21 @@ private static void restartOSGi() { } /** - * Restart the OSGI Framework + * Restart the OSGI Framework. Copies/Restores all files + * + * @param felixBasePath The default felix base path + * @param newDirectory The new directory to copy the files from */ - private static void restartOSGi(String felixBasePath) { - Config.setProperty(FELIX_BASE_DIR_KEY, felixBasePath); - restartOSGi(); + private static void restartOSGi(String felixBasePath, String newDirectory) { + try { + Config.setProperty(FELIX_BASE_DIR_KEY, felixBasePath); + + FileUtils.copyDirectory(new File(newDirectory), new File(felixBasePath)); + + restartOSGi(); + } catch (Exception ex) { + Logger.error(OSGIUtilTest.class, "Error restarting OSGI", ex); + } } /** @@ -105,10 +116,10 @@ public void test03CustomFelixDeployPath() throws Exception { Assert.assertNotNull(deployFelixPath); assertThat("Path ends with /WEB-INF/customfelix/load", deployFelixPath.endsWith("/WEB-INF/customfelix/load")); + restartOSGi(contextFelixPath, customFelixPath); + removeFolder(deployFelixPath); removeFolder(customFelixPath); - - restartOSGi(contextFelixPath); } /** @@ -127,10 +138,39 @@ public void test04CustomFelixUndeployPath() throws Exception { Assert.assertNotNull(undeployFelixPath); assertThat("Path ends with /WEB-INF/customfelix/undeployed", undeployFelixPath.endsWith("/WEB-INF/customfelix/undeployed")); + restartOSGi(contextFelixPath, customFelixPath); + removeFolder(undeployFelixPath); removeFolder(customFelixPath); + } + + /** + * Test the base directory exists using the servlet context + */ + @Test + public void test05GetBaseDirectoryFromServletContext() throws Exception { + ServletContextEvent context = new ServletContextEvent(Config.CONTEXT); - restartOSGi(contextFelixPath); + String baseDirectory = OSGIUtil.getInstance().getBaseDirectory(context); + assertThat("WEB-INF Base Directory exists", new File(baseDirectory).exists()); + } + + /** + * Test the base directory exists using the Config.CONTEXT + */ + @Test + public void test06GetBaseDirectoryFromConfigContext() throws Exception { + String baseDirectory = OSGIUtil.getInstance().getBaseDirectory(null); + assertThat("WEB-INF Base Directory exists", new File(baseDirectory).exists()); + } + + /** + * Test the parse base directory from 'felix.base.dir' property + */ + @Test + public void test07ParseBaseDirectory() throws Exception { + String baseDirectory = OSGIUtil.getInstance().parseBaseDirectoryFromConfig(); + assertThat("WEB-INF Path exists", new File(baseDirectory).exists()); } /** diff --git a/dotCMS/src/integration-test/resources/it-dotmarketing-config.properties b/dotCMS/src/integration-test/resources/it-dotmarketing-config.properties index d74393fc92ce..aab8ae6da420 100644 --- a/dotCMS/src/integration-test/resources/it-dotmarketing-config.properties +++ b/dotCMS/src/integration-test/resources/it-dotmarketing-config.properties @@ -826,4 +826,5 @@ api.system.ruleengine.actionlet.VisitorTagsActionlet.MAX_TAGS=20 ## Path to felix folder #context.path.felix=/webapps/ROOT/WEB-INF/felix +#felix.base.dir=/webapps/ROOT/WEB-INF/felix diff --git a/dotCMS/src/main/java/com/dotmarketing/util/OSGIUtil.java b/dotCMS/src/main/java/com/dotmarketing/util/OSGIUtil.java index 07aa357e0db4..7f1adc8c8f1f 100644 --- a/dotCMS/src/main/java/com/dotmarketing/util/OSGIUtil.java +++ b/dotCMS/src/main/java/com/dotmarketing/util/OSGIUtil.java @@ -451,7 +451,8 @@ public String getFelixUndeployPath() { */ private void verifyBundles(Properties props, ServletContextEvent context) { String bundlePath = props.getProperty(AUTO_DEPLOY_DIR_PROPERTY); - String baseDirectory = context.getServletContext().getRealPath("/WEB-INF"); + String baseDirectory = getBaseDirectory(context); + String defaultFelixPath = baseDirectory + File.separator + "felix"; String defaultBundlePath = defaultFelixPath + File.separator + "bundle"; @@ -480,4 +481,50 @@ private void verifyBundles(Properties props, ServletContextEvent context) { } } + /** + * Gets the base directory, fetching it from the real path on the servlet context. + * If not found, it tries to fetch it from configuration context. + * If still not found, it fetches it from the 'felix.base.dir' property + * If value is null an exception is thrown. + * + * @param context The servlet context + * @return String + */ + public String getBaseDirectory(ServletContextEvent context) { + String baseDirectory = null; + if (context != null) { + baseDirectory = context.getServletContext().getRealPath("/WEB-INF"); + } + + if (!UtilMethods.isSet(baseDirectory)) { + baseDirectory = Config.CONTEXT.getRealPath("/WEB-INF"); + + if (!UtilMethods.isSet(baseDirectory)) { + baseDirectory = parseBaseDirectoryFromConfig(); + } + } + + if (!UtilMethods.isSet(baseDirectory)) { + String errorMessage = "The default WEB-INF base directory is not found. Value is null"; + Logger.error(this, errorMessage); + + throw new RuntimeException(errorMessage); + } + + return baseDirectory; + } + + /** + * Parses the base directory from config + * + * @return String + */ + public String parseBaseDirectoryFromConfig() { + String baseDirectory = Config.getStringProperty(FELIX_BASE_DIR, "/WEB-INF"); + if (baseDirectory.endsWith("/WEB-INF")) { + baseDirectory = baseDirectory.substring(0, baseDirectory.indexOf(("/WEB-INF")) + 8); + } + + return baseDirectory; + } } \ No newline at end of file