diff options
author | Greg Wilkins | 2010-07-22 03:20:41 +0000 |
---|---|---|
committer | Greg Wilkins | 2010-07-22 03:20:41 +0000 |
commit | 0443786d8ed1af06427195bf022f58a8ecba0e33 (patch) | |
tree | c352c3b181ada70b780e712c034d555864d4bb0c /jetty-webapp | |
parent | 526cef3ab3ae13f7687e5dae7e9080e617da05e3 (diff) | |
download | org.eclipse.jetty.project-0443786d8ed1af06427195bf022f58a8ecba0e33.tar.gz org.eclipse.jetty.project-0443786d8ed1af06427195bf022f58a8ecba0e33.tar.xz org.eclipse.jetty.project-0443786d8ed1af06427195bf022f58a8ecba0e33.zip |
320073 reconsile configuration mechanism
Reconsiled the jetty-8 configuration mechanism with the capability to share the MetaData between instances
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2168 7e9141cc-0065-0410-87d8-b60c137991c4
Diffstat (limited to 'jetty-webapp')
7 files changed, 217 insertions, 132 deletions
diff --git a/jetty-webapp/src/main/config/etc/webdefault.xml b/jetty-webapp/src/main/config/etc/webdefault.xml index 277a77ef17..f353475958 100644 --- a/jetty-webapp/src/main/config/etc/webdefault.xml +++ b/jetty-webapp/src/main/config/etc/webdefault.xml @@ -131,7 +131,7 @@ </init-param> <init-param> <param-name>maxCachedFiles</param-name> - <param-value>1000</param-value> + <param-value>2048</param-value> </init-param> <init-param> <param-name>cacheType</param-name> diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/ClasspathPattern.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/ClasspathPattern.java index a7c2185fb6..44cd7e0547 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/ClasspathPattern.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/ClasspathPattern.java @@ -230,10 +230,10 @@ public class ClasspathPattern if (_entries != null) { - int startIdx = 0; name = name.replace('/','.'); name = name.replaceFirst("^[.]+",""); - + name = name.replaceAll("\\$.*$",""); + for (Entry entry : _entries) { if (entry != null) diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Configuration.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Configuration.java index 49f09fdc93..e03b60f4a7 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Configuration.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/Configuration.java @@ -24,6 +24,8 @@ public interface Configuration /* ------------------------------------------------------------------------------- */ /** Set up for configuration. + * <p> + * Typically this step discovers configuration resources * @throws Exception */ public void preConfigure (WebAppContext context) throws Exception; @@ -31,6 +33,9 @@ public interface Configuration /* ------------------------------------------------------------------------------- */ /** Configure WebApp. + * <p> + * Typically this step applies the discovered configuration resources to + * either the {@link WebAppContext} or the associated {@link MetaData}. * @throws Exception */ public void configure (WebAppContext context) throws Exception; diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java index 0206333089..9fd88272a0 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaData.java @@ -301,6 +301,7 @@ public class MetaData a.apply(); } } + } public boolean isDistributable () diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaDataConfiguration.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaDataConfiguration.java new file mode 100644 index 0000000000..b698c88e57 --- /dev/null +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/MetaDataConfiguration.java @@ -0,0 +1,38 @@ +package org.eclipse.jetty.webapp; + +import java.io.File; +import java.util.Enumeration; + +public class MetaDataConfiguration implements Configuration +{ + final WebAppContext _template; + + MetaDataConfiguration(WebAppContext template) + { + _template=template; + } + + public void preConfigure(WebAppContext context) throws Exception + { + File tmpDir=File.createTempFile(WebInfConfiguration.getCanonicalNameForWebAppTmpDir(context),"",_template.getTempDirectory().getParentFile()); + if (tmpDir.exists()) + tmpDir.delete(); + tmpDir.mkdir(); + tmpDir.deleteOnExit(); + context.setTempDirectory(tmpDir); + } + + public void configure(WebAppContext context) throws Exception + { + } + + public void postConfigure(WebAppContext context) throws Exception + { + } + + public void deconfigure(WebAppContext context) throws Exception + { + // TODO delete temp dir? + // TODO other stuff from other configuration deconfigures? + } +} diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java index b03c6dfc44..285d233043 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.security.PermissionCollection; +import java.util.Enumeration; import java.util.EventListener; import java.util.HashMap; import java.util.Map; @@ -81,7 +82,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL // System classes are classes that cannot be replaced by // the web application, and they are *always* loaded via // system classloader. - private final static String[] __dftSystemClasses = + public final static String[] __dftSystemClasses = { "java.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2) "javax.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2) @@ -99,14 +100,14 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL // loaded by the web application using system classloader, // so if web application needs to load any of such classes, // it has to include them in its distribution. - private final static String[] __dftServerClasses = - { - "-org.eclipse.jetty.continuation.", // don't hide continuation classes - "-org.eclipse.jetty.jndi.", // don't hide naming classes - "-org.eclipse.jetty.plus.jaas.", // don't hide jaas classes - "-org.eclipse.jetty.websocket.", // don't hide websocket extension - "-org.eclipse.jetty.servlet.DefaultServlet", // don't hide default servlet - "org.eclipse.jetty." // hide other jetty classes + public final static String[] __dftServerClasses = + { + "-org.eclipse.jetty.continuation.", // don't hide continuation classes + "-org.eclipse.jetty.jndi.", // don't hide naming classes + "-org.eclipse.jetty.plus.jaas.", // don't hide jaas classes + "-org.eclipse.jetty.websocket.", // don't hide websocket extension + "-org.eclipse.jetty.servlet.DefaultServlet", // don't hide default servlet + "org.eclipse.jetty." // hide other jetty classes } ; private String[] _configurationClasses = __dftConfigurationClasses; @@ -158,6 +159,38 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL //Make a new MetaData to hold descriptor and annotation metadata _metadata = new MetaData(); } + + /* ------------------------------------------------------------ */ + public WebAppContext(WebAppContext template) throws IOException + { + super(SESSIONS|SECURITY); + if (template.isStarted()) + throw new IllegalArgumentException("template is started"); + _scontext=new Context(); + setErrorHandler(new ErrorPageErrorHandler()); + //Make a new MetaData to hold descriptor and annotation metadata + _metadata = template.getMetaData(); + _configurations = new Configuration[]{new MetaDataConfiguration(template)}; + + System.err.println("webapp "+getContextPath()+" @ "+hashCode()); + setAliases(template.isAliases()); + setBaseResource(template.getBaseResource()); + setClassLoader(template.getClassLoader()); // TODO maybe not share classloader? + setContextPath(template.getContextPath()); + setCompactPath(template.isCompactPath()); + setDisplayName(template.getDisplayName()); + setLogger(template.getLogger()); // TODO maybe not shared ??? + setMaxFormContentSize(template.getMaxFormContentSize()); + + Enumeration names=template.getAttributeNames(); + while(names.hasMoreElements()) + { + String name = (String)names.nextElement(); + Object val = template.getAttribute(name); + if (!name.startsWith("javax.servlet.")) + setAttribute(name,val); + } + } /* ------------------------------------------------------------ */ /** @@ -193,18 +226,6 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL /* ------------------------------------------------------------ */ /** - */ - public WebAppContext(SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler) - { - super(null,sessionHandler,securityHandler,servletHandler,errorHandler); - _scontext=new Context(); - setErrorHandler(errorHandler!=null?errorHandler:new ErrorPageErrorHandler()); - //Make a new MetaData to hold descriptor and annotation metadata - _metadata = new MetaData(); - } - - /* ------------------------------------------------------------ */ - /** * @param servletContextName The servletContextName to set. */ @Override @@ -339,8 +360,76 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL { _configurationDiscovered = discovered; } + + /* ------------------------------------------------------------ */ + /** Pre configure the web application. + * <p> + * The method is normally called from {@link #start()}. It performs + * the discovery of the configurations to be applied to this context, + * specifically:<ul> + * <li>Instantiate the {@link Configuration} instances with a call to {@link #loadConfigurations()}. + * <li>Setup the default System classes by calling {@link #loadSystemClasses()} + * <li>Setup the default Server classes by calling {@link #loadServerClasses()} + * <li>Instantiates a classload (if one is not already set) + * <li>Calls the {@link Configuration#preConfigure(WebAppContext)} method of all + * Configuration instances. + * </ul> + * @throws Exception + */ + public void preConfigure() throws Exception + { + // Setup configurations + loadConfigurations(); + + // Setup system classes + loadSystemClasses(); + + // Setup server classes + loadServerClasses(); + + // Configure classloader + _ownClassLoader=false; + if (getClassLoader()==null) + { + WebAppClassLoader classLoader = new WebAppClassLoader(this); + setClassLoader(classLoader); + _ownClassLoader=true; + } + + if (Log.isDebugEnabled()) + { + ClassLoader loader = getClassLoader(); + Log.debug("Thread Context class loader is: " + loader); + loader=loader.getParent(); + while(loader!=null) + { + Log.debug("Parent class loader is: " + loader); + loader=loader.getParent(); + } + } + + // Prepare for configuration + for (int i=0;i<_configurations.length;i++) + _configurations[i].preConfigure(this); + } /* ------------------------------------------------------------ */ + public void configure() throws Exception + { + // Configure webapp + for (int i=0;i<_configurations.length;i++) + _configurations[i].configure(this); + } + + /* ------------------------------------------------------------ */ + public void postConfigure() throws Exception + { + // Clean up after configuration + for (int i=0;i<_configurations.length;i++) + _configurations[i].postConfigure(this); + } + + /* ------------------------------------------------------------ */ /* * @see org.eclipse.thread.AbstractLifeCycle#doStart() */ @@ -349,49 +438,9 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL { try { - // Setup configurations - loadConfigurations(); - - // Setup system classes - loadSystemClasses(); - - // Setup server classes - loadServerClasses(); - - // Configure classloader - _ownClassLoader=false; - if (getClassLoader()==null) - { - WebAppClassLoader classLoader = new WebAppClassLoader(this); - setClassLoader(classLoader); - _ownClassLoader=true; - } - - if (Log.isDebugEnabled()) - { - ClassLoader loader = getClassLoader(); - Log.debug("Thread Context class loader is: " + loader); - loader=loader.getParent(); - while(loader!=null) - { - Log.debug("Parent class loader is: " + loader); - loader=loader.getParent(); - } - } - - - // Prepare for configuration - - for (int i=0;i<_configurations.length;i++) - _configurations[i].preConfigure(this); - + preConfigure(); super.doStart(); - - - // Clean up after configuration - for (int i=0;i<_configurations.length;i++) - _configurations[i].postConfigure(this); - + postConfigure(); if (isLogUrlOnStart()) dumpUrl(); @@ -557,8 +606,9 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL return _systemClasses.match(name); } - - private void loadSystemClasses() + + /* ------------------------------------------------------------ */ + protected void loadSystemClasses() { if (_systemClasses != null) return; @@ -577,6 +627,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL _systemClasses = ClasspathPattern.fromArray(__dftSystemClasses); } + /* ------------------------------------------------------------ */ private void loadServerClasses() { if (_serverClasses != null) @@ -920,6 +971,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL setAttribute(TEMPDIR,_tmpDir); } + /* ------------------------------------------------------------ */ public File getTempDirectory () { return _tmpDir; @@ -997,10 +1049,8 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL protected void startContext() throws Exception { - // Configure webapp - for (int i=0;i<_configurations.length;i++) - _configurations[i].configure(this); - + configure(); + //resolve the metadata _metadata.resolve(this); diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebInfConfiguration.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebInfConfiguration.java index bbd400e9e1..d832fa4284 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebInfConfiguration.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebInfConfiguration.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.regex.Pattern; import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.PatternMatcher; import org.eclipse.jetty.util.URIUtil; @@ -20,7 +21,7 @@ import org.eclipse.jetty.util.resource.ResourceCollection; public class WebInfConfiguration implements Configuration { - public static final String TEMPDIR_CREATED = "org.eclipse.jetty.tmpdirCreated"; + public static final String TEMPDIR_CONFIGURED = "org.eclipse.jetty.tmpdirConfigured"; public static final String CONTAINER_JAR_PATTERN = "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern"; public static final String WEBINF_JAR_PATTERN = "org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern"; @@ -37,15 +38,17 @@ public class WebInfConfiguration implements Configuration public void preConfigure(final WebAppContext context) throws Exception { + // Look for a work directory + File work = findWorkDirectory(context); + if (work != null) + makeTempDirectory(work, context, false); + //Make a temp directory for the webapp if one is not already set resolveTempDirectory(context); //Extract webapp if necessary unpack (context); - File work = findWorkDirectory(context); - if (work != null) - makeTempDirectory(work, context, false); //Apply an initial ordering to the jars which governs which will be scanned for META-INF //info and annotations. The ordering is based on inclusion patterns. @@ -154,26 +157,20 @@ public class WebInfConfiguration implements Configuration public void deconfigure(WebAppContext context) throws Exception { // delete temp directory if we had to create it or if it isn't called work - Boolean containerCreated = (Boolean)context.getAttribute(TEMPDIR_CREATED); + Boolean tmpdirConfigured = (Boolean)context.getAttribute(TEMPDIR_CONFIGURED); - if (context.getTempDirectory()!=null && (containerCreated != null && containerCreated.booleanValue()) && !isTempWorkDirectory(context.getTempDirectory())) + if (context.getTempDirectory()!=null && (tmpdirConfigured == null || !tmpdirConfigured.booleanValue()) && !isTempWorkDirectory(context.getTempDirectory())) { IO.delete(context.getTempDirectory()); - setTempDirectory(null, context); + context.setTempDirectory(null); } - - context.setAttribute(TEMPDIR_CREATED, null); + context.setAttribute(TEMPDIR_CONFIGURED, null); context.setAttribute(context.TEMPDIR, null); //reset the base resource back to what it was before we did any unpacking of resources context.setBaseResource(_preUnpackBaseResource); } - - - - - /* ------------------------------------------------------------ */ /** @@ -213,10 +210,11 @@ public class WebInfConfiguration implements Configuration */ public void resolveTempDirectory (WebAppContext context) { - //If a tmp directory is already set, we're done + //If a tmp directory is already set, we're done File tmpDir = context.getTempDirectory(); if (tmpDir != null && tmpDir.isDirectory() && tmpDir.canWrite()) { + context.setAttribute(TEMPDIR_CONFIGURED, Boolean.TRUE); return; // Already have a suitable tmp dir configured } @@ -275,7 +273,7 @@ public class WebInfConfiguration implements Configuration tmpDir.delete(); tmpDir.mkdir(); tmpDir.deleteOnExit(); - setTempDirectory(tmpDir, context); + context.setTempDirectory(tmpDir); } catch(IOException e) { @@ -347,17 +345,11 @@ public class WebInfConfiguration implements Configuration if(!sentinel.exists()) sentinel.mkdir(); } - setTempDirectory(tmpDir, context); - } - } - - public void setTempDirectory (File tmpDir, WebAppContext context) - { - context.setAttribute(TEMPDIR_CREATED, Boolean.TRUE); - context.setAttribute(context.TEMPDIR,tmpDir); - context.setTempDirectory(tmpDir); - if(Log.isDebugEnabled())Log.debug("Set temp dir "+tmpDir); + if(Log.isDebugEnabled()) + Log.debug("Set temp dir "+tmpDir); + context.setTempDirectory(tmpDir); + } } @@ -527,8 +519,6 @@ public class WebInfConfiguration implements Configuration return null; } - - /** * Check if the tmpDir itself is called "work", or if the tmpDir @@ -557,35 +547,40 @@ public class WebInfConfiguration implements Configuration * context and virtual host uniquely identify the webapp * @return the canonical name for the webapp temp directory */ - public String getCanonicalNameForWebAppTmpDir (WebAppContext context) + public static String getCanonicalNameForWebAppTmpDir (WebAppContext context) { StringBuffer canonicalName = new StringBuffer(); - canonicalName.append("Jetty"); + canonicalName.append("jetty-"); //get the host and the port from the first connector - Connector[] connectors = context.getServer().getConnectors(); - - - //Get the host - canonicalName.append("_"); - String host = (connectors==null||connectors[0]==null?"":connectors[0].getHost()); - if (host == null) - host = "0.0.0.0"; - canonicalName.append(host.replace('.', '_')); - - //Get the port - canonicalName.append("_"); - //try getting the real port being listened on - int port = (connectors==null||connectors[0]==null?0:connectors[0].getLocalPort()); - //if not available (eg no connectors or connector not started), - //try getting one that was configured. - if (port < 0) - port = connectors[0].getPort(); - canonicalName.append(port); + Server server=context.getServer(); + if (server!=null) + { + Connector[] connectors = context.getServer().getConnectors(); + + if (connectors.length>0) + { + //Get the host + String host = (connectors==null||connectors[0]==null?"":connectors[0].getHost()); + if (host == null) + host = "0.0.0.0"; + canonicalName.append(host); + + //Get the port + canonicalName.append("-"); + //try getting the real port being listened on + int port = (connectors==null||connectors[0]==null?0:connectors[0].getLocalPort()); + //if not available (eg no connectors or connector not started), + //try getting one that was configured. + if (port < 0) + port = connectors[0].getPort(); + canonicalName.append(port); + canonicalName.append("-"); + } + } //Resource base - canonicalName.append("_"); try { Resource resource = context.getBaseResource(); @@ -606,6 +601,7 @@ public class WebInfConfiguration implements Configuration //get just the last part which is the filename int i = tmp.lastIndexOf("/"); canonicalName.append(tmp.substring(i+1, tmp.length())); + canonicalName.append("-"); } catch (Exception e) { @@ -613,33 +609,28 @@ public class WebInfConfiguration implements Configuration } //Context name - canonicalName.append("_"); String contextPath = context.getContextPath(); contextPath=contextPath.replace('/','_'); contextPath=contextPath.replace('\\','_'); canonicalName.append(contextPath); //Virtual host (if there is one) - canonicalName.append("_"); + canonicalName.append("-"); String[] vhosts = context.getVirtualHosts(); if (vhosts == null || vhosts.length <= 0) - canonicalName.append(""); + canonicalName.append("any"); else canonicalName.append(vhosts[0]); - //base36 hash of the whole string for uniqueness - String hash = Integer.toString(canonicalName.toString().hashCode(),36); - canonicalName.append("_"); - canonicalName.append(hash); - // sanitize for (int i=0;i<canonicalName.length();i++) { char c=canonicalName.charAt(i); - if (!Character.isJavaIdentifierPart(c)) + if (!Character.isJavaIdentifierPart(c) && "-.".indexOf(c)<0) canonicalName.setCharAt(i,'.'); } - + + canonicalName.append("-"); return canonicalName.toString(); } |