Search in sources :

Example 26 with ObjectRecipe

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

the class FlushableDataSourceHandler method createANewDelegate.

private void createANewDelegate() {
    final CommonDataSource old = delegate.get();
    try {
        final ObjectRecipe recipe = new ObjectRecipe(DataSourceFactory.class.getName(), "create", FACTORY_ARGS);
        recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
        recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
        recipe.allow(Option.NAMED_PARAMETERS);
        recipe.allow(Option.PRIVATE_PROPERTIES);
        recipe.setAllProperties(config.properties);
        recipe.setProperty("resettableHandler", resettableHandler);
        recipe.setProperty("flushableHandler", this);
        updateDataSource(CommonDataSource.class.cast(recipe.create()));
    } catch (final Exception e) {
        LOGGER.error("Can't recreate the datasource, keeping old one", e);
        return;
    }
    if (DataSourceFactory.knows(old)) {
        try {
            DataSourceFactory.destroy(old);
        } catch (final Throwable t) {
        //Ignore
        }
    }
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) CommonDataSource(javax.sql.CommonDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 27 with ObjectRecipe

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

the class PoolDataSourceCreator method build.

protected <T> T build(final Class<T> clazz, final Properties properties) {
    final ObjectRecipe serviceRecipe = new ObjectRecipe(clazz);
    recipeOptions(serviceRecipe);
    serviceRecipe.setAllProperties(properties);
    final T value = (T) serviceRecipe.create();
    if (trackRecipeFor(value)) {
        // avoid to keep config objects
        recipes.put(value, serviceRecipe);
    }
    return value;
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe)

Example 28 with ObjectRecipe

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

the class PoolDataSourceCreator method build.

protected <T> T build(final Class<T> clazz, final Object instance, final Properties properties) {
    final ObjectRecipe recipe = PassthroughFactory.recipe(instance);
    recipeOptions(recipe);
    recipe.setAllProperties(properties);
    final T value = (T) recipe.create();
    recipes.put(value, recipe);
    return value;
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe)

Example 29 with ObjectRecipe

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

the class InjectionProcessor method construct.

private void construct() throws OpenEJBException {
    if (instance != null) {
        throw new IllegalStateException("Instance already constructed");
    }
    Class<? extends T> clazz = beanClass;
    final ObjectRecipe objectRecipe;
    if (suppliedInstance != null) {
        clazz = (Class<? extends T>) suppliedInstance.getClass();
        objectRecipe = PassthroughFactory.recipe(suppliedInstance);
    } else {
        objectRecipe = new ObjectRecipe(clazz);
    }
    objectRecipe.allow(Option.FIELD_INJECTION);
    objectRecipe.allow(Option.PRIVATE_PROPERTIES);
    objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
    objectRecipe.allow(Option.NAMED_PARAMETERS);
    fillInjectionProperties(objectRecipe);
    bindings.clear();
    for (final Entry<String, Object> entry : properties.entrySet()) {
        objectRecipe.setProperty(entry.getKey(), entry.getValue());
    }
    final Object object;
    try {
        object = objectRecipe.create(clazz.getClassLoader());
    } catch (final Exception e) {
        throw new OpenEJBException("Error while creating bean " + clazz.getName(), e);
    }
    final Map unsetProperties = objectRecipe.getUnsetProperties();
    if (unsetProperties.size() > 0) {
        for (final Object property : unsetProperties.keySet()) {
            logger.warning("Injection: No such property '" + property + "' in class " + clazz.getName());
        }
    }
    instance = clazz.cast(object);
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NamingException(javax.naming.NamingException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with ObjectRecipe

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

the class StatelessInstanceManager method deploy.

@SuppressWarnings("unchecked")
public void deploy(final BeanContext beanContext) throws OpenEJBException {
    final Options options = new Options(beanContext.getProperties());
    final Duration accessTimeout = getDuration(options, "AccessTimeout", // default timeout
    getDuration(options, "Timeout", this.accessTimeout, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
    final Duration closeTimeout = getDuration(options, "CloseTimeout", this.closeTimeout, TimeUnit.MINUTES);
    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 StatelessSupplier supplier = new StatelessSupplier(beanContext);
    builder.setSupplier(supplier);
    builder.setExecutor(executor);
    builder.setScheduledExecutor(scheduledExecutor);
    final Data data = new Data(builder.build(), accessTimeout, closeTimeout);
    beanContext.setContainerData(data);
    beanContext.set(EJBContext.class, data.sessionContext);
    try {
        final Context context = beanContext.getJndiEnc();
        context.bind("comp/EJBContext", data.sessionContext);
        context.bind("comp/WebServiceContext", new EjbWsContext(data.sessionContext));
        context.bind("comp/TimerService", new TimerServiceWrapper());
    } catch (final NamingException e) {
        throw new OpenEJBException("Failed to bind EJBContext/WebServiceContext/TimerService", e);
    }
    final int min = builder.getMin();
    final long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
    final double maxAgeOffset = builder.getMaxAgeOffset();
    final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
    jmxName.set("J2EEServer", "openejb");
    jmxName.set("J2EEApplication", null);
    jmxName.set("EJBModule", beanContext.getModuleID());
    jmxName.set("StatelessSessionBean", beanContext.getEjbName());
    jmxName.set("name", beanContext.getEjbName());
    final MBeanServer server = LocalMBeanServer.get();
    // Create stats interceptor
    if (StatsInterceptor.isStatsActivated()) {
        StatsInterceptor stats = null;
        for (final InterceptorInstance interceptor : beanContext.getUserAndSystemInterceptors()) {
            if (interceptor.getInterceptor() instanceof StatsInterceptor) {
                stats = (StatsInterceptor) interceptor.getInterceptor();
            }
        }
        if (stats == null) {
            // normally useless
            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);
            data.add(objectName);
        } catch (final Exception e) {
            logger.error("Unable to register MBean ", 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);
    }
    // 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 stateless pool", e);
        }
    }
    data.getPool().start();
}
Also used : Options(org.apache.openejb.loader.Options) OpenEJBException(org.apache.openejb.OpenEJBException) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) TimerServiceWrapper(org.apache.openejb.core.timer.TimerServiceWrapper) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Pool(org.apache.openejb.util.Pool) NamingException(javax.naming.NamingException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) SessionContext(javax.ejb.SessionContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) EJBContext(javax.ejb.EJBContext) InstanceContext(org.apache.openejb.core.InstanceContext) ThreadContext(org.apache.openejb.core.ThreadContext) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) Duration(org.apache.openejb.util.Duration) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorInstance(org.apache.openejb.core.interceptor.InterceptorInstance) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) SystemException(org.apache.openejb.SystemException) ApplicationException(org.apache.openejb.ApplicationException) IOException(java.io.IOException) 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)

Aggregations

ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)35 Map (java.util.Map)14 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 Properties (java.util.Properties)8 NamingException (javax.naming.NamingException)7 OpenEJBException (org.apache.openejb.OpenEJBException)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 SuperProperties (org.apache.openejb.util.SuperProperties)4 MalformedURLException (java.net.MalformedURLException)3 URISyntaxException (java.net.URISyntaxException)3 TreeMap (java.util.TreeMap)3 TimeoutException (java.util.concurrent.TimeoutException)3 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)3 InvalidObjectException (java.io.InvalidObjectException)2 ObjectStreamException (java.io.ObjectStreamException)2 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 DefinitionException (javax.enterprise.inject.spi.DefinitionException)2 DeploymentException (javax.enterprise.inject.spi.DeploymentException)2