Search in sources :

Example 31 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class MSCPPlusIT method onAppStarting.

/**
 * onAppStarting
 *
 * @param args
 */
public void onAppStarting() {
    IConfigurationManager cm = ConfigurationManager.getInstance();
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.createInterceptContext(Event.WEBCONTAINER_INIT);
    context.put(InterceptConstants.WEBAPPLOADER, this.getClass().getClassLoader());
    context.put(InterceptConstants.WEBWORKDIR, cm.getContext(IConfigurationManager.ROOT));
    context.put(InterceptConstants.CONTEXTPATH, "");
    context.put(InterceptConstants.APPNAME, cm.getContext(IConfigurationManager.NODEAPPID));
    context.put(InterceptConstants.BASEPATH, "/" + cm.getContext(IConfigurationManager.NODEAPPID));
    iSupport.doIntercept(context);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) IConfigurationManager(com.creditease.agent.spi.IConfigurationManager)

Example 32 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class HookFactory method startHook.

/**
 * start application framework hook
 *
 * @param webapploader
 *            AppServer WebAppClassLoader
 * @param detectClass
 *            the class is used to defect if the application framework exists
 * @param loaderName
 *            the loader tag helps to load the hook jars
 * @param proxyClass
 *            the hook proxy class to run hook logic
 */
@SuppressWarnings("rawtypes")
public void startHook(ClassLoader webapploader, String detectClass, String loaderName, String proxyClass, Map hookConfig, HookContext cntx) {
    if (webapploader == null || detectClass == null || loaderName == null || proxyClass == null) {
        return;
    }
    // see if the app framework is there
    try {
        webapploader.loadClass(detectClass);
    } catch (ClassNotFoundException e) {
        // ignore
        if (log.isDebugable()) {
            log.debug("LOAD detectclass[" + detectClass + "] FAIL. ", e);
        }
        return;
    }
    InterceptContext context = (InterceptContext) cntx.get(HookConstants.INTERCEPTCONTEXT);
    Event evt = cntx.get(Event.class);
    if (log.isLogEnabled()) {
        log.info("START hook for proxy=" + proxyClass + ",detect=" + detectClass + ",jar=" + loaderName + ",evt=" + evt.toString());
    }
    /**
     * NOTE: there is only one HookProxy instance for each proxyClass and each webapploader
     */
    String contextPath = (String) context.get(InterceptConstants.CONTEXTPATH);
    String proxyKey = getHookProxyKey(contextPath, proxyClass);
    HookProxy proxy = null;
    boolean isNew = false;
    if (hooks.containsKey(proxyKey)) {
        proxy = hooks.get(proxyKey);
    } else {
        @SuppressWarnings("unchecked") List<String> supports = (List<String>) hookConfig.get("supports");
        // install hook jars
        this.installHookJars(webapploader, loaderName, supports);
        // new hookproxy instance
        try {
            Class<?> hookproxyClass = webapploader.loadClass(proxyClass);
            Constructor<?> con = hookproxyClass.getConstructor(new Class<?>[] { String.class, Map.class });
            proxy = (HookProxy) con.newInstance(new Object[] { proxyKey, hookConfig });
            isNew = true;
        } catch (Exception e) {
            log.error("CREATE hookproxy[" + proxyClass + "] instance FAIL. ", e);
            return;
        }
    }
    try {
        if (log.isDebugable()) {
            log.debug("START hookproxy[" + proxyClass + "] ", null);
        }
        // start hookproxy
        proxy.start(cntx, webapploader);
        // only start SUCCESS to store hookproxy
        if (isNew == true) {
            hooks.put(proxyKey, proxy);
        }
        if (log.isDebugable()) {
            log.debug("START hookproxy[" + proxyClass + "] END", null);
        }
    } catch (Exception e) {
        log.error("START hookproxy[" + proxyClass + "] FAIL. ", e);
    } catch (Error e) {
        log.error("START hookproxy[" + proxyClass + "] FAIL. ", e);
    }
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) Event(com.creditease.monitor.interceptframework.spi.InterceptContext.Event) List(java.util.List)

