Package org.apache.geronimo.deployment.cli

Source Code of org.apache.geronimo.deployment.cli.AbstractCommand

/**
*  Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You under the Apache License, Version 2.0
*  (the "License"); you may not use this file except in compliance with
*  the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.apache.geronimo.deployment.cli;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.Target;
import javax.enterprise.deploy.spi.TargetModuleID;
import javax.enterprise.deploy.spi.status.ProgressEvent;
import javax.enterprise.deploy.spi.status.ProgressListener;
import javax.enterprise.deploy.spi.status.ProgressObject;

import org.apache.geronimo.common.DeploymentException;

/**
* Base class for CLI deployer commands.  Tracks some simple properties and
* has common utility methods.
*
* @version $Rev: 535507 $ $Date: 2007-05-05 07:22:39 -0400 (Sat, 05 May 2007) $
*/
public abstract class AbstractCommand implements DeployCommand {
    private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    public AbstractCommand() {
    }

    public boolean isLocalOnly() {
        return false;
    }

    public void setOut(PrintWriter out) {
        this.out = out;
    }

    protected void emit(String message) {
        out.print(DeployUtils.reformat(message,4,72));
        out.flush();
    }

    /**
     * Busy-waits until the provided <code>ProgressObject</code>
     * indicates that it's no longer running.
     *
     * @param out a <code>PrintWriter</code> value, only used in case
     * of an <code>InterruptedException</code> to output the stack
     * trace.
     * @param po a <code>ProgressObject</code> value
     */
    protected void waitForProgress(PrintWriter out, ProgressObject po) {
        po.addProgressListener(new ProgressListener() {
            String last = null;
            public void handleProgressEvent(ProgressEvent event) {
                String msg = event.getDeploymentStatus().getMessage();
                if(last != null && !last.equals(msg)) {
                    emit(last);
                }
                last = msg;
            }
        });
        while(po.getDeploymentStatus().isRunning()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace(out);
            }
        }
        return;
    }

    protected static boolean isMultipleTargets(TargetModuleID[] ids) {
        Set set = new HashSet();
        for(int i = 0; i < ids.length; i++) {
            TargetModuleID id = ids[i];
            set.add(id.getTarget().getName());
        }
        return set.size() > 1;
    }

    protected static Target[] identifyTargets(List targetNames, final DeploymentManager mgr) throws DeploymentException {
        //TODO consider if nicknames that match multiple servers should be allowed.  Also if regexps should be used in matching
        Target[] tlist = new Target[targetNames.size()];
        Target[] all = mgr.getTargets();
        Set found = new HashSet();
        for (int i = 0; i < tlist.length; i++) {
            for (int j = 0; j < all.length; j++) {
                Target server = all[j];
                // check for exact target name match
                if(server.getName().equals(targetNames.get(i))
                   // check for "target-nickname" match (they match if
                   // the full target name contains the user-provided
                   // nickname)
                   || server.getName().indexOf(targetNames.get(i).toString()) > -1) {
                    tlist[i] = server;
                    if(found.contains(server.getName())) {
                        throw new DeploymentException("Target list should not contain duplicates or nicknames that match duplicates ("+targetNames.get(i)+")");
                    }
                    found.add(server.getName());
                    break;
                }
            }
            if(tlist[i] == null) {
                throw new DeploymentException("No target named or matching '"+targetNames.get(i)+"' was found");
            }
        }
        return tlist;
    }
}
TOP

Related Classes of org.apache.geronimo.deployment.cli.AbstractCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.