Search in sources :

Example 16 with Method

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

the class TransactionalInterceptorBase method getTransactionalAnnotation.

private Transactional getTransactionalAnnotation(InvocationContext invocationContext) {
    Optional<Transactional> optionalTransactional;
    // Try the Weld bindings first. This gives us the *exact* binding which caused this interceptor being called
    @SuppressWarnings("unchecked") Set<Annotation> bindings = (Set<Annotation>) invocationContext.getContextData().get("org.jboss.weld.interceptor.bindings");
    if (bindings != null) {
        optionalTransactional = bindings.stream().filter(annotation -> annotation.annotationType().equals(Transactional.class)).findAny().map(annotation -> Transactional.class.cast(annotation));
        if (optionalTransactional.isPresent()) {
            return optionalTransactional.get();
        }
    }
    BeanManager beanManager = CDI.current().getBeanManager();
    // Failing the Weld binding, check the method first
    optionalTransactional = getAnnotation(beanManager, invocationContext.getMethod(), Transactional.class);
    if (optionalTransactional.isPresent()) {
        return optionalTransactional.get();
    }
    // If nothing found on the method, check the the bean class
    optionalTransactional = getAnnotation(beanManager, invocationContext.getTarget().getClass(), Transactional.class);
    if (optionalTransactional.isPresent()) {
        return optionalTransactional.get();
    }
    // find it signals a critical error.
    throw new IllegalStateException("@Transactional not found on " + invocationContext.getTarget().getClass());
}
Also used : InvocationManager(org.glassfish.api.invocation.InvocationManager) InvocationContext(javax.interceptor.InvocationContext) Globals(org.glassfish.internal.api.Globals) LoggerInfo(org.glassfish.logging.annotation.LoggerInfo) NamingException(javax.naming.NamingException) SEVERE(java.util.logging.Level.SEVERE) Level(java.util.logging.Level) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) PreDestroy(javax.annotation.PreDestroy) NameNotFoundException(javax.naming.NameNotFoundException) FINE(java.util.logging.Level.FINE) Transaction(javax.transaction.Transaction) LogMessageInfo(org.glassfish.logging.annotation.LogMessageInfo) InitialContext(javax.naming.InitialContext) Transactional(javax.transaction.Transactional) WARNING(java.util.logging.Level.WARNING) Set(java.util.Set) CDI(javax.enterprise.inject.spi.CDI) Logger(java.util.logging.Logger) TransactionScopedCDIUtil.getAnnotation(org.glassfish.cdi.transaction.TransactionScopedCDIUtil.getAnnotation) Serializable(java.io.Serializable) SystemException(javax.transaction.SystemException) LogMessagesResourceBundle(org.glassfish.logging.annotation.LogMessagesResourceBundle) Annotation(java.lang.annotation.Annotation) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) TransactionOperationsManager(com.sun.enterprise.transaction.spi.TransactionOperationsManager) TransactionManager(javax.transaction.TransactionManager) BeanManager(javax.enterprise.inject.spi.BeanManager) Set(java.util.Set) BeanManager(javax.enterprise.inject.spi.BeanManager) TransactionScopedCDIUtil.getAnnotation(org.glassfish.cdi.transaction.TransactionScopedCDIUtil.getAnnotation) Annotation(java.lang.annotation.Annotation) Transactional(javax.transaction.Transactional)

Example 17 with Method

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

the class ConfigModularityUtils method getCurrentConfigBeanForDefaultValue.

public <T extends ConfigBeanProxy> T getCurrentConfigBeanForDefaultValue(ConfigBeanDefaultValue defaultValue) throws InvocationTargetException, IllegalAccessException {
    // TODO make this method target aware!
    Class parentClass = getOwningClassForLocation(defaultValue.getLocation());
    Class configBeanClass = getClassForFullName(defaultValue.getConfigBeanClassName());
    Method m = findSuitableCollectionGetter(parentClass, configBeanClass);
    if (m != null) {
        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) {
                // by default, people get the translated view.
                return new GlassFishConfigBean(serviceLocator, this, dom, configModel, xmlStreamReader);
            }
        };
        ConfigBeanProxy parent = getOwningObject(defaultValue.getLocation());
        ConfigurationPopulator populator = new ConfigurationPopulator(defaultValue.getXmlConfiguration(), doc, parent);
        populator.run(configParser);
        ConfigBeanProxy configBean = doc.getRoot().createProxy(configBeanClass);
        Collection col = (Collection) m.invoke(parent);
        return (T) getConfigBeanFromCollection(col, configBean, configBeanClass);
    }
    return null;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) XMLStreamReader(javax.xml.stream.XMLStreamReader) ConfigModel(org.jvnet.hk2.config.ConfigModel) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) GlassFishConfigBean(org.glassfish.config.support.GlassFishConfigBean) ConfigurationPopulator(com.sun.enterprise.config.modularity.parser.ConfigurationPopulator) ConfigParser(org.jvnet.hk2.config.ConfigParser) Collection(java.util.Collection) Method(java.lang.reflect.Method) DomDocument(org.jvnet.hk2.config.DomDocument)

