1 21 22 package com.rift.coad.commandline.jythondaemoncommandlinetool; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedReader ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileReader ; 30 import java.io.InputStreamReader ; 31 import com.rift.coad.daemon.jython.JythonDaemon; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Enumeration ; 35 import java.util.HashMap ; 36 import java.util.Hashtable ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.Set ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.rmi.PortableRemoteObject ; 43 import java.util.PropertyResourceBundle ; 44 45 52 public class Main { 53 public BufferedReader br = new BufferedReader ( 54 new InputStreamReader (System.in)); 55 public String [] args = null; 56 public String url = ""; 57 public String host = ""; 58 public String username = ""; 59 public String password = ""; 60 61 62 public Main() { 63 } 64 65 68 public static void main(String [] args) { 69 Main mainProgram = new Main(); 70 mainProgram.setArgs(args); 71 mainProgram.commandLine(); 72 } 73 74 79 public void setArgs(String [] args) { 80 this.args = args; 81 } 82 83 87 public void commandLine() { 88 try { 89 JythonDaemon sb = null; 90 int choice = -1; 91 String location = ""; 92 String name = ""; 93 String propertyFile = ""; 94 String jClass = ""; 95 String returnValue = ""; 96 byte[] fileBytes = null; 97 HashMap arguments = new HashMap (); 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 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 file = new File (location); 123 FileInputStream fis = new FileInputStream (file); 124 BufferedInputStream bis = new BufferedInputStream (fis); 125 int count2 = 0; 126 byte[] bytes = new byte[1024]; 127 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 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 file = new File (propertyFile); 153 FileInputStream fis = new FileInputStream (file); 154 PropertyResourceBundle prb = new 155 PropertyResourceBundle (fis); 156 arguments = new HashMap (); 157 Enumeration keys = prb.getKeys(); 158 while (keys.hasMoreElements()) { 159 String key = (String ) keys.nextElement(); 160 Object 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 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 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 258 public JythonDaemon createConnection() 259 throws Exception { 260 try { 261 Hashtable env = new Hashtable (); 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 ctx = new InitialContext (env); 269 270 Object 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 ("narrow failed."); 278 } else { 279 return beanInterface; 280 } 281 } catch (Exception ex) { 282 System.out.println(ex.getMessage()); 283 ex.printStackTrace(System.out); 284 throw new Exception (ex); 285 } 286 } 287 288 } 289 | Popular Tags |