00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.body.http;
00032
00033 import java.io.IOException;
00034 import java.net.MalformedURLException;
00035 import java.net.URL;
00036 import java.rmi.ConnectException;
00037 import java.util.Enumeration;
00038 import java.util.Hashtable;
00039
00040 import org.objectweb.proactive.core.ProActiveException;
00041 import org.objectweb.proactive.core.body.BodyAdapterImpl;
00042 import org.objectweb.proactive.core.body.RemoteBody;
00043 import org.objectweb.proactive.core.body.UniversalBody;
00044 import org.objectweb.proactive.core.body.http.util.exceptions.HTTPUnexpectedException;
00045 import org.objectweb.proactive.core.body.http.util.messages.HttpLookupMessage;
00046 import org.objectweb.proactive.core.rmi.ClassServer;
00047 import org.objectweb.proactive.core.util.UrlBuilder;
00048
00049
00058 public class HttpBodyAdapter extends BodyAdapterImpl {
00059
00064 protected static transient Hashtable<String, HttpBodyAdapter> urnBodys = new Hashtable<String, HttpBodyAdapter>();
00065
00066
00067
00068
00069 public HttpBodyAdapter() {
00070 }
00071
00072 public HttpBodyAdapter(UniversalBody body) throws ProActiveException {
00073 RemoteBody remoteBody = new HttpRemoteBodyImpl(body);
00074 construct(remoteBody);
00075
00076
00077 }
00078
00079
00080
00081
00082
00088 public void register(String urn) throws java.io.IOException {
00089
00090 URL u = null;
00091 String url = null;
00092 int port = ClassServer.getServerSocketPort();
00093 try {
00094 u = new URL(urn);
00095 port = u.getPort();
00096 if (port != ClassServer.getServerSocketPort()) {
00097 throw new IOException(
00098 "Bad registering port. You have to register on the same port as the runtime");
00099 }
00100 url = u.toString();
00101 urn = u.getPath();
00102 } catch (MalformedURLException e) {
00103 url = ClassServer.getUrl() + urn;
00104
00105 }
00106 urnBodys.put(urn, this);
00107
00108 if (bodyLogger.isInfoEnabled()) {
00109 bodyLogger.info("register object at " + url);
00110 bodyLogger.info(urnBodys);
00111 }
00112 }
00113
00118 public void unregister(String urn) throws java.io.IOException {
00119 urnBodys.put(urn, null);
00120 }
00121
00127 public UniversalBody lookup(String urn) throws java.io.IOException {
00128
00129
00130 URL u = null;
00131 int port = 0;
00132 try {
00133 u = new URL (urn);
00134 port = u.getPort();
00135
00136 } catch (MalformedURLException e) {
00137 if (!urn.startsWith("http://")) {
00138 urn = "http://" + urn;
00139 return lookup(urn);
00140 }
00141 throw e;
00142 }
00143 if (port == 0) {
00144 throw new HTTPUnexpectedException("You have to specify a port where the runtime can be reached");
00145 }
00146 String url = u.toString();
00147 urn = u.getPath();
00148 HttpLookupMessage message = new HttpLookupMessage(urn, url, port);
00149 message.send();
00150 UniversalBody result = message.getReturnedObject();
00151 if (result == null) {
00152 throw new java.io.IOException("The url " + url +
00153 " is not bound to any known object");
00154 } else {
00155 return result;
00156
00157 }
00158
00159
00160
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 }
00182
00188 public static synchronized UniversalBody getBodyFromUrn(String urn) {
00189 return (UniversalBody) urnBodys.get(urn);
00190 }
00191
00198
00199
00200
00201 public String [] list(String url) throws java.io.IOException {
00202 String [] names = null;
00203 names = new String [this.urnBodys.size()];
00204 Enumeration<String> e = urnBodys.keys();
00205 int i=0;
00206 while (e.hasMoreElements()) {
00207 names[i++] = e.nextElement();
00208 }
00209
00210 return names;
00211 }
00212 }