1 17 18 package org.apache.geronimo.deployment.cli; 19 20 import org.apache.geronimo.common.DeploymentException; 21 22 import javax.enterprise.deploy.spi.status.ProgressObject ; 23 import javax.enterprise.deploy.spi.status.ProgressListener ; 24 import javax.enterprise.deploy.spi.status.ProgressEvent ; 25 import javax.enterprise.deploy.spi.TargetModuleID ; 26 import javax.enterprise.deploy.spi.Target ; 27 import javax.enterprise.deploy.spi.DeploymentManager ; 28 import java.io.PrintWriter ; 29 import java.io.OutputStreamWriter ; 30 import java.util.List ; 31 import java.util.Set ; 32 import java.util.HashSet ; 33 import java.util.Collection ; 34 import java.util.LinkedList ; 35 36 42 public abstract class AbstractCommand implements DeployCommand { 43 private String command; 44 private String group; 45 private String helpArgumentList; 46 private String helpText; 47 private PrintWriter out = new PrintWriter (new OutputStreamWriter (System.out)); 48 49 public AbstractCommand(String command, String group, String helpArgumentList, String helpText) { 50 this.command = command; 51 this.group = group; 52 this.helpArgumentList = helpArgumentList; 53 this.helpText = helpText; 54 } 55 56 public String getCommandName() { 57 return command; 58 } 59 60 public String getHelpArgumentList() { 61 return helpArgumentList; 62 } 63 64 public String getHelpText() { 65 return helpText; 66 } 67 68 public String getCommandGroup() { 69 return group; 70 } 71 72 public boolean isLocalOnly() { 73 return false; 74 } 75 76 public void setOut(PrintWriter out) { 77 this.out = out; 78 } 79 80 protected void emit(String message) { 81 out.print(DeployUtils.reformat(message,4,72)); 82 out.flush(); 83 } 84 85 94 protected void waitForProgress(PrintWriter out, ProgressObject po) { 95 po.addProgressListener(new ProgressListener () { 96 String last = null; 97 public void handleProgressEvent(ProgressEvent event) { 98 String msg = event.getDeploymentStatus().getMessage(); 99 if(last != null && !last.equals(msg)) { 100 emit(last); 101 } 102 last = msg; 103 } 104 }); 105 while(po.getDeploymentStatus().isRunning()) { 106 try { 107 Thread.sleep(100); 108 } catch (InterruptedException e) { 109 e.printStackTrace(out); 110 } 111 } 112 return; 113 } 114 115 protected static boolean isMultipleTargets(TargetModuleID [] ids) { 116 Set set = new HashSet (); 117 for(int i = 0; i < ids.length; i++) { 118 TargetModuleID id = ids[i]; 119 set.add(id.getTarget().getName()); 120 } 121 return set.size() > 1; 122 } 123 124 protected static Target [] identifyTargets(List targetNames, final DeploymentManager mgr) throws DeploymentException { 125 Target [] tlist = new Target [targetNames.size()]; 126 Target [] all = mgr.getTargets(); 127 Set found = new HashSet (); 128 for (int i = 0; i < tlist.length; i++) { 129 if(found.contains(targetNames.get(i))) { 130 throw new DeploymentException("Target list should not contain duplicates ("+targetNames.get(i)+")"); 131 } 132 for (int j = 0; j < all.length; j++) { 133 Target server = all[j]; 134 if(server.getName().equals(targetNames.get(i))) { 135 tlist[i] = server; 136 found.add(server.getName()); 137 break; 138 } 139 } 140 if(tlist[i] == null) { 141 throw new DeploymentException("No target named '"+targetNames.get(i)+"' was found"); 142 } 143 } 144 return tlist; 145 } 146 } 147 | Popular Tags |