Search in sources :

Example 11 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class ClassLoaderUtil method createConfigurer.

private static ClassLoaderConfigurer createConfigurer(final String key, final String impl) {
    try {
        final ObjectRecipe recipe = new ObjectRecipe(impl);
        for (final Map.Entry<Object, Object> entry : SystemInstance.get().getProperties().entrySet()) {
            final String entryKey = entry.getKey().toString();
            if (entryKey.startsWith(key)) {
                final String newKey = entryKey.substring(key.length());
                if (!"clazz".equals(newKey)) {
                    recipe.setProperty(newKey, entry.getValue());
                }
            }
        }
        final Object instance = recipe.create();
        if (instance instanceof ClassLoaderConfigurer) {
            return (ClassLoaderConfigurer) instance;
        } else {
            logger.error(impl + " is not a classlaoder configurer, using default behavior");
        }
    } catch (final Exception e) {
        logger.error("Can't create classloader configurer " + impl + ", using default behavior");
    }
    return null;
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 12 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class ListConfigurator method getList.

public static <T> List<T> getList(final Properties properties, final String key, final ClassLoader classloader, final Class<T> filter) {
    if (properties == null) {
        return null;
    }
    final String features = properties.getProperty(key);
    if (features == null) {
        return null;
    }
    final List<T> list = new ArrayList<T>();
    final String[] split = features.trim().split(",");
    for (final String feature : split) {
        if (feature == null || feature.trim().isEmpty()) {
            continue;
        }
        final String prefix = key + "." + feature + ".";
        final ObjectRecipe recipe = new ObjectRecipe(feature);
        for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
            final String current = entry.getKey().toString();
            if (current.startsWith(prefix)) {
                final String property = current.substring(prefix.length());
                recipe.setProperty(property, entry.getValue());
            }
        }
        final Object instance = recipe.create(classloader);
        if (!filter.isInstance(instance)) {
            throw new OpenEJBRuntimeException(feature + " is not an abstract feature");
        }
        list.add(filter.cast(instance));
    }
    if (list.isEmpty()) {
        return null;
    }
    return list;
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 13 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class ObjectRecipeHelper method createMeFromSystemProps.

public static Object createMeFromSystemProps(final String prefix, final String suffix, final Class<?> clazz) {
    final Properties props = SystemInstance.get().getProperties();
    final Map<String, Object> usedOnes = new HashMap<String, Object>();
    for (final Map.Entry<Object, Object> entry : props.entrySet()) {
        final String key = entry.getKey().toString();
        if (prefix != null && !key.startsWith(prefix)) {
            continue;
        }
        if (suffix != null && !key.endsWith(suffix)) {
            continue;
        }
        String newKey = key;
        if (prefix != null) {
            newKey = newKey.substring(prefix.length());
        }
        if (suffix != null) {
            newKey = newKey.substring(0, newKey.length() - suffix.length());
        }
        usedOnes.put(newKey, entry.getValue());
    }
    final ObjectRecipe recipe = new ObjectRecipe(clazz);
    recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
    recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
    recipe.allow(Option.PRIVATE_PROPERTIES);
    recipe.allow(Option.FIELD_INJECTION);
    recipe.allow(Option.NAMED_PARAMETERS);
    recipe.setAllProperties(usedOnes);
    return recipe.create();
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) HashMap(java.util.HashMap) Properties(java.util.Properties) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class MdbInstanceManager method deploy.

public void deploy(final BeanContext beanContext, final ActivationSpec activationSpec, final EndpointFactory endpointFactory) throws OpenEJBException {
    if (inboundRecovery != null) {
        inboundRecovery.recover(resourceAdapter, activationSpec, containerID.toString());
    }
    final ObjectRecipe recipe = PassthroughFactory.recipe(new Pool.Builder(poolBuilder));
    recipe.allow(Option.CASE_INSENSITIVE_FACTORY);
    recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
    recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
    recipe.setAllProperties(beanContext.getProperties());
    final Pool.Builder builder = (Pool.Builder) recipe.create();
    setDefault(builder.getMaxAge(), TimeUnit.HOURS);
    setDefault(builder.getIdleTimeout(), TimeUnit.MINUTES);
    setDefault(builder.getInterval(), TimeUnit.MINUTES);
    final InstanceSupplier supplier = new InstanceSupplier(beanContext);
    builder.setSupplier(supplier);
    builder.setExecutor(executor);
    builder.setScheduledExecutor(scheduledExecutor);
    final int min = builder.getMin();
    final long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
    final double maxAgeOffset = builder.getMaxAgeOffset();
    final Data data = new Data(builder.build(), accessTimeout, closeTimeout);
    MdbContext mdbContext = new MdbContext(securityService, new Flushable() {

        @Override
        public void flush() throws IOException {
            data.flush();
        }
    });
    data.setBaseContext(mdbContext);
    beanContext.setContainerData(data);
    final MBeanServer server = LocalMBeanServer.get();
    final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
    jmxName.set("J2EEServer", "openejb");
    jmxName.set("J2EEApplication", null);
    jmxName.set("EJBModule", beanContext.getModuleID());
    jmxName.set("MessageDrivenBean", beanContext.getEjbName());
    jmxName.set("j2eeType", "");
    jmxName.set("name", beanContext.getEjbName());
    // Create stats interceptor
    if (StatsInterceptor.isStatsActivated()) {
        final StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
        beanContext.addFirstSystemInterceptor(stats);
        // register the invocation stats interceptor
        try {
            final ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
            if (server.isRegistered(objectName)) {
                server.unregisterMBean(objectName);
            }
            server.registerMBean(new ManagedMBean(stats), objectName);
            jmxNames.add(objectName);
        } catch (final Exception e) {
            logger.error("Unable to register MBean ", e);
        }
    }
    // activate the endpoint
    try {
        final MdbPoolContainer.MdbActivationContext activationContext = new MdbPoolContainer.MdbActivationContext(Thread.currentThread().getContextClassLoader(), beanContext, resourceAdapter, endpointFactory, activationSpec);
        activationContexts.put(beanContext, activationContext);
        boolean activeOnStartup = true;
        String activeOnStartupSetting = beanContext.getActivationProperties().get("MdbActiveOnStartup");
        if (activeOnStartupSetting == null) {
            activeOnStartupSetting = beanContext.getActivationProperties().get("DeliveryActive");
        }
        if (activeOnStartupSetting != null) {
            activeOnStartup = Boolean.parseBoolean(activeOnStartupSetting);
        }
        if (activeOnStartup) {
            activationContext.start();
        } else {
            logger.info("Not auto-activating endpoint for " + beanContext.getDeploymentID());
        }
        String jmxControlName = beanContext.getActivationProperties().get("MdbJMXControl");
        if (jmxControlName == null) {
            jmxControlName = "true";
        }
        addJMxControl(beanContext, jmxControlName, activationContext);
    } catch (final ResourceException e) {
        throw new OpenEJBException(e);
    }
    final Options options = new Options(beanContext.getProperties());
    // Finally, fill the pool and start it
    if (!options.get("BackgroundStartup", false) && min > 0) {
        final ExecutorService es = Executors.newFixedThreadPool(min);
        for (int i = 0; i < min; i++) {
            es.submit(new InstanceCreatorRunnable(maxAge, i, min, maxAgeOffset, data, supplier));
        }
        es.shutdown();
        try {
            es.awaitTermination(5, TimeUnit.MINUTES);
        } catch (final InterruptedException e) {
            logger.error("can't fill the message driven bean pool", e);
        }
    }
    // register the pool
    try {
        final ObjectName objectName = jmxName.set("j2eeType", "Pool").build();
        if (server.isRegistered(objectName)) {
            server.unregisterMBean(objectName);
        }
        server.registerMBean(new ManagedMBean(data.pool), objectName);
        data.add(objectName);
    } catch (final Exception e) {
        logger.error("Unable to register MBean ", e);
    }
    data.getPool().start();
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Options(org.apache.openejb.loader.Options) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Pool(org.apache.openejb.util.Pool) ResourceException(javax.resource.ResourceException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) IOException(java.io.IOException) Flushable(java.io.Flushable) ResourceException(javax.resource.ResourceException) TimeoutException(java.util.concurrent.TimeoutException) ReflectionException(javax.management.ReflectionException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(org.apache.openejb.SystemException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ApplicationException(org.apache.openejb.ApplicationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ManagedMBean(org.apache.openejb.monitoring.ManagedMBean)

Example 15 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class MdbPoolContainer method createActivationSpec.

private ActivationSpec createActivationSpec(final BeanContext beanContext) throws OpenEJBException {
    try {
        // initialize the object recipe
        final ObjectRecipe objectRecipe = new ObjectRecipe(activationSpecClass);
        objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
        objectRecipe.disallow(Option.FIELD_INJECTION);
        final Map<String, String> activationProperties = beanContext.getActivationProperties();
        for (final Map.Entry<String, String> entry : activationProperties.entrySet()) {
            objectRecipe.setMethodProperty(entry.getKey(), entry.getValue());
        }
        objectRecipe.setMethodProperty("beanClass", beanContext.getBeanClass());
        // create the activationSpec
        final ActivationSpec activationSpec = (ActivationSpec) objectRecipe.create(activationSpecClass.getClassLoader());
        // verify all properties except "destination" and "destinationType" were consumed
        final Set<String> unusedProperties = new TreeSet<String>(objectRecipe.getUnsetProperties().keySet());
        unusedProperties.remove("destination");
        unusedProperties.remove("destinationType");
        unusedProperties.remove("destinationLookup");
        unusedProperties.remove("connectionFactoryLookup");
        unusedProperties.remove("beanClass");
        unusedProperties.remove("MdbActiveOnStartup");
        unusedProperties.remove("MdbJMXControl");
        unusedProperties.remove("DeliveryActive");
        if (!unusedProperties.isEmpty()) {
            final String text = "No setter found for the activation spec properties: " + unusedProperties;
            if (failOnUnknownActivationSpec) {
                throw new IllegalArgumentException(text);
            } else {
                logger.warning(text);
            }
        }
        // validate the activation spec
        try {
            activationSpec.validate();
        } catch (final UnsupportedOperationException uoe) {
            logger.info("ActivationSpec does not support validate. Implementation of validate is optional");
        }
        // also try validating using Bean Validation if there is a Validator available in the context.
        try {
            final Validator validator = (Validator) beanContext.getJndiContext().lookup("comp/Validator");
            final Set generalSet = validator.validate(activationSpec);
            if (!generalSet.isEmpty()) {
                throw new ConstraintViolationException("Constraint violation for ActivationSpec " + activationSpecClass.getName(), generalSet);
            }
        } catch (final NamingException e) {
            logger.debug("No Validator bound to JNDI context");
        }
        // set the resource adapter into the activation spec
        activationSpec.setResourceAdapter(resourceAdapter);
        return activationSpec;
    } catch (final Exception e) {
        throw new OpenEJBException("Unable to create activation spec", e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) TreeSet(java.util.TreeSet) Set(java.util.Set) ResourceException(javax.resource.ResourceException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) NamingException(javax.naming.NamingException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConstraintViolationException(javax.validation.ConstraintViolationException) SystemException(org.apache.openejb.SystemException) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) TreeSet(java.util.TreeSet) ActivationSpec(javax.resource.spi.ActivationSpec) ConstraintViolationException(javax.validation.ConstraintViolationException) NamingException(javax.naming.NamingException) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Validator(javax.validation.Validator)

Aggregations

ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)41 Map (java.util.Map)18 HashMap (java.util.HashMap)13 Properties (java.util.Properties)12 IOException (java.io.IOException)11 OpenEJBException (org.apache.openejb.OpenEJBException)9 NamingException (javax.naming.NamingException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Set (java.util.Set)4 TreeMap (java.util.TreeMap)4 ApplicationException (org.apache.openejb.ApplicationException)4 SystemException (org.apache.openejb.SystemException)4 SuperProperties (org.apache.openejb.util.SuperProperties)4 MalformedURLException (java.net.MalformedURLException)3 URISyntaxException (java.net.URISyntaxException)3 TimeoutException (java.util.concurrent.TimeoutException)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 Realm (org.apache.catalina.Realm)3 Connector (org.apache.catalina.connector.Connector)3