Search in sources :

Example 16 with Type

use of org.glassfish.hk2.external.org.objectweb.asm.Type in project Payara by payara.

the class HK2IntegrationExtension method afterDeploymentValidation.

/**
 * Called by CDI after it has been completely validated.  Will add the JIT resolver to HK2
 * with the BeanManager
 *
 * @param event This is just to mark the type of the event
 * @param manager The manager that will be used to get references
 */
@SuppressWarnings("unused")
private void afterDeploymentValidation(@Observes AfterDeploymentValidation event) {
    if (locator == null)
        return;
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    config.addActiveDescriptor(CDISecondChanceResolver.class);
    config.addActiveDescriptor(CDIContextBridge.class);
    config.commit();
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration)

Example 17 with Type

use of org.glassfish.hk2.external.org.objectweb.asm.Type in project Payara by payara.

the class ConfigurationParser method parseAndSetConfigBean.

/**
 * @param <T> the ConfigBeanProxy type we are looking for
 */
public <T extends ConfigBeanProxy> void parseAndSetConfigBean(List<ConfigBeanDefaultValue> values) {
    ConfigParser configParser = new ConfigParser(serviceLocator);
    // I don't use the GlassFish document here as I don't need persistence
    final DomDocument doc = new DomDocument<GlassFishConfigBean>(serviceLocator) {

        @Override
        public Dom make(final ServiceLocator serviceLocator, XMLStreamReader xmlStreamReader, GlassFishConfigBean dom, ConfigModel configModel) {
            return new GlassFishConfigBean(serviceLocator, this, dom, configModel, xmlStreamReader);
        }
    };
    // the solution is to put the loop into the apply method...  But it would be some fine amount of work
    for (final ConfigBeanDefaultValue configBeanDefaultValue : values) {
        final ConfigBeanProxy parent = configModularityUtils.getOwningObject(configBeanDefaultValue.getLocation());
        if (parent == null)
            continue;
        ConfigurationPopulator populator = null;
        if (replaceSystemProperties)
            try {
                populator = new ConfigurationPopulator(configModularityUtils.replacePropertiesWithCurrentValue(configBeanDefaultValue.getXmlConfiguration(), configBeanDefaultValue), doc, parent);
            } catch (Exception e) {
                LOG.log(Level.SEVERE, ConfigApiLoggerInfo.CFG_EXT_ADD_FAILED, e);
            }
        else {
            // Check that parent is not null!
            populator = new ConfigurationPopulator(configBeanDefaultValue.getXmlConfiguration(), doc, parent);
        }
        populator.run(configParser);
        synchronized (configModularityUtils) {
            boolean oldValue = configModularityUtils.isIgnorePersisting();
            try {
                Class configBeanClass = configModularityUtils.getClassForFullName(configBeanDefaultValue.getConfigBeanClassName());
                final ConfigBeanProxy pr = doc.getRoot().createProxy(configBeanClass);
                configModularityUtils.setIgnorePersisting(true);
                ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {

                    public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
                        configModularityUtils.setConfigBean(pr, configBeanDefaultValue, param);
                        return param;
                    }
                }, parent);
            } catch (TransactionFailure e) {
                LOG.log(Level.SEVERE, ConfigApiLoggerInfo.CFG_EXT_ADD_FAILED, e);
            } finally {
                configModularityUtils.setIgnorePersisting(oldValue);
            }
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) XMLStreamReader(javax.xml.stream.XMLStreamReader) ConfigParser(org.jvnet.hk2.config.ConfigParser) PropertyVetoException(java.beans.PropertyVetoException) DomDocument(org.jvnet.hk2.config.DomDocument) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) PropertyVetoException(java.beans.PropertyVetoException) ConfigModel(org.jvnet.hk2.config.ConfigModel) ConfigBeanDefaultValue(com.sun.enterprise.config.modularity.customization.ConfigBeanDefaultValue) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) GlassFishConfigBean(org.glassfish.config.support.GlassFishConfigBean)

Example 18 with Type

use of org.glassfish.hk2.external.org.objectweb.asm.Type in project Payara by payara.

the class Realm method doInstantiate.

/**
 * Instantiates a Realm class of the given type and invokes its init()
 */
