001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.asynchronous;
023:
024: import java.security.Principal;
025: import org.jboss.aop.advice.Interceptor;
026: import org.jboss.aop.joinpoint.Invocation;
027: import org.jboss.aop.joinpoint.MethodInvocation;
028: import org.jboss.aspects.asynch.AsynchAspect;
029: import org.jboss.aspects.asynch.Future;
030: import org.jboss.aspects.asynch.FutureHolder;
031: import org.jboss.aspects.asynch.ThreadPoolExecutor;
032: import org.jboss.aspects.remoting.InvokeRemoteInterceptor;
033: import org.jboss.aspects.tx.ClientTxPropagationInterceptor;
034: import org.jboss.logging.Logger;
035: import org.jboss.remoting.InvokerLocator;
036: import org.jboss.security.SecurityAssociation;
037: import org.jboss.tm.TransactionPropagationContextFactory;
038: import org.jboss.tm.TransactionPropagationContextUtil;
039:
040: /**
041: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
042: * @version $Revision: 57207 $
043: */
044: public class AsynchronousInterceptor extends AsynchAspect implements
045: Interceptor {
046: private static final Logger log = Logger
047: .getLogger(AsynchronousInterceptor.class);
048:
049: public static final String ASYNCH = "ASYNCH";
050: public static final String INVOKE_ASYNCH = "INVOKE_ASYNCH";
051: public static final String FUTURE_HOLDER = "FUTURE_HOLDER";
052:
053: public AsynchronousInterceptor() {
054: try {
055: super .executor = (org.jboss.aspects.asynch.ExecutorAbstraction) ThreadPoolExecutor.class
056: .newInstance();
057: } catch (InstantiationException e) {
058: throw new RuntimeException(e);
059: } catch (IllegalAccessException e) {
060: throw new RuntimeException(e);
061: }
062: }
063:
064: public String getName() {
065: return "AsynchronousInterceptor";
066: }
067:
068: public Object invoke(Invocation invocation) throws Throwable {
069: MethodInvocation mi = (MethodInvocation) invocation;
070:
071: if (invocation.getMetaData(ASYNCH, INVOKE_ASYNCH) != null) {
072: //TODO This should maybe go somewhere nicer
073: InvokerLocator locator = (InvokerLocator) invocation
074: .getMetaData(InvokeRemoteInterceptor.REMOTING,
075: InvokeRemoteInterceptor.INVOKER_LOCATOR);
076: if (locator == null) {
077: //We are a local invocation. Add security and current transaction info to the invocation
078: // (for remote invocations this is done by the client side interceptors)
079: TransactionPropagationContextFactory tpcFactory = TransactionPropagationContextUtil
080: .getTPCFactoryClientSide();
081: if (tpcFactory != null) {
082: Object tpc = tpcFactory
083: .getTransactionPropagationContext();
084: if (tpc != null) {
085: invocation
086: .getMetaData()
087: .addMetaData(
088: ClientTxPropagationInterceptor.TRANSACTION_PROPAGATION_CONTEXT,
089: ClientTxPropagationInterceptor.TRANSACTION_PROPAGATION_CONTEXT,
090: tpc);
091: }
092: }
093:
094: Principal principal = SecurityAssociation
095: .getPrincipal();
096: if (principal != null)
097: invocation.getMetaData().addMetaData("security",
098: "principal", principal);
099:
100: Object credential = SecurityAssociation.getCredential();
101: if (credential != null)
102: invocation.getMetaData().addMetaData("security",
103: "credential", credential);
104: }
105:
106: return super .execute(mi);
107: }
108: return mi.invokeNext();
109: }
110:
111: //@Override
112: protected void setupLocalFuture(MethodInvocation invocation,
113: Future future) {
114: FutureHolder provider = (FutureHolder) invocation.getMetaData(
115: ASYNCH, FUTURE_HOLDER);
116: provider.setFuture(future);
117: }
118:
119: /**
120: * We don't want to generate proxies for ejb 3 clients, to avoid dependencies on javassist.
121: * Use a dynamic proxy to the future instead
122: */
123: //@Override
124: protected boolean generateProxy() {
125: return false;
126: }
127:
128: }
|