1 21 22 package com.rift.coad.commandline.deploymentdaemoncommandlinetool; 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.deployment.DeploymentDaemon; 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 import org.apache.log4j.BasicConfigurator; 45 46 53 public class Main { 54 public BufferedReader br = new BufferedReader ( 55 new InputStreamReader (System.in)); 56 public String [] args = null; 57 public String url = ""; 58 public String host = ""; 59 public String username = ""; 60 public String password = ""; 61 62 63 public Main() { 64 BasicConfigurator.configure(); 65 } 66 67 70 public static void main(String [] args) { 71 Main mainProgram = new Main(); 72 mainProgram.setArgs(args); 73 mainProgram.commandLine(); 74 } 75 76 81 public void setArgs(String [] args) { 82 this.args = args; 83 } 84 85 89 public void commandLine() { 90 try { 91 DeploymentDaemon sb = null; 92 int choice = -1; 93 String location = ""; 94 String name = ""; 95 String path = ""; 96 byte[] fileBytes = null; 97 HashMap arguments = new HashMap (); 98 for (int i = 0; i < args.length; i ++) { 99 if (args[i].equals("-d")) { 100 choice = 0; 101 } else if (args[i].equals("-f")) { 102 choice = 1; 103 } else if (args[i].equals("-h")) { 104 choice = 2; 105 } else if (args[i].equals("-l")) { 106 i ++; 107 String spacer = ""; 108 location = ""; 109 boolean flag = true; 110 while (flag) { 111 location += spacer + args[i]; 112 spacer = " "; 113 i ++; 114 if (i == args.length) { 115 flag = false; 116 } else if (args[i].charAt(0) == '-') { 117 flag = false; 118 } 119 } 120 i --; 121 File file = new File (location); 122 FileInputStream fis = new FileInputStream (file); 123 BufferedInputStream bis = new BufferedInputStream (fis); 124 int count = 0; 125 byte[] bytes = new byte[1024]; 126 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 127 while ((count = bis.read(bytes)) != -1) { 128 baos.write(bytes,0,count); 129 } 130 fileBytes = baos.toByteArray(); 131 } else if (args[i].equals("-n")) { 132 i ++; 133 name = args[i]; 134 } else if (args[i].equals("-p")) { 135 i ++; 136 String spacer = ""; 137 path = ""; 138 boolean flag = true; 139 while (flag) { 140 path += spacer + args[i]; 141 spacer = " "; 142 i ++; 143 if (i == args.length) { 144 flag = false; 145 } else if (args[i].charAt(0) == '-') { 146 flag = false; 147 } 148 } 149 i --; 150 } else if (args[i].equals("-s")) { 151 i ++; 152 host = args[i]; 153 if (host.indexOf(":") == -1) { 154 host += ":2000"; 155 } 156 } else if (args[i].equals("-u")) { 157 i ++; 158 url = args[i]; 159 } else if (args[i].equals("-U")) { 160 i ++; 161 username = args[i]; 162 } else if (args[i].equals("-P")) { 163 i ++; 164 password = args[i]; 165 } 166 } 167 switch (choice) { 168 case 0: if (!url.equals("") && !host.equals("")) { 169 sb = createConnection(); 170 sb.daemonDeployer(fileBytes,name); 171 System.out.println("The daemon has been deployed."); 172 } else { 173 System.out.println("Statment missing elements."); 174 help(); 175 } 176 break; 177 case 1: if (!url.equals("") && !host.equals("")) { 178 sb = createConnection(); 179 sb.copyFile(fileBytes,name,path); 180 System.out.println("The file has been uploaded."); 181 } else { 182 System.out.println("Statment missing elements."); 183 help(); 184 } 185 break; 186 case 2: help(); 187 break; 188 default: System.out.println("There was a problem with the " + 189 "statement"); break; 190 } 191 } catch (Exception ex) { 192 ex.printStackTrace(); 193 } 194 } 195 196 public void help() { 197 System.out.println("Help"); 198 System.out.println("\t-d \tIs used in order to deploy a Daemon."); 199 System.out.println("\t-f \tIs used to upload a file."); 200 System.out.println("\t-l \tIs followed by the location of the daemon " + 201 "or file to be uploaded."); 202 System.out.println("\t-n \tThis is followed by the name of the daemon" + 203 " or file."); 204 System.out.println("\t-p \tThis is followed by the path to which the " + 205 "file is to be stored."); 206 System.out.println("\t-s \tThis is to be followed by the host and " + 207 "port on which the Deployment Daemon is running and is " + 208 "required."); 209 System.out.println("\t-u \tThis is to be followed by the JNDI for " + 210 "the Deployment Daemon and is required."); 211 System.out.println("\t-U \tThis must be followed by the username for " + 212 "the connection and is required."); 213 System.out.println("\t-P \tThis must be followed by the password for " + 214 "the connection and is required."); 215 } 216 217 222 public DeploymentDaemon 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 DeploymentDaemon beanInterface = 236 (DeploymentDaemon) 237 PortableRemoteObject.narrow(obj, 238 com.rift.coad.daemon.deployment.DeploymentDaemon.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 } 253 | Popular Tags |