KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > http > interfaces > HttpInvokerProxy


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.interfaces;
23
24 import java.io.IOException JavaDoc;
25 import java.io.Externalizable JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.rmi.ServerException JavaDoc;
33
34 import org.jboss.invocation.Invocation;
35 import org.jboss.invocation.InvocationException;
36 import org.jboss.invocation.Invoker;
37 import org.jboss.invocation.MarshalledInvocation;
38 import org.jboss.invocation.MarshalledValue;
39 import org.jboss.logging.Logger;
40
41 /** The client side Http invoker proxy that posts an invocation to the
42  InvokerServlet using the HttpURLConnection created from the proxy
43  externalURL.
44
45 * @author Scott.Stark@jboss.org
46 * @version $Revision: 37459 $
47 */

48 public class HttpInvokerProxy
49    implements Invoker, Externalizable JavaDoc
50 {
51    // Constants -----------------------------------------------------
52
private static Logger log = Logger.getLogger(HttpInvokerProxy.class);
53
54    /** Serial Version Identifier. */
55    static final long serialVersionUID = -8249272784108192267L;
56
57    // Attributes ----------------------------------------------------
58

59    // URL to the remote JMX node invoker
60
protected String JavaDoc externalURLValue;
61    protected transient URL JavaDoc externalURL;
62
63    // Constructors --------------------------------------------------
64
public HttpInvokerProxy()
65    {
66       // For externalization to work
67
}
68
69    /**
70    @param externalURL, the URL through which clients should contact the
71     InvokerServlet.
72    */

73    public HttpInvokerProxy(String JavaDoc externalURLValue)
74    {
75       this.externalURLValue = externalURLValue;
76    }
77
78    // Public --------------------------------------------------------
79

80    public String JavaDoc getServerHostName() throws Exception JavaDoc
81    {
82       if( externalURL == null )
83          externalURL = Util.resolveURL(externalURLValue);
84       return externalURL.getHost();
85    }
86    public String JavaDoc getExternalURLValue()
87    {
88       return externalURLValue;
89    }
90    public URL JavaDoc getExternalURL()
91    {
92       return externalURL;
93    }
94
95    public String JavaDoc toString()
96    {
97       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(super.toString());
98       tmp.append('(');
99       tmp.append("externalURL:");
100       tmp.append(externalURL);
101       tmp.append(')');
102       return tmp.toString();
103    }
104
105    /** This method builds a MarshalledInvocation from the invocation passed
106     in and then does a post to the target URL.
107    */

108    public Object JavaDoc invoke(Invocation invocation)
109       throws Exception JavaDoc
110    {
111       // We are going to go through a Remote invocation, switch to a Marshalled Invocation
112
MarshalledInvocation mi = new MarshalledInvocation(invocation);
113
114       if( externalURL == null )
115          externalURL = Util.resolveURL(externalURLValue);
116       try
117       {
118          Object JavaDoc value = Util.invoke(externalURL, mi);
119          return value;
120       }
121       catch (InvocationException e)
122       {
123          // Unwrap the invoker servlet InvocationExceptions
124
Throwable JavaDoc t = e.getTargetException();
125          if( t instanceof Exception JavaDoc )
126             throw (Exception JavaDoc) t;
127          else if (t instanceof Error JavaDoc)
128             throw (Error JavaDoc) t;
129          throw new InvocationTargetException JavaDoc(t);
130       }
131       catch (IOException JavaDoc e)
132       {
133          throw new ServerException JavaDoc("IOE", e);
134       }
135    }
136
137    /** Externalize this instance.
138    */

139    public void writeExternal(final ObjectOutput JavaDoc out)
140       throws IOException JavaDoc
141    {
142       out.writeObject(externalURLValue);
143    }
144
145    /** Un-externalize this instance.
146    */

147    public void readExternal(final ObjectInput JavaDoc in)
148       throws IOException JavaDoc, ClassNotFoundException JavaDoc
149    {
150       externalURLValue = (String JavaDoc) in.readObject();
151       externalURL = Util.resolveURL(externalURLValue);
152    }
153
154 }
155
Popular Tags