KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > dist > rmi > RMIDistd


1 /*
2   Copyright (C) 2001 Lionel Seinturier
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser Generaly Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.core.dist.rmi;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.lang.Runtime JavaDoc;
23 import java.rmi.Naming JavaDoc;
24 import java.rmi.RMISecurityManager JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import org.apache.log4j.Logger;
27 import org.objectweb.jac.core.dist.Distd;
28 import org.objectweb.jac.core.dist.RemoteContainer;
29
30 /**
31  * RMIDistd is a jac daemon that supports the RMI communication protocol.
32  *
33  * Daemons hold containers (only one for the moment) which themselves hold
34  * remote objects.
35  *
36  * @author <a HREF="http://www-src.lip6.fr/homepages/Lionel.Seinturier/index-eng.html">Lionel Seinturier</a>
37  */

38  
39 public class RMIDistd extends Distd {
40     static final Logger logger = Logger.getLogger("dist.rmi");
41
42     /**
43      * This method initializes the RMI communication protocol.
44      */

45     public void init() {
46
47         /** Create and install a security manager */
48        
49         if (System.getSecurityManager() == null)
50             System.setSecurityManager(new RMISecurityManager JavaDoc());
51
52         try {
53             File JavaDoc path;
54             File JavaDoc jar = new File JavaDoc(org.objectweb.jac.core.Jac.getJacRoot()+"jac.jar");
55             logger.debug("$JAC_ROOT/jac.jar = "+jar.getPath());
56             if (jar.exists()) {
57                 path = jar;
58             } else {
59                 path = new File JavaDoc(org.objectweb.jac.core.Jac.getJacRoot()+"classes");
60             }
61             logger.debug("Launching rmiregistry with CLASSPATH="+path.getPath());
62             rmiProcess = Runtime.getRuntime().exec(
63                 new String JavaDoc[] { "rmiregistry",
64                                "-J-classpath",
65                                "-J"+path.getPath()});
66         } catch (IOException JavaDoc e) {
67             logger.error("Could not start rmiregistry",e);
68         }
69     }
70
71     static Process JavaDoc rmiProcess = null;
72
73     static {
74         Runtime.getRuntime().addShutdownHook(
75             new Thread JavaDoc() {
76                     public void run() {
77                         if (rmiProcess!=null) {
78                             logger.info("Stopping rmiregistry...");
79                             rmiProcess.destroy();
80                         }
81                     }
82                 }
83         );
84     }
85
86     /**
87      * This method creates a new container and returns it.
88      *
89      * @param name the container name
90      * @return the container reference
91      */

92     protected RemoteContainer newContainer(String JavaDoc name) throws Exception JavaDoc {
93
94         RMIRemoteContainer remCont = null;
95       
96         try {
97             remCont = new RMIRemoteContainer(verbose);
98         } catch(Exception JavaDoc e) {
99             logger.error("newContainer("+name+
100                          "): Instantiation of RMIRemoteContainer failed",e);
101             return null;
102         }
103       
104         //try {
105
registerContainer(remCont, name);
106             /*
107         } catch(Exception e) {
108             // It may fail because rmiregistry is not ready yet
109             // But we don't care since Distd will retry
110             throw new RuntimeException(e.toString());
111         }
112             */

113         remCont.getDelegate().setName(getFullHostName(name));
114
115         return remCont.getDelegate();
116     }
117   
118
119     /**
120      * This method creates a new container, instantiates a given class, and
121      * returns the container.
122      *
123      * @param name the container name
124      * @param className the name of the class to instantiate
125      * @return the container reference
126      */

127     protected RemoteContainer newContainer(String JavaDoc name, String JavaDoc className)
128         throws Exception JavaDoc
129     {
130         RMIRemoteContainer remCont = null;
131       
132         try {
133             remCont = new RMIRemoteContainer(className,verbose);
134         } catch( Exception JavaDoc e ) {
135             e.printStackTrace();
136             return null;
137         }
138
139         registerContainer(remCont, name);
140
141         remCont.getDelegate().setName(getFullHostName(name));
142
143         return remCont.getDelegate();
144     }
145    
146
147     /**
148      * Registers a container in the RMI registry.
149      *
150      * @param container the container reference
151      * @param name the container name
152      */

153     protected void registerContainer(
154         RMIRemoteContainer container,
155         String JavaDoc name)
156         throws java.rmi.RemoteException JavaDoc
157     {
158         /** Register the container in the RMI registry */
159         String JavaDoc fullName = getFullHostName(name);
160      
161         try {
162             Naming.rebind(fullName, container);
163         } catch( java.net.MalformedURLException JavaDoc e ) {
164             logger.error("registerContainer("+container+","+name+")",e);
165         }
166
167         logger.info(
168             "--- JAC Distd (RMI): new container " + fullName + " ---"
169         );
170         logger.info(
171             "--- Default class repository: " + referenceContainerName + " ---"
172         );
173      
174     }
175
176     /**
177      * This method enters the event loop of
178      * the underlying communication protocol.
179      */

180     public void run() {
181         logger.info( "--- JAC Distd (RMI) is running ---" );
182     }
183
184     /**
185      * The is the main constructor.
186      *
187      * @param args command line arguments
188      */

189     public RMIDistd(String JavaDoc[] args) {
190         super(args);
191     }
192
193     public static void main(String JavaDoc[] args) {
194         new RMIDistd(args);
195     }
196    
197 }
198
Popular Tags