Skip to content

Commit

Permalink
O3-3438: Update SPA module to load the bundled frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
wikumChamith committed Aug 7, 2024
1 parent 490ad71 commit fd8d955
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
34 changes: 32 additions & 2 deletions omod/src/main/java/org/openmrs/module/spa/SpaActivator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,42 @@
import org.openmrs.api.GlobalPropertyListener;
import org.openmrs.api.context.Context;
import org.openmrs.module.BaseModuleActivator;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.util.PrivilegeConstants;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.io.File;

import static org.openmrs.module.spa.SpaConstants.BUNDLED_FRONTEND_DIRECTORY;
import static org.openmrs.module.spa.SpaConstants.GP_LOCAL_DIRECTORY;

@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();
File indexHtml = new File(OpenmrsUtil.getApplicationDataDirectory() + File.separator + SpaConstants.DEFAULT_FRONTEND_DIRECTORY, "index.html");
if (indexHtml.exists()) {
spaDirectoryResolver = new SpaDirectoryResolver();
} else {
indexHtml = new File(servletContext.getRealPath("/"), "WEB-INF" + File.separator + BUNDLED_FRONTEND_DIRECTORY + File.separator + "index.html");
if (indexHtml.exists()) {
try {
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
Context.getAdministrationService().setGlobalProperty(GP_LOCAL_DIRECTORY, BUNDLED_FRONTEND_DIRECTORY);
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
}
spaDirectoryResolver = new SpaDirectoryResolver(servletContext);
}
}
Context.getAdministrationService().addGlobalPropertyListener(spaDirectoryResolver);
}

Expand All @@ -33,4 +58,9 @@ public void stopped() {
spaDirectoryResolver = null;
log.info("SPA module stopped");
}

@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
2 changes: 2 additions & 0 deletions omod/src/main/java/org/openmrs/module/spa/SpaConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,10 +26,17 @@ public class SpaDirectoryResolver implements GlobalPropertyListener {

private static final AtomicReference<String> spaDirectory = new AtomicReference<>();

private static ServletContext servletContext;

public SpaDirectoryResolver() {
resolveDirectory(getDirectoryFromSettings());
}

public SpaDirectoryResolver(ServletContext servletContext) {
SpaDirectoryResolver.servletContext = servletContext;
resolveDirectory(getDirectoryFromSettings());
}

@Override
public boolean supportsPropertyName(String property) {
return GP_LOCAL_DIRECTORY.equals(property);
Expand Down Expand Up @@ -75,6 +85,12 @@ private static void resolveDirectory(String spaDirectory) {
return;
}

if(spaDirectory.equals(BUNDLED_FRONTEND_DIRECTORY)){
String openmrsRootPath = servletContext.getRealPath("/");
SpaDirectoryResolver.spaDirectory.set(Paths.get(openmrsRootPath, "WEB-INF", spaDirectory).toAbsolutePath().toString());
return;
}

Path spaDirectoryPath = Paths.get(spaDirectory);
if (!spaDirectoryPath.isAbsolute()) {
String applicationDataDirectory = OpenmrsUtil.getApplicationDataDirectory();
Expand All @@ -86,4 +102,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();
}
}
1 change: 1 addition & 0 deletions omod/src/main/resources/webModuleApplicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<context:annotation-config />
<bean id="org.openmrs.module.spa.SpaActivator" class="org.openmrs.module.spa.SpaActivator" />
<bean id="spaResourceLoader" class="org.openmrs.module.spa.SpaResourceLoader" />
<bean class="org.openmrs.module.spa.SpaController" />
<bean class="org.openmrs.module.spa.SpaResourceConverter" />
Expand Down

0 comments on commit fd8d955

Please sign in to comment.