Skip to content

Instantly share code, notes, and snippets.

@rmannibucau
Created August 17, 2012 15:43
Show Gist options
  • Save rmannibucau/3380015 to your computer and use it in GitHub Desktop.
Save rmannibucau/3380015 to your computer and use it in GitHub Desktop.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.webbeans.web.tomcat;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class TomcatUtil
{
private final static Map<Object,Object> CREATIONAL_CONTEXT_INSTANCES = new ConcurrentHashMap<Object, Object>();
public static Object inject(Object object, ClassLoader loader) throws Exception
{
// why all this reflection stuff here?
final Class<?> injector = loader.loadClass("org.apache.webbeans.inject.OWBInjector");
final Class<?> bmClazz = loader.loadClass("javax.enterprise.inject.spi.BeanManager");
final Class<?> ccClazz = loader.loadClass("javax.enterprise.context.spi.CreationalContext");
final Method method = injector.getMethod("inject", bmClazz, Object.class, ccClazz);
final Object bm = getBeanManager(loader);
final Class<?> c = loader.loadClass("javax.enterprise.context.spi.Contextual");
final Method ccc = bmClazz.getMethod("createCreationalContext", c);
final Object cc = ccc.invoke(bm);
CREATIONAL_CONTEXT_INSTANCES.put(object, cc);
return method.invoke(object, bm, object, cc);
}
private static Object getBeanManager(ClassLoader loader) throws Exception
{
final Class<?> wbcClazz = loader.loadClass("org.apache.webbeans.config.WebBeansContext");
final Method getBm = wbcClazz.getMethod("getBeanManagerImpl");
final Object wbc = wbcClazz.getMethod("currentInstance").invoke(null);
return getBm.invoke(wbc);
}
public static void destroy(Object injectorInstance, ClassLoader loader) throws Exception
{
final Object bm = getBeanManager(loader);
final Method gitw = bm.getClass().getMethod("getInjectionTargetWrapper", Class.class);
final Object itw = gitw.invoke(injectorInstance.getClass());
final Object ocw = CREATIONAL_CONTEXT_INSTANCES.remove(injectorInstance);
if (itw != null)
{
loader.loadClass("org.apache.webbeans.component.InjectionTargetWrapper")
.getMethod("dispose")
.invoke(injectorInstance);
}
else
{
if (ocw != null) {
final Class<?> ccClazz = loader.loadClass("javax.enterprise.context.spi.CreationalContext");
ccClazz.getMethod("release").invoke(ocw);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment