KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
25 import javax.management.JMException JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.ReflectionException JavaDoc;
30 import javax.management.MBeanInfo JavaDoc;
31 import javax.management.MBeanAttributeInfo JavaDoc;
32 import javax.management.MBeanConstructorInfo JavaDoc;
33 import javax.management.MBeanOperationInfo JavaDoc;
34 import javax.management.MBeanNotificationInfo JavaDoc;
35 import javax.management.MBeanParameterInfo JavaDoc;
36
37 import org.jboss.ha.framework.interfaces.HARMIResponse;
38 import org.jboss.ha.framework.interfaces.GenericClusteringException;
39 import org.jboss.ha.framework.server.HATarget;
40 import org.jboss.invocation.Invocation;
41 import org.jboss.logging.Logger;
42 import org.jboss.mx.util.JMXExceptionDecoder;
43 import org.jboss.mx.util.DynamicMBeanSupport;
44
45
46 /** This is an invoker that delegates to the target invoker and handles the
47  * wrapping of the response in an HARMIResponse with any updated HATarget info.
48  * @see HttpProxyFactoryHA
49  *
50  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
51  * @version $Revision: 37459 $
52  */

53 public class HAInvokerWrapper extends DynamicMBeanSupport
54 {
55    private static Logger log = Logger.getLogger(HAInvokerWrapper.class);
56    private MBeanServer JavaDoc mbeanServer;
57    private MBeanInfo JavaDoc info;
58
59    private ObjectName JavaDoc targetName;
60    private HATarget target;
61
62    public HAInvokerWrapper(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc targetName, HATarget target)
63    {
64       this.mbeanServer = mbeanServer;
65       this.targetName = targetName;
66       this.target = target;
67       MBeanAttributeInfo JavaDoc[] attrInfo = null;
68       MBeanConstructorInfo JavaDoc[] ctorInfo = null;
69       MBeanParameterInfo JavaDoc[] sig = {
70         new MBeanParameterInfo JavaDoc("invocation", Invocation.class.getName(),
71            "The invocation content information")
72       };
73       MBeanOperationInfo JavaDoc[] opInfo = {
74          new MBeanOperationInfo JavaDoc("invoke", "The detached invoker entry point",
75             sig, "java.lang.Object", MBeanOperationInfo.ACTION)
76       };
77       MBeanNotificationInfo JavaDoc[] eventInfo = null;
78       this.info = new MBeanInfo JavaDoc(getClass().getName(),
79          "A wrapper inovker that delegates to the target invoker",
80          attrInfo,
81          ctorInfo,
82          opInfo,
83          eventInfo);
84    }
85
86    /** The JMX DynamicMBean invoke entry point. This only handles the
87     * invoke(Invocation) operation.
88     *
89     * @param actionName
90     * @param params
91     * @param signature
92     * @return
93     * @throws MBeanException
94     * @throws ReflectionException
95     */

96    public Object JavaDoc invoke(String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature)
97       throws MBeanException JavaDoc, ReflectionException JavaDoc
98    {
99       if( params == null || params.length != 1 ||
100          (params[0] instanceof Invocation) == false )
101       {
102          NoSuchMethodException JavaDoc e = new NoSuchMethodException JavaDoc(actionName);
103          throw new ReflectionException JavaDoc(e, actionName);
104       }
105
106       Invocation invocation = (Invocation) params[0];
107       try
108       {
109          Object JavaDoc value = invoke(invocation);
110          return value;
111       }
112       catch(Exception JavaDoc e)
113       {
114          throw new ReflectionException JavaDoc(e, "Invoke failure");
115       }
116    }
117
118    /** The invoker entry point.
119     * @param invocation
120     * @return A HARMIResponse that wraps the result of calling invoke(Invocation)
121     * on the targetName MBean
122     * @throws Exception
123     */

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