Example 18 with Method

use of org.glassfish.hk2.external.org.objectweb.asm.commons.Method 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 19 with Method

use of org.glassfish.hk2.external.org.objectweb.asm.commons.Method 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)

Example 20 with Method

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

the class ResourceUtil method getMethodMetaData2.

public static MethodMetaData getMethodMetaData2(Dom parent, ConfigModel childModel, int parameterType) {
    MethodMetaData methodMetaData = new MethodMetaData();
    List<Class<?>> interfaces = new ArrayList<Class<?>>();
    Map<String, ParameterMetaData> params = new HashMap<String, ParameterMetaData>();
    try {
        Class<? extends ConfigBeanProxy> configBeanProxy = (Class<? extends ConfigBeanProxy>) childModel.classLoaderHolder.loadClass(childModel.targetTypeName);
        getInterfaces(configBeanProxy, interfaces);
        Set<String> attributeNames = childModel.getAttributeNames();
        for (String attributeName : attributeNames) {
            String methodName = ResourceUtil.getAttributeMethodName(attributeName);
            // camelCase the attributeName before passing out
            attributeName = Util.eleminateHypen(attributeName);
            ParameterMetaData parameterMetaData = params.get(attributeName);
            if (parameterMetaData == null) {
                parameterMetaData = new ParameterMetaData();
                params.put(attributeName, parameterMetaData);
            }
            // Check parent interfaces
            for (int i = interfaces.size() - 1; i >= 0; i--) {
                Class<?> intf = interfaces.get(i);
                try {
                    Method method = intf.getMethod(methodName);
                    Attribute attribute = method.getAnnotation(Attribute.class);
                    if (attribute != null) {
                        ParameterMetaData localParam = ResourceUtil.getParameterMetaData(attribute);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.DEFAULT_VALUE);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.KEY);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.TYPE);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.OPTIONAL);
                    }
                } catch (NoSuchMethodException e) {
                }
            }
            // Check ConfigBean
            try {
                Method method = configBeanProxy.getMethod(methodName);
                Attribute attribute = method.getAnnotation(Attribute.class);
                if (attribute != null) {
                    ParameterMetaData localParam = ResourceUtil.getParameterMetaData(attribute);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.DEFAULT_VALUE);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.KEY);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.TYPE);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.OPTIONAL);
                }
            } catch (NoSuchMethodException e) {
            }
            methodMetaData.putParameterMetaData(attributeName, parameterMetaData);
        }
    } catch (MultiException cnfe) {
        throw new RuntimeException(cnfe);
    }
    return methodMetaData;
}
Also used : HashMap(java.util.HashMap) Attribute(org.jvnet.hk2.config.Attribute) ArrayList(java.util.ArrayList) MethodMetaData(org.glassfish.admin.rest.provider.MethodMetaData) Method(java.lang.reflect.Method) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) MultiException(org.glassfish.hk2.api.MultiException) ParameterMetaData(org.glassfish.admin.rest.provider.ParameterMetaData)

Aggregations

MultiException (org.glassfish.hk2.api.MultiException)18 Method (java.lang.reflect.Method)16 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)12 GeneratorAdapter (org.glassfish.hk2.external.org.objectweb.asm.commons.GeneratorAdapter)8 Method (org.glassfish.hk2.external.org.objectweb.asm.commons.Method)8 PrivilegedActionException (java.security.PrivilegedActionException)7 ConnectorRuntimeException (com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)6 URISyntaxException (java.net.URISyntaxException)6 ExecutionException (java.util.concurrent.ExecutionException)6 ResourceAdapterInternalException (javax.resource.spi.ResourceAdapterInternalException)6 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)6 File (java.io.File)5 HashMap (java.util.HashMap)5 ConfigModel (org.jvnet.hk2.config.ConfigModel)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Attribute (org.jvnet.hk2.config.Attribute)4 PropertyVetoException (java.beans.PropertyVetoException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashSet (java.util.HashSet)3