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.jythondaemoncommandlinetool;
023:
024: import java.io.BufferedInputStream;
025: import java.io.BufferedReader;
026: import java.io.ByteArrayOutputStream;
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FileReader;
030: import java.io.InputStreamReader;
031: import com.rift.coad.daemon.jython.JythonDaemon;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Enumeration;
035: import java.util.HashMap;
036: import java.util.Hashtable;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.Set;
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.rmi.PortableRemoteObject;
043: import java.util.PropertyResourceBundle;
044:
045: // logging import
046: import org.apache.log4j.BasicConfigurator;
047:
048: /**
049: *
050: * @author Glynn Chaldecott
051: *
052: * This class is a command line tool for the Jython daemon and this will give a
053: * user basic Jython functionality through the command line.
054: */
055: public class Main {
056: public BufferedReader br = new BufferedReader(
057: new InputStreamReader(System.in));
058: public String[] args = null;
059: public String url = null;
060: public String host = null;
061: public String username = null;
062: public String password = null;
063:
064: /** Creates a new instance of Main */
065: public Main(String[] args) {
066: this .args = args;
067: }
068:
069: /**
070: * @param args the command line arguments
071: */
072: public static void main(String[] args) {
073: BasicConfigurator.configure();
074: Main mainProgram = new Main(args);
075: mainProgram.commandLine();
076: }
077:
078: /**
079: * This method breaks the arguments down and uses them to perform the
080: * necessary task.
081: */
082: public void commandLine() {
083: try {
084: JythonDaemon sb = null;
085: int choice = -1;
086: String location = "";
087: String name = "";
088: String propertyFile = "";
089: String jClass = "";
090: String returnValue = "";
091: byte[] fileBytes = null;
092: HashMap arguments = new HashMap();
093: for (int i = 0; i < args.length; i++) {
094: if (args[i].equals("-h")) {
095: choice = 2;
096: break;
097: } else if ((choice == -1) && (args[i].equals("-e"))) {
098: choice = 1;
099: i++;
100: name = args[i];
101: } else if ((choice == -1) && (args[i].equals("-r"))) {
102: choice = 0;
103: } else if ((choice == -1 || choice == 0)
104: && (args[i].equals("-l"))) {
105: i++;
106: String spacer = "";
107: location = "";
108: boolean flag = true;
109: while (flag) {
110: location += spacer + args[i];
111: spacer = " ";
112: i++;
113: if (i == args.length) {
114: flag = false;
115: } else if (args[i].charAt(0) == '-') {
116: flag = false;
117: }
118: }
119: i--;
120: File file = new File(location);
121: int size = (int) file.length();
122: FileInputStream fis = new FileInputStream(file);
123: byte[] bytes = new byte[size];
124: fis.read(bytes, 0, size);
125: fileBytes = bytes;
126: name = file.getName();
127: } else if ((choice == -1 || choice == 1)
128: && (args[i].equals("-p"))) {
129: i++;
130: String spacer = "";
131: propertyFile = "";
132: boolean flag = true;
133: while (flag) {
134: propertyFile += spacer + args[i];
135: spacer = " ";
136: i++;
137: if (i == args.length) {
138: flag = false;
139: } else if (args[i].charAt(0) == '-') {
140: flag = false;
141: }
142: }
143: i--;
144: File file = new File(propertyFile);
145: FileInputStream fis = new FileInputStream(file);
146: PropertyResourceBundle prb = new PropertyResourceBundle(
147: fis);
148: arguments = new HashMap();
149: Enumeration keys = prb.getKeys();
150: while (keys.hasMoreElements()) {
151: String key = (String) keys.nextElement();
152: Object value = prb.handleGetObject(key);
153: arguments.put(key, value);
154: }
155: } else if ((choice == -1 || choice == 1)
156: && (args[i].equals("-t"))) {
157: i++;
158: jClass = args[i];
159: } else if ((choice == -1 || choice == 1)
160: && (args[i].equals("-v"))) {
161: i++;
162: returnValue = args[i];
163: } else if (args[i].equals("-s")) {
164: i++;
165: host = args[i];
166: if (host.indexOf(":") == -1) {
167: host += ":2000";
168: }
169: } else if (args[i].equals("-u")) {
170: i++;
171: url = args[i];
172: } else if (args[i].equals("-U")) {
173: i++;
174: username = args[i];
175: } else if (args[i].equals("-P")) {
176: i++;
177: password = args[i];
178: }
179: }
180:
181: // validate the arguments
182: if ((url == null) || (host == null) || (username == null)
183: || (password == null)) {
184: System.out
185: .println("The <-u jndi url> <-s host:port> "
186: + "<-U username> <-P password> must be supplied.");
187: help();
188: return;
189: }
190:
191: switch (choice) {
192: case 0:
193: if (!location.equals("") && !name.equals("")) {
194: sb = createConnection();
195: sb.registerScript(fileBytes, name);
196: System.out.println("The script has been "
197: + "registered.");
198: } else {
199: System.out
200: .println("Must supply a location of a script "
201: + "when registering a script.");
202: help();
203: }
204: break;
205: case 1:
206: if (!name.equals("") && !returnValue.equals("")
207: && !jClass.equals("")) {
208: sb = createConnection();
209: Object returnedValue = null;
210: if (arguments.size() == 0) {
211: returnedValue = sb.runScript(name, returnValue,
212: Class.forName(jClass));
213: } else {
214: returnedValue = sb.runScript(name, returnValue,
215: Class.forName(jClass), arguments);
216: }
217: System.out.println(returnedValue.toString());
218: } else {
219: System.out
220: .println("Must supply a "
221: + "<-v return variable> and <-t return type> .");
222: help();
223: }
224: break;
225: case 2:
226: default:
227: System.out.println("There was a problem with the "
228: + "statement");
229: help();
230: break;
231: }
232: } catch (Exception ex) {
233: ex.printStackTrace();
234: System.out.println("Command line failed because : "
235: + ex.getMessage());
236: help();
237: }
238: }
239:
240: public void help() {
241: System.out.println("Help");
242: System.out.println("\t-r \tRegister a script.");
243: System.out
244: .println("\t-e \tExecute a script with a given name.");
245: System.out.println("\t-l \tLocation of script to register.");
246: System.out
247: .println("\t-p \tThis is to be followed by the full path of"
248: + " a property file which supplies values for the script that "
249: + "is to be run.");
250: System.out
251: .println("\t-t \tThis is a java class type for the result.");
252: System.out
253: .println("\t-v \tThis is to be followed by the name of a "
254: + "variable that is to be returned from the script.");
255: System.out
256: .println("\t-s \tThis is to be followed by the host and "
257: + "port on which the Jython Daemon is running and is "
258: + "required.");
259: System.out
260: .println("\t-u \tThis is to be followed by the JNDI for "
261: + "the Jython Daemon and is required.");
262: System.out
263: .println("\t-U \tThis must be followed by the username for "
264: + "the connection and is required.");
265: System.out
266: .println("\t-P \tThis must be followed by the password for "
267: + "the connection and is required.");
268: }
269:
270: /**
271: * This method connects to the Timer Daemon and then returns the Object.
272: *
273: * @return This method returns a connection to the Timer Daemon.
274: */
275: public JythonDaemon createConnection() throws Exception {
276: try {
277: Hashtable env = new Hashtable();
278: env.put(Context.INITIAL_CONTEXT_FACTORY,
279: "com.rift.coad.client.naming."
280: + "CoadunationInitialContextFactory");
281: env.put(Context.PROVIDER_URL, host);
282: env.put("com.rift.coad.username", username);
283: env.put("com.rift.coad.password", password);
284: Context ctx = new InitialContext(env);
285:
286: Object obj = ctx.lookup(url);
287: JythonDaemon beanInterface = (JythonDaemon) PortableRemoteObject
288: .narrow(
289: obj,
290: com.rift.coad.daemon.jython.JythonDaemon.class);
291:
292: if (beanInterface == null) {
293: throw new Exception("narrow failed.");
294: } else {
295: return beanInterface;
296: }
297: } catch (Exception ex) {
298: ex.printStackTrace(System.out);
299: System.out
300: .println("Failed to connect to the jython daemon. "
301: + "Check the JDNI reference and host information.");
302: System.exit(-1);
303: return null;
304: }
305: }
306:
307: }
|