geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r593391 - in /geronimo/gshell/trunk/gshell-core/src: main/java/org/apache/geronimo/gshell/layout/ main/java/org/apache/geronimo/gshell/layout/model/ test/java/org/apache/geronimo/gshell/layout/ test/java/org/apache/geronimo/gshell/layout/mo...
Date Fri, 09 Nov 2007 02:11:28 GMT
Author: jdillon
Date: Thu Nov  8 18:11:26 2007
New Revision: 593391

URL: http://svn.apache.org/viewvc?rev=593391&view=rev
Log:
(GSHELL-42) Adding basic layout support, update commands to use real ids and put name/alias
in the layout

Added:
    geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
  (with props)
    geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/GroupNodeTest.java
      - copied, changed from r582198, geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java
    geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java
  (contents, props changed)
      - copied, changed from r582198, geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java
Removed:
    geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java
Modified:
    geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
    geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/GroupNode.java
    geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Layout.java
    geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Node.java

Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java?rev=593391&r1=593390&r2=593391&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/DefaultLayoutManager.java
Thu Nov  8 18:11:26 2007
@@ -19,9 +19,15 @@
 
 package org.apache.geronimo.gshell.layout;
 
+import java.io.IOException;
+
 import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.layout.loader.LayoutLoader;
+import org.apache.geronimo.gshell.layout.model.AliasNode;
+import org.apache.geronimo.gshell.layout.model.CommandNode;
+import org.apache.geronimo.gshell.layout.model.GroupNode;
 import org.apache.geronimo.gshell.layout.model.Layout;
+import org.apache.geronimo.gshell.layout.model.Node;
 import org.apache.geronimo.gshell.registry.CommandRegistry;
 import org.apache.geronimo.gshell.registry.NotRegisteredException;
 import org.apache.geronimo.gshell.shell.Environment;
@@ -33,11 +39,11 @@
 import org.slf4j.LoggerFactory;
 
 /**
- * The default implementation of a {@link LayoutManager}.
+ * The default implementation of the {@link LayoutManager}.
  *
  * @version $Rev$ $Date$
  */
-@Component(role=LayoutManager.class, hint="default")
+@Component(role=LayoutManager.class)
 public class DefaultLayoutManager
     implements LayoutManager, Initializable
 {
@@ -62,45 +68,100 @@
         this.env = env;
     }
 
+    public DefaultLayoutManager(final CommandRegistry commandRegistry, final Layout layout,
final Environment env) {
+        this.commandRegistry = commandRegistry;
+        this.layout = layout;
+        this.env = env;
+    }
+
     public void initialize() throws InitializationException {
         assert loader != null;
 
-        //
-        // FIXME: Turn this off for now...
-        //
-
-        /*
         try {
             layout = loader.load();
         }
         catch (IOException e) {
             throw new InitializationException(e.getMessage(), e);
         }
-        */
     }
 
     public Layout getLayout() {
-        if (layout == null) {
-            throw new IllegalStateException("Layout has not been initalized");
-        }
-        
         return layout;
     }
 
     public Command find(final String path) throws NotFoundException {
         assert path != null;
 
-        log.debug("Searching for command for path: {}", path);
+        log.debug("Searching for command: {}", path);
 
-        //
-        // HACK: For now, assume the path is just the id... should eventually change this
-        //
+        Node start;
+
+        if (path.startsWith("/")) {
+            start = layout;
+        }
+        else {
+            //
+            // FIXME: Use a FQN for this and expose as static final
+            //
+
+            start = (Node) env.getVariables().get("CURRENT_NODE");
+
+            if (start == null) {
+                start = layout;
+            }
+        }
+
+        String id = findCommandId(start, path);
 
         try {
-            return commandRegistry.lookup(path);
+            return commandRegistry.lookup(id);
         }
         catch (NotRegisteredException e) {
             throw new NotFoundException(e.getMessage());
         }
+    }
+
+    private String findCommandId(final Node start, final String path) throws NotFoundException
{
+        assert start != null;
+        assert path != null;
+
+        Node node = findNode(start, path);
+
+        if (node instanceof CommandNode) {
+            return ((CommandNode)node).getId();
+        }
+        else if (node instanceof AliasNode) {
+            String cmd = ((AliasNode)node).getCommand();
+
+            return findCommandId(layout, cmd);
+        }
+
+        throw new NotFoundException(path);
+    }
+
+    private Node findNode(final Node start, final String path) throws NotFoundException {
+        assert start != null;
+        assert path != null;
+
+        Node current = start;
+
+        String[] elements = path.split("/");
+        
+        for (String element : elements) {
+            if (current instanceof GroupNode) {
+                Node node = ((GroupNode)current).find(element);
+
+                if (node == null) {
+                    throw new NotFoundException(path);
+                }
+
+                current = node;
+            }
+            else {
+                throw new NotFoundException(path);
+            }
+        }
+
+        return current;
     }
 }

Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/GroupNode.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/GroupNode.java?rev=593391&r1=593390&r2=593391&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/GroupNode.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/GroupNode.java
Thu Nov  8 18:11:26 2007
@@ -19,8 +19,9 @@
 
 package org.apache.geronimo.gshell.layout.model;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import com.thoughtworks.xstream.annotations.XStreamAlias;
 
