001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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 java.io.OutputStreamWriter;
021    import java.io.PrintWriter;
022    import java.util.HashSet;
023    import java.util.List;
024    import java.util.Set;
025    
026    import javax.enterprise.deploy.spi.DeploymentManager;
027    import javax.enterprise.deploy.spi.Target;
028    import javax.enterprise.deploy.spi.TargetModuleID;
029    import javax.enterprise.deploy.spi.status.ProgressEvent;
030    import javax.enterprise.deploy.spi.status.ProgressListener;
031    import javax.enterprise.deploy.spi.status.ProgressObject;
032    
033    import org.apache.geronimo.common.DeploymentException;
034    
035    /**
036     * Base class for CLI deployer commands.  Tracks some simple properties and
037     * has common utility methods.
038     *
039     * @version $Rev: 535507 $ $Date: 2007-05-05 07:22:39 -0400 (Sat, 05 May 2007) $
040     */
041    public abstract class AbstractCommand implements DeployCommand {
042        private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
043    
044        public AbstractCommand() {
045        }
046    
047        public boolean isLocalOnly() {
048            return false;
049        }
050    
051        public void setOut(PrintWriter out) {
052            this.out = out;
053        }
054    
055        protected void emit(String message) {
056            out.print(DeployUtils.reformat(message,4,72));
057            out.flush();
058        }
059    
060        /**
061         * Busy-waits until the provided <code>ProgressObject</code>
062         * indicates that it's no longer running.
063         *
064         * @param out a <code>PrintWriter</code> value, only used in case
065         * of an <code>InterruptedException</code> to output the stack
066         * trace.
067         * @param po a <code>ProgressObject</code> value
068         */
069        protected void waitForProgress(PrintWriter out, ProgressObject po) {
070            po.addProgressListener(new ProgressListener() {
071                String last = null;
072                public void handleProgressEvent(ProgressEvent event) {
073                    String msg = event.getDeploymentStatus().getMessage();
074                    if(last != null && !last.equals(msg)) {
075                        emit(last);
076                    }
077                    last = msg;
078                }
079            });
080            while(po.getDeploymentStatus().isRunning()) {
081                try {
082                    Thread.sleep(100);
083                } catch (InterruptedException e) {
084                    e.printStackTrace(out);
085                }
086            }
087            return;
088        }
089    
090        protected static boolean isMultipleTargets(TargetModuleID[] ids) {
091            Set set = new HashSet();
092            for(int i = 0; i < ids.length; i++) {
093                TargetModuleID id = ids[i];
094                set.add(id.getTarget().getName());
095            }
096            return set.size() > 1;
097        }
098    
099        protected static Target[] identifyTargets(List targetNames, final DeploymentManager mgr) throws DeploymentException {
100            //TODO consider if nicknames that match multiple servers should be allowed.  Also if regexps should be used in matching
101            Target[] tlist = new Target[targetNames.size()];
102            Target[] all = mgr.getTargets();
103            Set found = new HashSet();
104            for (int i = 0; i < tlist.length; i++) {
105                for (int j = 0; j < all.length; j++) {
106                    Target server = all[j];
107                    // check for exact target name match
108                    if(server.getName().equals(targetNames.get(i))
109                       // check for "target-nickname" match (they match if
110                       // the full target name contains the user-provided
111                       // nickname)
112                       || server.getName().indexOf(targetNames.get(i).toString()) > -1) {
113                        tlist[i] = server;
114                        if(found.contains(server.getName())) {
115                            throw new DeploymentException("Target list should not contain duplicates or nicknames that match duplicates ("+targetNames.get(i)+")");
116                        }
117                        found.add(server.getName());
118                        break;
119                    }
120                }
121                if(tlist[i] == null) {
122                    throw new DeploymentException("No target named or matching '"+targetNames.get(i)+"' was found");
123                }
124            }
125            return tlist;
126        }
127    }