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);
}
}
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 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 TomcatPlusIT method onResourceCreate.
/**
* on Resource Create
*/
public Object onResourceCreate(Object... args) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
/**
* after tomcat8, tomcat use ParallelWebappClassLoader instead of WebappClassLoader as it's webapp's
* classloader, both of them are extends WebappClassLoaderBase
*/
Class<?> cls = cl.loadClass("org.apache.catalina.loader.WebappClassLoaderBase");
if (!cls.isAssignableFrom(cl.getClass())) {
return args[0];
}
} catch (ClassNotFoundException e) {
/**
* before tomcat7.0.64(include), WebappClassLoaderBase doesn't exist
*/
if (!WebappClassLoader.class.isAssignableFrom(cl.getClass())) {
return args[0];
}
}
/**
* for Application Starting's Resource Create
*/
InterceptSupport iSupport = InterceptSupport.instance();
InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_CREATE, false);
if (context == null) {
return args[0];
}
StandardContext sc = (StandardContext) context.get(InterceptConstants.CONTEXTOBJ);
if (sc == null) {
return args[0];
}
context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader());
context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath());
context.put(InterceptConstants.CONTEXTPATH, ReflectionHelper.getField(StandardContext.class, sc, "encodedPath", true));
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);
getBasePath(context, sContext);
context.put(InterceptConstants.RESOURCEOBJ, args[0]);
context.put(InterceptConstants.RESOURCECFG, args[1]);
iSupport.doIntercept(context);
InterceptContext ic = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_CREATE);
ic.put(InterceptConstants.CONTEXTOBJ, sc);
return context.get(InterceptConstants.RESOURCEOBJ);
}
Aggregations