@@ -33,13 +34,54 @@
 public class GroupNode
     extends Node
 {
-    protected List<Node> nodes = new ArrayList<Node>();
+    protected Set<Node> nodes = new HashSet<Node>();
 
     public GroupNode(final String name) {
         super(name);
     }
 
-    public List<Node> nodes() {
-        return nodes;
+    public void add(final Node child) {
+        assert child != null;
+
+        child.setParent(this);
+
+        nodes.add(child);
+    }
+
+    public Node find(final String name) {
+        assert name != null;
+        
+        for (Node child : nodes) {
+            if (name.equals(child.getName())) {
+                return child;
+            }
+        }
+
+        return null;
+    }
+
+    public Set<Node> nodes() {
+        return Collections.unmodifiableSet(nodes);
+    }
+
+    public int size() {
+        return nodes.size();
+    }
+
+    public boolean isEmpty() {
+        return nodes == null || nodes.isEmpty();
+    }
+    
+    /**
+     * Link children to their parent when deserializing.
+     */
+    private Object readResolve() {
+        if (!isEmpty()) {
+            for (Node child : nodes) {
+                child.setParent(this);
+            }
+        }
+
+        return this;
     }
 }

Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Layout.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Layout.java?rev=593391&r1=593390&r2=593391&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Layout.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Layout.java
Thu Nov  8 18:11:26 2007
@@ -20,15 +20,10 @@
 package org.apache.geronimo.gshell.layout.model;
 
 import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
 
 import com.thoughtworks.xstream.XStream;
 import com.thoughtworks.xstream.annotations.Annotations;
 import com.thoughtworks.xstream.annotations.XStreamAlias;
-import com.thoughtworks.xstream.io.xml.DomDriver;
-import org.apache.geronimo.gshell.common.tostring.ReflectionToStringBuilder;
-import org.apache.geronimo.gshell.common.tostring.ToStringStyle;
 
 /**
  * The root container for a layout tree.
@@ -37,36 +32,19 @@
  */
 @XStreamAlias("layout")
 public class Layout
+    extends GroupNode
 {
-    protected String name;
-
-    protected List<Node> nodes = new ArrayList<Node>();
-
-    public Layout(final String name) {
-        assert name != null;
-
-        this.name = name;
-    }
-
-    public List<Node> nodes() {
-        return nodes;
+    public Layout() {
+        super(ROOT);
     }
 
-    public String getName() {
-        return name;
-    }
-
-    public String toString() {
-        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
-    }
-    
     //
     // XML Conversion
     //
     
     private static XStream createXStream() {
-        XStream xs = new XStream(new DomDriver());
-        
+        XStream xs = new XStream();
+
         Annotations.configureAliases(xs, Layout.class, GroupNode.class, CommandNode.class,
AliasNode.class);
 
         return xs;

Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Node.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Node.java?rev=593391&r1=593390&r2=593391&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Node.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/layout/model/Node.java
Thu Nov  8 18:11:26 2007
@@ -19,18 +19,24 @@
 
 package org.apache.geronimo.gshell.layout.model;
 
+import com.thoughtworks.xstream.annotations.XStreamOmitField;
 import org.apache.geronimo.gshell.common.tostring.ReflectionToStringBuilder;
 import org.apache.geronimo.gshell.common.tostring.ToStringStyle;
 
 /**
- * The rudimentary elemnet of a layout.
+ * The rudimentary element of a layout.
  *
  * @version $Rev$ $Date$
  */
 public abstract class Node
 {
+    public static final String ROOT = "/";
+    
     protected String name;
 
+    @XStreamOmitField
+    protected Node parent;
+
     protected Node(final String name) {
         assert name != null;
         
@@ -39,6 +45,14 @@
 
     public String getName() {
         return name;
+    }
+
+    public Node getParent() {
+        return parent;
+    }
+
+    public void setParent(final Node parent) {
+        this.parent = parent;
     }
 
     public String toString() {

Added: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java?rev=593391&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
(added)
+++ geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
Thu Nov  8 18:11:26 2007
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.layout;
+
+import junit.framework.TestCase;
+import org.apache.geronimo.gshell.DefaultEnvironment;
+import org.apache.geronimo.gshell.command.Command;
+import org.apache.geronimo.gshell.command.CommandContext;
+import org.apache.geronimo.gshell.command.IO;
+import org.apache.geronimo.gshell.layout.loader.XMLLayoutLoader;
+import org.apache.geronimo.gshell.layout.model.Layout;
+import org.apache.geronimo.gshell.layout.model.CommandNode;
+import org.apache.geronimo.gshell.layout.model.AliasNode;
+import org.apache.geronimo.gshell.layout.model.GroupNode;
+import org.apache.geronimo.gshell.registry.CommandRegistry;
+import org.apache.geronimo.gshell.registry.DefaultCommandRegistry;
+import org.apache.geronimo.gshell.shell.Environment;
+
+/**
+ * Tests for the {@link DefaultLayoutManager} class.
+ *
+ * @version $Rev$ $Date$
+ */
+public class DefaultLayoutManagerTest
+    extends TestCase
+{
+    private DefaultLayoutManager layoutManager;
+
+    private CommandRegistry registry;
+
+    private Layout layout;
+
+    private Environment env;
+
+    protected void setUp() throws Exception {
+        registry = new DefaultCommandRegistry();
+        env = new DefaultEnvironment(new IO());
+
+        layout = new Layout();
+        layout.add(new CommandNode("help", "help"));
+        GroupNode g = new GroupNode("test");
+        g.add(new CommandNode("foo", "foo"));
+        layout.add(g);
+
+        layoutManager = new DefaultLayoutManager(registry, layout, env);
+        // layoutManager.initialize();
+    }
+
+    public void testFind() throws Exception {
+        registry.register(new Command() {
+            public String getId() {
+                return "help";
+            }
+
+            public String getDescription() {
+                return null;
+            }
+
+            public Object execute(CommandContext context, Object... args) throws Exception
{
+                return null;
+            }
+        });
+
+        layoutManager.find("help");
+    }
+
+    public void testFindInGroup() throws Exception {
+        registry.register(new Command() {
+            public String getId() {
+                return "foo";
+            }
+
+            public String getDescription() {
+                return null;
+            }
+
+            public Object execute(CommandContext context, Object... args) throws Exception
{
+                return null;
+            }
+        });
+
+        layoutManager.find("test/foo");
+    }
+
+    public void testFindNotFound() throws Exception {
+        try {
+            layoutManager.find("no-such-command");
+            fail();
+        }
+        catch(NotFoundException expected) {}
+    }
+}
\ No newline at end of file

Propchange: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/DefaultLayoutManagerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/GroupNodeTest.java
(from r582198, geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/GroupNodeTest.java?p2=geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/GroupNodeTest.java&p1=geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java&r1=582198&r2=593391&rev=593391&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/GroupNodeTest.java
Thu Nov  8 18:11:26 2007
@@ -19,40 +19,47 @@
 
 package org.apache.geronimo.gshell.layout.model;
 
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.annotations.Annotations;
 import junit.framework.TestCase;
 
 /**
- * Basic validation of the layout model muck.
+ * Test for the {@link GroupNode} class.
  *
  * @version $Rev$ $Date$
  */
-public class BasicModelValidationTest
+public class GroupNodeTest
     extends TestCase
 {
-    public void testReadLayout1() throws Exception {
-        Layout layout = Layout.fromXML(getClass().getResourceAsStream("layout1.xml"));
-        assertNotNull(layout);
-    }
+    public void testAddLinks() throws Exception {
+        GroupNode group = new GroupNode("test");
+
+        Node n1 = new CommandNode("a", "b");
+
+        group.add(n1);
 
-    public void testReadLayout2() throws Exception {
-        Layout layout = Layout.fromXML(getClass().getResourceAsStream("layout2.xml"));
-        assertNotNull(layout);
+        assertEquals(group, n1.getParent());
     }
-    
-    public void testDumpLayout1() throws Exception {
-        Layout layout = new Layout("/");
-        
-        layout.nodes().add(new CommandNode("foo", "bar"));
-        layout.nodes().add(new AliasNode("f", "foo"));
 
-        GroupNode g = new GroupNode("test");
-        g.nodes().add(new CommandNode("a", "b"));
-        g.nodes().add(new CommandNode("c", "d"));
+    public void testDeserializeLinks() throws Exception {
+        GroupNode g1 = new GroupNode("test");
+
+        Node n1 = new CommandNode("a", "b");
 
-        layout.nodes().add(g);
+        g1.add(n1);
+
+        XStream xs = new XStream();
+
+        Annotations.configureAliases(xs, GroupNode.class);
         
-        String xml = Layout.toXML(layout);
+        String xml = xs.toXML(g1);
 
         System.err.println("XML: " + xml);
+
+        GroupNode g2 = (GroupNode) xs.fromXML(xml);
+
+        Node n2 = g2.nodes().iterator().next();
+        
+        assertEquals(g2, n2.getParent());
     }
-}
+}
\ No newline at end of file

Copied: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java
(from r582198, geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java?p2=geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java&p1=geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java&r1=582198&r2=593391&rev=593391&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/BasicModelValidationTest.java
(original)
+++ geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java
Thu Nov  8 18:11:26 2007
@@ -22,11 +22,11 @@
 import junit.framework.TestCase;
 
 /**
- * Basic validation of the layout model muck.
+ * Tests for the {@link Layout} class.
  *
  * @version $Rev$ $Date$
  */
-public class BasicModelValidationTest
+public class LayoutTest
     extends TestCase
 {
     public void testReadLayout1() throws Exception {
@@ -40,16 +40,16 @@
     }
     
     public void testDumpLayout1() throws Exception {
-        Layout layout = new Layout("/");
+        Layout layout = new Layout();
         
-        layout.nodes().add(new CommandNode("foo", "bar"));
-        layout.nodes().add(new AliasNode("f", "foo"));
+        layout.add(new CommandNode("foo", "bar"));
+        layout.add(new AliasNode("f", "foo"));
 
         GroupNode g = new GroupNode("test");
-        g.nodes().add(new CommandNode("a", "b"));
-        g.nodes().add(new CommandNode("c", "d"));
+        g.add(new CommandNode("a", "b"));
+        g.add(new CommandNode("c", "d"));
 
-        layout.nodes().add(g);
+        layout.add(g);
         
         String xml = Layout.toXML(layout);
 

Propchange: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/layout/model/LayoutTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message