Example 33 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class SpringBootTomcatPlusIT method onResourceInit.

@Override
public void onResourceInit(Object... args) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (!WebappClassLoader.class.isAssignableFrom(cl.getClass())) {
        return;
    }
    /**
     * for Application Starting's Resource Init
     */
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_RESOURCE_INIT);
    StandardContext sc = (StandardContext) context.get(InterceptConstants.CONTEXTOBJ);
    /**
     * 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());
    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));
    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);
    iSupport.doIntercept(context);
}
Also used : WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) StandardContext(org.apache.catalina.core.StandardContext) WebappClassLoader(org.apache.catalina.loader.WebappClassLoader) ServletContext(javax.servlet.ServletContext)

Example 34 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class SpringBootTomcatPlusIT method onServletStart.

/**
 * onServletStart
 *
 * @param args
 */
@Override
public void onServletStart(Object... args) {
    StandardWrapper sw = (StandardWrapper) args[0];
    Servlet servlet = (Servlet) args[1];
    InterceptSupport iSupport = InterceptSupport.instance();
    InterceptContext context = iSupport.createInterceptContext(Event.AFTER_SERVET_INIT);
    context.put(InterceptConstants.SERVLET_INSTANCE, servlet);
    /**
     * 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, Thread.currentThread().getContextClassLoader().getParent());
    context.put(InterceptConstants.CONTEXTPATH, sw.getServletContext().getContextPath());
    iSupport.doIntercept(context);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) Servlet(javax.servlet.Servlet) StandardWrapper(org.apache.catalina.core.StandardWrapper)

Example 35 with InterceptContext

use of com.creditease.monitor.interceptframework.spi.InterceptContext in project uavstack by uavorg.

the class SpringBootTomcatPlusIT method startServer.

/**
 * startUAVServer
 */
public void startServer(String port, String appName, Object arg) {
    if (!isWebServerContext(arg)) {
        return;
    }
    // integrate Tomcat log
    UAVServer.instance().setLog(new TomcatLog("MonitorServer"));
    // start Monitor Server when server starts
    UAVServer.instance().start(new Object[] { UAVServer.ServerVendor.SPRINGBOOT });
    // set appid
    // setAppid(contextPath);
    // set the connector port
    UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_LISTEN_PORT, DataConvertHelper.toInt(port, 8080));
    InterceptSupport iSupport = InterceptSupport.instance();
    // this context will be transmited from springboot mainThread to webcontainerInit thread then back to mainThread
    InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED);
    context.put(InterceptConstants.APPNAME, appName);
}
Also used : InterceptContext(com.creditease.monitor.interceptframework.spi.InterceptContext) InterceptSupport(com.creditease.monitor.interceptframework.InterceptSupport) TomcatLog(com.creditease.tomcat.plus.util.TomcatLog)

Aggregations

InterceptContext (com.creditease.monitor.interceptframework.spi.InterceptContext)66 InterceptSupport (com.creditease.monitor.interceptframework.InterceptSupport)28 ServletContext (javax.servlet.ServletContext)12 StandardContext (org.apache.catalina.core.StandardContext)12 Servlet (javax.servlet.Servlet)6 ProfileElementInstance (com.creditease.uav.profiling.spi.ProfileElementInstance)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 StandardWrapper (org.apache.catalina.core.StandardWrapper)4 WebappClassLoader (org.apache.catalina.loader.WebappClassLoader)4 UAVServer (com.creditease.monitor.UAVServer)3 Event (com.creditease.monitor.interceptframework.spi.InterceptContext.Event)3 DynamicProxyProcessor (com.creditease.uav.monitorframework.dproxy.DynamicProxyProcessor)3 DPMethod (com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod)3 DubboServiceProfileInfo (com.creditease.uav.profiling.handlers.dubbo.DubboServiceProfileInfo)3 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 ServletException (javax.servlet.ServletException)3 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)3