KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > ha > HAService


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.test.jmx.ha;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
27 import java.security.Principal JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.jboss.ha.jmx.HAServiceMBeanSupport;
33 import org.jboss.invocation.Invocation;
34 import org.jboss.invocation.MarshalledInvocation;
35 import org.jboss.security.SecurityAssociation;
36 import org.jboss.system.Registry;
37
38 public class HAService
39    extends HAServiceMBeanSupport
40    implements HAServiceRemote, HAServiceMBean
41 {
42    private int count = 0;
43
44    private Map JavaDoc marshalledInvocationMapping;
45
46    public void startService()
47       throws Exception JavaDoc
48    {
49       super.startService();
50       
51       // Calulate method hashes for remote invocation
52
Method JavaDoc[] methods = HAServiceRemote.class.getMethods();
53       HashMap JavaDoc tmpMap = new HashMap JavaDoc(methods.length);
54       for(int m = 0; m < methods.length; m ++)
55       {
56          Method JavaDoc method = methods[m];
57          Long JavaDoc hash = new Long JavaDoc(MarshalledInvocation.calculateHash(method));
58          tmpMap.put(hash, method);
59       }
60       marshalledInvocationMapping = Collections.unmodifiableMap(tmpMap);
61
62       // Place our ObjectName hash into the Registry so invokers can resolve it
63
Registry.bind(new Integer JavaDoc(serviceName.hashCode()), serviceName);
64    }
65
66    public void stopService()
67       throws Exception JavaDoc
68    {
69       super.stopService();
70       
71       // No longer available to the invokers
72
Registry.unbind(new Integer JavaDoc(serviceName.hashCode()));
73    }
74
75    /**
76     * Expose the client mapping
77     */

78    public Map JavaDoc getMethodMap()
79    {
80       return marshalledInvocationMapping;
81    }
82
83    /**
84     * This is the "remote" entry point
85     */

86    public Object JavaDoc invoke(Invocation invocation)
87       throws Exception JavaDoc
88    {
89       // Invoked remotely, inject method resolution
90
if (invocation instanceof MarshalledInvocation)
91       {
92          MarshalledInvocation mi = (MarshalledInvocation) invocation;
93          mi.setMethodMap(marshalledInvocationMapping);
94       }
95       Method JavaDoc method = invocation.getMethod();
96       Object JavaDoc[] args = invocation.getArguments();
97
98       // Setup any security context (only useful if something checks it, this impl doesn't)
99
Principal JavaDoc principal = invocation.getPrincipal();
100       Object JavaDoc credential = invocation.getCredential();
101       SecurityAssociation.setPrincipal(principal);
102       SecurityAssociation.setCredential(credential);
103
104       // Dispatch the invocation
105
try
106       {
107          return method.invoke(this, args);
108       }
109       catch(InvocationTargetException JavaDoc e)
110       {
111          Throwable JavaDoc t = e.getTargetException();
112          if( t instanceof Exception JavaDoc )
113             throw (Exception JavaDoc) t;
114          else
115             throw new UndeclaredThrowableException JavaDoc(t, method.toString());
116       }
117       finally
118       {
119          // Clear the security context
120
SecurityAssociation.clear();
121       }
122    }
123
124    // Implementation of remote methods
125

126    public String JavaDoc hello()
127    {
128       return "Hello";
129    }
130    
131    public String JavaDoc getClusterNode()
132    {
133       return getPartition().getNodeName();
134    }
135
136 }
137
Popular Tags