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