KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > commandline > servicebrokercommandlinetool > Main


1 /*
2  * Timer: The timer class
3  * Copyright (C) 2006-2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Main.java
20  */

21
22 package com.rift.coad.commandline.servicebrokercommandlinetool;
23
24 import com.rift.coad.daemon.servicebroker.ServiceBroker;
25 import com.rift.coad.daemon.servicebroker.ServiceBrokerException;
26 import java.lang.Exception JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import java.io.*;
34
35 /**
36  *
37  * @author Glynn Chaldecott
38  *
39  * This class is a command line tool for the Service Broker Daemon and gives a
40  * user basic functionality via the command line.
41  */

42 public class Main {
43     public BufferedReader br = new BufferedReader(
44             new InputStreamReader(System.in));
45     public String JavaDoc[] args = null;
46     public String JavaDoc url = "";
47     public String JavaDoc host = "";
48     public String JavaDoc username = "";
49     public String JavaDoc password = "";
50     
51     /** Creates a new instance of Main */
52     public Main() {
53         
54     }
55     
56     /**
57      * @param args the command line arguments
58      */

59     public static void main(String JavaDoc[] args) throws ServiceBrokerException {
60         Main mainProgram = new Main();
61         mainProgram.setArgs(args);
62         mainProgram.commandLine();
63     }
64     
65     /**
66      * This method simple sets the public args variable.
67      *
68      * @param args This is the args passed into the main method.
69      */

70     public void setArgs(String JavaDoc[] args) {
71         this.args = args;
72     }
73     
74     /**
75      * This method breaks the arguments down and uses them to perform the
76      * necessary task.
77      */

78     public void commandLine() {
79         try {
80             ServiceBroker sb = null;
81             int choice = -1;
82             List JavaDoc service = new ArrayList JavaDoc();
83             String JavaDoc jndi = "";
84             for (int i = 0; i < args.length; i ++) {
85                 if (args[i].equals("-r")) {
86                     choice = 0;
87                 } else if (args[i].equals("-o")) {
88                     choice = 1;
89                 } else if (args[i].equals("-m")) {
90                     choice = 2;
91                 } else if (args[i].equals("-d")) {
92                     choice = 3;
93                 } else if (args[i].equals("-S")) {
94                     i ++;
95                     service.add(args[i]);
96                 } else if (args[i].equals("-j")) {
97                     i ++;
98                     jndi = args[i];
99                 } else if (args[i].equals("-h")) {
100                     choice = 4;
101                 } else if (args[i].equals("-s")) {
102                     i ++;
103                     host = args[i];
104                     if (host.indexOf(":") == -1) {
105                         host += ":2000";
106                     }
107                 } else if (args[i].equals("-u")) {
108                     i ++;
109                     url = args[i];
110                 } else if (args[i].equals("-U")) {
111                     i ++;
112                     username = args[i];
113                 } else if (args[i].equals("-P")) {
114                     i ++;
115                     password = args[i];
116                 }
117             }
118             switch (choice) {
119                 case 0: if (!url.equals("") && !host.equals("")) {
120                             sb = createConnection();
121                             sb.registerService(jndi,service);
122                             System.out.println("The service has been " +
123                                     "registered.");
124                         } else {
125                             System.out.println("Statment missing elements.");
126                             help();
127                         }
128                         break;
129                 case 1: if (!url.equals("") && !host.equals("")) {
130                             sb = createConnection();
131                             String JavaDoc temp = sb.getServiceProvider(service);
132                             System.out.println(temp);
133                         } else {
134                             System.out.println("Statment missing elements.");
135                             help();
136                         }
137                         break;
138                 case 2: if (!url.equals("") && !host.equals("")) {
139                             sb = createConnection();
140                             List JavaDoc temp2 = sb.getServiceProviders(service);
141                             for (int i = 0; i < temp2.size(); i ++) {
142                                 System.out.println((String JavaDoc) temp2.get(i));
143                             }
144                         } else {
145                             System.out.println("Statment missing elements.");
146                             help();
147                         }
148                         break;
149                 case 3: if (!url.equals("") && !host.equals("")) {
150                             sb = createConnection();
151                             sb.removeServiceProviders(jndi,service);
152                             System.out.println("The service has been deleted.");
153                         } else {
154                             System.out.println("Statment missing elements.");
155                             help();
156                         }
157                         break;
158                 case 4: help();
159                         break;
160                 default: System.out.println("There was a problem with the " +
161                         "statement"); break;
162             }
163         } catch (Exception JavaDoc ex) {
164             ex.printStackTrace();
165         }
166     }
167     
168     public void help() {
169         System.out.println("Help");
170         System.out.println("\t-r \tThis is to be user to register a service.");
171         System.out.println("\t-o \tThis is to be used to retrieve a single " +
172                 "JNDI");
173         System.out.println("\t-m \tThis is to be used to retrieve multiple " +
174                 "JNDI's");
175         System.out.println("\t-d \tThis is to be used to delete a service");
176         System.out.println("\t-S \tThis is to be followed by the name of the " +
177                 "service. This can be used multiple times in a single " +
178                 "statement.");
179         System.out.println("\t-j \tThis is to be followed by the JNDI for " +
180                 "the service. This can only be called once in a statment.");
181         System.out.println("\t-s \tThis is to be followed by the host and " +
182                 "port on which the Service Broker Daemon is running and is " +
183                 "required.");
184         System.out.println("\t-u \tThis is to be followed by the JNDI for " +
185                 "the Service Broker Daemon and is required.");
186         System.out.println("\t-U \tThis must be followed by the username for " +
187                 "the connection and is required.");
188         System.out.println("\t-P \tThis must be followed by the password for " +
189                 "the connection and is required.");
190     }
191     
192     /**
193      * This method connects to the Timer Daemon and then returns the Object.
194      *
195      * @return This method returns a connection to the Timer Daemon.
196      */

197     public com.rift.coad.daemon.servicebroker.ServiceBroker createConnection()
198             throws Exception JavaDoc {
199         try {
200             Hashtable JavaDoc env = new Hashtable JavaDoc();
201             env.put(Context.INITIAL_CONTEXT_FACTORY,
202                     "com.rift.coad.client.naming." +
203                     "CoadunationInitialContextFactory");
204             env.put(Context.PROVIDER_URL,host);
205             env.put("com.rift.coad.username",username);
206             env.put("com.rift.coad.password",password);
207             Context JavaDoc ctx = new InitialContext JavaDoc(env);
208             
209             Object JavaDoc obj = ctx.lookup(url);
210             com.rift.coad.daemon.servicebroker.ServiceBroker beanInterface =
211                     (com.rift.coad.daemon.servicebroker.ServiceBroker)
212                     PortableRemoteObject.narrow(obj,
213                     com.rift.coad.daemon.servicebroker.ServiceBroker.class);
214             
215             if (beanInterface == null) {
216                 throw new Exception JavaDoc("narrow failed.");
217             } else {
218                 return beanInterface;
219             }
220         } catch (Exception JavaDoc ex) {
221             System.out.println(ex.getMessage());
222             ex.printStackTrace(System.out);
223             throw new Exception JavaDoc(ex);
224         }
225     }
226     
227 }
228
Popular Tags