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