public SeContainer initialize()

in cdi-owb/src/main/java/org/apache/aries/cdi/owb/core/OWBCDIContainerInitializer.java [105:219]


	public SeContainer initialize() {
		requireNonNull(spiLoader).handleResources(
			s -> (s != null) && s.startsWith("META-INF/openwebbeans/"),
			this::getResources
		).findClass(
			s -> (s != null) && (s.startsWith("org.apache.webbeans.") || s.startsWith("sun.misc.")),
			this::loadClass);

		spiLoader.getBundles().add(owbBundleContext.getBundle());

		BundleWiring bundleWiring = owbBundleContext.getBundle().adapt(BundleWiring.class);
		List<BundleWire> requiredWires = bundleWiring.getRequiredWires(PACKAGE_NAMESPACE);

		for (BundleWire bundleWire : requiredWires) {
			BundleCapability capability = bundleWire.getCapability();
			Map<String, Object> attributes = capability.getAttributes();
			String packageName = (String)attributes.get(PACKAGE_NAMESPACE);
			if (!packageName.startsWith("org.apache.webbeans.")) {
				continue;
			}

			Bundle wireBundle = bundleWire.getProvider().getBundle();
			if (!spiLoader.getBundles().contains(wireBundle)) {
				spiLoader.getBundles().add(wireBundle);
			}
		}

		Thread currentThread = Thread.currentThread();
		ClassLoader current = currentThread.getContextClassLoader();

		try {
			currentThread.setContextClassLoader(spiLoader);
			startObject = requireNonNull(clientBundleContext);

			final Map<Class<?>, Object> services = new HashMap<>();
			properties.setProperty(
				DefiningClassService.class.getName(),
				OSGiDefiningClassService.class.getName());

			services.put(
				OSGiDefiningClassService.ClassLoaders.class,
				new OSGiDefiningClassService.ClassLoaders(current, spiLoader));
			services.put(
				ApplicationBoundaryService.class,
				new OsgiApplicationBoundaryService(current, spiLoader));
			services.put(
				ScannerService.class,
				new CdiScannerService(beanClasses, beanDescriptorURLs));
			services.put(BundleContext.class, clientBundleContext);

			// If we find the OpenWebBeans "aries.cdi.http" Extension enable web mode.
			// This Extension will have properties:
			//    osgi.cdi.extension = aries.cdi.http
			//    aries.cdi.http.provider = OpenWebBeans
			extensions.entrySet().stream()
					.filter(it -> StartObjectSupplier.class.isInstance(it.getKey()))
					.max(comparing(it -> StartObjectSupplier.class.cast(it.getKey()).ordinal()))
					.ifPresent(entry -> {
						// The service properties of the extension should list any properties needed
						// to configure OWB for web support.
						properties.putAll(entry.getValue());

						// Extract the start instance to ensure it works with the configured services (properties)
						final StartObjectSupplier<?> objectSupplier = StartObjectSupplier.class.cast(entry.getKey());
						properties.putAll(objectSupplier.properties());
						startObject = objectSupplier.getStartObject();
					});

			bootstrap = new WebBeansContext(services, properties) {
				private final ExtensionLoader overridenExtensionLoader = new ExtensionLoader(this) {
					private boolean added;

					@Override
					public void loadExtensionServices() {
						if (added) {
							return;
						}
						try {
							Method method = ExtensionLoader.class.getDeclaredMethod("addExtensions", List.class);
							method.invoke(this, extensions.keySet().stream().collect(Collectors.toList()));
						}
						catch (ReflectiveOperationException nsme1) {
							try {
								Method method = ExtensionLoader.class.getDeclaredMethod("addExtension", Extension.class);
								for (Entry<Extension, Map<String, Object>> entry : extensions.entrySet()) {
									method.invoke(this, entry.getKey());
								}
							}
							catch (ReflectiveOperationException nsme2) {
								throw new RuntimeException(
									"Could not locate either addExtensions(List) or addExtension(Extension) on " +
										ExtensionLoader.class.getName());
							}
						}
						added = true;
					}
				};

				@Override
				public ExtensionLoader getExtensionLoader() {
					return overridenExtensionLoader;
				}
			};

			final DefaultSingletonService singletonService = getSingletonService();
			singletonService.register(spiLoader, bootstrap);
			final ContainerLifecycle lifecycle = bootstrap.getService(ContainerLifecycle.class);
			lifecycle.startApplication(startObject);

			return new OWBSeContainer();
		}
		finally {
			currentThread.setContextClassLoader(current);
		}
	}