1 21 22 package com.rift.coad.commandline.servicebrokercommandlinetool; 23 24 import com.rift.coad.daemon.servicebroker.ServiceBroker; 25 import com.rift.coad.daemon.servicebroker.ServiceBrokerException; 26 import java.lang.Exception ; 27 import java.util.ArrayList ; 28 import java.util.Hashtable ; 29 import java.util.List ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.rmi.PortableRemoteObject ; 33 import java.io.*; 34 35 42 public class Main { 43 public BufferedReader br = new BufferedReader( 44 new InputStreamReader(System.in)); 45 public String [] args = null; 46 public String url = ""; 47 public String host = ""; 48 public String username = ""; 49 public String password = ""; 50 51 52 public Main() { 53 54 } 55 56 59 public static void main(String [] args) throws ServiceBrokerException { 60 Main mainProgram = new Main(); 61 mainProgram.setArgs(args); 62 mainProgram.commandLine(); 63 } 64 65 70 public void setArgs(String [] args) { 71 this.args = args; 72 } 73 74 78 public void commandLine() { 79 try { 80 ServiceBroker sb = null; 81 int choice = -1; 82 List service = new ArrayList (); 83 String jndi = ""; 84 for (int i = 0; i < args.length; i ++) { 85 if (args[i].equals("-r")) { 86 choice = 0; 87 } else if (args[i].equals("-o")) { 88 choice = 1; 89 } else if (args[i].equals("-m")) { 90 choice = 2; 91 } else if (args[i].equals("-d")) { 92 choice = 3; 93 } else if (args[i].equals("-S")) { 94 i ++; 95 service.add(args[i]); 96 } else if (args[i].equals("-j")) { 97 i ++; 98 jndi = args[i]; 99 } else if (args[i].equals("-h")) { 100 choice = 4; 101 } else if (args[i].equals("-s")) { 102 i ++; 103 host = args[i]; 104 if (host.indexOf(":") == -1) { 105 host += ":2000"; 106 } 107 } else if (args[i].equals("-u")) { 108 i ++; 109 url = args[i]; 110 } else if (args[i].equals("-U")) { 111 i ++; 112 username = args[i]; 113 } else if (args[i].equals("-P")) { 114 i ++; 115 password = args[i]; 116 } 117 } 118 switch (choice) { 119 case 0: if (!url.equals("") && !host.equals("")) { 120 sb = createConnection(); 121 sb.registerService(jndi,service); 122 System.out.println("The service has been " + 123 "registered."); 124 } else { 125 System.out.println("Statment missing elements."); 126 help(); 127 } 128 break; 129 case 1: if (!url.equals("") && !host.equals("")) { 130 sb = createConnection(); 131 String temp = sb.getServiceProvider(service); 132 System.out.println(temp); 133 } else { 134 System.out.println("Statment missing elements."); 135 help(); 136 } 137 break; 138 case 2: if (!url.equals("") && !host.equals("")) { 139 sb = createConnection(); 140 List temp2 = sb.getServiceProviders(service); 141 for (int i = 0; i < temp2.size(); i ++) { 142 System.out.println((String ) temp2.get(i)); 143 } 144 } else { 145 System.out.println("Statment missing elements."); 146 help(); 147 } 148 break; 149 case 3: if (!url.equals("") && !host.equals("")) { 150 sb = createConnection(); 151 sb.removeServiceProviders(jndi,service); 152 System.out.println("The service has been deleted."); 153 } else { 154 System.out.println("Statment missing elements."); 155 help(); 156 } 157 break; 158 case 4: help(); 159 break; 160 default: System.out.println("There was a problem with the " + 161 "statement"); break; 162 } 163 } catch (Exception ex) { 164 ex.printStackTrace(); 165 } 166 } 167 168 public void help() { 169 System.out.println("Help"); 170 System.out.println("\t-r \tThis is to be user to register a service."); 171 System.out.println("\t-o \tThis is to be used to retrieve a single " + 172 "JNDI"); 173 System.out.println("\t-m \tThis is to be used to retrieve multiple " + 174 "JNDI's"); 175 System.out.println("\t-d \tThis is to be used to delete a service"); 176 System.out.println("\t-S \tThis is to be followed by the name of the " + 177 "service. This can be used multiple times in a single " + 178 "statement."); 179 System.out.println("\t-j \tThis is to be followed by the JNDI for " + 180 "the service. This can only be called once in a statment."); 181 System.out.println("\t-s \tThis is to be followed by the host and " + 182 "port on which the Service Broker Daemon is running and is " + 183 "required."); 184 System.out.println("\t-u \tThis is to be followed by the JNDI for " + 185 "the Service Broker Daemon and is required."); 186 System.out.println("\t-U \tThis must be followed by the username for " + 187 "the connection and is required."); 188 System.out.println("\t-P \tThis must be followed by the password for " + 189 "the connection and is required."); 190 } 191 192 197 public com.rift.coad.daemon.servicebroker.ServiceBroker createConnection() 198 throws Exception { 199 try { 200 Hashtable env = new Hashtable (); 201 env.put(Context.INITIAL_CONTEXT_FACTORY, 202 "com.rift.coad.client.naming." + 203 "CoadunationInitialContextFactory"); 204 env.put(Context.PROVIDER_URL,host); 205 env.put("com.rift.coad.username",username); 206 env.put("com.rift.coad.password",password); 207 Context ctx = new InitialContext (env); 208 209 Object obj = ctx.lookup(url); 210 com.rift.coad.daemon.servicebroker.ServiceBroker beanInterface = 211 (com.rift.coad.daemon.servicebroker.ServiceBroker) 212 PortableRemoteObject.narrow(obj, 213 com.rift.coad.daemon.servicebroker.ServiceBroker.class); 214 215 if (beanInterface == null) { 216 throw new Exception ("narrow failed."); 217 } else { 218 return beanInterface; 219 } 220 } catch (Exception ex) { 221 System.out.println(ex.getMessage()); 222 ex.printStackTrace(System.out); 223 throw new Exception (ex); 224 } 225 } 226 227 } 228 | Popular Tags |