void createInstance(Class type) { WeldContainer weld = new Weld() .disableDiscovery() .extensions(new Fabric8Extension()) .beanClasses(MyFactory.class, MyBean.class) .initialize(); CreationalContext ctx = weld.getBeanManager().createCreationalContext(null); for (Bean bean : weld.getBeanManager().getBeans(type)) { weld.getBeanManager().getReference(bean, type, ctx); } }
@Override @SuppressWarnings("unchecked") public JCDIInjectionContext createManagedObject( Class managedClass, BundleDescriptor bundle, boolean invokePostConstruct) { JCDIInjectionContext context = null; Object managedObject = null; try { managedObject = createEEManagedObject(bundle.getManagedBeanByBeanClass(managedClass.getName())); } catch (Exception e) { e.printStackTrace(); } WeldContainer wc = getWeldContainer(); if (wc != null) { BeanManager beanManager = wc.getBeanManager(); AnnotatedType annotatedType = beanManager.createAnnotatedType(managedClass); InjectionTarget target = beanManager.createInjectionTarget(annotatedType); CreationalContext cc = beanManager.createCreationalContext(null); target.inject(managedObject, cc); if (invokePostConstruct) { target.postConstruct(managedObject); } context = new JCDIInjectionContextImpl(target, cc, managedObject); } return context; }
@Override @SuppressWarnings("unchecked") public void injectManagedObject(Object managedObject, BundleDescriptor bundle) { WeldContainer wc = getWeldContainer(); if (wc != null) { BeanManager beanManager = wc.getBeanManager(); AnnotatedType annotatedType = beanManager.createAnnotatedType(managedObject.getClass()); InjectionTarget target = beanManager.createInjectionTarget(annotatedType); CreationalContext cc = beanManager.createCreationalContext(null); target.inject(managedObject, cc); } }
@Override public <T> T createInterceptorInstance(Class<T> interceptorClass, BundleDescriptor bundle) { T interceptorInstance = null; WeldContainer wc = getWeldContainer(); if (wc != null) { BeanManager beanManager = wc.getBeanManager(); AnnotatedType annotatedType = beanManager.createAnnotatedType(interceptorClass); InjectionTarget target = ((WeldManager) beanManager) .getInjectionTargetFactory(annotatedType) .createInterceptorInjectionTarget(); CreationalContext cc = beanManager.createCreationalContext(null); interceptorInstance = (T) target.produce(cc); target.inject(interceptorInstance, cc); } return interceptorInstance; }
@Override public ArtifactLoader produce(final Dependencies dependencies, final PropertyLookup properties) { return new CdiArtifactLoader(container.getBeanManager()); }
@Test public void createMultpleJarAndFileResources() throws IOException, ClassNotFoundException, InterruptedException { createKProjectJar("jar1", true); createKProjectJar("jar2", true); createKProjectJar("jar3", true); createKProjectJar("fol4", false); ClassLoader origCl = Thread.currentThread().getContextClassLoader(); try { java.io.File file1 = fileManager.newFile("jar1.jar"); java.io.File file2 = fileManager.newFile("jar2.jar"); java.io.File file3 = fileManager.newFile("jar3.jar"); java.io.File fol4 = fileManager.newFile("fol4"); URLClassLoader urlClassLoader = new URLClassLoader( new URL[] { file1.toURI().toURL(), file2.toURI().toURL(), file3.toURI().toURL(), fol4.toURI().toURL() }); Thread.currentThread().setContextClassLoader(urlClassLoader); Enumeration<URL> e = urlClassLoader.getResources("META-INF/kproject.xml"); while (e.hasMoreElements()) { URL url = e.nextElement(); System.out.println(url); } Class cls = Thread.currentThread() .getContextClassLoader() .loadClass("org.drools.cdi.test.KProjectTestClassjar1"); assertNotNull(cls); cls = Thread.currentThread() .getContextClassLoader() .loadClass("org.drools.cdi.test.KProjectTestClassjar2"); assertNotNull(cls); cls = Thread.currentThread() .getContextClassLoader() .loadClass("org.drools.cdi.test.KProjectTestClassjar3"); assertNotNull(cls); Weld weldContainer = new Weld(); WeldContainer weld = weldContainer.initialize(); Set<Bean<?>> beans = weld.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("jar1")); Bean bean = (Bean) beans.toArray()[0]; KProjectTestClass o1 = (KProjectTestClass) bean.create(weld.getBeanManager().createCreationalContext(null)); assertNotNull(o1); testEntry(o1, "jar1"); beans = weld.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("jar2")); bean = (Bean) beans.toArray()[0]; KProjectTestClass o2 = (KProjectTestClass) bean.create(weld.getBeanManager().createCreationalContext(null)); assertNotNull(o2); testEntry(o2, "jar2"); beans = weld.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("jar3")); bean = (Bean) beans.toArray()[0]; KProjectTestClass o3 = (KProjectTestClass) bean.create(weld.getBeanManager().createCreationalContext(null)); assertNotNull(o3); testEntry(o3, "jar3"); beans = weld.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("fol4")); bean = (Bean) beans.toArray()[0]; KProjectTestClass o4 = (KProjectTestClass) bean.create(weld.getBeanManager().createCreationalContext(null)); assertNotNull(o4); testEntry(o4, "fol4"); weldContainer.shutdown(); } finally { Thread.currentThread().setContextClassLoader(origCl); } }
@Override protected void visitJobOperatorModel(final JobOperatorModel model) throws Exception { super.visitJobOperatorModel(model); model.getArtifactLoaders().add().setValue(new CdiArtifactLoader(container.getBeanManager())); }