/** * Create a canonical name for a webapp temp directory. The form of the name is: <code> * "Jetty_"+host+"_"+port+"__"+resourceBase+"_"+context+"_"+virtualhost+base36_hashcode_of_whole_string * </code> host and port uniquely identify the server context and virtual host uniquely identify * the webapp * * @return the canonical name for the webapp temp directory */ public static String getCanonicalNameForWebAppTmpDir(WebAppContext context) { StringBuffer canonicalName = new StringBuffer(); canonicalName.append("jetty-"); // get the host and the port from the first connector 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 try { Resource resource = context.getBaseResource(); if (resource == null) { if (context.getWar() == null || context.getWar().length() == 0) resource = context.newResource(context.getResourceBase()); // Set dir or WAR resource = context.newResource(context.getWar()); } String tmp = URIUtil.decodePath(resource.getURL().getPath()); if (tmp.endsWith("/")) tmp = tmp.substring(0, tmp.length() - 1); if (tmp.endsWith("!")) tmp = tmp.substring(0, tmp.length() - 1); // 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) { LOG.warn("Can't generate resourceBase as part of webapp tmp dir name", e); } // Context name String contextPath = context.getContextPath(); contextPath = contextPath.replace('/', '_'); contextPath = contextPath.replace('\\', '_'); canonicalName.append(contextPath); // Virtual host (if there is one) canonicalName.append("-"); String[] vhosts = context.getVirtualHosts(); if (vhosts == null || vhosts.length <= 0) canonicalName.append("any"); else canonicalName.append(vhosts[0]); // sanitize for (int i = 0; i < canonicalName.length(); i++) { char c = canonicalName.charAt(i); if (!Character.isJavaIdentifierPart(c) && "-.".indexOf(c) < 0) canonicalName.setCharAt(i, '.'); } canonicalName.append("-"); return canonicalName.toString(); }