1 22 package org.objectweb.petals.system.launch; 23 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStreamReader ; 28 29 import javax.naming.NamingException ; 30 31 import org.apache.commons.io.FileUtils; 32 33 import org.objectweb.petals.kernel.admin.service.PetalsAdminServiceMBean; 34 import org.objectweb.petals.util.JNDIUtil; 35 import org.objectweb.petals.util.SystemUtil; 36 37 42 public class CommandReader { 43 44 protected static int STOP_CODE = 0; 45 46 protected static int ERROR_CODE = -1; 47 48 protected static int OK_CODE = 1; 49 50 protected File currentPath; 51 52 protected File installPath; 53 54 protected File installedPath; 55 56 protected File uninstalledPath; 57 58 protected PetalsAdminServiceMBean petalsService; 59 60 public CommandReader(PetalsAdminServiceMBean petalsService) { 61 currentPath = new File ("."); 62 File petalsPath = SystemUtil.getPetalsInstallDirectory(); 63 installPath = new File (petalsPath, "install"); 64 installedPath = new File (petalsPath, "installed"); 65 uninstalledPath = new File (petalsPath, "uninstalled"); 66 this.petalsService = petalsService; 67 } 68 69 73 public void read() { 74 System.out.println("Petals prompt. Tape 'help' for help."); 75 BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 76 77 int code=1; 78 79 while (code>0) { 80 try { 81 System.out.println(); 82 System.out.print("> "); 83 code = processCommandLine(br.readLine()); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } 87 } 88 System.out.println("exiting Petals..."); 89 System.exit(code); 90 } 91 92 98 protected int processCommandLine(String command) { 99 if (command != null) { 100 if ("help".equalsIgnoreCase(command) 101 || "h".equalsIgnoreCase(command)) { 102 return help(); 103 } else if (command.toLowerCase().startsWith("hotdeploy")) { 104 return hotdeploy(command.substring(9, command.length()).trim()); 105 } else if (command.toLowerCase().startsWith("hd")) { 106 return hotdeploy(command.substring(2, command.length()).trim()); 107 } else if (command.toLowerCase().startsWith("hotundeploy")) { 108 return hotundeploy(command.substring(11, command.length()).trim()); 109 } else if (command.toLowerCase().startsWith("hu")) { 110 return hotundeploy(command.substring(2, command.length()).trim()); 111 } else if (command.toLowerCase().startsWith("setpath")) { 112 return setpath(command.substring(7, command.length()).trim()); 113 } else if (command.toLowerCase().startsWith("sp")) { 114 return setpath(command.substring(2, command.length()).trim()); 115 } else if ("path".equalsIgnoreCase(command) 116 || "p".equalsIgnoreCase(command)) { 117 return path(); 118 } else if ("jndi".equalsIgnoreCase(command) 119 || "d".equalsIgnoreCase(command)) { 120 return jndi(); 121 } else if ("stop".equalsIgnoreCase(command) 122 || "q".equalsIgnoreCase(command)) { 123 return stop(); 124 } else if ("exit".equalsIgnoreCase(command) 125 || "x".equalsIgnoreCase(command)) { 126 return shutdown(); 127 } else { 128 System.out.println("Unknown command"); 129 } 130 } 131 return OK_CODE; 132 } 133 134 139 protected int help() { 140 System.out.println("commands:"); 141 System.out.println("---------"); 142 System.out 143 .println(" - hotdeploy [ZIP filePath] (or hd): install and start a component, a service assembly or a share library"); 144 System.out 145 .println(" - hotundeploy [ZIP fileName] (or hu): shutdown and uninstall a component, a service assembly or a share library"); 146 System.out.println(" - path (or p) : display current file system path"); 147 System.out 148 .println(" - setpath [Path] (or sp): change current file system path"); 149 System.out.println(" - jndi (or d) : display the naming directory"); 150 System.out.println(); 151 System.out.println(" - help (or h) : this help"); 152 System.out.println(" - stop (or q) : stop Petals"); 153 System.out.println(" - shutdown (or x) : shutdown Petals (remove from the Petals network"); 154 return OK_CODE; 155 } 156 157 162 protected int path() { 163 System.out.println("current path is :"); 164 System.out.println(currentPath.getAbsolutePath()); 165 return OK_CODE; 166 } 167 168 173 protected int setpath(String newPath) { 174 currentPath = new File (newPath); 175 System.out.println("new current path is :"); 176 System.out.println(currentPath.getAbsolutePath()); 177 return OK_CODE; 178 } 179 180 185 protected int hotdeploy(String zip) { 186 File source = new File (zip); 187 if (!source.exists()) { 188 source = new File (currentPath, zip); 189 } 190 191 if (!source.exists()) { 192 System.out.println("File not found : " + source.getAbsolutePath()); 193 } else { 194 try { 195 FileUtils.copyFileToDirectory(source, installPath); 196 Thread.sleep(1000); 197 } catch (IOException e) { 198 System.out 199 .println("File found, but can not copy and deploy file"); 200 e.printStackTrace(); 201 } catch (InterruptedException e) { 202 e.printStackTrace(); 203 } 204 } 205 return OK_CODE; 206 } 207 208 213 protected int hotundeploy(String zip) { 214 File source = new File (installedPath, zip); 215 216 if (!source.exists()) { 217 System.out.println("File to undeploy not found : " 218 + source.getAbsolutePath()); 219 } else { 220 File uninstalledFile = new File (uninstalledPath, zip); 221 source.renameTo(uninstalledFile); 222 } 223 return OK_CODE; 224 } 225 226 231 protected int jndi() { 232 try { 233 System.out.println(JNDIUtil.browseJNDI(null)); 234 } catch (NamingException e) { 235 e.printStackTrace(); 236 } 237 return OK_CODE; 238 } 239 240 244 protected int stop(){ 245 System.out.println("quit Petals... (stop the server)"); 246 return STOP_CODE; 247 } 248 249 254 protected int shutdown(){ 255 try { 256 System.out.println("exit Petals... (shutdown the server)"); 257 petalsService.shutdownContainer(); 258 return STOP_CODE; 259 } catch (Exception e) { 260 e.printStackTrace(); 261 return ERROR_CODE; 262 } 263 } 264 265 } 266 | Popular Tags |