001: /*
002: * Timer: The timer class
003: * Copyright (C) 2006-2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * Main.java
020: */
021:
022: package com.rift.coad.commandline.servicebrokercommandlinetool;
023:
024: // java imports
025: import java.util.ArrayList;
026: import java.util.Hashtable;
027: import java.util.List;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.rmi.PortableRemoteObject;
031: import java.io.*;
032:
033: // log4j
034: import org.apache.log4j.BasicConfigurator;
035:
036: // coadunation imports
037: import com.rift.coad.daemon.servicebroker.ServiceBroker;
038: import com.rift.coad.daemon.servicebroker.ServiceBrokerException;
039:
040: /**
041: *
042: * @author Glynn Chaldecott
043: *
044: * This class is a command line tool for the Service Broker Daemon and gives a
045: * user basic functionality via the command line.
046: */
047: public class Main {
048: public BufferedReader br = new BufferedReader(
049: new InputStreamReader(System.in));
050: public String[] args = null;
051: public String url = "";
052: public String host = "";
053: public String username = "";
054: public String password = "";
055:
056: /** Creates a new instance of Main */
057: public Main() {
058:
059: }
060:
061: /**
062: * @param args the command line arguments
063: */
064: public static void main(String[] args)
065: throws ServiceBrokerException {
066: BasicConfigurator.configure();
067: Main mainProgram = new Main();
068: mainProgram.setArgs(args);
069: mainProgram.commandLine();
070: }
071:
072: /**
073: * This method simple sets the public args variable.
074: *
075: * @param args This is the args passed into the main method.
076: */
077: public void setArgs(String[] args) {
078: this .args = args;
079: }
080:
081: /**
082: * This method breaks the arguments down and uses them to perform the
083: * necessary task.
084: */
085: public void commandLine() {
086: try {
087: ServiceBroker sb = null;
088: int choice = -1;
089: List service = new ArrayList();
090: String jndi = "";
091: for (int i = 0; i < args.length; i++) {
092: if (args[i].equals("-r")) {
093: choice = 0;
094: } else if (args[i].equals("-o")) {
095: choice = 1;
096: } else if (args[i].equals("-m")) {
097: choice = 2;
098: } else if (args[i].equals("-d")) {
099: choice = 3;
100: } else if (args[i].equals("-S")) {
101: i++;
102: service.add(args[i]);
103: } else if (args[i].equals("-j")) {
104: i++;
105: jndi = args[i];
106: } else if (args[i].equals("-h")) {
107: choice = 4;
108: } else if (args[i].equals("-s")) {
109: i++;
110: host = args[i];
111: if (host.indexOf(":") == -1) {
112: host += ":2000";
113: }
114: } else if (args[i].equals("-u")) {
115: i++;
116: url = args[i];
117: } else if (args[i].equals("-U")) {
118: i++;
119: username = args[i];
120: } else if (args[i].equals("-P")) {
121: i++;
122: password = args[i];
123: }
124: }
125: switch (choice) {
126: case 0:
127: if (!url.equals("") && !host.equals("")) {
128: sb = createConnection();
129: sb.registerService(jndi, service);
130: System.out.println("The service has been "
131: + "registered.");
132: } else {
133: System.out.println("Statment missing elements.");
134: help();
135: }
136: break;
137: case 1:
138: if (!url.equals("") && !host.equals("")) {
139: sb = createConnection();
140: String temp = sb.getServiceProvider(service);
141: System.out.println(temp);
142: } else {
143: System.out.println("Statment missing elements.");
144: help();
145: }
146: break;
147: case 2:
148: if (!url.equals("") && !host.equals("")) {
149: sb = createConnection();
150: List temp2 = sb.getServiceProviders(service);
151: for (int i = 0; i < temp2.size(); i++) {
152: System.out.println((String) temp2.get(i));
153: }
154: } else {
155: System.out.println("Statment missing elements.");
156: help();
157: }
158: break;
159: case 3:
160: if (!url.equals("") && !host.equals("")) {
161: sb = createConnection();
162: sb.removeServiceProviders(jndi, service);
163: System.out.println("The service has been deleted.");
164: } else {
165: System.out.println("Statment missing elements.");
166: help();
167: }
168: break;
169: case 4:
170: help();
171: break;
172: default:
173: System.out.println("There was a problem with the "
174: + "statement");
175: help();
176: break;
177: }
178: } catch (Exception ex) {
179: ex.printStackTrace();
180: }
181: }
182:
183: public void help() {
184: System.out.println("Help");
185: System.out
186: .println("\t-r \tThis is to be user to register a service.");
187: System.out
188: .println("\t-o \tThis is to be used to retrieve a single "
189: + "JNDI");
190: System.out
191: .println("\t-m \tThis is to be used to retrieve multiple "
192: + "JNDI's");
193: System.out
194: .println("\t-d \tThis is to be used to delete a service");
195: System.out
196: .println("\t-S \tThis is to be followed by the name of the "
197: + "service. This can be used multiple times in a single "
198: + "statement.");
199: System.out
200: .println("\t-j \tThis is to be followed by the JNDI for "
201: + "the service. This can only be called once in a statment.");
202: System.out
203: .println("\t-s \tThis is to be followed by the host and "
204: + "port on which the Service Broker Daemon is running and is "
205: + "required.");
206: System.out
207: .println("\t-u \tThis is to be followed by the JNDI for "
208: + "the Service Broker Daemon and is required.");
209: System.out
210: .println("\t-U \tThis must be followed by the username for "
211: + "the connection and is required.");
212: System.out
213: .println("\t-P \tThis must be followed by the password for "
214: + "the connection and is required.");
215: }
216:
217: /**
218: * This method connects to the Timer Daemon and then returns the Object.
219: *
220: * @return This method returns a connection to the Timer Daemon.
221: */
222: public com.rift.coad.daemon.servicebroker.ServiceBroker createConnection()
223: throws Exception {
224: try {
225: Hashtable env = new Hashtable();
226: env.put(Context.INITIAL_CONTEXT_FACTORY,
227: "com.rift.coad.client.naming."
228: + "CoadunationInitialContextFactory");
229: env.put(Context.PROVIDER_URL, host);
230: env.put("com.rift.coad.username", username);
231: env.put("com.rift.coad.password", password);
232: Context ctx = new InitialContext(env);
233:
234: Object obj = ctx.lookup(url);
235: com.rift.coad.daemon.servicebroker.ServiceBroker beanInterface = (com.rift.coad.daemon.servicebroker.ServiceBroker) PortableRemoteObject
236: .narrow(
237: obj,
238: com.rift.coad.daemon.servicebroker.ServiceBroker.class);
239:
240: if (beanInterface == null) {
241: throw new Exception("narrow failed.");
242: } else {
243: return beanInterface;
244: }
245: } catch (Exception ex) {
246: System.out.println(ex.getMessage());
247: ex.printStackTrace(System.out);
248: throw new Exception(ex);
249: }
250: }
251:
252: }
|