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.PrintWriter;
021 import java.io.IOException;
022 import java.util.Arrays;
023 import java.util.List;
024
025 import javax.enterprise.deploy.spi.DeploymentManager;
026 import javax.enterprise.deploy.spi.Target;
027 import javax.enterprise.deploy.spi.TargetModuleID;
028 import javax.enterprise.deploy.spi.exceptions.TargetException;
029
030 import org.apache.geronimo.cli.deployer.CommandArgs;
031 import org.apache.geronimo.cli.deployer.ListModulesCommandArgs;
032 import org.apache.geronimo.common.DeploymentException;
033 import jline.ConsoleReader;
034
035 /**
036 * The CLI deployer logic to list modules.
037 *
038 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039 */
040 public class CommandListModules extends AbstractCommand {
041
042 public void execute(ConsoleReader consoleReader, ServerConnection connection, CommandArgs commandArgs) throws DeploymentException {
043 if (!(commandArgs instanceof ListModulesCommandArgs)) {
044 throw new DeploymentSyntaxException("CommandArgs has the type [" + commandArgs.getClass() + "]; expected [" + ListModulesCommandArgs.class + "]");
045 }
046 ListModulesCommandArgs listModulesCommandArgs = (ListModulesCommandArgs) commandArgs;
047
048 Boolean started = null;
049 if (listModulesCommandArgs.isStarted()) {
050 started = Boolean.TRUE;
051 } else if (listModulesCommandArgs.isStopped()) {
052 started = Boolean.FALSE;
053 }
054
055 List targets = Arrays.asList(listModulesCommandArgs.getArgs());
056
057 final DeploymentManager mgr = connection.getDeploymentManager();
058 TargetModuleID[] running = null, notrunning = null;
059 Target[] tlist = identifyTargets(targets, mgr);
060 if(tlist.length == 0) {
061 tlist = mgr.getTargets();
062 }
063 try {
064 if(started == null || started.booleanValue()) {
065 running = mgr.getRunningModules(null, tlist);
066 }
067 if(started == null || !started.booleanValue()) {
068 notrunning = mgr.getNonRunningModules(null, tlist);
069 }
070 } catch (TargetException e) {
071 throw new DeploymentException("Unable to query modules", e);
072 } catch (IllegalStateException e) {
073 throw new DeploymentSyntaxException(e.getMessage(), e);
074 }
075 if(running == null) {
076 running = new TargetModuleID[0];
077 }
078 if(notrunning == null) {
079 notrunning = new TargetModuleID[0];
080 }
081
082 // print the module count, and if there are more than one
083 // targets print that count, too
084 int total = running.length+notrunning.length;
085 try {
086 consoleReader.printString("Found "+total+" module"+(total != 1 ? "s" : ""));
087 if ((tlist != null) && (tlist.length > 1)) {
088 consoleReader.printString(" deployed to " + tlist.length + " target" + (tlist.length != 1 ? "s" : ""));
089 }
090 consoleReader.printNewline();
091
092 // for each target, print the modules that were deployed to it
093 for (int i = 0; (tlist != null) && (i < tlist.length); i++) {
094 Target target = tlist[i];
095 if (tlist.length > 1) {
096 consoleReader.printNewline();
097 consoleReader.printString(" Target " + target);
098 consoleReader.printNewline();
099 }
100 printTargetModules(consoleReader, target, running, " + ");
101 printTargetModules(consoleReader, target, notrunning, " ");
102 }
103 } catch (IOException e) {
104 throw new DeploymentException("Could not print to console", e);
105 }
106 }
107
108
109 /**
110 * Prints the names of the modules (that belong to the target) on
111 * the provided PrintWriter.
112 *
113 * @param out a <code>PrintWriter</code>
114 * @param target a <code>Target</code> value; only the modules
115 * whose target equals this one will be listed. Must not be null.
116 * @param modules a <code>TargetModuleID[]</code> value, must not
117 * be null.
118 * @param prefix a <code>String</code> value that will be
119 * prepended to each module
120 */
121 void printTargetModules(ConsoleReader out, Target target, TargetModuleID[] modules, String prefix) throws IOException {
122 for (int i = 0; i < modules.length; i++) {
123 TargetModuleID result = modules[i];
124 if (result.getTarget().equals(target)) {
125 out.printString(prefix+result.getModuleID());
126 out.printNewline();
127 if(result.getChildTargetModuleID() != null) {
128 for (int j = 0; j < result.getChildTargetModuleID().length; j++) {
129 TargetModuleID child = result.getChildTargetModuleID()[j];
130 out.printString(" `-> "+child.getModuleID());
131 out.printNewline();
132 }
133 }
134 }
135 }
136 }
137 }