KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > Deployment > DeplDockImpl


1 /* $Id: DeplDockImpl.java,v 1.4 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Run.Deployment;
3
4 import java.net.InetAddress JavaDoc;
5 import java.net.UnknownHostException JavaDoc;
6
7 import org.omg.CosNaming.NameComponent JavaDoc;
8
9 import SOFA.SOFAnode.Run.DeplDockRegistry.DeplDockRegistry;
10 import SOFA.SOFAnode.Run.DeplDockRegistry.DeplDockRegistryHelper;
11
12
13 /** Implementation of deployment descriptor.
14   *
15   * @author Petr Hnetynka
16   */

17 public class DeplDockImpl implements DeplDock {
18
19   /// Technology description of deployment dock.
20
protected TechnologyDescriptor[] description;
21
22   /** Name of the deployment dock. */
23   protected String JavaDoc name;
24   /** Name of the sofa node */
25   protected String JavaDoc node;
26   /** Deployment dock registry */
27   protected DeplDockRegistry ddr;
28   
29   /** All CM of components in this dock. */
30   protected java.util.Hashtable JavaDoc components;
31
32   /** SRoles of CM in this dock. */
33   protected java.util.Hashtable JavaDoc sroles;
34
35   /** All applications (system) in this dock. */
36   protected java.util.Hashtable JavaDoc applications;
37
38   /** Constructor.
39     * @param _name name of the deployment dock
40     * @param _node name of the sofanode
41     * @param _ddr deployment dock registry
42     */

43   public DeplDockImpl(String JavaDoc _name, String JavaDoc _node, DeplDockRegistry _ddr) {
44     name = _name;
45     node = _node;
46     ddr = _ddr;
47     components = new java.util.Hashtable JavaDoc();
48     sroles = new java.util.Hashtable JavaDoc();
49     applications = new java.util.Hashtable JavaDoc();
50     defineDescription();
51   }
52
53   /** Define the technology <tt>description</tt> of the dock.
54     * It is called from constructor and
55     * in subclasses, this method should be rewritten to change description.
56     * !!! This is only testing implementation !!!
57     */

58   protected void defineDescription() {
59     description = new TechnologyDescriptor [3];
60 /* description[0] = new TechnologyProperty("os","any");
61     description[1] = new TechnologyProperty("exe","java");
62     description[2] = new TechnologyProperty("","");*/

63   }
64
65 /* public DeplDockRegistry getDeplDockRegistry() {
66     // zrusit !!!
67     return null;
68   } */

69   
70   public TechnologyDescriptor[] describeUnderlyingEnvironment() {
71     return description;
72   }
73   
74   public SOFA.Connector.Reference instantiate(DeploymentDescriptor dd, String JavaDoc name) throws DeploymentException {
75     String JavaDoc id = ddr.getUniqueID();
76     ((DeploymentDescriptorImpl) dd).setInstanceId(id);
77     if (name == null || name.compareTo("")==0)
78       name = dd.getInstanceName();
79     String JavaDoc cid = name+":"+id;
80     SOFA.Component.DCUP.DCUPComponentManager cm;
81     try {
82       cm = new SOFA.Component.DCUP.DCUPComponentManagerImpl(dd, node, name, this, id);
83     } catch (SOFA.Component.ComponentLifecycleException e) {
84       throw new DeploymentException("ComponentLifecycleException: "+e.getMessage(), e);
85     }
86     SOFA.Connector.RoleBase srole;
87 /* try {
88       srole = SOFA.Connector.Boot.DCUPComponentManagerConnector.createSrv(cm, id);
89     } catch (SOFA.Connector.ConnectorException e) {
90       throw new DeploymentException("ConnectorException: "+e.getMessage(), e);
91     }*/

92     srole = ((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).connToThisCM();
93     
94     components.put(cid, cm);
95     sroles.put(cid, srole);
96     if (dd.isSystem())
97       applications.put(cid, cm);
98
99     //((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).connToThisCM(srole);
100

101     return srole.getSOFAReference();
102 /* return new SOFA.Connector.Reference("TEST", new SOFA.Connector.TaggedProfile[] { new SOFA.Connector.ECG.Profiles.RMIProfile("rmi://localhost/OK") } ); */
103   }
104
105
106   public String JavaDoc[] namesOfAllRunningComponents() {
107     String JavaDoc[] ret = new String JavaDoc [components.size()];
108     int i = 0;
109     for (java.util.Enumeration JavaDoc e = components.keys() ; e.hasMoreElements() ; i++) {
110       ret[i] = new String JavaDoc ((String JavaDoc) e.nextElement());
111     }
112     return ret;
113   }
114   
115   public String JavaDoc[] namesOfAllRunningApplications() {
116     String JavaDoc[] ret = new String JavaDoc [applications.size()];
117     int i = 0;
118     for (java.util.Enumeration JavaDoc e = applications.keys() ; e.hasMoreElements() ; i++) {
119       ret[i] = new String JavaDoc ((String JavaDoc) e.nextElement());
120     }
121     return ret;
122   }
123
124
125
126   /** Removes LOCAL profile from SOFA reference.
127     * @param ref sofa reference
128     * @return sofa reference without LOCAL profile
129     */

130   public static SOFA.Connector.Reference removeLocalProfile(SOFA.Connector.Reference ref) {
131     boolean b = false;
132     for(int i=0; i<ref.profiles.length; i++) {
133       if (ref.profiles[i] instanceof SOFA.Connector.ECG.Profiles.LocalProfile) {
134         b = true;
135       }
136     }
137     if (b) {
138       SOFA.Connector.TaggedProfile[] p = new SOFA.Connector.TaggedProfile [ref.profiles.length];
139       int j = 0;
140       for (int i=0; i<ref.profiles.length; i++) {
141         if (!(ref.profiles[i] instanceof SOFA.Connector.ECG.Profiles.LocalProfile)) {
142           p[j] = ref.profiles[i];
143           j++;
144         }
145       }
146       ref.profiles = p;
147     }
148     return ref;
149   }
150
151   /** Converts sofa reference to serialized form.
152     * @param ref sofa reference
153     * @return serialized form of sofa reference or <tt>null</tt>
154     */

155   public static byte[] sofaReferenceToBytes(SOFA.Connector.Reference ref) {
156     try {
157 // System.out.println("1");
158
cz.cuni.sofa.lib.Impl.ByteArrayOutputStream os = new cz.cuni.sofa.lib.Impl.ByteArrayOutputStream();
159 // System.out.println(os);
160
ref._write(os);
161       return os.toByteArray();
162     } catch (java.io.IOException JavaDoc e) {
163       return null;
164     }
165   }
166
167   synchronized public void removeComponent(String JavaDoc name) throws SOFA.SOFAnode.Run.Deployment.DeploymentException {
168     if (!sroles.containsKey(name))
169       throw new DeploymentException("No component with given name");
170     SOFA.Component.DCUP.DCUPComponentManagerImpl cm = (SOFA.Component.DCUP.DCUPComponentManagerImpl) components.get(name);
171     if (cm == null) {
172       sroles.remove(name);
173       throw new DeploymentException("Error in the component registering");
174     }
175     if (!cm.isStopped())
176       throw new DeploymentException("Component can't be removed");
177     sroles.remove(name);
178     components.remove(name);
179     if (applications.containsKey(name))
180       applications.remove(name);
181   }
182   
183   /** Converts serialized form of sofa reference to real sofa reference.
184     * @param ref byte array with sofa reference
185     * @return sofa reference or <tt>null</tt>
186     */

187   public static SOFA.Connector.Reference bytesToSofaReference(byte[] ref) {
188     try {
189       cz.cuni.sofa.lib.Impl.ByteArrayInputStream is = new cz.cuni.sofa.lib.Impl.ByteArrayInputStream (ref);
190       SOFA.Connector.Reference ret = new SOFA.Connector.Reference ();
191       ret._read(is);
192       return ret;
193     } catch (java.io.IOException JavaDoc e) {
194       return null;
195     }
196   }
197
198   synchronized public SOFA.Connector.Reference getReference(String JavaDoc name) throws SOFA.SOFAnode.Run.Deployment.DeploymentException {
199     if (!sroles.containsKey(name))
200       throw new DeploymentException("No component with given name");
201     return ((SOFA.Connector.RoleBase) sroles.get(name)).getSOFAReference();
202   }
203
204
205   /** Launching of depldock. Name of the depldock must be set by cmdline arguments.
206     * If you want distributed SOFA node (i.e. depldock of one SOFA node run on several computers), you must
207     * set "sofa.sofanode" property to the name of the SOFA node (it should be name of computer, where
208     * DeplDockRegistry runs. And also you should set properties "sofa.rmihost", "sofa.rmiport",
209     * "sofa.orbport", "sofa.orbhost".
210     * @param argv cmdline arguments<br>1st argument - name of the dock
211     */

212   public static void main(String JavaDoc[] argv) {
213     if (argv.length != 1) {
214       System.out.println("Set name of the dock");
215       System.exit(1);
216     }
217     
218     if (System.getSecurityManager() == null) {
219       System.setSecurityManager(new java.rmi.RMISecurityManager JavaDoc());
220     }
221
222     String JavaDoc sofaNodeName = System.getProperty("sofa.nodename","");
223     if (sofaNodeName.compareTo("")==0) {
224       try {
225         sofaNodeName = InetAddress.getLocalHost().getHostName();
226       } catch (UnknownHostException JavaDoc e) {
227         System.out.println("Can't get host name. Set name to property \"sofa.nodename\"");
228         System.exit(1);
229       }
230     }
231
232     System.setProperty("sofa.nodename",sofaNodeName);
233
234     SOFA.Util.CORBAAccess ca = new SOFA.Util.CORBAAccess();
235
236     NameComponent JavaDoc nc = new NameComponent JavaDoc("DeplDockRegistry", "");
237     NameComponent JavaDoc path[] = {nc};
238     DeplDockRegistry ddr = null;
239     try {
240       ddr = DeplDockRegistryHelper.narrow(SOFA.Util.CORBAAccess.ncRef.resolve(path));
241     } catch (Exception JavaDoc e) {
242       System.out.println("Can't get DeplDock registry.");
243       e.printStackTrace();
244       System.exit(1);
245     }
246
247     DeplDockImpl ddi = new DeplDockImpl(argv[0], sofaNodeName, ddr);
248     SOFA.Connector.RoleBase srole = null;
249     try {
250        srole = SOFA.Connector.Boot.DeplDockConnector.createSrv((DeplDock) ddi, argv[0]);
251     } catch (SOFA.Connector.ConnectorException e) {
252       System.out.println("Can't create connector to depldock");
253       e.printStackTrace();
254       System.exit(1);
255     }
256     SOFA.Connector.Reference ref = srole.getSOFAReference();
257 // ref = removeLocalProfile(ref);
258
byte[] strRef = sofaReferenceToBytes(ref);
259 // System.out.println("###"+new String(strRef));
260
ddr.register(strRef, argv[0]);
261     System.out.println("DeplDock "+argv[0]+" is registered and running.");
262     
263     ca.run();
264   }
265 }
266
Popular Tags