KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > http > server > HttpInvokerHA


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.invocation.http.server;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import javax.management.JMException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.jboss.mx.util.JMXExceptionDecoder;
32 import org.jboss.ha.framework.interfaces.HARMIResponse;
33 import org.jboss.ha.framework.server.HATarget;
34 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
35 import org.jboss.ha.framework.interfaces.GenericClusteringException;
36 import org.jboss.invocation.http.interfaces.HttpInvokerProxyHA;
37 import org.jboss.invocation.Invocation;
38 import org.jboss.invocation.Invoker;
39 import org.jboss.invocation.InvokerHA;
40 import org.jboss.system.Registry;
41
42 /** An extension of the HttpInvoker and supports clustering of HTTP invokers.
43  *
44  * @author <a HREF="mailto:scott.stark@jboss.org>Scott Stark</a>
45  * @version $Revision: 37459 $
46  */

47 public class HttpInvokerHA extends HttpInvoker
48    implements InvokerHA
49 {
50    protected HashMap JavaDoc targetMap = new HashMap JavaDoc();
51
52    // Public --------------------------------------------------------
53

54    protected void startService()
55       throws Exception JavaDoc
56    {
57       // Export the Invoker interface
58
ObjectName JavaDoc name = super.getServiceName();
59       Registry.bind(name, this);
60       // Make sure the invoker URL is valid
61
super.checkInvokerURL();
62       log.debug("Bound HttpHA invoker for JMX node");
63    }
64
65    protected void stopService()
66    {
67       // Unxport the Invoker interface
68
ObjectName JavaDoc name = super.getServiceName();
69       Registry.unbind(name);
70       log.debug("Unbound HttpHA invoker for JMX node");
71    }
72
73    protected void destroyService()
74    {
75       // Export references to the bean
76
Registry.unbind(serviceName);
77    }
78
79    public void registerBean(ObjectName JavaDoc targetName, HATarget target) throws Exception JavaDoc
80    {
81       Integer JavaDoc hash = new Integer JavaDoc(targetName.hashCode());
82       log.debug("Registered targetName("+targetName+"), hash="+hash
83          + ", target="+target);
84       if (targetMap.containsKey(hash))
85       {
86          throw new IllegalStateException JavaDoc("Duplicate targetName("+targetName
87             + ") hashCode: "+hash);
88       }
89       targetMap.put(hash, target);
90    }
91
92    public void unregisterBean(ObjectName JavaDoc targetName) throws Exception JavaDoc
93    {
94       Integer JavaDoc hash = new Integer JavaDoc(targetName.hashCode());
95       targetMap.remove(hash);
96       log.debug("Unregistered targetName("+targetName+"), hash="+hash);
97    }
98
99    public Invoker createProxy(ObjectName JavaDoc targetName, LoadBalancePolicy policy,
100                               String JavaDoc proxyFamilyName)
101       throws Exception JavaDoc
102    {
103       Integer JavaDoc hash = new Integer JavaDoc(targetName.hashCode());
104       HATarget target = (HATarget) targetMap.get(hash);
105       if (target == null)
106       {
107          throw new IllegalStateException JavaDoc("The targetName("+targetName
108             + "), hashCode("+hash+") not found");
109       }
110       Invoker proxy = new HttpInvokerProxyHA(target.getReplicants(), target.getCurrentViewId (),
111                                              policy, proxyFamilyName);
112       return proxy;
113    }
114
115    public Serializable JavaDoc getStub()
116    {
117       return super.getInvokerURL();
118    }
119
120    /**
121     * Invoke a Remote interface method.
122     */

123    public Object JavaDoc invoke(Invocation invocation)
124       throws Exception JavaDoc
125    {
126       ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
127       try
128       {
129          Integer JavaDoc nameHash = (Integer JavaDoc) invocation.getObjectName();
130          ObjectName JavaDoc mbean = (ObjectName JavaDoc) Registry.lookup(nameHash);
131
132          // The cl on the thread should be set in another interceptor
133
Object JavaDoc[] args = {invocation};
134          String JavaDoc[] sig = {"org.jboss.invocation.Invocation"};
135          Object JavaDoc rtn = super.getServer().invoke(mbean,
136             "invoke", args, sig);
137
138          // Update the targets list if the client view is out of date
139
Long JavaDoc clientViewId = (Long JavaDoc) invocation.getValue("CLUSTER_VIEW_ID");
140          HARMIResponse rsp = new HARMIResponse();
141          HATarget target = (HATarget) targetMap.get(nameHash);
142          if (target == null)
143          {
144             throw new IllegalStateException JavaDoc("The name for hashCode("+nameHash+") was not found");
145          }
146          if (clientViewId.longValue() != target.getCurrentViewId())
147          {
148             rsp.newReplicants = new ArrayList JavaDoc(target.getReplicants());
149             rsp.currentViewId = target.getCurrentViewId();
150          }
151          rsp.response = rtn;
152
153          // Return the raw object and let the http layer marshall it
154
return rsp;
155       }
156       catch (Exception JavaDoc e)
157       {
158          // Unwrap any JMX exceptions
159
e = (Exception JavaDoc) JMXExceptionDecoder.decode(e);
160          // Don't send JMX exception back to client to avoid needing jmx
161
if( e instanceof JMException JavaDoc )
162             e = new GenericClusteringException (GenericClusteringException.COMPLETED_NO, e.getMessage());
163
164          // Only log errors if trace is enabled
165
if( log.isTraceEnabled() )
166             log.trace("operation failed", e);
167          throw e;
168       }
169       finally
170       {
171          Thread.currentThread().setContextClassLoader(oldCl);
172       }
173    }
174 }
175
Popular Tags