org/objectweb/proactive/osgi/ProActiveServicesImpl.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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                                         // TODO Auto-generated method stub
00172                                         return false;
00173                                 }
00174 
00175                                 public URL getResource(String arg0) {
00176                                         try {
00177                                                 return new URL(aliasServlet + "?" + arg0);
00178                                         } catch (MalformedURLException e) {
00179                                                 // TODO Auto-generated catch block
00180                                                 e.printStackTrace();
00181                                         }
00182                                         return null;
00183                                 }
00184 
00185                                 public String getMimeType(String arg0) {
00186                                         // TODO Auto-generated method stub
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         /* kill Nodes */
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         /* unregister Servlet */
00221         this.http.unregister(aliasServlet);
00222         
00223     }
00224 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1