Skip to content

Commit

Permalink
Issue 11481 r41 osgi util change (#11544)
Browse files Browse the repository at this point in the history
* #11481: Yet another change on the logic of the OSGIUtil to fit the UnitTest

* #11481: UnitTest added to OSGUtil for base directory
  • Loading branch information
danlaguna authored and oarrietadotcms committed May 10, 2017
1 parent 9ab58d5 commit 586785c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
3 changes: 3 additions & 0 deletions dotCMS/build-aws-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 47 additions & 7 deletions dotCMS/src/integration-test/java/com/dotcms/osgi/OSGIUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,5 @@ api.system.ruleengine.actionlet.VisitorTagsActionlet.MAX_TAGS=20

## Path to felix folder
#context.path.felix=<tomcat8>/webapps/ROOT/WEB-INF/felix
#felix.base.dir=<tomcat8>/webapps/ROOT/WEB-INF/felix

49 changes: 48 additions & 1 deletion dotCMS/src/main/java/com/dotmarketing/util/OSGIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 586785c

Please sign in to comment.