KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > Dispatcher


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.aop;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.jboss.aop.joinpoint.Invocation;
29 import org.jboss.aop.joinpoint.InvocationResponse;
30 import org.jboss.aop.joinpoint.MethodInvocation;
31 import org.jboss.aop.proxy.ClassProxy;
32 import org.jboss.aop.proxy.ClassProxyFactory;
33 import org.jboss.aop.proxy.Proxy;
34 import org.jboss.aop.util.reference.MethodPersistentReference;
35
36 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
37
38 /**
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 44133 $
41  */

42
43 public class Dispatcher
44 {
45    public static final String JavaDoc DISPATCHER = "DISPATCHER";
46    public static final String JavaDoc OID = "OID";
47    public static final Dispatcher singleton = new Dispatcher();
48
49    Map JavaDoc targetMap = new ConcurrentReaderHashMap();
50
51
52    public boolean isRegistered(Object JavaDoc oid)
53    {
54       return targetMap.containsKey(oid);
55    }
56
57    /**
58     * Register an Object ID with an actual target object
59     */

60    public void registerTarget(Object JavaDoc oid, Object JavaDoc target)
61    {
62       targetMap.put(oid, target);
63    }
64
65    public void unregisterTarget(Object JavaDoc oid)
66    {
67       targetMap.remove(oid);
68    }
69
70    public Object JavaDoc getRegistered(Object JavaDoc oid)
71    {
72       return targetMap.get(oid);
73    }
74
75
76    public InvocationResponse invoke(Invocation invocation) throws NotFoundInDispatcherException, Throwable JavaDoc
77    {
78       Object JavaDoc oid = invocation.getMetaData(DISPATCHER, OID);
79
80       Object JavaDoc target = null;
81       target = targetMap.get(oid);
82
83       if (target == null)
84       {
85          throw new NotFoundInDispatcherException(oid);
86       }
87
88       if (target instanceof ClassProxy)
89       {
90          ClassProxy proxy = (ClassProxy) target;
91          return proxy._dynamicInvoke(invocation);
92       }
93       else if (target instanceof Proxy)
94       {
95          ClassProxy proxy = (ClassProxy) target;
96          return proxy._dynamicInvoke(invocation);
97       }
98       else if (target instanceof Advised)
99       {
100          Advisor advisor = ((Advised) target)._getAdvisor();
101          return advisor.dynamicInvoke(target, invocation);
102       }
103       else if (target instanceof Advisor)
104       {
105          Advisor advisor = (Advisor) target;
106          return advisor.dynamicInvoke(null, invocation);
107       }
108       else
109       {
110          if (invocation instanceof MethodInvocation)
111          {
112             MethodInvocation methodInvocation = (MethodInvocation) invocation;
113             // For non-advised methods, we can only do public method invocations
114
long methodHash = methodInvocation.getMethodHash();
115             HashMap JavaDoc methodMap = ClassProxyFactory.getMethodMap(target.getClass());
116             MethodPersistentReference ref = (MethodPersistentReference)methodMap.get(new Long JavaDoc(methodHash));
117             Method JavaDoc method = (Method JavaDoc)ref.get();
118             Object JavaDoc[] args = methodInvocation.getArguments();
119             try
120             {
121                return new InvocationResponse(method.invoke(target, args));
122             }
123             catch (InvocationTargetException JavaDoc ex)
124             {
125                throw ex.getTargetException();
126             }
127          }
128          else
129          {
130             throw new RuntimeException JavaDoc("field invocations not implemented");
131          }
132       }
133    }
134 }
135
Popular Tags