Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.weld.environment.tomcat;
  
  import static org.jboss.weld.environment.servlet.util.Reflections.findDeclaredField;
  import static org.jboss.weld.environment.servlet.util.Reflections.findDeclaredMethod;
  import static org.jboss.weld.environment.servlet.util.Reflections.getFieldValue;
  import static org.jboss.weld.environment.servlet.util.Reflections.invokeMethod;
  import static org.jboss.weld.environment.servlet.util.Reflections.setFieldValue;
  
 
 import  javax.servlet.ServletContext;
 
 import  org.apache.catalina.core.ApplicationContext;
 import  org.apache.catalina.core.ApplicationContextFacade;
 import  org.apache.catalina.core.StandardContext;
 import  org.apache.tomcat.InstanceManager;
Forwards all calls in turn to two delegates: first to InstanceManager, then to WeldInstanceManager

Author(s):
Matija Mazi
 
     private static final String INSTANCE_MANAGER_FIELD_NAME = "instanceManager";
     private final InstanceManager firstProcessor;
     private final InstanceManager secondProcessor;
 
     public WeldForwardingInstanceManager(InstanceManager originalAnnotationProcessor, InstanceManager weldProcessor) {
         this. = originalAnnotationProcessor;
         this. = weldProcessor;
     }
 
     @Override
     protected InstanceManager delegate() {
         return ;
     }
 
     @Override
         super.destroyInstance(o);
         .destroyInstance(o);
     }
 
     @Override
         super.newInstance(o);
         .newInstance(o);
     }
 
     @Override
         Object a = super.newInstance(fqcnclassLoader);
         .newInstance(a);
         return a;
     }
 
     @Override
         Object a = super.newInstance(fqcn);
         .newInstance(a);
         return a;
     }
 
     @Override
         Object a = super.newInstance(clazz);
         .newInstance(a);
         return a;
     }
 
     public static void replaceInstanceManager(ServletContext contextWeldManager manager) {
         StandardContext stdContext = getStandardContext(context);
         setInstanceManager(stdContextcreateInstance(managerstdContext));
     }
 
     private static WeldForwardingInstanceManager createInstance(WeldManager manager, StandardContext stdContext) {
         try {
 
             InstanceManager weldProcessor = new WeldInstanceManager(manager);
             return new WeldForwardingInstanceManager(getInstanceManager(stdContext), weldProcessor);
         } catch (Exception e) {
         }
     }
 
     private static StandardContext getStandardContext(ServletContext context) {
         try {
             // Hack into Tomcat to replace the InstanceManager using
             // reflection to access private fields
             ApplicationContext appContext = (ApplicationContext) getContextFieldValue((ApplicationContextFacade) context, ApplicationContextFacade.class);
             return (StandardContext) getContextFieldValue(appContext, ApplicationContext.class);
         } catch (Exception e) {
             throw ..cannotGetStandardContext(e);
        }
    }
    private static <E> Object getContextFieldValue(E objClass<E> clazzthrows NoSuchFieldExceptionIllegalAccessException {
        Field f = clazz.getDeclaredField("context");
        f.setAccessible(true);
        return f.get(obj);
    }
    public static void restoreInstanceManager(ServletContext context) {
        StandardContext stdContext = getStandardContext(context);
        InstanceManager im = getInstanceManager(stdContext);
        if (im instanceof WeldForwardingInstanceManager) {
            setInstanceManager(stdContext, ((WeldForwardingInstanceManagerim).);
        }
    }
    private static InstanceManager getInstanceManager(StandardContext stdContext) {
        Method method = findDeclaredMethod(stdContext.getClass(), "getInstanceManager");
        if (method != null) {
            return invokeMethod(method, InstanceManager.classstdContext);
        }
        Field field = findDeclaredField(stdContext.getClass(), );
        if (field != null) {
            return getFieldValue(fieldstdContext, InstanceManager.class);
        }
    }
    private static void setInstanceManager(StandardContext stdContext, InstanceManager instanceManager) {
        Method method = findDeclaredMethod(stdContext.getClass(), "setInstanceManager", InstanceManager.class);
        if (method != null) {
            invokeMethod(methodvoid.classstdContextinstanceManager);
            return;
        }
        Field field = findDeclaredField(stdContext.getClass(), );
        if (field != null) {
            setFieldValue(fieldstdContextinstanceManager);
            return;
        }
    }
New to GrepCode? Check out our FAQ X