Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetty 12 temp directory cleanup #9153

Merged
merged 8 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
</Call>
</Set>
<Set name="scanInterval" property="jetty.deploy.scanInterval"/>
<Set name="tempDir" property="jetty.deploy.tempDir" />
</New>
</Arg>
</Call>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,5 @@ contextHandlerClass?=org.eclipse.jetty.server.handler.ResourceHandler$ResourceCo
## Monitored directory scan period (seconds)
# jetty.deploy.scanInterval=1

## Base temporary directory for deployed web applications.
# jetty.deploy.tempDir=

## Default ContextHandler class for core deployments
# contextHandlerClass=org.eclipse.jetty.server.handler.ResourceHandler$ResourceContext
Original file line number Diff line number Diff line change
Expand Up @@ -272,46 +272,6 @@ public String[] getConfigurationClasses()
return cc == null ? new String[0] : cc.split(",");
}

/**
* Set the temporary directory for deployment.
* <p>
* This is equivalent to setting the {@link Deployable#BASE_TEMP_DIR} property.
* If not set, then the <code>java.io.tmpdir</code> System Property is used.
*
* @param directory the new work directory
*/
public void setTempDir(String directory)
{
_properties.put(Deployable.BASE_TEMP_DIR, directory);
}

/**
* Set the temporary directory for deployment.
* <p>
* This is equivalent to setting the {@link Deployable#BASE_TEMP_DIR} property.
* If not set, then the <code>java.io.tmpdir</code> System Property is used.
*
* @param directory the new work directory
*/
public void setTempDir(File directory)
{
_properties.put(Deployable.BASE_TEMP_DIR, directory.getAbsolutePath());
}

/**
* Get the temporary directory for deployment.
* <p>
* This is equivalent to getting the {@link Deployable#BASE_TEMP_DIR} property.
*
* @return the user supplied work directory (null if user has not set Temp Directory yet)
*/
@ManagedAttribute("temp directory for use, null if no user set temp directory")
public File getTempDir()
{
String tmpDir = _properties.get(Deployable.BASE_TEMP_DIR);
return tmpDir == null ? null : new File(tmpDir);
}

protected ContextHandler initializeContextHandler(Object context, Path path, Map<String, String> properties)
{
if (LOG.isDebugEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
</Call>
</Set>
<Set name="scanInterval" property="jetty.deploy.scanInterval"/>
<Set name="tempDir" property="jetty.deploy.tempDir" />
<Call name="loadPropertiesFromString">
<Arg>
<Property name="jetty.deploy.common.properties"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface Deployable
};

String ATTRIBUTE_PREFIX = "jetty.deploy.attribute.";
String BASE_TEMP_DIR = "jetty.deploy.tempDir";
String TEMP_DIR = "jetty.deploy.tempDir";
String CONFIGURATION_CLASSES = "jetty.deploy.configurationClasses";
String CONTAINER_SCAN_JARS = "jetty.deploy.containerScanJarPattern";
String CONTEXT_PATH = "jetty.deploy.contextPath";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

package org.eclipse.jetty.osgi;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -26,15 +21,10 @@
import org.eclipse.jetty.deploy.AppProvider;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.ee.Deployable;
import org.eclipse.jetty.osgi.util.BundleFileLocatorHelperFactory;
import org.eclipse.jetty.osgi.util.OSGiClassLoader;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.Environment;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
Expand Down Expand Up @@ -199,45 +189,6 @@ public String[] getConfigurationClasses()
return cc == null ? new String[0] : cc.split(",");
}

/**
* Set the temporary directory for deployment.
* <p>
* This is equivalent to setting the {@link Deployable#BASE_TEMP_DIR} property.
* If not set, then the <code>java.io.tmpdir</code> System Property is used.
*
* @param directory the new work directory
*/
public void setTempDir(String directory)
{
_properties.put(Deployable.BASE_TEMP_DIR, directory);
}

/**
* Set the temporary directory for deployment.
* <p>
* This is equivalent to setting the {@link Deployable#BASE_TEMP_DIR} property.
* If not set, then the <code>java.io.tmpdir</code> System Property is used.
*
* @param directory the new work directory
*/
public void setTempDir(File directory)
{
_properties.put(Deployable.BASE_TEMP_DIR, directory.getAbsolutePath());
}

/**
* Get the temporary directory for deployment.
* <p>
* This is equivalent to getting the {@link Deployable#BASE_TEMP_DIR} property.
*
* @return the user supplied work directory (null if user has not set Temp Directory yet)
*/
public File getTempDir()
{
String tmpDir = _properties.get(Deployable.BASE_TEMP_DIR);
return tmpDir == null ? null : new File(tmpDir);
}

