Coverage Report - org.apache.xbean.command.CommandRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
CommandRegistry
0%
0/31
0%
0/4
1.5
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.xbean.command;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.Map;
 22  
 
 23  0
 public class CommandRegistry {
 24  
     
 25  0
     protected static final HashMap commands = new HashMap();
 26  
 
 27  
     static {
 28  0
         loadCommandList();
 29  
     }
 30  
 
 31  0
     protected static final CommandRegistry unknownCommand = new CommandRegistry();
 32  
 
 33  
     public static Map getCommandMap() {
 34  0
         HashMap rc = new HashMap();
 35  0
         for (Iterator iter = commands.keySet().iterator(); iter.hasNext();) {
 36  0
             String name = (String) iter.next();
 37  0
             Command command = getCommand(name);
 38  0
             rc.put(name, command);
 39  0
         }
 40  0
         return rc;
 41  
     }
 42  
 
 43  
     public static void register(String name, Command cmd) {
 44  0
         commands.put(name, cmd);
 45  0
     }
 46  
 
 47  
     public static void register(String name, Class cmd) {
 48  0
         commands.put(name, cmd);
 49  0
     }
 50  
 
 51  
     public static Command getCommand(String name) {
 52  0
         Object cmd = commands.get(name);
 53  0
         if (cmd instanceof Class) {
 54  0
             cmd = loadCommand((Class) cmd);
 55  0
             register(name, (Command) cmd);
 56  
         }
 57  0
         return (Command) cmd;
 58  
     }
 59  
 
 60  
 
 61  
     // - Protected methods - //
 62  
     protected static Command loadCommand(Class commandClass) {
 63  0
         Command cmd = null;
 64  
         try {
 65  0
             cmd = (Command) commandClass.newInstance();
 66  0
         } catch (Exception e) {
 67  
             //throw new IOException("Cannot instantiate command class "+commandClass+"\n"+e.getClass().getName()+":\n"+e.getMessage());
 68  0
         }
 69  0
         return cmd;
 70  
     }
 71  
 
 72  
     /**
 73  
      * TODO: Replace this part with the classpath magic
 74  
      */
 75  
     protected static void loadCommandList() {
 76  0
         Exit.register();
 77  0
         Help.register();
 78  0
         Lookup.register();
 79  0
         Version.register();
 80  0
         GroovySh.register();
 81  0
     }
 82  
 }