private static synchronized Realm doInstantiate(String name, String className, Properties props) throws BadRealmException {
    ServiceLocator habitat = Globals.getDefaultHabitat();
    RealmsManager mgr = null;
    try {
        mgr = getRealmsManager();
        Class realmClass = null;
        // try a HK2 route first
        Realm r = habitat.getService(Realm.class, name);
        if (r == null) {
            try {
                // TODO: workaround here. Once fixed in V3 we should be able to use
                // Context ClassLoader instead.
                ClassLoaderHierarchy hierarchy = habitat.getService(ClassLoaderHierarchy.class);
                realmClass = hierarchy.getCommonClassLoader().loadClass(className);
                Object obj = realmClass.newInstance();
                r = (Realm) obj;
            } catch (ClassNotFoundException ex) {
                realmClass = Class.forName(className);
                Object obj = realmClass.newInstance();
                r = (Realm) obj;
            }
        }
        r.setName(name);
        r.init(props);
        if (mgr == null) {
            throw new BadRealmException("Unable to locate RealmsManager Service");
        }
        _logger.log(Level.FINER, SecurityLoggerInfo.realmCreated, new Object[] { name, className });
        return r;
    } catch (NoSuchRealmException | InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
        throw new BadRealmException(ex);
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ClassLoaderHierarchy(org.glassfish.internal.api.ClassLoaderHierarchy)

Example 19 with Type

use of org.glassfish.hk2.external.org.objectweb.asm.Type in project Payara by payara.

the class CombinedJavaConfigSystemPropertyListener method logFine.

private void logFine(TYPE ct, JavaConfig njc) {
    final Level level = Level.FINE;
    if (logger.isLoggable(level)) {
        logger.log(level, "<java-config> changed");
        int os = oldProps.size(), ns = njc.getJvmOptions().size();
        if (os > ns) {
            logger.log(level, "a system property or a JVM option was removed (old size = {0}), new size: ({1}), restart is required, based on the property", new Object[] { os, ns });
        } else if (os < ns) {
            logger.log(level, "a system property or a JVM option was added, (old size = {0}), new size: ({1}), restart is required, based on the property", new Object[] { os, ns });
        } else {
            logger.log(level, "an attribute was changed, restart required");
        }
    }
}
Also used : Level(java.util.logging.Level) RunLevel(org.glassfish.hk2.runlevel.RunLevel)

Example 20 with Type

use of org.glassfish.hk2.external.org.objectweb.asm.Type in project Payara by payara.

the class ApplicationLifecycle method setupContainerInfos.

/**
 * set up containers and prepare the sorted ModuleInfos
 * @param handler
 * @param sniffers
 * @param context
 * @return
 * @throws java.lang.Exception
 */
@Override
public List<EngineInfo> setupContainerInfos(final ArchiveHandler handler, Collection<? extends Sniffer> sniffers, DeploymentContext context) throws Exception {
    final ActionReport report = context.getActionReport();
    DeploymentTracing tracing = context.getModuleMetaData(DeploymentTracing.class);
    Map<Deployer, EngineInfo> containerInfosByDeployers = new LinkedHashMap<Deployer, EngineInfo>();
    for (Sniffer sniffer : sniffers) {
        if (sniffer.getContainersNames() == null || sniffer.getContainersNames().length == 0) {
            report.failure(logger, "no container associated with application of type : " + sniffer.getModuleType(), null);
            return null;
        }
        final String containerName = sniffer.getContainersNames()[0];
        if (tracing != null) {
            tracing.addContainerMark(DeploymentTracing.ContainerMark.SNIFFER_DONE, containerName);
        }
        // start all the containers associated with sniffers.
        EngineInfo engineInfo = containerRegistry.getContainer(containerName);
        if (engineInfo == null) {
            // need to synchronize on the registry to not end up starting the same container from
            // different threads.
            Collection<EngineInfo> containersInfo = null;
            synchronized (containerRegistry) {
                if (containerRegistry.getContainer(containerName) == null) {
                    if (tracing != null) {
                        tracing.addContainerMark(DeploymentTracing.ContainerMark.BEFORE_CONTAINER_SETUP, containerName);
                    }
                    containersInfo = setupContainer(sniffer, logger, context);
                    if (tracing != null) {
                        tracing.addContainerMark(DeploymentTracing.ContainerMark.AFTER_CONTAINER_SETUP, containerName);
                    }
                    if (containersInfo == null || containersInfo.size() == 0) {
                        String msg = "Cannot start container(s) associated to application of type : " + sniffer.getModuleType();
                        report.failure(logger, msg, null);
                        throw new Exception(msg);
                    }
                }
            }
            // now start all containers, by now, they should be all setup...
            if (containersInfo != null && !startContainers(containersInfo, logger, context)) {
                final String msg = "Aborting, Failed to start container " + containerName;
                report.failure(logger, msg, null);
                throw new Exception(msg);
            }
        }
        engineInfo = containerRegistry.getContainer(sniffer.getContainersNames()[0]);
        if (tracing != null) {
            tracing.addContainerMark(DeploymentTracing.ContainerMark.GOT_CONTAINER, containerName);
        }
        if (engineInfo == null) {
            final String msg = "Aborting, Failed to start container " + containerName;
            report.failure(logger, msg, null);
            throw new Exception(msg);
        }
        Deployer deployer = getDeployer(engineInfo);
        if (deployer == null) {
            if (!startContainers(Collections.singleton(engineInfo), logger, context)) {
                final String msg = "Aborting, Failed to start container " + containerName;
                report.failure(logger, msg, null);
                throw new Exception(msg);
            }
            deployer = getDeployer(engineInfo);
            if (deployer == null) {
                report.failure(logger, "Got a null deployer out of the " + engineInfo.getContainer().getClass() + " container, is it annotated with @Service ?");
                return null;
            }
        }
        if (tracing != null) {
            tracing.addContainerMark(DeploymentTracing.ContainerMark.GOT_DEPLOYER, containerName);
        }
        containerInfosByDeployers.put(deployer, engineInfo);
    }
    // all containers that have recognized parts of the application being deployed
    // have now been successfully started. Start the deployment process.
    List<ApplicationMetaDataProvider> providers = new LinkedList<ApplicationMetaDataProvider>();
    providers.addAll(habitat.<ApplicationMetaDataProvider>getAllServices(ApplicationMetaDataProvider.class));
    List<EngineInfo> sortedEngineInfos = new ArrayList<EngineInfo>();
    Map<Class, ApplicationMetaDataProvider> typeByProvider = new HashMap<Class, ApplicationMetaDataProvider>();
    for (ApplicationMetaDataProvider provider : habitat.<ApplicationMetaDataProvider>getAllServices(ApplicationMetaDataProvider.class)) {
        if (provider.getMetaData() != null) {
            for (Class provided : provider.getMetaData().provides()) {
                typeByProvider.put(provided, provider);
            }
        }
    }
    // check if everything is provided.
    for (ApplicationMetaDataProvider provider : habitat.<ApplicationMetaDataProvider>getAllServices(ApplicationMetaDataProvider.class)) {
        if (provider.getMetaData() != null) {
            for (Class dependency : provider.getMetaData().requires()) {
                if (!typeByProvider.containsKey(dependency)) {
                    // at this point, I only log problems, because it maybe that what I am deploying now
                    // will not require this application metadata.
                    logger.log(Level.WARNING, KernelLoggerInfo.applicationMetaDataProvider, new Object[] { provider, dependency });
                }
            }
        }
    }
    Map<Class, Deployer> typeByDeployer = new HashMap<Class, Deployer>();
    for (Deployer deployer : containerInfosByDeployers.keySet()) {
        if (deployer.getMetaData() != null) {
            for (Class provided : deployer.getMetaData().provides()) {
                typeByDeployer.put(provided, deployer);
            }
        }
    }
    for (Deployer deployer : containerInfosByDeployers.keySet()) {
        if (deployer.getMetaData() != null) {
            for (Class dependency : deployer.getMetaData().requires()) {
                if (!typeByDeployer.containsKey(dependency) && !typeByProvider.containsKey(dependency)) {
                    Service s = deployer.getClass().getAnnotation(Service.class);
                    String serviceName;
                    if (s != null && s.name() != null && s.name().length() > 0) {
                        serviceName = s.name();
                    } else {
                        serviceName = deployer.getClass().getSimpleName();
                    }
                    report.failure(logger, serviceName + " deployer requires " + dependency + " but no other deployer provides it", null);
                    return null;
                }
            }
        }
    }
    // ok everything is satisfied, just a matter of running things in order
    List<Deployer> orderedDeployers = new ArrayList<Deployer>();
    for (Deployer deployer : containerInfosByDeployers.keySet()) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Keyed Deployer " + deployer.getClass());
        }
        loadDeployer(orderedDeployers, deployer, typeByDeployer, typeByProvider, context);
    }
    // now load metadata from deployers.
    for (Deployer deployer : orderedDeployers) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Ordered Deployer " + deployer.getClass());
        }
        final MetaData metadata = deployer.getMetaData();
        try {
            if (metadata != null) {
                if (metadata.provides() == null || metadata.provides().length == 0) {
                    deployer.loadMetaData(null, context);
                } else {
                    for (Class<?> provide : metadata.provides()) {
                        if (context.getModuleMetaData(provide) == null) {
                            context.addModuleMetaData(deployer.loadMetaData(provide, context));
                        } else {
                            deployer.loadMetaData(null, context);
                        }
                    }
                }
            } else {
                deployer.loadMetaData(null, context);
            }
        } catch (Exception e) {
            report.failure(logger, "Exception while invoking " + deployer.getClass() + " prepare method", e);
            throw e;
        }
        sortedEngineInfos.add(containerInfosByDeployers.get(deployer));
    }
    return sortedEngineInfos;
}
Also used : Service(org.jvnet.hk2.annotations.Service) Sniffer(org.glassfish.api.container.Sniffer) PropertyVetoException(java.beans.PropertyVetoException) RetryableException(org.jvnet.hk2.config.RetryableException) MultiException(org.glassfish.hk2.api.MultiException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException) DeploymentTracing(org.glassfish.internal.deployment.DeploymentTracing)

Aggregations

ServiceLocator (org.glassfish.hk2.api.ServiceLocator)18 ActionReport (org.glassfish.api.ActionReport)13 MultiException (org.glassfish.hk2.api.MultiException)13 ParameterMap (org.glassfish.api.admin.ParameterMap)12 List (java.util.List)10 Map (java.util.Map)10 GeneratorAdapter (org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter)7 Method (org.glassfish.hk2.external.org.objectweb.asm.commons.Method)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Sniffer (org.glassfish.api.container.Sniffer)6 CommandRunner (org.glassfish.embeddable.CommandRunner)6 File (java.io.File)5 Types (org.glassfish.hk2.classmodel.reflect.Types)5 PropertyVetoException (java.beans.PropertyVetoException)4 Method (java.lang.reflect.Method)4 Type (java.lang.reflect.Type)4 JsonString (javax.json.JsonString)4 PrivilegedActionException (java.security.PrivilegedActionException)3