001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.invocation.http.interfaces;
023:
024: import java.io.IOException;
025: import java.io.Externalizable;
026: import java.io.ObjectInput;
027: import java.io.ObjectOutput;
028: import java.lang.reflect.InvocationTargetException;
029: import java.lang.reflect.Method;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.rmi.ServerException;
033:
034: import org.jboss.invocation.Invocation;
035: import org.jboss.invocation.InvocationException;
036: import org.jboss.invocation.Invoker;
037: import org.jboss.invocation.MarshalledInvocation;
038: import org.jboss.invocation.MarshalledValue;
039: import org.jboss.logging.Logger;
040:
041: /** The client side Http invoker proxy that posts an invocation to the
042: InvokerServlet using the HttpURLConnection created from the proxy
043: externalURL.
044:
045: * @author Scott.Stark@jboss.org
046: * @version $Revision: 57209 $
047: */
048: public class HttpInvokerProxy implements Invoker, Externalizable {
049: // Constants -----------------------------------------------------
050: private static Logger log = Logger
051: .getLogger(HttpInvokerProxy.class);
052:
053: /** Serial Version Identifier. */
054: static final long serialVersionUID = -8249272784108192267L;
055:
056: // Attributes ----------------------------------------------------
057:
058: // URL to the remote JMX node invoker
059: protected String externalURLValue;
060: protected transient URL externalURL;
061:
062: // Constructors --------------------------------------------------
063: public HttpInvokerProxy() {
064: // For externalization to work
065: }
066:
067: /**
068: @param externalURL, the URL through which clients should contact the
069: InvokerServlet.
070: */
071: public HttpInvokerProxy(String externalURLValue) {
072: this .externalURLValue = externalURLValue;
073: }
074:
075: // Public --------------------------------------------------------
076:
077: public String getServerHostName() throws Exception {
078: if (externalURL == null)
079: externalURL = Util.resolveURL(externalURLValue);
080: return externalURL.getHost();
081: }
082:
083: public String getExternalURLValue() {
084: return externalURLValue;
085: }
086:
087: public URL getExternalURL() {
088: return externalURL;
089: }
090:
091: public String toString() {
092: StringBuffer tmp = new StringBuffer(super .toString());
093: tmp.append('(');
094: tmp.append("externalURL:");
095: tmp.append(externalURL);
096: tmp.append(')');
097: return tmp.toString();
098: }
099:
100: /** This method builds a MarshalledInvocation from the invocation passed
101: in and then does a post to the target URL.
102: */
103: public Object invoke(Invocation invocation) throws Exception {
104: // We are going to go through a Remote invocation, switch to a Marshalled Invocation
105: MarshalledInvocation mi = new MarshalledInvocation(invocation);
106:
107: if (externalURL == null)
108: externalURL = Util.resolveURL(externalURLValue);
109: try {
110: Object value = Util.invoke(externalURL, mi);
111: return value;
112: } catch (InvocationException e) {
113: // Unwrap the invoker servlet InvocationExceptions
114: Throwable t = e.getTargetException();
115: if (t instanceof Exception)
116: throw (Exception) t;
117: else if (t instanceof Error)
118: throw (Error) t;
119: throw new InvocationTargetException(t);
120: } catch (IOException e) {
121: throw new ServerException("IOE", e);
122: }
123: }
124:
125: /** Externalize this instance.
126: */
127: public void writeExternal(final ObjectOutput out)
128: throws IOException {
129: out.writeObject(externalURLValue);
130: }
131:
132: /** Un-externalize this instance.
133: */
134: public void readExternal(final ObjectInput in) throws IOException,
135: ClassNotFoundException {
136: externalURLValue = (String) in.readObject();
137: externalURL = Util.resolveURL(externalURLValue);
138: }
139:
140: }
|