/**
* @param tldBundles Comma separated list of bundles that contain tld jars
* that should be setup on the context instances created here.
Expand Down
1 change: 1 addition & 0 deletions jetty-core/jetty-server/src/main/config/etc/jetty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,6 @@
<Set name="stopTimeout"><Property name="jetty.server.stopTimeout" default="5000"/></Set>
<Set name="dumpAfterStart" property="jetty.server.dumpAfterStart"/>
<Set name="dumpBeforeStop" property="jetty.server.dumpBeforeStop"/>
<Set name="tempDirectory" property="jetty.server.tempDirectory"/>

</Configure>
3 changes: 3 additions & 0 deletions jetty-core/jetty-server/src/main/config/modules/server.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ etc/jetty.xml
## Dump the state of the Jetty server, components, and webapps after startup
# jetty.server.dumpAfterStart=false

## The temporary directory used by the Jetty server and as a root for its contexts
# jetty.server.tempDirectory=

## Dump the state of the Jetty server, components, and webapps before shutdown
# jetty.server.dumpBeforeStop=false
# end::documentation-server-config[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.jetty.server;

import java.io.File;
import java.util.List;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -70,4 +71,9 @@ public interface Context extends Attributes, Decorator, Executor
* The empty string is returned if the full path is exactly the context path.
*/
String getPathInContext(String fullPath);

/**
* @return A temporary directory, configured either for the context, the server or the JVM. Never null.
*/
File getTempDirectory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.jetty.server;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand All @@ -22,6 +23,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -39,6 +41,7 @@
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.Uptime;
Expand All @@ -61,7 +64,6 @@

public class Server extends Handler.Wrapper implements Attributes
{
public static final String BASE_TEMP_DIR_ATTR = "org.eclipse.jetty.server.BaseTempDir";
private static final Logger LOG = LoggerFactory.getLogger(Server.class);
private static final String __serverInfo = "jetty/" + Server.getVersion();

Expand All @@ -81,6 +83,7 @@ public class Server extends Handler.Wrapper implements Attributes
private volatile DateField _dateField;
private long _stopTimeout;
private InvocationType _invocationType = InvocationType.NON_BLOCKING;
private File _tempDirectory;

public Server()
{
Expand Down Expand Up @@ -132,6 +135,48 @@ public String getServerInfo()
return _serverInfo;
}

/**
* <p>Convenience method to call {@link #setTempDirectory(File)} from a String representation
* of the temporary directory.</p>
* @param temp A string representation of the temporary directory.
* @see #setTempDirectory(File)
*/
public void setTempDirectory(String temp)
{
setTempDirectory(new File(temp));
}

/**
* <p>Set the temporary directory returned by {@link Context#getTempDirectory()} for the root
* {@link Context} returned {@link #getContext()}. If not set explicitly here, then the root
* {@link Context#getTempDirectory()} will return either the directory found at
* {@code new File(IO.asFile(System.getProperty("jetty.base")), "work")} if it exists,
* else the JVMs temporary directory as {@code IO.asFile(System.getProperty("java.io.tmpdir"))}.
* @param temp A directory that must exist and be writable or null to get the default.
*/
public void setTempDirectory(File temp)
{
if (isStarted())
throw new IllegalStateException(getState());
if (temp != null && !temp.exists())
throw new IllegalArgumentException("Does not exist: " + temp);
if (temp != null && !temp.canWrite())
throw new IllegalArgumentException("Cannot write: " + temp);
_tempDirectory = temp;
}

/**
* @return The server temporary directory if set, else null. To always obtain a non-null
* temporary directory use {@link Context#getTempDirectory()} on {@link #getContext()}.
* @see #getContext()
* @see Context#getTempDirectory()
*/
@ManagedAttribute("temporary directory")
public File getTempDirectory()
{
return _tempDirectory;
}

public void setServerInfo(String serverInfo)
{
_serverInfo = serverInfo;
Expand Down Expand Up @@ -721,6 +766,10 @@ private static class DynamicErrorProcessor extends ErrorProcessor {}

class ServerContext extends Attributes.Wrapper implements Context
{
private final File jettyBase = IO.asFile(System.getProperty("jetty.base"));
private final File workDir = jettyBase != null && jettyBase.isDirectory() && jettyBase.canWrite() ? new File(jettyBase, "work") : null;
gregw marked this conversation as resolved.
Show resolved Hide resolved
private final File tempDir = workDir != null && workDir.isDirectory() && workDir.canWrite() ? workDir : IO.asFile(System.getProperty("java.io.tmpdir"));

private ServerContext()
{
super(Server.this);
Expand Down Expand Up @@ -804,6 +853,12 @@ public String getPathInContext(String fullPath)
{
return fullPath;
}

@Override
public File getTempDirectory()
{
return Objects.requireNonNullElse(Server.this.getTempDirectory(), tempDir);
}
}

private class ServerEnvironment extends Attributes.Wrapper implements Environment
Expand Down
Loading