private Set<String> scanPackages() { if (packages.isEmpty()) { return Collections.emptySet(); } Set<String> foundClasses = new HashSet<String>(); for (PackInfo packInfo : packages) { ClassLoader cl = packInfo.getClassLoaderRef().get(); if (cl == null) { continue; } String packName = packInfo.getPackName(); URL resourceUrl = cl.getResource( packInfo.getPackClassName().replace('.', '/') + Files.CLASS_FILE_EXTENSION); if (resourceUrl != null) { WeldSELogger.LOG.scanningPackage(packName, resourceUrl); try { URI resourceUri = resourceUrl.toURI(); if (PROCOTOL_FILE.equals(resourceUrl.getProtocol())) { // Get the package directory, e.g. "file:///home/weld/org/jboss handleDir( new File(resourceUri).getParentFile(), packInfo.isScanRecursively(), packName, foundClasses); } else if (PROCOTOL_JAR.equals(resourceUrl.getProtocol())) { handleJar(resourceUri, packInfo.isScanRecursively(), packName, foundClasses); } else { WeldSELogger.LOG.resourceUrlProtocolNotSupported(resourceUrl); } } catch (URISyntaxException e) { CommonLogger.LOG.couldNotReadResource(resourceUrl, e); } } else { WeldSELogger.LOG.packageNotFound(packName); } } return foundClasses; }
private Class<?> loadClass(String className) { log.trace("Loading class with class loader: " + className); Class<?> clazz = null; try { clazz = classLoader.loadClass(className); } catch (ClassNotFoundException ex) { throw WeldSELogger.LOG.unableToLoadClass(className); } return clazz; }
/** * @param id * @param manager * @param bootstrap * @return the initialized Weld container */ static WeldContainer initialize(String id, WeldManager manager, Bootstrap bootstrap) { if (SINGLETON.isSet(id)) { throw WeldSELogger.LOG.weldContainerAlreadyRunning(id); } WeldContainer weldContainer = new WeldContainer(id, manager, bootstrap); SINGLETON.set(id, weldContainer); RUNNING_CONTAINER_IDS.add(id); WeldSELogger.LOG.weldContainerInitialized(id); manager.fireEvent(new ContainerInitialized(id), InitializedLiteral.APPLICATION); // If needed, register one shutdown hook for all containers if (isShutdownHookNeeded() && shutdownHook == null) { synchronized (LOCK) { if (shutdownHook == null) { shutdownHook = new ShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownHook); } } } return weldContainer; }
/** * Shutdown the container. * * @see Weld#initialize() */ public synchronized void shutdown() { if (isRunning()) { try { manager.fireEvent(new ContainerShutdown(id), DestroyedLiteral.APPLICATION); } finally { SINGLETON.clear(id); RUNNING_CONTAINER_IDS.remove(id); // Destroy all the dependent beans correctly creationalContext.release(); bootstrap.shutdown(); WeldSELogger.LOG.weldContainerShutdown(id); } } else { if (WeldSELogger.LOG.isTraceEnabled()) { WeldSELogger.LOG.tracev( "Spurious call to shutdown from: {0}", (Object[]) Thread.currentThread().getStackTrace()); } throw WeldSELogger.LOG.weldContainerAlreadyShutDown(id); } }
/** * Extensions to Weld SE can subclass and override this method to customize the deployment before * weld boots up. For example, to add a custom ResourceLoader, you would subclass Weld like so: * * <pre> * public class MyWeld extends Weld { * protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) { * return super.createDeployment(new MyResourceLoader(), bootstrap); * } * } * </pre> * * <p>This could then be used as normal: * * <pre> * WeldContainer container = new MyWeld().initialize(); * </pre> * * @param resourceLoader * @param bootstrap */ protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) { final Iterable<Metadata<Extension>> extensions = getExtensions(WeldResourceLoader.getClassLoader(), bootstrap); final TypeDiscoveryConfiguration typeDiscoveryConfiguration = bootstrap.startExtensions(extensions); final Deployment deployment; final Set<WeldBeanDeploymentArchive> beanArchives = new HashSet<WeldBeanDeploymentArchive>(); final Map<Class<? extends Service>, Service> additionalServices = new HashMap<>(); if (discoveryEnabled) { DiscoveryStrategy strategy = DiscoveryStrategyFactory.create( resourceLoader, bootstrap, ImmutableSet.<Class<? extends Annotation>>builder() .addAll(typeDiscoveryConfiguration.getKnownBeanDefiningAnnotations()) // Add ThreadScoped manually as Weld SE doesn't support implicit bean archives // without beans.xml .add(ThreadScoped.class) .build()); beanArchives.addAll(strategy.performDiscovery()); ClassFileServices classFileServices = strategy.getClassFileServices(); if (classFileServices != null) { additionalServices.put(ClassFileServices.class, classFileServices); } } if (isSyntheticBeanArchiveRequired()) { ImmutableSet.Builder<String> beanClassesBuilder = ImmutableSet.builder(); beanClassesBuilder.addAll(scanPackages()); WeldBeanDeploymentArchive syntheticBeanArchive = new WeldBeanDeploymentArchive( WeldDeployment.SYNTHETIC_BDA_ID, beanClassesBuilder.build(), buildSyntheticBeansXml(), Collections.emptySet(), ImmutableSet.copyOf(beanClasses)); beanArchives.add(syntheticBeanArchive); } if (beanArchives.isEmpty() && beanBuilders.isEmpty()) { throw WeldSELogger.LOG.weldContainerCannotBeInitializedNoBeanArchivesFound(); } Multimap<String, BeanDeploymentArchive> problems = BeanArchives.findBeanClassesDeployedInMultipleBeanArchives(beanArchives); if (!problems.isEmpty()) { // Right now, we only log a warning for each bean class deployed in multiple bean archives for (Entry<String, Collection<BeanDeploymentArchive>> entry : problems.entrySet()) { WeldSELogger.LOG.beanClassDeployedInMultipleBeanArchives( entry.getKey(), WeldCollections.toMultiRowString(entry.getValue())); } } String isolation = AccessController.doPrivileged( new GetSystemPropertyAction(ARCHIVE_ISOLATION_SYSTEM_PROPERTY)); if (isolation != null && Boolean.valueOf(isolation).equals(Boolean.FALSE)) { Set<WeldBeanDeploymentArchive> flatDeploymentArchives = new HashSet<WeldBeanDeploymentArchive>(); flatDeploymentArchives.add(WeldBeanDeploymentArchive.merge(bootstrap, beanArchives)); deployment = new WeldDeployment(resourceLoader, bootstrap, flatDeploymentArchives, extensions); CommonLogger.LOG.archiveIsolationDisabled(); } else { deployment = new WeldDeployment(resourceLoader, bootstrap, beanArchives, extensions); CommonLogger.LOG.archiveIsolationEnabled(); } deployment.getServices().addAll(additionalServices.entrySet()); return deployment; }