Skip to content

Commit

Permalink
[JSPWIKI-1172] Improve PropertyReader and Installer for workDir setup
Browse files Browse the repository at this point in the history
This commit makes a few key changes:
1. PropertyReader now checks for the existence of jspwiki.workDir. If it's missing, the servlet context's temporary directory (or the system's temporary directory if that's unavailable) is set as the working directory.
2. Removed direct usage of java.io.tmpdir in WikiEngine since jspwiki.workDir is now ensured to be set in PropertyReader.
3. Refactored the Installer to use wiki properties (specifically, jspwiki.workDir) instead of directly using java.io.tmpdir.

These changes streamline the handling of the working directory and ensure that it's consistently sourced from the same place (the wiki properties).
  • Loading branch information
arturobernalg authored and juanpablo-santos committed Jul 29, 2023
1 parent 4cc284e commit f1cb2f6
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 8 deletions.
Expand Up @@ -43,7 +43,7 @@ public class WikiTest {
public void testWikiInit() {
Mockito.doReturn( sc ).when( conf ).getServletContext();
final Properties properties = Wiki.init( sc );
Assertions.assertEquals( 5, properties.size() );
Assertions.assertEquals( 6, properties.size() );

// verify SPIs are initialized and can be invoked
Assertions.assertNull( Wiki.acls().acl() );
Expand Down
Expand Up @@ -27,10 +27,12 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.wiki.api.spi.Wiki;
import org.apache.wiki.util.TextUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand Down Expand Up @@ -97,5 +99,4 @@ ConfigurationSource createConfigurationSource( final Properties properties ) {
@Override
public void contextDestroyed( final ServletContextEvent sce ) {
}

}
Expand Up @@ -28,6 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import javax.servlet.ServletContextEvent;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;


@ExtendWith( MockitoExtension.class )
public class WikiBootstrapServletContextListenerTest {
Expand All @@ -41,7 +43,7 @@ public void testWikiInit() {
final WikiBootstrapServletContextListener listener = new WikiBootstrapServletContextListener();
final Properties properties = listener.initWikiSPIs( sce );

Assertions.assertEquals( 35, properties.size() );
Assertions.assertEquals( 36, properties.size() );
}

@Test
Expand All @@ -62,5 +64,4 @@ public void testServletContextListenerLifeCycle() {
Assertions.assertDoesNotThrow( () -> listener.contextInitialized( sce ) );
Assertions.assertDoesNotThrow( () -> listener.contextDestroyed( sce ) );
}

}
3 changes: 0 additions & 3 deletions jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
Expand Up @@ -348,9 +348,6 @@ public void initialize( final Properties props ) throws WikiException {

void createAndFindWorkingDirectory( final Properties props ) throws WikiException {
m_workDir = TextUtil.getStringProperty( props, PROP_WORKDIR, null );
if( StringUtils.isBlank( m_workDir ) ) {
m_workDir = System.getProperty( "java.io.tmpdir", "." ) + File.separator + Release.APPNAME + "-" + m_appid;
}

final File f = new File( m_workDir );
try {
Expand Down
3 changes: 2 additions & 1 deletion jspwiki-main/src/main/java/org/apache/wiki/ui/Installer.java
Expand Up @@ -66,7 +66,7 @@ public class Installer {
public static final String WORK_DIR = Engine.PROP_WORKDIR;
public static final String ADMIN_GROUP = "Admin";
public static final String PROPFILENAME = "jspwiki-custom.properties" ;
public static final String TMP_DIR = System.getProperty("java.io.tmpdir");
public static String TMP_DIR;
private final Session m_session;
private final File m_propertyFile;
private final Properties m_props;
Expand All @@ -86,6 +86,7 @@ public Installer( final HttpServletRequest request, final ServletConfig config )
// Stash the request
m_request = request;
m_validated = false;
TMP_DIR = m_engine.getWikiProperties().getProperty( "jspwiki.workDir" );
}

/**
Expand Down
7 changes: 7 additions & 0 deletions jspwiki-util/pom.xml
Expand Up @@ -94,5 +94,12 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Expand Up @@ -139,6 +139,9 @@ public static Properties loadWebAppProps( final ServletContext context ) {
// now load the cascade (new in 2.5)
loadWebAppPropsCascade( context, props );

// sets the JSPWiki working directory (jspwiki.workDir)
setWorkDir( context, props );

// add system properties beginning with jspwiki...
final Map< String, String > sysprops = collectPropertiesFrom( System.getProperties().entrySet().stream()
.collect( Collectors.toMap( Object::toString, Object::toString ) ) );
Expand Down Expand Up @@ -393,4 +396,33 @@ static String createResourceLocation( final String path, final String name ) {
return result.toString();
}

/**
* This method sets the JSPWiki working directory (jspwiki.workDir). It first checks if this property
* is already set. If it isn't, it attempts to use the servlet container's temporary directory
* (javax.servlet.context.tempdir). If that is also unavailable, it defaults to the system's temporary
* directory (java.io.tmpdir).
* <p>
* This method is package-private to allow for unit testing.
*
* @param properties the JSPWiki properties
* @param servletContext the Servlet context from which to fetch the tempdir if needed
* @since JSPWiki 2.11.1
*/
static void setWorkDir( final ServletContext servletContext, final Properties properties ) {
final String workDir = TextUtil.getStringProperty(properties, "jspwiki.workDir", null);
if (workDir == null) {
final File tempDir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
if (tempDir != null) {
properties.setProperty("jspwiki.workDir", tempDir.getAbsolutePath());
LOG.info("Setting jspwiki.workDir to ServletContext's temporary directory: {}", tempDir.getAbsolutePath());
} else {
final String defaultTmpDir = System.getProperty("java.io.tmpdir");
properties.setProperty("jspwiki.workDir", defaultTmpDir);
LOG.info("ServletContext's temporary directory not found. Setting jspwiki.workDir to system's temporary directory: {}", defaultTmpDir);
}
} else {
LOG.info("jspwiki.workDir is already set to: {}", workDir);
}
}

}
Expand Up @@ -22,11 +22,16 @@ Licensed to the Apache Software Foundation (ASF) under one

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.mockito.Mockito.mock;


/**
* Unit test for PropertyReader.
Expand Down Expand Up @@ -122,4 +127,30 @@ public void testCollectPropertiesFrom() {
Assertions.assertNull( test.get( "secretEnv" ) );
}

@Test
public void testSetWorkDir() {
final Properties properties = new Properties();
final ServletContext servletContext = mock(ServletContext.class);
Mockito.when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(new File("/tmp"));

PropertyReader.setWorkDir(servletContext, properties);

// Test when the "jspwiki.workDir" is not set, it should get set to servlet's temporary directory
String workDir = properties.getProperty("jspwiki.workDir");
Assertions.assertEquals("/tmp", workDir);

// Test when the "jspwiki.workDir" is set, it should remain as it is
properties.setProperty("jspwiki.workDir", "/custom/dir");
PropertyReader.setWorkDir(servletContext, properties);
workDir = properties.getProperty("jspwiki.workDir");
Assertions.assertEquals("/custom/dir", workDir);

// Test when the servlet's temporary directory is null, it should get set to system's temporary directory
Mockito.when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(null);
properties.remove("jspwiki.workDir");
PropertyReader.setWorkDir(servletContext, properties);
workDir = properties.getProperty("jspwiki.workDir");
Assertions.assertEquals(System.getProperty("java.io.tmpdir"), workDir);
}

}

0 comments on commit f1cb2f6

Please sign in to comment.