Search in sources :

Example 1 with MethodContext

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

the class Assembler method initEjbs.

public List<BeanContext> initEjbs(final ClassLoader classLoader, final AppInfo appInfo, final AppContext appContext, final Set<Injection> injections, final List<BeanContext> allDeployments, final String webappId) throws OpenEJBException {
    final String globalTimersOn = SystemInstance.get().getProperty(OPENEJB_TIMERS_ON, "true");
    final EjbJarBuilder ejbJarBuilder = new EjbJarBuilder(props, appContext);
    for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
        if (isSkip(appInfo, webappId, ejbJar)) {
            continue;
        }
        final HashMap<String, BeanContext> deployments = ejbJarBuilder.build(ejbJar, injections, classLoader);
        final JaccPermissionsBuilder jaccPermissionsBuilder = new JaccPermissionsBuilder();
        final PolicyContext policyContext = jaccPermissionsBuilder.build(ejbJar, deployments);
        jaccPermissionsBuilder.install(policyContext);
        final TransactionPolicyFactory transactionPolicyFactory = createTransactionPolicyFactory(ejbJar, classLoader);
        for (final BeanContext beanContext : deployments.values()) {
            beanContext.setTransactionPolicyFactory(transactionPolicyFactory);
        }
        final MethodTransactionBuilder methodTransactionBuilder = new MethodTransactionBuilder();
        methodTransactionBuilder.build(deployments, ejbJar.methodTransactions);
        final MethodConcurrencyBuilder methodConcurrencyBuilder = new MethodConcurrencyBuilder();
        methodConcurrencyBuilder.build(deployments, ejbJar.methodConcurrency);
        for (final BeanContext beanContext : deployments.values()) {
            containerSystem.addDeployment(beanContext);
        }
        // bind ejbs into global jndi
        jndiBuilder.build(ejbJar, deployments);
        // setup timers/asynchronous methods - must be after transaction attributes are set
        for (final BeanContext beanContext : deployments.values()) {
            if (beanContext.getComponentType() != BeanType.STATEFUL) {
                final Method ejbTimeout = beanContext.getEjbTimeout();
                boolean timerServiceRequired = false;
                if (ejbTimeout != null) {
                    // If user set the tx attribute to RequiresNew change it to Required so a new transaction is not started
                    if (beanContext.getTransactionType(ejbTimeout) == TransactionType.RequiresNew) {
                        beanContext.setMethodTransactionAttribute(ejbTimeout, TransactionType.Required);
                    }
                    timerServiceRequired = true;
                }
                for (final Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
                    final Map.Entry<Method, MethodContext> entry = it.next();
                    final MethodContext methodContext = entry.getValue();
                    if (methodContext.getSchedules().size() > 0) {
                        timerServiceRequired = true;
                        final Method method = entry.getKey();
                        // TODO Need ?
                        if (beanContext.getTransactionType(method) == TransactionType.RequiresNew) {
                            beanContext.setMethodTransactionAttribute(method, TransactionType.Required);
                        }
                    }
                }
                if (timerServiceRequired && "true".equalsIgnoreCase(appInfo.properties.getProperty(OPENEJB_TIMERS_ON, globalTimersOn))) {
                    // Create the timer
                    final EjbTimerServiceImpl timerService = new EjbTimerServiceImpl(beanContext, newTimerStore(beanContext));
                    // Load auto-start timers
                    final TimerStore timerStore = timerService.getTimerStore();
                    for (final Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
                        final Map.Entry<Method, MethodContext> entry = it.next();
                        final MethodContext methodContext = entry.getValue();
                        for (final ScheduleData scheduleData : methodContext.getSchedules()) {
                            timerStore.createCalendarTimer(timerService, (String) beanContext.getDeploymentID(), null, entry.getKey(), scheduleData.getExpression(), scheduleData.getConfig(), true);
                        }
                    }
                    beanContext.setEjbTimerService(timerService);
                } else {
                    beanContext.setEjbTimerService(new NullEjbTimerServiceImpl());
                }
            }
            // TODO ???
            for (final Iterator<Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
                final Entry<Method, MethodContext> entry = it.next();
                if (entry.getValue().isAsynchronous() && beanContext.getTransactionType(entry.getKey()) == TransactionType.RequiresNew) {
                    beanContext.setMethodTransactionAttribute(entry.getKey(), TransactionType.Required);
                }
            }
            // if local bean or mdb generate proxy class now to avoid bottleneck on classloader later
            if (beanContext.isLocalbean() && !beanContext.getComponentType().isMessageDriven() && !beanContext.isDynamicallyImplemented()) {
                final List<Class> interfaces = new ArrayList<Class>(3);
                interfaces.add(Serializable.class);
                interfaces.add(IntraVmProxy.class);
                final BeanType type = beanContext.getComponentType();
                if (BeanType.STATEFUL.equals(type) || BeanType.MANAGED.equals(type)) {
                    interfaces.add(BeanContext.Removable.class);
                }
                beanContext.set(BeanContext.ProxyClass.class, new BeanContext.ProxyClass(beanContext, interfaces.toArray(new Class<?>[interfaces.size()])));
            }
        }
        // process application exceptions
        for (final ApplicationExceptionInfo exceptionInfo : ejbJar.applicationException) {
            try {
                final Class exceptionClass = classLoader.loadClass(exceptionInfo.exceptionClass);
                for (final BeanContext beanContext : deployments.values()) {
                    beanContext.addApplicationException(exceptionClass, exceptionInfo.rollback, exceptionInfo.inherited);
                }
            } catch (final ClassNotFoundException e) {
                logger.error("createApplication.invalidClass", e, exceptionInfo.exceptionClass, e.getMessage());
            }
        }
        allDeployments.addAll(deployments.values());
    }
    final List<BeanContext> ejbs = sort(allDeployments);
    for (final BeanContext b : ejbs) {
        // otherwise for ears we have duplicated beans
        if (appContext.getBeanContexts().contains(b)) {
            continue;
        }
        appContext.getBeanContexts().add(b);
    }
    return ejbs;
}
Also used : ScheduleData(org.apache.openejb.core.timer.ScheduleData) ArrayList(java.util.ArrayList) TimerStore(org.apache.openejb.core.timer.TimerStore) MemoryTimerStore(org.apache.openejb.core.timer.MemoryTimerStore) Entry(java.util.Map.Entry) JtaTransactionPolicyFactory(org.apache.openejb.core.transaction.JtaTransactionPolicyFactory) TransactionPolicyFactory(org.apache.openejb.core.transaction.TransactionPolicyFactory) MethodContext(org.apache.openejb.MethodContext) NullEjbTimerServiceImpl(org.apache.openejb.core.timer.NullEjbTimerServiceImpl) Method(java.lang.reflect.Method) EjbTimerServiceImpl(org.apache.openejb.core.timer.EjbTimerServiceImpl) NullEjbTimerServiceImpl(org.apache.openejb.core.timer.NullEjbTimerServiceImpl) BeanContext(org.apache.openejb.BeanContext) BeanType(org.apache.openejb.BeanType) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 2 with MethodContext

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

