KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > Deployment > ServerActivatorImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Briclet Frederic
23 Contributor(s): ___________________________________________________.
24
25 ====================================================================*/

26
27
28 package org.objectweb.openccm.Deployment;
29
30 import org.objectweb.openccm.corba.*;
31 /**
32  * Interface definition : ServerActivator
33  *
34  * @author OpenORB Compiler
35  */

36 public class ServerActivatorImpl extends org.omg.Components.Deployment.ServerActivatorPOA
37 {
38     /*Last attributed component server number */
39     private int last_cs_num;
40     
41     /*Table for storage of cs*/
42     private java.util.Hashtable JavaDoc c_servers;
43     
44     /*Table for storage of each component server*/
45     private java.util.Hashtable JavaDoc process;
46     
47     /*Name of the local computer*/
48     private String JavaDoc localName ;
49     
50     /*OpenCCM directory */
51     private String JavaDoc openCCMHomeDir ;
52     
53     /*Temporary boolean definition to resolve PDA distinction*/
54     private boolean PDA;
55
56     /**
57      * Main constructor of ServerActivator two parameter are necessary
58      * to be able to start new component server.
59      * @param localName The currnet computer name which use to fix component
60      * server name.
61      * @param openCCMHomeDir Which use to refer component server XML launcher descriptor
62      */

63     public ServerActivatorImpl(String JavaDoc localName, String JavaDoc openCCMHomeDir){
64     last_cs_num=0;
65     c_servers= new java.util.Hashtable JavaDoc();
66     process=new java.util.Hashtable JavaDoc();
67     this.localName=localName;
68     this.openCCMHomeDir=openCCMHomeDir;
69         this.PDA=false;
70     }
71     
72     /**
73      * Peciliar constructor for PDA use
74      */

75     public ServerActivatorImpl(String JavaDoc localName, String JavaDoc openCCMHomeDir,boolean PDA){
76         last_cs_num=0;
77         c_servers= new java.util.Hashtable JavaDoc();
78         process=new java.util.Hashtable JavaDoc();
79         this.localName=localName;
80         this.openCCMHomeDir=openCCMHomeDir;
81         this.PDA=PDA;
82     }
83  
84     /**
85      * Operation create_component_server :
86      * Three main stape buildare lead in this method :
87      * - Build arguments for the command to execute
88      * - Build a new launcher (with this argument) whitch start the Component Server
89      * - Retrieve information ComponentServer reference and process
90      * TODO: Consider the config value
91      */

92     public org.omg.Components.Deployment.ComponentServer
93     create_component_server(org.omg.Components.ConfigValue[] config)
94         throws org.omg.Components.CreateFailure, org.omg.Components.Deployment.InvalidConfiguration
95     {
96     
97         org.omg.Components.Deployment.ComponentServer serverRef =null;
98         try{
99             /* args [0] nom de la machine locale
100              * args[1] non du fichier a ecrire
101              */

102             
103             //Build the name of new Component Server base on a number
104
String JavaDoc server_name="ComponentServer"+last_cs_num++;
105             
106         if(!PDA){
107         //Path to JCS xml launcher descriptor
108
String JavaDoc targetToLaunch =openCCMHomeDir+"/xml/launcher/JCS.xml" ;
109             //Arguments need to launch a Component Server
110
String JavaDoc args []= {server_name,localName,"-ORBInitRef NameService="+TheORB.getORB()
111                  .object_to_string(TheORB.resolve_initial_reference("NameService"))};
112             //Launching the new Component server throw launcher tool
113
org.objectweb.util.launcher.Launcher launcher=
114             new org.objectweb.util.launcher.Launcher(targetToLaunch,"jcs_interne",args);
115         
116         java.io.BufferedReader JavaDoc br = new java.io.BufferedReader JavaDoc(
117                                 new java.io.InputStreamReader JavaDoc(
118                                 launcher.getProcess().getInputStream()));
119         
120         // Skip the first line which signal component server ready
121
br.readLine();
122         
123         // retrieve the component server reference throw the outputed String ior
124
//System.out.println("CS IOR:"+launcher.getServerIOR());
125
serverRef=org.omg.Components.Deployment.
126             ComponentServerHelper.narrow(TheORB.getORB().string_to_object(br.readLine()));
127         
128         //store the reference of component server process
129
process.put(server_name+localName,launcher.getProcess());
130         //all is OK
131
System.out.println("Component Server ["+server_name+localName+"] started");
132         }
133         else{
134         //PDA version avoiding to create new JVM
135
org.objectweb.openccm.Components.ConfigValueImpl[] config1=
136             new org.objectweb.openccm.Components.ConfigValueImpl[1];
137         
138         org.omg.CORBA.Any JavaDoc any= org.objectweb.openccm.corba.TheORB.getORB().create_any();
139         
140                     //store the component server name in config parameter
141
any.insert_string(server_name+localName);
142         config1[0]= new org.objectweb.openccm.Components.ConfigValueImpl("ServerName",any);
143         //creation of new Component Server into the current JVM memory space
144
ComponentServerImpl csi = new ComponentServerImpl(config1);
145         // Activate it.
146
org.omg.CORBA.Object JavaDoc ref =
147             csi._this_object(org.objectweb.openccm.corba.TheORB.getORB());
148         //Retrieve the corba reference
149
serverRef=org.omg.Components.Deployment.ComponentServerHelper.narrow(ref);
150         // Obtain the Name Service.
151
org.objectweb.openccm.corba.NamingContext ns =
152             org.objectweb.openccm.corba.TheNameService.getNamingContext();
153                 
154         // Bind the server object in the name service.
155
ns.rebind(server_name+localName, serverRef);
156         }
157             
158             //store the reference of the component server
159
c_servers.put(server_name+localName,serverRef);
160             //return component server ref
161
return serverRef;
162     }
163     catch(Exception JavaDoc e){e.printStackTrace();}
164     
165     // return serverRef null if this statement is reached
166
return serverRef;
167     }
168     
169     /**
170      * Operation remove_component_server, by stoping him and killing his process.
171      * The component server give in parameter must be created by this Server Activator
172      * else a RemoveFailure exception will be raise.
173      * Call the remove() function on the server pass to parameter.
174      */

175     public void remove_component_server(org.omg.Components.Deployment.ComponentServer server)
176         throws org.omg.Components.RemoveFailure{
177         
178           //Retrive configuration informations
179
org.omg.Components.ConfigValue[] config =server.configuration();
180     // the first config value of component server must be component server name
181
if(!config[0].name.equals("ServerName"))
182         throw new org.omg.Components.RemoveFailure();
183     
184      // test if this Component server was start by this Server Activator
185
if(!c_servers.containsKey(config[0].value.extract_string()))
186         throw new org.omg.Components.RemoveFailure();
187     
188     try{
189         //Start the removing processing for the component server
190
server.remove();
191         if(!PDA){
192             //remove the reference of the process
193
Process JavaDoc p=(Process JavaDoc)process.remove(config[0].value.extract_string());
194         //Remove the reference of the component server
195
c_servers.remove(config[0].value.extract_string());
196         //Kill the process
197
p.destroy();
198         }
199     }
200     catch(Exception JavaDoc e)
201         {
202         e.printStackTrace();
203         System.out.println(e.getMessage());
204         }
205     }
206     
207     
208     /**
209      * Kill the component server process and destroy the delete the CS reference
210      * from the list. Used to break the remove circle reference.
211      */

212     public void remove_component_server_interne(org.omg.Components.Deployment.ComponentServer server)
213     throws org.omg.Components.RemoveFailure
214     {
215     //Retrive configuration informations
216
org.omg.Components.ConfigValue[] config =server.configuration();
217     // the first config value of component server must be component server name
218
if(!config[0].name.equals("ServerName"))
219         throw new org.omg.Components.RemoveFailure();
220     // test if this Component server was start by this Server Activator
221
if(!c_servers.containsKey(config[0].value.extract_string()))
222         throw new org.omg.Components.RemoveFailure();
223     
224     try{
225         //Remove the reference of the component server
226
c_servers.remove(config[0].value.extract_string());
227             
228             if(!PDA){
229         // Retrieve the process reference
230
Process JavaDoc p=(Process JavaDoc)process.remove(config[0].value.extract_string());
231                    //Kill the process
232
p.destroy();
233         }
234     }
235     catch(Exception JavaDoc e){e.printStackTrace();
236     System.out.println(e.getMessage());
237     }
238     
239     }
240     
241     /**
242      * Operation get_component_servers: Return a set of all active components servers
243      * created by this Server Activator
244      */

245     public org.omg.Components.Deployment.ComponentServer[] get_component_servers(){
246         return (org.omg.Components.Deployment.ComponentServer[])
247         c_servers.values().toArray(new org.omg.Components.Deployment.
248         ComponentServer[c_servers.size()]);
249     }
250     
251 }
252
Popular Tags