Search in sources :

Example 76 with BeanContext

use of org.apache.openejb.BeanContext 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, data::flush);
    try {
        final Context context = beanContext.getJndiEnc();
        context.bind("comp/EJBContext", mdbContext);
        context.bind("comp/TimerService", new TimerServiceWrapper());
    } catch (final NamingException e) {
        throw new OpenEJBException("Failed to bind EJBContext/TimerService", e);
    }
    beanContext.set(EJBContext.class, mdbContext);
    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) TimerServiceWrapper(org.apache.openejb.core.timer.TimerServiceWrapper) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Pool(org.apache.openejb.util.Pool) NamingException(javax.naming.NamingException) ResourceException(javax.resource.ResourceException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) BaseContext(org.apache.openejb.core.BaseContext) 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) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) ResourceException(javax.resource.ResourceException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) 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) 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 77 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class BeanPropertiesTest method assertContexts.

private void assertContexts(final ContainerSystem containerSystem) {
    final BeanContext beanContext = containerSystem.getBeanContext("WidgetBean");
    final ModuleContext moduleContext = beanContext.getModuleContext();
    final AppContext appContext = moduleContext.getAppContext();
    {
        // Assert as Properties
        // BeanContext should have color property
        assertProperty(beanContext.getProperties(), "color", "orange");
        // ModuleContext and AppContext should not
        assertNoProperty(moduleContext.getProperties(), "color");
        assertNoProperty(appContext.getProperties(), "color");
        // Try all the above again with mixed case
        assertProperty(beanContext.getProperties(), "coLOr", "orange");
        assertNoProperty(moduleContext.getProperties(), "coLOr");
        assertNoProperty(appContext.getProperties(), "coLOr");
    }
    {
        // Assert as Options
        // ModuleContext should have color option
        assertOption(beanContext.getOptions(), "color", "orange");
        // AppContext and ModuleContext should remain unpolluted
        assertNoOption(moduleContext.getOptions(), "color");
        assertNoOption(appContext.getOptions(), "color");
        // Try all the above again using mixed case
        assertOption(beanContext.getOptions(), "coLoR", "orange");
        assertNoOption(moduleContext.getOptions(), "coLoR");
        assertNoOption(appContext.getOptions(), "coLoR");
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Example 78 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class ServerSideResolver method resolve.

@Override
public Object resolve(final EJBObjectHandler handler) {
    try {
        final EJBMetaDataImpl ejb = handler.getEjb();
        final InterfaceType interfaceType = (ejb.getRemoteInterfaceClass() == null) ? InterfaceType.BUSINESS_REMOTE_HOME : InterfaceType.EJB_HOME;
        final ArrayList<Class> interfaces = new ArrayList<>();
        if (interfaceType.isBusiness()) {
            interfaces.addAll(ejb.getBusinessClasses());
        } else {
            interfaces.add(ejb.getRemoteInterfaceClass());
        }
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        final BeanContext beanContext = containerSystem.getBeanContext(ejb.getDeploymentID());
        return EjbObjectProxyHandler.createProxy(beanContext, handler.getPrimaryKey(), interfaceType, interfaces, ejb.getMainInterface());
    } catch (Exception e) {
        Logger.getInstance(LogCategory.OPENEJB_SERVER_REMOTE, "org.apache.openejb.server.util.resources").error("ServerSideResolver.resolve() failed, falling back to ClientSideResolver: " + e.getClass().getName() + ": " + e.getMessage(), e);
        return new EJBObjectProxyHandle.ClientSideResovler().resolve(handler);
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) EJBMetaDataImpl(org.apache.openejb.client.EJBMetaDataImpl) InterfaceType(org.apache.openejb.InterfaceType) ArrayList(java.util.ArrayList)

Example 79 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class ServerSideResolver method resolve.

@Override
public Object resolve(final EJBHomeHandler handler) {
    try {
        final EJBMetaDataImpl ejb = handler.getEjb();
        final InterfaceType interfaceType = (ejb.getRemoteInterfaceClass() == null) ? InterfaceType.BUSINESS_REMOTE_HOME : InterfaceType.EJB_HOME;
        final ArrayList<Class> interfaces = new ArrayList<>();
        if (interfaceType.isBusiness()) {
            interfaces.addAll(ejb.getBusinessClasses());
        } else {
            interfaces.add(ejb.getRemoteInterfaceClass());
        }
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        final BeanContext beanContext = containerSystem.getBeanContext(ejb.getDeploymentID());
        return EjbHomeProxyHandler.createHomeProxy(beanContext, interfaceType, interfaces, ejb.getMainInterface());
    } catch (Exception e) {
        Logger.getInstance(LogCategory.OPENEJB_SERVER_REMOTE, "org.apache.openejb.server.util.resources").error("ServerSideResolver.resolve() failed, falling back to ClientSideResolver: " + e.getClass().getName() + ": " + e.getMessage(), e);
        return new EJBHomeProxyHandle.ClientSideResovler().resolve(handler);
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) EJBMetaDataImpl(org.apache.openejb.client.EJBMetaDataImpl) InterfaceType(org.apache.openejb.InterfaceType) ArrayList(java.util.ArrayList)

Example 80 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class DeploymentIndexTest method setUp.

@Before
public void setUp() throws SystemException {
    method = Method.class.getMethods()[0];
    beanContext = new BeanContext("aDeploymentId", null, new ModuleContext("", null, "", new AppContext("", SystemInstance.get(), null, null, null, false), null, null), DeploymentIndexTest.class, null, null, null, null, null, null, null, null, null, null, false, false);
    deploymentIndex = new DeploymentIndex(new BeanContext[] { beanContext, beanContext });
}
Also used : BeanContext(org.apache.openejb.BeanContext) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext) Before(org.junit.Before)

Aggregations

BeanContext (org.apache.openejb.BeanContext)198 OpenEJBException (org.apache.openejb.OpenEJBException)40 ThreadContext (org.apache.openejb.core.ThreadContext)40 Method (java.lang.reflect.Method)38 ContainerSystem (org.apache.openejb.spi.ContainerSystem)28 ArrayList (java.util.ArrayList)27 AppContext (org.apache.openejb.AppContext)26 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)26 NamingException (javax.naming.NamingException)24 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)23 Context (javax.naming.Context)22 ApplicationException (org.apache.openejb.ApplicationException)20 HashMap (java.util.HashMap)19 EJBLocalObject (javax.ejb.EJBLocalObject)18 EJBObject (javax.ejb.EJBObject)18 SystemException (org.apache.openejb.SystemException)18 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)17 ModuleContext (org.apache.openejb.ModuleContext)16 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)16 FinderException (javax.ejb.FinderException)14