geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r410468 - in /geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command: CommandDefinition.java CommandManager.java
Date Wed, 31 May 2006 08:52:06 GMT
Author: jdillon
Date: Wed May 31 01:52:05 2006
New Revision: 410468

URL: http://svn.apache.org/viewvc?rev=410468&view=rev
Log:
Added CommandDefinition to store/validate the details needed to create a command
CommandManager is now mutable

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
  (with props)
Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java?rev=410468&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
Wed May 31 01:52:05 2006
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2006 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.gshell.command;
+
+import java.util.Properties;
+
+/**
+ * Container for the very basic information about a command.
+ *
+ * @version $Id$
+ */
+public class CommandDefinition
+{
+    private final String name;
+
+    private final String classname;
+
+    public CommandDefinition(final Properties props) throws CommandException {
+        this.name = props.getProperty("name");
+        if (name == null) {
+            throw new CommandInstantiationException("Missing 'name' property for command
definition: " + props);
+        }
+
+        this.classname = props.getProperty("class");
+        if (classname == null) {
+            throw new CommandInstantiationException("Missing 'class' property for command
definition: " + props);
+        }
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String getClassName() {
+        return classname;
+    }
+
+    public String toString() {
+        return getName() + "=" + getClassName();
+    }
+
+    public Class loadClass() throws ClassNotFoundException {
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        return cl.loadClass(getClassName());
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandDefinition.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java?rev=410468&r1=410467&r2=410468&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
Wed May 31 01:52:05 2006
@@ -18,7 +18,9 @@
 
 import org.apache.xbean.finder.ResourceFinder;
 
-import java.util.Iterator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import java.util.Map;
 import java.util.Properties;
 import java.util.HashMap;
@@ -32,7 +34,9 @@
  */
 public class CommandManager
 {
-    private Map<String,Properties> commandDefMap = new HashMap<String,Properties>();
+    private static final Log log = LogFactory.getLog(CommandManager.class);
+
+    private Map<String,CommandDefinition> commandDefMap = new HashMap<String,CommandDefinition>();
 
     public CommandManager() throws CommandException {
         try {
@@ -44,21 +48,31 @@
     }
 
     private void discoverCommands() throws Exception {
+        log.info("Discovering commands");
+
         ResourceFinder finder = new ResourceFinder("META-INF/");
         Map<String, Properties> map = finder.mapAllProperties("org.apache.geronimo.gshell.command");
-        Iterator<String> iter = map.keySet().iterator();
 
         for (String filename : map.keySet()) {
             Properties props = map.get(filename);
+            CommandDefinition def = new CommandDefinition(props);
+            addCommandDefinition(def);
+        }
+    }
+
+    public void addCommandDefinition(final CommandDefinition def) {
+        if (def == null) {
+            throw new IllegalArgumentException("Def is null");
+        }
+
+        commandDefMap.put(def.getName(), def);
 
-            String name = props.getProperty("name");
-            if (name != null) {
-                commandDefMap.put(name, props);
-            }
+        if (log.isDebugEnabled()) {
+            log.debug("Added definition: " + def);
         }
     }
 
-    public Command getCommand(final String name) throws CommandNotFoundException, CommandInstantiationException
{
+    public CommandDefinition getCommandDefinition(final String name) throws CommandNotFoundException
{
         if (name == null) {
             throw new IllegalArgumentException("Name is null");
         }
@@ -66,22 +80,22 @@
             throw new IllegalArgumentException("Name is empty");
         }
 
-        Properties props = commandDefMap.get(name);
-
-        // No props means command was not discovered
-        if (props == null) {
+        CommandDefinition def = commandDefMap.get(name);
+        if (def == null) {
             throw new CommandNotFoundException(name);
         }
 
-        String classname = props.getProperty("class");
-        if (classname == null) {
-            throw new CommandInstantiationException("Missing 'class' property for command:
" + name);
-        }
+        return def;
+    }
+
+    public Command getCommand(final String name) throws CommandNotFoundException, CommandInstantiationException
{
+        // name checked by getCommandDefinition()
+
+        CommandDefinition def = getCommandDefinition(name);
 
         Command cmd;
         try {
-            ClassLoader cl = Thread.currentThread().getContextClassLoader();
-            Class type = cl.loadClass(classname);
+            Class type = def.loadClass();
             cmd = (Command)type.newInstance();
         }
         catch (Exception e) {



Mime
View raw message