Search in sources :

Example 1 with PopulatorPostProcessor

use of org.glassfish.hk2.bootstrap.PopulatorPostProcessor in project Payara by payara.

the class ACCModulesManager method prepareHabitat.

/**
 * Sets up the HK2 habitat.
 * <p>
 * Must be invoked at least once before an AppClientContainerBuilder
 * returns a new AppClientContainer to the caller.
 * @param classLoader
 * @param logger
 * @throws com.sun.enterprise.module.bootstrap.BootException
 * @throws java.net.URISyntaxException
 */
private static ServiceLocator prepareHabitat(final ClassLoader loader) {
    ServiceLocator serviceLocator = ServiceLocatorFactory.getInstance().create("default");
    habitat = serviceLocator;
    ContextDuplicatePostProcessor duplicateProcessor = new ContextDuplicatePostProcessor();
    List<PopulatorPostProcessor> postProcessors = new LinkedList<PopulatorPostProcessor>();
    postProcessors.add(duplicateProcessor);
    try {
        HK2Populator.populate(serviceLocator, new ClasspathDescriptorFileFinder(loader), postProcessors);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return habitat;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ClasspathDescriptorFileFinder(org.glassfish.hk2.bootstrap.impl.ClasspathDescriptorFileFinder) ContextDuplicatePostProcessor(com.sun.enterprise.module.bootstrap.ContextDuplicatePostProcessor) PopulatorPostProcessor(org.glassfish.hk2.bootstrap.PopulatorPostProcessor) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Example 2 with PopulatorPostProcessor

use of org.glassfish.hk2.bootstrap.PopulatorPostProcessor in project Payara by payara.

the class StaticGlassFishRuntime method newGlassFish.

/**
 * Creates a new GlassFish instance and add it to a Map of instances
 * created by this runtime.
 *
 * @param glassFishProperties
 * @return
 * @throws Exception
 */
@Override
public synchronized GlassFish newGlassFish(GlassFishProperties glassFishProperties) throws GlassFishException {
    // some code to be executed which may be depending on the environment variable values.
    try {
        // Don't set temporarily created instanceroot in the user supplied
        // GlassFishProperties, hence clone it.
        Properties cloned = new Properties();
        cloned.putAll(glassFishProperties.getProperties());
        final GlassFishProperties gfProps = new GlassFishProperties(cloned);
        setEnv(gfProps);
        final StartupContext startupContext = new StartupContext(gfProps.getProperties());
        ModulesRegistry modulesRegistry = SingleHK2Factory.getInstance().createModulesRegistry();
        ServiceLocator serviceLocator = main.createServiceLocator(modulesRegistry, startupContext, Arrays.asList((PopulatorPostProcessor) new EmbeddedInhabitantsParser(), new ContextDuplicatePostProcessor()), null);
        final ModuleStartup gfKernel = main.findStartupService(modulesRegistry, serviceLocator, null, startupContext);
        // create a new GlassFish instance
        GlassFishImpl gfImpl = new GlassFishImpl(gfKernel, serviceLocator, gfProps.getProperties()) {

            @Override
            public void dispose() throws GlassFishException {
                try {
                    super.dispose();
                } finally {
                    gfMap.remove(gfProps.getInstanceRoot());
                    if ("true".equalsIgnoreCase(gfProps.getProperties().getProperty(autoDelete)) && gfProps.getInstanceRoot() != null) {
                        File instanceRoot = new File(gfProps.getInstanceRoot());
                        if (instanceRoot.exists()) {
                            // might have been deleted already.
                            Util.deleteRecursive(instanceRoot);
                        }
                    }
                }
            }
        };
        // Add this newly created instance to a Map
        gfMap.put(gfProps.getInstanceRoot(), gfImpl);
        return gfImpl;
    } catch (GlassFishException e) {
        throw e;
    } catch (Exception e) {
        throw new GlassFishException(e);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) StartupContext(com.sun.enterprise.module.bootstrap.StartupContext) ContextDuplicatePostProcessor(com.sun.enterprise.module.bootstrap.ContextDuplicatePostProcessor) PopulatorPostProcessor(org.glassfish.hk2.api.PopulatorPostProcessor) Hk2LoaderPopulatorPostProcessor(org.glassfish.hk2.bootstrap.impl.Hk2LoaderPopulatorPostProcessor) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) GlassFishException(org.glassfish.embeddable.GlassFishException) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ModulesRegistry(com.sun.enterprise.module.ModulesRegistry) File(java.io.File) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) ModuleStartup(com.sun.enterprise.module.bootstrap.ModuleStartup)

Example 3 with PopulatorPostProcessor

use of org.glassfish.hk2.bootstrap.PopulatorPostProcessor in project Payara by payara.

the class ApplicationInfo method populateApplicationServiceLocator.

/**
 * Populates the ApplicationServiceLocator with services using the current
 * appClassLoader.  Services must be described in files named
 * META-INF/hk2-locator/application.  {@link PopulationPostProcessor} may be defined
 * in the META-INF/services standard way
 *
 * @throws IOException On failure to read the service files
 */
private void populateApplicationServiceLocator() throws IOException {
    createServiceLocator();
    ServiceLoader<PopulatorPostProcessor> postProcessors = ServiceLoader.load(PopulatorPostProcessor.class, appClassLoader);
    LinkedList<PopulatorPostProcessor> allProcessors = new LinkedList<PopulatorPostProcessor>();
    for (PopulatorPostProcessor postProcessor : postProcessors) {
        allProcessors.add(postProcessor);
    }
    // Add this one AFTER all the other processors
    allProcessors.addLast(new ApplicationClassLoadingPostProcessor(appClassLoader));
    HK2Populator.populate(appServiceLocator, new ApplicationDescriptorFileFinder(appClassLoader, APPLICATION_LOADER_FILES), allProcessors);
    HashSet<ClassLoader> treatedLoaders = new HashSet<ClassLoader>();
    treatedLoaders.add(appClassLoader);
    for (ModuleInfo module : modules) {
        ClassLoader moduleClassLoader = module.getModuleClassLoader();
        if ((moduleClassLoader == null) || treatedLoaders.contains(moduleClassLoader)) {
            continue;
        }
        treatedLoaders.add(moduleClassLoader);
        allProcessors.removeLast();
        allProcessors.addLast(new ApplicationClassLoadingPostProcessor(moduleClassLoader));
        HK2Populator.populate(appServiceLocator, new ApplicationDescriptorFileFinder(moduleClassLoader, WEB_LOADER_FILES), allProcessors);
    }
}
Also used : PopulatorPostProcessor(org.glassfish.hk2.bootstrap.PopulatorPostProcessor)

Aggregations

ContextDuplicatePostProcessor (com.sun.enterprise.module.bootstrap.ContextDuplicatePostProcessor)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2 PopulatorPostProcessor (org.glassfish.hk2.bootstrap.PopulatorPostProcessor)2 ModulesRegistry (com.sun.enterprise.module.ModulesRegistry)1 ModuleStartup (com.sun.enterprise.module.bootstrap.ModuleStartup)1 StartupContext (com.sun.enterprise.module.bootstrap.StartupContext)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 GlassFishException (org.glassfish.embeddable.GlassFishException)1 GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)1 PopulatorPostProcessor (org.glassfish.hk2.api.PopulatorPostProcessor)1 ClasspathDescriptorFileFinder (org.glassfish.hk2.bootstrap.impl.ClasspathDescriptorFileFinder)1 Hk2LoaderPopulatorPostProcessor (org.glassfish.hk2.bootstrap.impl.Hk2LoaderPopulatorPostProcessor)1