diff --git a/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java b/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java index f806403..f922724 100644 --- a/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java +++ b/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java @@ -13,17 +13,22 @@ import org.openmrs.api.GlobalPropertyListener; import org.openmrs.api.context.Context; import org.openmrs.module.BaseModuleActivator; +import org.springframework.web.context.ServletContextAware; + +import javax.servlet.ServletContext; @Slf4j -public class SpaActivator extends BaseModuleActivator { +public class SpaActivator extends BaseModuleActivator implements ServletContextAware { // here so we can register the listener on load and de-register it when stopped private GlobalPropertyListener spaDirectoryResolver = null; + private static ServletContext servletContext; + @Override public void started() { log.info("SPA module started"); - spaDirectoryResolver = new SpaDirectoryResolver(); + spaDirectoryResolver = new SpaDirectoryResolver(servletContext); Context.getAdministrationService().addGlobalPropertyListener(spaDirectoryResolver); } @@ -33,4 +38,9 @@ public void stopped() { spaDirectoryResolver = null; log.info("SPA module stopped"); } + + @Override + public void setServletContext(ServletContext servletContext) { + this.servletContext = servletContext; + } } \ No newline at end of file diff --git a/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java b/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java index 795d130..8ea11e5 100644 --- a/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java +++ b/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java @@ -19,6 +19,8 @@ private SpaConstants() {} public static final String DEFAULT_FRONTEND_DIRECTORY = "frontend"; + public static final String BUNDLED_FRONTEND_DIRECTORY = "bundledFrontend"; + public static final String GP_LOCAL_DIRECTORY = "spa.local.directory"; } diff --git a/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java b/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java index 6a3ab3c..539034a 100644 --- a/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java +++ b/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java @@ -7,10 +7,13 @@ import org.openmrs.util.OpenmrsUtil; import org.openmrs.util.PrivilegeConstants; +import javax.servlet.ServletContext; +import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.concurrent.atomic.AtomicReference; +import static org.openmrs.module.spa.SpaConstants.BUNDLED_FRONTEND_DIRECTORY; import static org.openmrs.module.spa.SpaConstants.DEFAULT_FRONTEND_DIRECTORY; import static org.openmrs.module.spa.SpaConstants.GP_LOCAL_DIRECTORY; @@ -23,7 +26,19 @@ public class SpaDirectoryResolver implements GlobalPropertyListener { private static final AtomicReference spaDirectory = new AtomicReference<>(); - public SpaDirectoryResolver() { + private static ServletContext servletContext; + + public SpaDirectoryResolver(ServletContext servletContext) { + this.servletContext = servletContext; + if (isBundledFrontend()){ + try { + Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES); + Context.getAdministrationService().setGlobalProperty(GP_LOCAL_DIRECTORY, BUNDLED_FRONTEND_DIRECTORY); + } + finally { + Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES); + } + } resolveDirectory(getDirectoryFromSettings()); } @@ -75,6 +90,13 @@ private static void resolveDirectory(String spaDirectory) { return; } + if(spaDirectory.equals(BUNDLED_FRONTEND_DIRECTORY)){ + String openmrsRootPath = servletContext.getRealPath("/"); + File file = new File(openmrsRootPath, "WEB-INF" + File.separator + spaDirectory); + SpaDirectoryResolver.spaDirectory.set(Paths.get(file.toURI()).normalize().toAbsolutePath().toString()); + return; + } + Path spaDirectoryPath = Paths.get(spaDirectory); if (!spaDirectoryPath.isAbsolute()) { String applicationDataDirectory = OpenmrsUtil.getApplicationDataDirectory(); @@ -86,4 +108,9 @@ private static void resolveDirectory(String spaDirectory) { SpaDirectoryResolver.spaDirectory.set(spaDirectoryPath.normalize().toString()); } } + + private static boolean isBundledFrontend() { + String openmrsRootPath = servletContext.getRealPath("/"); + return new File(openmrsRootPath, "WEB-INF" + File.separator + BUNDLED_FRONTEND_DIRECTORY).exists(); + } } diff --git a/omod/src/main/resources/webModuleApplicationContext.xml b/omod/src/main/resources/webModuleApplicationContext.xml index 1c474c0..ae3a424 100644 --- a/omod/src/main/resources/webModuleApplicationContext.xml +++ b/omod/src/main/resources/webModuleApplicationContext.xml @@ -16,6 +16,7 @@ http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> +