KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > commandline > jythondaemoncommandlinetool > 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.jythondaemoncommandlinetool;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import com.rift.coad.daemon.jython.JythonDaemon;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Hashtable JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Set JavaDoc;
40 import javax.naming.Context JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.rmi.PortableRemoteObject JavaDoc;
43 import java.util.PropertyResourceBundle JavaDoc;
44
45 /**
46  *
47  * @author Glynn Chaldecott
48  *
49  * This class is a command line tool for the Jython daemon and this will give a
50  * user basic Jython functionality through the command line.
51  */

52 public class Main {
53     public BufferedReader JavaDoc br = new BufferedReader JavaDoc(
54             new InputStreamReader JavaDoc(System.in));
55     public String JavaDoc[] args = null;
56     public String JavaDoc url = "";
57     public String JavaDoc host = "";
58     public String JavaDoc username = "";
59     public String JavaDoc password = "";
60     
61     /** Creates a new instance of Main */
62     public Main() {
63     }
64     
65     /**
66      * @param args the command line arguments
67      */

68     public static void main(String JavaDoc[] args) {
69         Main mainProgram = new Main();
70         mainProgram.setArgs(args);
71         mainProgram.commandLine();
72     }
73     
74     /**
75      * This method simple sets the public args variable.
76      *
77      * @param args This is the args passed into the main method.
78      */

79     public void setArgs(String JavaDoc[] args) {
80         this.args = args;
81     }
82     
83     /**
84      * This method breaks the arguments down and uses them to perform the
85      * necessary task.
86      */

87     public void commandLine() {
88         try {
89             JythonDaemon sb = null;
90             int choice = -1;
91             String JavaDoc location = "";
92             String JavaDoc name = "";
93             String JavaDoc propertyFile = "";
94             String JavaDoc jClass = "";
95             String JavaDoc returnValue = "";
96             byte[] fileBytes = null;
97             HashMap JavaDoc arguments = new HashMap JavaDoc();
98             int count = 0;
99             for (int i = 0; i < args.length; i ++) {
100                 if (args[i].equals("-r")) {
101                     choice = 0;
102                 } else if (args[i].equals("-e")) {
103                     choice = 1;
104                 } else if (args[i].equals("-h")) {
105                     choice = 2;
106                 } else if (args[i].equals("-l")) {
107                     i ++;
108                     String JavaDoc spacer = "";
109                     location = "";
110                     boolean flag = true;
111                     while (flag) {
112                         location += spacer + args[i];
113                         spacer = " ";
114                         i ++;
115                         if (i == args.length) {
116                             flag = false;
117                         } else if (args[i].charAt(0) == '-') {
118                             flag = false;
119                         }
120                     }
121                     i --;
122                     File JavaDoc file = new File JavaDoc(location);
123                     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
124                     BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
125                     int count2 = 0;
126                     byte[] bytes = new byte[1024];
127                     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
128                     while ((count2 = bis.read(bytes)) != -1) {
129                         baos.write(bytes,0,count2);
130                     }
131                     fileBytes = baos.toByteArray();
132                 } else if (args[i].equals("-n")) {
133                     i ++;
134                     name = args[i];
135                     count ++;
136                 } else if (args[i].equals("-p")) {
137                     i ++;
138                     String JavaDoc spacer = "";
139                     propertyFile = "";
140                     boolean flag = true;
141                     while (flag) {
142                         propertyFile += spacer + args[i];
143                         spacer = " ";
144                         i ++;
145                         if (i == args.length) {
146                             flag = false;
147                         } else if (args[i].charAt(0) == '-') {
148                             flag = false;
149                         }
150                     }
151                     i --;
152                     File JavaDoc file = new File JavaDoc(propertyFile);
153                     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
154                     PropertyResourceBundle JavaDoc prb = new
155                             PropertyResourceBundle JavaDoc(fis);
156                     arguments = new HashMap JavaDoc();
157                     Enumeration JavaDoc keys = prb.getKeys();
158                     while (keys.hasMoreElements()) {
159                         String JavaDoc key = (String JavaDoc) keys.nextElement();
160                         Object JavaDoc value = prb.handleGetObject(key);
161                         arguments.put(key,value);
162                     }
163                     count ++;
164                 } else if (args[i].equals("-j")) {
165                     i ++;
166                     jClass = args[i];
167                     count ++;
168                 } else if (args[i].equals("-v")) {
169                     i ++;
170                     returnValue = args[i];
171                     count ++;
172                 } else if (args[i].equals("-s")) {
173                     i ++;
174                     host = args[i];
175                     if (host.indexOf(":") == -1) {
176                         host += ":2000";
177                     }
178                 } else if (args[i].equals("-u")) {
179                     i ++;
180                     url = args[i];
181                 } else if (args[i].equals("-U")) {
182                     i ++;
183                     username = args[i];
184                 } else if (args[i].equals("-P")) {
185                     i ++;
186                     password = args[i];
187                 }
188             }
189             switch (choice) {
190                 case 0: if (!url.equals("") && !host.equals("")) {
191                             sb = createConnection();
192                             sb.registerScript(fileBytes,name);
193                             System.out.println("The script has been " +
194                                     "registered.");
195                         } else {
196                             System.out.println("Statment missing elements.");
197                             help();
198                         }
199                         break;
200                 case 1: if (!url.equals("") && !host.equals("")) {
201                             sb = createConnection();
202                             Object JavaDoc returnedValue = null;
203                             if (count == 3) {
204                                 returnedValue = sb.runScript(name,returnValue,
205                                         Class.forName(jClass));
206                             } else if (count == 4) {
207                                 returnedValue = sb.runScript(name,returnValue,
208                                         Class.forName(jClass),arguments);
209                             }
210                             System.out.println(returnedValue.toString());
211                         } else {
212                             System.out.println("Statment missing elements.");
213                             help();
214                         }
215                         break;
216                 case 2: help();
217                         break;
218                 default: System.out.println("There was a problem with the " +
219                         "statement"); break;
220             }
221         } catch (Exception JavaDoc ex) {
222             ex.printStackTrace();
223         }
224     }
225     
226     public void help() {
227         System.out.println("Help");
228         System.out.println("\t-r \tIs used to register a script.");
229         System.out.println("\t-e \tIs used to execute a script. This is to be" +
230                 " followed by the name of the script.");
231         System.out.println("\t-l \tThis is to be followed by location of the " +
232                 "local script file to be uploaded.");
233         System.out.println("\t-n \tThis is to be followed by the name of the " +
234                 "script that is being uploaded.");
235         System.out.println("\t-p \tThis is to be followed by the full path of" +
236                 " a property file which supplies values for the script that " +
237                 "is to be run.");
238         System.out.println("\t-j \tThis is a java class that determines the " +
239                 "type of object returned from the script.");
240         System.out.println("\t-v \tThis is to be followed by the name of a " +
241                 "value that is to be returned from the script.");
242         System.out.println("\t-s \tThis is to be followed by the host and " +
243                 "port on which the Jython Daemon is running and is " +
244                 "required.");
245         System.out.println("\t-u \tThis is to be followed by the JNDI for " +
246                 "the Jython Daemon and is required.");
247         System.out.println("\t-U \tThis must be followed by the username for " +
248                 "the connection and is required.");
249         System.out.println("\t-P \tThis must be followed by the password for " +
250                 "the connection and is required.");
251     }
252     
253     /**
254      * This method connects to the Timer Daemon and then returns the Object.
255      *
256      * @return This method returns a connection to the Timer Daemon.
257      */

258     public JythonDaemon createConnection()
259             throws Exception JavaDoc {
260         try {
261             Hashtable JavaDoc env = new Hashtable JavaDoc();
262             env.put(Context.INITIAL_CONTEXT_FACTORY,
263                     "com.rift.coad.client.naming." +
264                     "CoadunationInitialContextFactory");
265             env.put(Context.PROVIDER_URL,host);
266             env.put("com.rift.coad.username",username);
267             env.put("com.rift.coad.password",password);
268             Context JavaDoc ctx = new InitialContext JavaDoc(env);
269             
270             Object JavaDoc obj = ctx.lookup(url);
271             JythonDaemon beanInterface =
272                     (JythonDaemon)
273                     PortableRemoteObject.narrow(obj,
274                     com.rift.coad.daemon.jython.JythonDaemon.class);
275             
276             if (beanInterface == null) {
277                 throw new Exception JavaDoc("narrow failed.");
278             } else {
279                 return beanInterface;
280             }
281         } catch (Exception JavaDoc ex) {
282             System.out.println(ex.getMessage());
283             ex.printStackTrace(System.out);
284             throw new Exception JavaDoc(ex);
285         }
286     }
287     
288 }
289
Popular Tags