/**
 *
 * Copyright 2003-2004 The Apache Software Foundation
 *
 *  Licensed 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.console.cli.controller;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import javax.enterprise.deploy.shared.ModuleType;
import javax.enterprise.deploy.spi.TargetModuleID;
import javax.enterprise.deploy.spi.exceptions.TargetException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.console.cli.DeploymentContext;
import org.apache.geronimo.console.cli.TextController;

/**
 * List deployed modules for the selected targets
 *
 * @version $Revision: 1.5 $ $Date: 2004/07/22 03:14:48 $
 */
public class ListDeployments extends TextController {
    private static final Log log = LogFactory.getLog(ListDeployments.class);
    ModuleType type = null;
    boolean selected = false;

    public ListDeployments(DeploymentContext context) {
        super(context);
    }

    public void execute() {
        while(true) {
            newScreen("Display Deployments");
            if(!context.connected) {
                println("ERROR: cannot display deployments in disconnected mode.");
                return;
            }
            if(selected) {
                showModules();
                if(!context.connected) {
                    return;
                }
            }
            String choice;
            while(true) {
                context.out.print("Action ([E]JB, [C]lient, [W]eb app, [R]A, [A]pp, A[L]L modules or [B]ack): ");
                context.out.flush();
                try {
                    choice = context.in.readLine().trim().toLowerCase();
                } catch(IOException e) {
                    log.error("Unable to read user input", e);
                    return;
                }
                if(choice.equals("b")) {
                    return;
                } else if(choice.equals("e")) {
                    selected = true;
                    type = ModuleType.EJB;
                    break;
                } else if(choice.equals("c")) {
                    selected = true;
                    type = ModuleType.CAR;
                    break;
                } else if(choice.equals("w")) {
                    selected = true;
                    type = ModuleType.WAR;
                    break;
                } else if(choice.equals("r")) {
                    selected = true;
                    type = ModuleType.RAR;
                    break;
                } else if(choice.equals("a")) {
                    selected = true;
                    type = ModuleType.EAR;
                    break;
                } else if(choice.equals("l")) {
                    selected = true;
                    type = null;
                    break;
                }
            }
        }
    }

    private void showModules() {
        TargetModuleID[] ids;
        try {
            if(type != null) {
                ids = context.deployer.getAvailableModules(type, context.targets);
            } else {
                List list = new ArrayList();
                TargetModuleID[] modules = context.deployer.getAvailableModules(ModuleType.CAR, context.targets);
                if ( null != modules ) {
                    list.addAll(Arrays.asList(modules));
                }
                modules = context.deployer.getAvailableModules(ModuleType.EAR, context.targets);
                if ( null != modules ) {
                    list.addAll(Arrays.asList(modules));
                }
                modules = context.deployer.getAvailableModules(ModuleType.EJB, context.targets);
                if ( null != modules ) {
                    list.addAll(Arrays.asList(modules));
                }
                modules = context.deployer.getAvailableModules(ModuleType.RAR, context.targets);
                if ( null != modules ) {
                    list.addAll(Arrays.asList(modules));
                }
                modules = context.deployer.getAvailableModules(ModuleType.WAR, context.targets);
                if ( null != modules ) {
                    list.addAll(Arrays.asList(modules));
                }
                ids = (TargetModuleID[])list.toArray(new TargetModuleID[list.size()]);
            }
            println(null == ids || ids.length == 0 ? "No matching modules found." : "Found "+ids.length+" matching module"+(ids.length == 1 ? "" : "s"));
            for(int i=0; i<ids.length; i++) {
                println("  "+ids[i].toString());
            }
            println("");
        } catch(TargetException e) {
            println("ERROR: "+e.getMessage());
        } catch(IllegalStateException e) {
            println("ERROR: No longer connected to server.");
            context.connected = false;
        }
    }
}

@@ -1231,9 +1231,9 @@
 : 1.
-5
+6
  $ $
@@ -1251,18 +1251,18 @@
 07/2
-2 03:14:48
+3 07:27:50
  $%0A 
@@ -1318,16 +1318,16 @@
 oller %7B%0A
-
     priv
@@ -1394,16 +1394,197 @@
 .class);
+%0A%0A    private static final ModuleType%5B%5D ALL = new ModuleType%5B%5D %7BModuleType.CAR,%0A        ModuleType.EAR,%0A        ModuleType.EJB,%0A        ModuleType.RAR,%0A        ModuleType.WAR%7D;%0A    
 %0A    Mod
@@ -3818,188 +3818,8 @@
 s);%0A
-            %7D else %7B%0A                List list = new ArrayList();%0A                TargetModuleID%5B%5D modules = context.deployer.getAvailableModules(ModuleType.CAR, context.targets);%0A
     
@@ -3836,33 +3836,29 @@
   if ( null 
-!= module
+== id
 s ) %7B%0A      
@@ -3875,255 +3875,118 @@
     
-list.addAll(Arrays.asList(modules));%0A                %7D%0A                modules = context.deployer.getAvailableModules(ModuleType.EAR, context.targets);%0A                if ( null != modules ) %7B%0A                    list.addAll(
+ids = new TargetModuleID%5B0%5D;%0A                %7D%0A            %7D else %7B%0A                List list = new 
 Array
-s.as
 List(
-modules)
 );%0A 
@@ -4004,238 +4004,83 @@
     
-%7D%0A                modules = context.deployer.getAvailableModules(ModuleType.EJB, context.targets);%0A                if ( null != modules ) %7B%0A                    list.addAll(Arrays.asList(modules));%0A                %7D%0A               
+for (int i = 0; i %3C ALL.length; i++) %7B%0A                    TargetModuleID%5B%5D
  mod
@@ -4123,30 +4123,22 @@
 Modules(
-ModuleType.RAR
+ALL%5Bi%5D
 , contex
@@ -4157,32 +4157,36 @@
                 
+    
 if ( null != mod
@@ -4186,32 +4186,36 @@
  != modules ) %7B%0A
+    
                 
@@ -4275,204 +4275,13 @@
     
-%7D%0A                modules = context.deployer.getAvailableModules(ModuleType.WAR, context.targets);%0A                if ( null != modules ) %7B%0A                    list.addAll(Arrays.asList(modules));
+    %7D
 %0A   
@@ -4420,23 +4420,8 @@
 tln(
-null == ids %7C%7C 
 ids.

Fix a NullPointerException when no module is identified.