// TODO: lazy init for the temp dir - only when a JSP is compiled or // get temp dir is called we need to create it. This will avoid the // need for the baseDir
// TODO: allow contexts without a base dir - i.e. // only programmatic. This would disable the default servlet.
privatestaticfinal StringManager sm = StringManager.getManager(Tomcat.class);
// Some logging implementations use weak references for loggers so there is // the possibility that logging configuration could be lost if GC runs just // after Loggers are configured but before they are used. The purpose of // this Map is to retain strong references to explicitly configured loggers // so that configuration is not lost. privatefinal Map<String, Logger> pinnedLoggers = new HashMap<>();
ContextName cn = new ContextName(contextPath, null);
// Make sure a conflicting web application has not already been deployed
Host h = getHost(); if (h.findChild(cn.getName()) != null) { thrownew IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictChild",
source, contextPath, cn.getName()));
}
// Make sure appBase does not contain a conflicting web application
File targetWar = new File(h.getAppBaseFile(), cn.getBaseName() + ".war");
File targetDir = new File(h.getAppBaseFile(), cn.getBaseName());
if (targetWar.exists()) { thrownew IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile",
source, contextPath, targetWar.getAbsolutePath()));
} if (targetDir.exists()) { thrownew IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile",
source, contextPath, targetDir.getAbsolutePath()));
}
// Should be good to copy the WAR now
URLConnection uConn = source.openConnection();
try (InputStream is = uConn.getInputStream();
OutputStream os = new FileOutputStream(targetWar)) {
IOTools.flow(is, os);
}
/** *Staticversionof{@link#addServlet(String,String,String)} *@paramctxContexttoaddServletto *@paramservletNameServletname(usedinmappings) *@paramservletClassTheclasstobeusedfortheServlet *@returnThewrapperfortheservlet
*/ publicstatic Wrapper addServlet(Context ctx,
String servletName,
String servletClass) { // will do class for name and set init params
Wrapper sw = ctx.createWrapper(); if (sw == null) { returnnull;
}
sw.setServletClass(servletClass);
sw.setName(servletName);
ctx.addChild(sw);
/** *Staticversionof{@link#addServlet(String,String,Servlet)}. *@paramctxContexttoaddServletto *@paramservletNameServletname(usedinmappings) *@paramservletTheServlettoadd *@returnThewrapperfortheservlet
*/ publicstatic Wrapper addServlet(Context ctx,
String servletName,
Servlet servlet) { // will do class for name and set init params
Wrapper sw = new ExistingStandardWrapper(servlet);
sw.setName(servletName);
ctx.addChild(sw);
/** *Addaroletoauser. *@see#addUser(String,String) *@paramuserTheusername *@paramroleTherolename
*/ publicvoid addRole(String user, String role) {
userRoles.computeIfAbsent(user, k -> new ArrayList<>()).add(role);
}
// ------- Extra customization ------- // You can tune individual Tomcat objects, using internal APIs
/** *GetthedefaultHTTPconnectorthatisusedbytheembedded *Tomcat.Itisfirstconfiguredconnectorintheservice. *Ifthere'snoconnectordefined,itwillcreateandaddadefault *connectorusingtheportandaddressspecifiedinthisTomcat *instance,andreturnitforfurthercustomization. * *@returnTheconnectorobject
*/ public Connector getConnector() {
Service service = getService(); if (service.findConnectors().length > 0) { return service.findConnectors()[0];
} // The same as in standard Tomcat configuration. // This creates a NIO HTTP connector.
Connector connector = new Connector("HTTP/1.1");
connector.setPort(port);
service.addConnector(connector); return connector;
}
/** *Setthespecifiedconnectorintheservice,ifitisnotalready *present. *@paramconnectorTheconnectorinstancetoadd
*/ publicvoid setConnector(Connector connector) {
Service service = getService(); boolean found = false; for (Connector serviceConnector : service.findConnectors()) { if (connector == serviceConnector) {
found = true; break;
}
} if (!found) {
service.addConnector(connector);
}
}
/** *Gettheserviceobject.Canbeusedtoaddmore *connectorsandfewotherglobalsettings. *@returnTheservice
*/ public Service getService() { return getServer().findServices()[0];
}
/** *Setsthecurrenthost-allfuturewebappswill *beaddedtothishost.Whentomcatstarts,the *hostwillbethedefaulthost. * *@paramhostThecurrenthost
*/ publicvoid setHost(Host host) {
Engine engine = getEngine(); boolean found = false; for (Container engineHost : engine.findChildren()) { if (engineHost == host) {
found = true; break;
}
} if (!found) {
engine.addChild(host);
}
}
public Host getHost() {
Engine engine = getEngine(); if (engine.findChildren().length > 0) { return (Host) engine.findChildren()[0];
}
Host host = new StandardHost();
host.setName(hostname);
getEngine().addChild(host); return host;
}
/** *Accesstotheengine,forfurthercustomization. *@returnTheengine
*/ public Engine getEngine() {
Service service = getServer().findServices()[0]; if (service.getContainer() != null) { return service.getContainer();
}
Engine engine = new StandardEngine();
engine.setName( "Tomcat" );
engine.setDefaultHost(hostname);
engine.setRealm(createDefaultRealm());
service.setContainer(engine); return engine;
}
/** *Gettheserverobject.Youcanaddlistenersandfewmore *customizations.JNDIisdisabledbydefault. *@returnTheServer
*/ public Server getServer() {
if(addDefaultWebXmlToWebapp&&(configinstanceofContextConfig)){ // prevent it from looking ( if it finds one - it'll have dup error ) ((ContextConfig)config).setDefaultWebXml(noDefaultWebXmlPath()); }
/** *EnablesJNDInamingwhichisdisabledbydefault.Servermustimplement *{@linkLifecycle}inorderforthe{@linkNamingContextListener}tobe *used. *
*/ publicvoid enableNaming() { // Make sure getServer() has been called as that is where naming is // disabled
getServer();
server.addLifecycleListener(new NamingContextListener());
System.setProperty("catalina.useNaming", "true");
String value = "org.apache.naming";
String oldValue =
System.getProperty(javax.naming.Context.URL_PKG_PREFIXES); if (oldValue != null) { if (oldValue.contains(value)) {
value = oldValue;
} else {
value = value + ":" + oldValue;
}
}
System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value);
value = System.getProperty
(javax.naming.Context.INITIAL_CONTEXT_FACTORY); if (value == null) {
System.setProperty
(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
}
}
private URL getWebappConfigFileFromDirectory(File docBase, String contextName) {
URL result = null;
File webAppContextXml = new File(docBase, Constants.ApplicationContextXml); if (webAppContextXml.exists()) { try {
result = webAppContextXml.toURI().toURL();
} catch (MalformedURLException e) {
Logger.getLogger(getLoggerName(getHost(), contextName)).log(Level.WARNING,
sm.getString("tomcat.noContextXml", docBase), e);
}
} return result;
}
private URL getWebappConfigFileFromWar(File docBase, String contextName) {
URL result = null; try (JarFile jar = new JarFile(docBase)) {
JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml); if (entry != null) {
result = UriUtil.buildJarUrl(docBase, Constants.ApplicationContextXml);
}
} catch (IOException e) {
Logger.getLogger(getLoggerName(getHost(), contextName)).log(Level.WARNING,
sm.getString("tomcat.noContextXml", docBase), e);
} return result;
}
static { // Graal native images don't load any configuration except the VM default
eCompatisGraalAvailable()) { try (InputStream is = new FileInputStream(new File(System.getProperty("java.util.logging.config.file", "conf/logging.properties")))) {
LogManager.getLogManager().readConfiguration(is);
} catch (SecurityException | IOException e) { / Ignore, the VM default will be used
java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
}
/** *MainexecutablemethodforusewithaMavenpackager. *@paramargsthecommandlinearguments *@throwsExceptionifanerroroccurs
*/ publicstaticvoid main(String[] args) throws Exception { // Process some command line parameters
String[] catalinaArguments = null; for (int i = 0; i < args.length; i++) { if (args[i].equals("--no-jmx")) {
Registry.disableRegistry();
} elseif (args[i].equals("--catalina")) { // This was already processed before // Skip the rest of the arguments as they are for Catalina
ArrayList<String> result = new ArrayList<>(); for (int j = i + 1; j < args.length; j++) {
result.add(args[j]);
}
catalinaArguments = result.toArray(new String[0]); break;
}
}
SecurityClassLoad.securityClassLoad(Thread.currentThread().getContextClassLoader());
Tomcat tomcat = new Tomcat(); // Create a Catalina instance and let it parse the configuration files // It will also set a shutdown hook to stop the Server when needed // Use the default configuration source
tomcat.init(null, catalinaArguments); boolean await = false;
String path = ""; // Process command line parameters for (int i = 0; i < args.length; i++) { if (args[i].equals("--war")) { if (++i >= args.length) { thrownew IllegalArgumentException(sm.getString("tomcat.invalidCommandLine", args[i - 1]));
}
File war = new File(args[i]);
tomcat.addWebapp(path, war.getAbsolutePath());
} elseif (args[i].equals("--path")) { if (++i >= args.length) { thrownew IllegalArgumentException(sm.getString("tomcat.invalidCommandLine", args[i - 1]));
}
path = args[i];
} elseif (args[i].equals("--await")) {
await = true;
} elseif (args[i].equals("--no-jmx")) { // This was already processed before
} elseif (args[i].equals("--catalina")) { // This was already processed before // Skip the rest of the arguments as they are for Catalina break;
} else { thrownew IllegalArgumentException(sm.getString("tomcat.invalidCommandLine", args[i]));
}
}
tomcat.start(); // Ideally the utility threads are non daemon if (await) {
tomcat.getServer().await();
}
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.84Bemerkung:
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.