the class MethodConcurrencyBuilder method applyConcurrencyAttributes.

public static void applyConcurrencyAttributes(final BeanContext beanContext, final List<MethodConcurrencyInfo> methodConcurrencyInfos) throws OpenEJBException {
    if (beanContext.isBeanManagedConcurrency()) {
        return;
    }
    final Logger log = Logger.getInstance(LogCategory.OPENEJB_STARTUP.createChild("attributes"), MethodConcurrencyBuilder.class);
    final List<MethodConcurrencyInfo> lockInfos = new ArrayList<MethodConcurrencyInfo>();
    final List<MethodConcurrencyInfo> accessTimeoutInfos = new ArrayList<MethodConcurrencyInfo>();
    MethodConcurrencyBuilder.normalize(methodConcurrencyInfos, lockInfos, accessTimeoutInfos);
    Map<Method, MethodAttributeInfo> attributes;
    // handle @Lock
    attributes = MethodInfoUtil.resolveAttributes(lockInfos, beanContext);
    if (log.isDebugEnabled()) {
        for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
            final Method method = entry.getKey();
            final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
            log.debug("Lock: " + method + " -- " + MethodInfoUtil.toString(value.methods.get(0)) + " " + value.concurrencyAttribute);
        }
    }
    for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
        final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
        final MethodContext methodContext = beanContext.getMethodContext(entry.getKey());
        final String s = value.concurrencyAttribute.toUpperCase();
        methodContext.setLockType(LockType.valueOf(s));
    }
    // handle @AccessTimeout
    attributes = MethodInfoUtil.resolveAttributes(accessTimeoutInfos, beanContext);
    if (log.isDebugEnabled()) {
        for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
            final Method method = entry.getKey();
            final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
            log.debug("AccessTimeout: " + method + " -- " + MethodInfoUtil.toString(value.methods.get(0)) + " " + " " + value.accessTimeout.time + " " + value.accessTimeout.unit);
        }
    }
    for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
        final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
        final MethodContext methodContext = beanContext.getMethodContext(entry.getKey());
        final Duration accessTimeout = new Duration(value.accessTimeout.time, TimeUnit.valueOf(value.accessTimeout.unit));
        methodContext.setAccessTimeout(accessTimeout);
    }
}
Also used : ArrayList(java.util.ArrayList) MethodContext(org.apache.openejb.MethodContext) Duration(org.apache.openejb.util.Duration) Method(java.lang.reflect.Method) Logger(org.apache.openejb.util.Logger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MethodContext

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

the class TimerData method doReadObject.

protected void doReadObject(final ObjectInputStream in) throws IOException {
    id = in.readLong();
    deploymentId = in.readUTF();
    persistent = in.readBoolean();
    autoScheduled = in.readBoolean();
    try {
        timer = (Timer) in.readObject();
        primaryKey = in.readObject();
        timerService = (EjbTimerServiceImpl) in.readObject();
        info = in.readObject();
        trigger = AbstractTrigger.class.cast(in.readObject());
    } catch (final ClassNotFoundException e) {
        throw new IOException(e);
    }
    final String mtd = in.readUTF();
    final BeanContext beanContext = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(deploymentId);
    scheduler = timerService.getScheduler();
    for (final Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
        final MethodContext methodContext = it.next().getValue();
        /* this doesn't work in all cases
            if (methodContext.getSchedules().isEmpty()) {
                continue;
            }
            */
        final Method method = methodContext.getBeanMethod();
        if (method != null && method.getName().equals(mtd)) {
            // maybe we should check parameters too
            setTimeoutMethod(method);
            break;
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) AbstractTrigger(org.apache.openejb.quartz.impl.triggers.AbstractTrigger) MethodContext(org.apache.openejb.MethodContext) IOException(java.io.IOException) Method(java.lang.reflect.Method)

Example 4 with MethodContext

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

the class EjbTimerImplSerializableTest method timerData.

public static CalendarTimerData timerData() throws Exception {
    final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext("EJBWithTimer");
    final EjbTimerService timer = context.getEjbTimerService();
    final MethodContext ctx = context.getMethodContext(EJBWithTimer.class.getMethod("doSthg"));
    final ScheduleData sd = ctx.getSchedules().iterator().next();
    return new CalendarTimerData(1, (EjbTimerServiceImpl) timer, context.getDeploymentID().toString(), null, ctx.getBeanMethod(), sd.getConfig(), sd.getExpression(), false);
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) CalendarTimerData(org.apache.openejb.core.timer.CalendarTimerData) ScheduleData(org.apache.openejb.core.timer.ScheduleData) MethodContext(org.apache.openejb.MethodContext) EjbTimerService(org.apache.openejb.core.timer.EjbTimerService)

Example 5 with MethodContext

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

the class MethodScheduleBuilder method build.

public void build(final BeanContext beanContext, final EnterpriseBeanInfo beanInfo) {
    final Class<?> clazz = beanContext.getBeanClass();
    for (final MethodScheduleInfo info : beanInfo.methodScheduleInfos) {
        Method timeoutMethodOfSchedule = null;
        if (info.method.methodParams == null) {
            logger.info("Schedule timeout method with 'null' method parameters is invalid: " + info.method.methodName);
        } else {
            try {
                timeoutMethodOfSchedule = MethodInfoUtil.toMethod(clazz, info.method);
            } catch (final IllegalStateException e) {
                // method doesn't exist
                logger.warning("Schedule method does not exist: " + info.method.methodName, e);
                continue;
            }
        }
        MethodContext methodContext = null;
        if (timeoutMethodOfSchedule == null && beanContext.getEjbTimeout() != null) {
            methodContext = beanContext.getMethodContext(beanContext.getEjbTimeout());
        } else if (info.method.className == null || timeoutMethodOfSchedule.getDeclaringClass().getName().equals(info.method.className)) {
            methodContext = beanContext.getMethodContext(timeoutMethodOfSchedule);
        }
        this.addSchedulesToMethod(methodContext, info);
    }
}
Also used : MethodContext(org.apache.openejb.MethodContext) Method(java.lang.reflect.Method)

Aggregations

MethodContext (org.apache.openejb.MethodContext)5 Method (java.lang.reflect.Method)4 BeanContext (org.apache.openejb.BeanContext)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ScheduleData (org.apache.openejb.core.timer.ScheduleData)2 ContainerSystem (org.apache.openejb.spi.ContainerSystem)2 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 BeanType (org.apache.openejb.BeanType)1 CalendarTimerData (org.apache.openejb.core.timer.CalendarTimerData)1 EjbTimerService (org.apache.openejb.core.timer.EjbTimerService)1 EjbTimerServiceImpl (org.apache.openejb.core.timer.EjbTimerServiceImpl)1 MemoryTimerStore (org.apache.openejb.core.timer.MemoryTimerStore)1 NullEjbTimerServiceImpl (org.apache.openejb.core.timer.NullEjbTimerServiceImpl)1 TimerStore (org.apache.openejb.core.timer.TimerStore)1 JtaTransactionPolicyFactory (org.apache.openejb.core.transaction.JtaTransactionPolicyFactory)1 TransactionPolicyFactory (org.apache.openejb.core.transaction.TransactionPolicyFactory)1