KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > asynchronous > AsynchronousInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.asynchronous;
23
24 import java.security.Principal JavaDoc;
25 import org.jboss.aop.advice.Interceptor;
26 import org.jboss.aop.joinpoint.Invocation;
27 import org.jboss.aop.joinpoint.MethodInvocation;
28 import org.jboss.aspects.asynch.AsynchAspect;
29 import org.jboss.aspects.asynch.Future;
30 import org.jboss.aspects.asynch.FutureHolder;
31 import org.jboss.aspects.asynch.ThreadPoolExecutor;
32 import org.jboss.aspects.remoting.InvokeRemoteInterceptor;
33 import org.jboss.aspects.tx.ClientTxPropagationInterceptor;
34 import org.jboss.logging.Logger;
35 import org.jboss.remoting.InvokerLocator;
36 import org.jboss.security.SecurityAssociation;
37 import org.jboss.tm.TransactionPropagationContextFactory;
38 import org.jboss.tm.TransactionPropagationContextUtil;
39
40
41 /**
42  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
43  * @version $Revision: 43261 $
44  */

45 public class AsynchronousInterceptor extends AsynchAspect implements Interceptor
46 {
47    private static final Logger log = Logger.getLogger(AsynchronousInterceptor.class);
48
49    public static final String JavaDoc ASYNCH = "ASYNCH";
50    public static final String JavaDoc INVOKE_ASYNCH = "INVOKE_ASYNCH";
51    public static final String JavaDoc FUTURE_HOLDER = "FUTURE_HOLDER";
52
53    public AsynchronousInterceptor()
54    {
55       try
56       {
57          super.executor = (org.jboss.aspects.asynch.ExecutorAbstraction)ThreadPoolExecutor.class.newInstance();
58       }
59       catch (InstantiationException JavaDoc e)
60       {
61          throw new RuntimeException JavaDoc(e);
62       }
63       catch (IllegalAccessException JavaDoc e)
64       {
65          throw new RuntimeException JavaDoc(e);
66       }
67    }
68
69    public String JavaDoc getName()
70    {
71       return "AsynchronousInterceptor";
72    }
73
74    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
75    {
76       MethodInvocation mi = (MethodInvocation) invocation;
77
78       if (invocation.getMetaData(ASYNCH, INVOKE_ASYNCH) != null)
79       {
80          //TODO This should maybe go somewhere nicer
81
InvokerLocator locator = (InvokerLocator) invocation.getMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR);
82          if (locator == null)
83          {
84             //We are a local invocation. Add security and current transaction info to the invocation
85
// (for remote invocations this is done by the client side interceptors)
86
TransactionPropagationContextFactory tpcFactory = TransactionPropagationContextUtil.getTPCFactoryClientSide();
87             if (tpcFactory != null)
88             {
89                Object JavaDoc tpc = tpcFactory.getTransactionPropagationContext();
90                if (tpc != null)
91                {
92                   invocation.getMetaData().addMetaData(ClientTxPropagationInterceptor.TRANSACTION_PROPAGATION_CONTEXT,
93                                                        ClientTxPropagationInterceptor.TRANSACTION_PROPAGATION_CONTEXT, tpc);
94                }
95             }
96
97             Principal JavaDoc principal = SecurityAssociation.getPrincipal();
98             if (principal != null) invocation.getMetaData().addMetaData("security", "principal", principal);
99
100             Object JavaDoc credential = SecurityAssociation.getCredential();
101             if (credential != null) invocation.getMetaData().addMetaData("security", "credential", credential);
102          }
103
104          return super.execute(mi);
105       }
106       return mi.invokeNext();
107    }
108
109    //@Override
110
protected void setupLocalFuture(MethodInvocation invocation, Future future)
111    {
112       FutureHolder provider = (FutureHolder) invocation.getMetaData(ASYNCH, FUTURE_HOLDER);
113       provider.setFuture(future);
114    }
115
116    /**
117     * We don't want to generate proxies for ejb 3 clients, to avoid dependencies on javassist.
118     * Use a dynamic proxy to the future instead
119     */

120    //@Override
121
protected boolean generateProxy()
122    {
123       return false;
124    }
125
126    
127 }
128
Popular Tags