use of org.apache.catalina.core.StandardContext in project tomcat by apache.
the class StandardContextSF method store.
/**
* Store a Context as Separate file as configFile value from context exists.
* filename can be relative to catalina.base.
*
* @see org.apache.catalina.storeconfig.IStoreFactory#store(java.io.PrintWriter,
* int, java.lang.Object)
*/
@Override
public void store(PrintWriter aWriter, int indent, Object aContext) throws Exception {
if (aContext instanceof StandardContext) {
StoreDescription desc = getRegistry().findDescription(aContext.getClass());
if (desc.isStoreSeparate()) {
URL configFile = ((StandardContext) aContext).getConfigFile();
if (configFile != null) {
if (desc.isExternalAllowed()) {
if (desc.isBackup())
storeWithBackup((StandardContext) aContext);
else
storeContextSeparate(aWriter, indent, (StandardContext) aContext);
return;
}
} else if (desc.isExternalOnly()) {
// Set a configFile so that the configuration is actually saved
Context context = ((StandardContext) aContext);
Host host = (Host) context.getParent();
File configBase = host.getConfigBaseFile();
ContextName cn = new ContextName(context.getName(), false);
String baseName = cn.getBaseName();
File xml = new File(configBase, baseName + ".xml");
context.setConfigFile(xml.toURI().toURL());
if (desc.isBackup())
storeWithBackup((StandardContext) aContext);
else
storeContextSeparate(aWriter, indent, (StandardContext) aContext);
return;
}
}
}
super.store(aWriter, indent, aContext);
}
use of org.apache.catalina.core.StandardContext in project uavstack by uavorg.
the class SpringBootTomcatPlusIT method onAppStart.
/**
* onAppStart
*
* @param args
*/
@Override
public void onAppStart(Object... args) {
StandardContext sc = (StandardContext) args[0];
String contextPath = (String) ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true);
// springboot use threadlocalContext to store the WEBCONTAINER_STARTED Event context, just return when it's uav's inner app in case of rewriting the real app's context
if ("/com.creditease.uav".equals(contextPath)) {
return;
}
InterceptSupport iSupport = InterceptSupport.instance();
InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED);
/**
* NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do
* anything on this we may use its webappclassloader's parent as the classloader
*/
context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader().getParent());
context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
context.put(InterceptConstants.CONTEXTPATH, contextPath);
if (context.get(InterceptConstants.APPNAME) == null) {
context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
}
ServletContext sContext = (ServletContext) ReflectionHelper.getField(StandardContext.class, sc, "context", true);
context.put(InterceptConstants.SERVLET_CONTEXT, sContext);
String basePath = sContext.getRealPath("");
/*
* NOTE: springboot couldn't get the basePath through method "getRealPath", temporary process
*/
if (basePath == null) {
basePath = "";
} else if (basePath.lastIndexOf("/") == (basePath.length() - 1) || basePath.lastIndexOf("\\") == (basePath.length() - 1)) {
basePath = basePath.substring(0, basePath.length() - 1);
}
context.put(InterceptConstants.BASEPATH, basePath);
// we don't doIntercept here cause some pre-profile(like dubbo) not happen yet, profile will be done after
// finishRefresh
}
use of org.apache.catalina.core.StandardContext in project uavstack by uavorg.
the class SpringBootTomcatPlusIT method onAppInit.
@Override
public void onAppInit(Object... args) {
StandardContext sc = (StandardContext) args[0];
InterceptSupport iSupport = InterceptSupport.instance();
InterceptContext ic = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
InterceptContext ic2 = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_CREATE);
/**
* NOTE: onAppInit, we put the Context Object into threadlocal, then all other later process for
* PRE_WEBCONTAINER_INIT, which can get the object, as not everywhere we can get the object
*
* for example, the DataSource related injection
*/
ic.put(InterceptConstants.CONTEXTOBJ, sc);
ic2.put(InterceptConstants.CONTEXTOBJ, sc);
}
use of org.apache.catalina.core.StandardContext in project uavstack by uavorg.
the class SpringBootTomcatPlusIT method onAppStop.
/**
* onAppStop
*
* @param args
*/
@Override
public void onAppStop(Object... args) {
StandardContext sc = (StandardContext) args[0];
InterceptSupport iSupport = InterceptSupport.instance();
InterceptContext context = iSupport.createInterceptContext(Event.WEBCONTAINER_STOPPED);
if (null == context || null == sc) {
return;
}
/**
* NOTE: spring boot will reset tomcat webappclassloader to null when shutdown, we may use the currentThread's
* classloader as the classloader
*/
context.put(InterceptConstants.WEBAPPLOADER, Thread.currentThread().getContextClassLoader());
context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
String contextPath = (String) ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true);
context.put(InterceptConstants.CONTEXTPATH, contextPath);
context.put(InterceptConstants.APPNAME, ReflectionHelper.getField(StandardContext.class, sc, "displayName", true));
context.put(InterceptConstants.SERVLET_CONTEXT, ReflectionHelper.getField(StandardContext.class, sc, "context", true));
iSupport.doIntercept(context);
}
use of org.apache.catalina.core.StandardContext in project uavstack by uavorg.
the class SpringBootTomcatPlusIT method onDeployUAVApp.
public void onDeployUAVApp(Object... args) {
if (UAVServer.ServerVendor.SPRINGBOOT != UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR)) {
return;
}
Tomcat tomcat = (Tomcat) args[0];
String mofRoot = (String) args[1];
// add uavApp
StandardContext context = new StandardContext();
context.setName("com.creditease.uav");
context.setPath("/com.creditease.uav");
context.setDocBase(mofRoot + "/com.creditease.uav");
context.addLifecycleListener(new Tomcat.FixContextListener());
tomcat.getHost().addChild(context);
// add default servlet
Wrapper servlet = context.createWrapper();
servlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
servlet.setName("default");
context.addChild(servlet);
servlet.setOverridable(true);
context.addServletMapping("/", "default");
// init webapp classloader
context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader()));
context.setDelegate(true);
// after tomcat8, skip jarscan
Object obj = ReflectionHelper.newInstance("org.apache.tomcat.util.scan.StandardJarScanner", Thread.currentThread().getContextClassLoader());
if (obj != null) {
ReflectionHelper.invoke("org.apache.tomcat.util.scan.StandardJarScanner", obj, "setScanAllFiles", new Class<?>[] { Boolean.class }, new Object[] { false }, Thread.currentThread().getContextClassLoader());
ReflectionHelper.invoke("org.apache.tomcat.util.scan.StandardJarScanner", obj, "setScanClassPath", new Class<?>[] { Boolean.class }, new Object[] { false }, Thread.currentThread().getContextClassLoader());
ReflectionHelper.invoke("org.apache.tomcat.util.scan.StandardJarScanner", obj, "setScanAllDirectories", new Class<?>[] { Boolean.class }, new Object[] { false }, Thread.currentThread().getContextClassLoader());
context.setJarScanner((JarScanner) obj);
}
}
Aggregations