Search in sources :

Example 1 with StandardContext

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);
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext) Host(org.apache.catalina.Host) File(java.io.File) URL(java.net.URL) ContextName(org.apache.catalina.util.ContextName)

Example 2 with StandardContext

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
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) ServletContext(javax.servlet.ServletContext)

Example 3 with StandardContext

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);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext)

Example 4 with StandardContext

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);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext)

Example 5 with StandardContext

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);
    }
}
Also used : Wrapper(org.apache.catalina.Wrapper) StandardWrapper(org.apache.catalina.core.StandardWrapper) Tomcat(org.apache.catalina.startup.Tomcat) StandardContext(org.apache.catalina.core.StandardContext) WebappLoader(org.apache.catalina.loader.WebappLoader)

Aggregations

StandardContext (org.apache.catalina.core.StandardContext)181 File (java.io.File)72 Tomcat (org.apache.catalina.startup.Tomcat)64 Test (org.junit.Test)52 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)41 Context (org.apache.catalina.Context)32 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)31 StandardHost (org.apache.catalina.core.StandardHost)25 IOException (java.io.IOException)22 Host (org.apache.catalina.Host)18 MalformedURLException (java.net.MalformedURLException)16 JarFile (java.util.jar.JarFile)16 ServletContext (javax.servlet.ServletContext)16 StandardRoot (org.apache.catalina.webresources.StandardRoot)16 URL (java.net.URL)13 InterceptSupport (com.creditease.monitor.interceptframework.InterceptSupport)12 InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)12 HashMap (java.util.HashMap)12 List (java.util.List)12 Container (org.apache.catalina.Container)12