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.osgi;
00032
00033
00034 import org.objectweb.proactive.ActiveObjectCreationException;
00035 import org.objectweb.proactive.ProActive;
00036 import org.objectweb.proactive.core.ProActiveException;
00037 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00038 import org.objectweb.proactive.core.node.Node;
00039 import org.objectweb.proactive.core.node.NodeException;
00040 import org.objectweb.proactive.core.node.NodeFactory;
00041 import org.objectweb.proactive.core.rmi.ClassServer;
00042 import org.objectweb.proactive.core.rmi.ClassServerServlet;
00043 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
00044
00045 import org.osgi.framework.BundleContext;
00046
00047 import org.osgi.service.http.HttpContext;
00048 import org.osgi.service.http.HttpService;
00049
00050 import org.ungoverned.gravity.servicebinder.ServiceBinderContext;
00051
00052 import java.io.IOException;
00053 import java.net.MalformedURLException;
00054 import java.net.URL;
00055 import java.rmi.AlreadyBoundException;
00056
00057 import javax.servlet.Servlet;
00058 import javax.servlet.http.HttpServletRequest;
00059 import javax.servlet.http.HttpServletResponse;
00060
00061
00067 public class ProActiveServicesImpl implements ProActiveService {
00068 private Node node;
00069 private Servlet servlet;
00070 private BundleContext bc;
00071 private static final String aliasServlet = ClassServerServlet.SERVLET_NAME;
00072 private static final String OSGI_NODE_NAME = "OSGiNode";
00073 private HttpService http;
00074 private int port;
00075
00076 static {
00077 ProActiveConfiguration.load();
00078 }
00079
00080 public ProActiveServicesImpl() {
00081 }
00082
00083 public ProActiveServicesImpl(ServiceBinderContext context) {
00084 this.bc = context.getBundleContext();
00085 this.port = Integer.parseInt(this.bc.getProperty(
00086 "org.osgi.service.http.port"));
00087 }
00088
00089 public void setNode(Node node) {
00090 this.node = node;
00091 }
00092
00096 public Object newActive(String className, Object[] params)
00097 throws ActiveObjectCreationException, NodeException {
00098 return ProActive.newActive(className, params, this.node);
00099 }
00100
00104 public void register(Object obj, String url) throws IOException {
00105 ProActive.register(obj, url);
00106 }
00107
00111 public void unregister(String url) throws IOException {
00112 ProActive.unregister(url);
00113 }
00114
00119 public void bind(HttpService ref) {
00120 this.http = ref;
00121 createProActiveService();
00122 }
00123
00128 public void unbind(HttpService ref) {
00129 System.out.println("Node is no more accessible by Http, temination of ProActiveService");
00130 terminate ();
00131 }
00132
00137 private void createProActiveService() {
00138 this.servlet = new ClassServerServlet(port);
00139 boolean b = registerServlet();
00140 createNode();
00141 }
00142
00147 private void createNode() {
00148 try {
00149
00150 this.node = NodeFactory.createNode(ClassServer.getUrl() +'/' +
00151 OSGI_NODE_NAME);
00152 } catch (NodeException e) {
00153 e.printStackTrace();
00154 } catch (AlreadyBoundException e) {
00155 e.printStackTrace();
00156 }
00157 }
00158
00163 private boolean registerServlet() {
00164 try {
00165
00166
00167 HttpContext myContext = new HttpContext () {
00168
00169
00170 public boolean handleSecurity(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
00171
00172 return false;
00173 }
00174
00175 public URL getResource(String arg0) {
00176 try {
00177 return new URL(aliasServlet + "?" + arg0);
00178 } catch (MalformedURLException e) {
00179
00180 e.printStackTrace();
00181 }
00182 return null;
00183 }
00184
00185 public String getMimeType(String arg0) {
00186
00187 return null;
00188 }
00189
00190 };
00191
00192 this.http.registerServlet(aliasServlet, this.servlet, null, null);
00193 this.http.registerResources("/",aliasServlet + "doc" ,myContext);
00194
00195
00196 return true;
00197 } catch (Throwable t) {
00198 t.printStackTrace();
00199 return false;
00200 }
00201 }
00202
00206 public void terminate() {
00207
00208
00209 try {
00210 this.node.killAllActiveObjects();
00211 ProActiveRuntimeImpl.getProActiveRuntime().killNode(OSGI_NODE_NAME);
00212 } catch (NodeException e) {
00213 e.printStackTrace();
00214 } catch (IOException e) {
00215 e.printStackTrace();
00216 } catch (ProActiveException e) {
00217 e.printStackTrace();
00218 }
00219
00220
00221 this.http.unregister(aliasServlet);
00222
00223 }
00224 }