KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import javax.management.MBeanException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.RuntimeMBeanException JavaDoc;
29 import javax.management.RuntimeOperationsException JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.transaction.Transaction JavaDoc;
33
34 import org.jboss.invocation.Invocation;
35 import org.jboss.invocation.Invoker;
36 import org.jboss.invocation.MarshalledInvocation;
37 import org.jboss.invocation.http.interfaces.HttpInvokerProxy;
38 import org.jboss.system.Registry;
39 import org.jboss.system.ServiceMBeanSupport;
40 import org.jboss.system.server.ServerConfigUtil;
41 import org.jboss.util.StringPropertyReplacer;
42
43 /**
44  * The HttpInvoker ... into the JMX base.
45  *
46  * @author <a HREF="mailto:scott.stark@jboss.org>Scott Stark</a>
47  * @version $Revision: 37459 $
48  */

49 public class HttpInvoker extends ServiceMBeanSupport
50    implements HttpInvokerMBean
51 {
52    private String JavaDoc invokerURL;
53    private String JavaDoc invokerURLPrefix = "http://";
54    private String JavaDoc invokerURLSuffix = ":8080/invoker/JMXInvokerServlet";
55    private boolean useHostName = false;
56
57    // Public --------------------------------------------------------
58

59    public String JavaDoc getInvokerURL()
60    {
61       return invokerURL;
62    }
63    public void setInvokerURL(String JavaDoc invokerURL)
64    {
65       // Replace any system properties in the URL
66
String JavaDoc tmp = StringPropertyReplacer.replaceProperties(invokerURL);
67       this.invokerURL = tmp;
68       log.debug("Set invokerURL to "+this.invokerURL);
69    }
70
71    public String JavaDoc getInvokerURLPrefix()
72    {
73       return invokerURLPrefix;
74    }
75    public void setInvokerURLPrefix(String JavaDoc invokerURLPrefix)
76    {
77       this.invokerURLPrefix = invokerURLPrefix;
78    }
79
80    public String JavaDoc getInvokerURLSuffix()
81    {
82       return invokerURLSuffix;
83    }
84    public void setInvokerURLSuffix(String JavaDoc invokerURLSuffix)
85    {
86       this.invokerURLSuffix = invokerURLSuffix;
87    }
88
89    public boolean getUseHostName()
90    {
91       return useHostName;
92    }
93    public void setUseHostName(boolean flag)
94    {
95       this.useHostName = flag;
96    }
97
98    protected void startService()
99       throws Exception JavaDoc
100    {
101       checkInvokerURL();
102       Invoker delegateInvoker = new HttpInvokerProxy(invokerURL);
103
104       // Export the Invoker interface
105
ObjectName JavaDoc name = super.getServiceName();
106       Registry.bind(name, delegateInvoker);
107       log.debug("Bound Http invoker for JMX node");
108    }
109
110    protected void stopService()
111    {
112       try
113       {
114          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
115          ctx.unbind("invokers/"+InetAddress.getLocalHost().getHostName()+"/http");
116       }
117       catch (NamingException JavaDoc ignore)
118       {
119       }
120       catch (Throwable JavaDoc e)
121       {
122          log.error("Failed", e);
123          return;
124       }
125    }
126
127    protected void destroyService()
128    {
129       // Export references to the bean
130
Registry.unbind(serviceName);
131    }
132   
133    /**
134     * Invoke a Remote interface method.
135     */

136    public Object JavaDoc invoke(Invocation invocation)
137       throws Exception JavaDoc
138    {
139       ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
140       try
141       {
142          // Deserialize the transaction if it is there
143
MarshalledInvocation mi = (MarshalledInvocation) invocation;
144          Object JavaDoc tpc = mi.getTransactionPropagationContext();
145          Transaction JavaDoc tx = importTPC(tpc);
146          invocation.setTransaction(tx);
147
148          Integer JavaDoc nameHash = (Integer JavaDoc) invocation.getObjectName();
149          ObjectName JavaDoc mbean = (ObjectName JavaDoc) Registry.lookup(nameHash);
150
151          // The cl on the thread should be set in another interceptor
152
Object JavaDoc[] args = {invocation};
153          String JavaDoc[] sig = {"org.jboss.invocation.Invocation"};
154          Object JavaDoc obj = super.getServer().invoke(mbean,
155             "invoke", args, sig);
156
157          // Return the raw object and let the http layer marshall it
158
return obj;
159       }
160       catch (Exception JavaDoc e)
161       {
162          if (e instanceof MBeanException JavaDoc)
163             e = ((MBeanException JavaDoc)e).getTargetException();
164          
165          if (e instanceof RuntimeMBeanException JavaDoc)
166             e = ((RuntimeMBeanException JavaDoc)e).getTargetException();
167          
168          if (e instanceof RuntimeOperationsException JavaDoc)
169             e = ((RuntimeOperationsException JavaDoc)e).getTargetException();
170          
171          // Only log errors if trace is enabled
172
if( log.isTraceEnabled() )
173             log.trace("operation failed", e);
174          throw e;
175       }
176       finally
177       {
178          Thread.currentThread().setContextClassLoader(oldCl);
179       }
180    }
181
182    // Package protected ---------------------------------------------
183

184    // Protected -----------------------------------------------------
185

186    // Private -------------------------------------------------------
187

188    /** Not implemented, and should not be
189     */

190    protected Transaction JavaDoc importTPC(Object JavaDoc tpc)
191    {
192       return null;
193    }
194
195    /** Validate that the invokerURL is set, and if not build it from
196     * the invokerURLPrefix + host + invokerURLSuffix. The host value will be
197     * taken from the jboss.bind.address system property if its a valid
198     * address, InetAddress.getLocalHost otherwise.
199     */

200    protected void checkInvokerURL() throws UnknownHostException JavaDoc
201    {
202       if( invokerURL == null )
203       {
204          InetAddress JavaDoc addr = InetAddress.getLocalHost();
205          // First check for a global bind address
206
String JavaDoc host = ServerConfigUtil.getSpecificBindAddress();
207          if( host == null )
208          {
209             host = useHostName ? addr.getHostName() : addr.getHostAddress();
210          }
211          String JavaDoc url = invokerURLPrefix + host + invokerURLSuffix;
212          setInvokerURL(url);
213       }
214    }
215 }
216
217
Popular Tags