KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > KillApl


1 /* $Id: KillApl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Run;
3
4 import org.omg.CosNaming.NameComponent JavaDoc;
5
6 import SOFA.SOFAnode.Run.DeplDockRegistry.DeplDockRegistry;
7 import SOFA.SOFAnode.Run.DeplDockRegistry.DeplDockRegistryHelper;
8 import SOFA.SOFAnode.Run.DeplDockRegistry.NotFoundException;
9 import SOFA.SOFAnode.Run.Deployment.DeplDock;
10
11 /** Stops a SOFA application.
12   *
13   * @author Petr Hnetynka
14   */

15 public class KillApl {
16   /** Stops a SOFA application.
17     * Arguments: 1st - (required) name of the depldock, 2nd - (optional) name of the component
18     */

19   public static void main(String JavaDoc[] argv) {
20     if ((argv.length < 1) && (argv.length > 2)) {
21       System.out.println("Bad number of arguments.");
22       System.exit(1);
23     }
24     if (System.getSecurityManager() == null) {
25       System.setSecurityManager(new java.rmi.RMISecurityManager JavaDoc());
26     }
27
28     SOFA.Util.CORBAAccess ca = new SOFA.Util.CORBAAccess();
29
30     NameComponent JavaDoc nc = new NameComponent JavaDoc("DeplDockRegistry", "");
31     NameComponent JavaDoc path[] = {nc};
32     DeplDockRegistry ddr = null;
33     try {
34       System.out.print("Obtaining depldock registry...");
35       ddr = DeplDockRegistryHelper.narrow(SOFA.Util.CORBAAccess.ncRef.resolve(path));
36       System.out.println("OK");
37     } catch (Exception JavaDoc e) {
38       System.out.println("Can't get DeplDock registry.");
39       e.printStackTrace();
40       System.exit(1);
41     }
42     
43     
44     byte[] serRef = null;
45     try {
46       System.out.print("Obtaining reference to depldock...");
47       serRef = ddr.lookup(argv[0]);
48       System.out.println("OK");
49     } catch (NotFoundException e) {
50       System.out.println("No deployment dock with name \""+argv[1]+"\".");
51       System.exit(1);
52     }
53     
54     SOFA.Connector.Reference ddRef = SOFA.SOFAnode.Run.Deployment.DeplDockImpl.bytesToSofaReference(serRef);
55    
56     DeplDock ddock = null;
57     try {
58       System.out.print("Creating connector to depldock...");
59       ddock = (DeplDock) SOFA.Connector.Boot.DeplDockConnector.createClt(ddRef);
60       System.out.println("OK");
61     } catch (SOFA.Connector.ConnectorException e) {
62       System.out.println("Can't create connector to deployment dock.");
63       System.exit(1);
64     }
65
66     String JavaDoc nameOK = null;
67     String JavaDoc[] names = ddock.namesOfAllRunningApplications();
68     
69     if (argv.length == 2) {
70       boolean found = false;
71       for (int i=0; i< names.length; i++) {
72         if (names[i].compareTo(argv[1])==0) {
73           found = true;
74           break;
75         }
76       }
77       if (!found) {
78         System.out.println("No application with given name");
79         System.exit(1);
80       }
81       nameOK = argv[1];
82     } else {
83       System.out.println("Choose application to destroy");
84       System.out.println("=============================");
85       for (int i=0;i<names.length; i++) {
86         System.out.print(i);
87         System.out.print("...");
88         System.out.println(names[i]);
89       }
90       try {
91         java.io.BufferedReader JavaDoc in = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
92         String JavaDoc ln = in.readLine();
93         ln.trim();
94         int num = Integer.parseInt(ln);
95         if (num<0 || num>=names.length) {
96           System.out.println("Bad number");
97           System.exit(1);
98         }
99         nameOK = names[num];
100       } catch (Exception JavaDoc e) {
101         System.out.println("Exception: "+e.getMessage());
102         e.printStackTrace();
103         System.exit(1);
104       }
105     }
106
107     SOFA.Connector.Reference apl = null;
108     try {
109       System.out.print("Obtaining reference to CM...");
110       apl = ddock.getReference(nameOK);
111       System.out.println("OK");
112     } catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {
113       System.out.println("DeploymentException: "+e.getMessage());
114       e.printStackTrace();
115       System.exit(1);
116     }
117
118     SOFA.Component.DCUP.DCUPComponentManager aplcm = null;
119     try {
120       System.out.print("Creating connector to CM...");
121       aplcm = (SOFA.Component.DCUP.DCUPComponentManager) SOFA.Connector.Boot.DCUPComponentManagerConnector.createClt(apl);
122       System.out.println("OK");
123     } catch (SOFA.Connector.ConnectorException e) {
124       System.out.println("Can't create connector to CM.");
125       System.exit(1);
126     }
127
128     try {
129       System.out.print("Destroying component...");
130       aplcm.destroyComponent(new SOFA.SOFAnode.Run.StorageImpl());
131       System.out.println("OK");
132     } catch (SOFA.Component.ComponentLifecycleException e) {
133       System.out.println("ComponentLifecycleException: "+e.getMessage());
134       e.printStackTrace();
135       System.exit(1);
136     }
137   }
138 }
139
Popular Tags