001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.deployment.cli; 019 020 import org.apache.geronimo.common.DeploymentException; 021 022 import javax.enterprise.deploy.spi.status.ProgressObject; 023 import javax.enterprise.deploy.spi.status.ProgressListener; 024 import javax.enterprise.deploy.spi.status.ProgressEvent; 025 import javax.enterprise.deploy.spi.TargetModuleID; 026 import javax.enterprise.deploy.spi.Target; 027 import javax.enterprise.deploy.spi.DeploymentManager; 028 import java.io.PrintWriter; 029 import java.io.OutputStreamWriter; 030 import java.util.List; 031 import java.util.Set; 032 import java.util.HashSet; 033 import java.util.Collection; 034 import java.util.LinkedList; 035 036 /** 037 * Base class for CLI deployer commands. Tracks some simple properties and 038 * has common utility methods. 039 * 040 * @version $Rev: 405624 $ $Date: 2006-05-09 21:08:49 -0700 (Tue, 09 May 2006) $ 041 */ 042 public abstract class AbstractCommand implements DeployCommand { 043 private String command; 044 private String group; 045 private String helpArgumentList; 046 private String helpText; 047 private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 048 049 public AbstractCommand(String command, String group, String helpArgumentList, String helpText) { 050 this.command = command; 051 this.group = group; 052 this.helpArgumentList = helpArgumentList; 053 this.helpText = helpText; 054 } 055 056 public String getCommandName() { 057 return command; 058 } 059 060 public String getHelpArgumentList() { 061 return helpArgumentList; 062 } 063 064 public String getHelpText() { 065 return helpText; 066 } 067 068 public String getCommandGroup() { 069 return group; 070 } 071 072 public boolean isLocalOnly() { 073 return false; 074 } 075 076 public void setOut(PrintWriter out) { 077 this.out = out; 078 } 079 080 protected void emit(String message) { 081 out.print(DeployUtils.reformat(message,4,72)); 082 out.flush(); 083 } 084 085 /** 086 * Busy-waits until the provided <code>ProgressObject</code> 087 * indicates that it's no longer running. 088 * 089 * @param out a <code>PrintWriter</code> value, only used in case 090 * of an <code>InterruptedException</code> to output the stack 091 * trace. 092 * @param po a <code>ProgressObject</code> value 093 */ 094 protected void waitForProgress(PrintWriter out, ProgressObject po) { 095 po.addProgressListener(new ProgressListener() { 096 String last = null; 097 public void handleProgressEvent(ProgressEvent event) { 098 String msg = event.getDeploymentStatus().getMessage(); 099 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 }