geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r411473 - in /geronimo/sandbox/gshell/trunk: gshell-api/src/main/java/org/apache/geronimo/gshell/command/ gshell-core/src/main/java/org/apache/geronimo/gshell/ gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/ gshell-core/src/m...
Date Sat, 03 Jun 2006 21:45:25 GMT
Author: jdillon
Date: Sat Jun  3 14:45:25 2006
New Revision: 411473

URL: http://svn.apache.org/viewvc?rev=411473&view=rev
Log:
Introduced simple Spring-like MessageSource for commands to pull messages from

Added:
    geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/MessageSource.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/MessageSourceImpl.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MessageSourceImplTest.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties
  (with props)
Modified:
    geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
    geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/SetCommand.java

Modified: geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java?rev=411473&r1=411472&r2=411473&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandContext.java
Sat Jun  3 14:45:25 2006
@@ -28,4 +28,6 @@
     IO getIO();
     
     Variables getVariables();
+
+    MessageSource getMessageSource();
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java?rev=411473&r1=411472&r2=411473&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
Sat Jun  3 14:45:25 2006
@@ -160,6 +160,10 @@
         return getCommandContext().getIO();
     }
 
+    protected MessageSource getMessageSource() {
+        return getCommandContext().getMessageSource();
+    }
+
     //
     // Execute Helpers
     //

Added: geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/MessageSource.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/MessageSource.java?rev=411473&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/MessageSource.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/command/MessageSource.java
Sat Jun  3 14:45:25 2006
@@ -0,0 +1,29 @@
+/*
+ * 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;
+
+/**
+ * ???
+ *
+ * @version $Id$
+ */
+public interface MessageSource
+{
+    String getMessage(String code);
+
+    String getMessage(String code, Object... args);
+}
\ No newline at end of file

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

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

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

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java?rev=411473&r1=411472&r2=411473&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
Sat Jun  3 14:45:25 2006
@@ -29,6 +29,8 @@
 import org.apache.geronimo.gshell.command.CommandException;
 import org.apache.geronimo.gshell.command.CommandDefinition;
 import org.apache.geronimo.gshell.command.CommandManagerImpl;
+import org.apache.geronimo.gshell.command.MessageSource;
+import org.apache.geronimo.gshell.command.MessageSourceImpl;
 import org.apache.geronimo.gshell.commandline.CommandLineBuilder;
 import org.apache.geronimo.gshell.commandline.CommandLine;
 import org.apache.geronimo.gshell.util.Arguments;
@@ -130,14 +132,15 @@
         // Setup the command container
         ShellContainer container = new ShellContainer(shellContainer);
 
-        CommandDefinition def = commandManager.getCommandDefinition(commandName);
+        final CommandDefinition def = commandManager.getCommandDefinition(commandName);
+        final Class type = def.loadClass();
 
         //
         // TODO: Pass the command instance the name it was registered with?, could be an
alias
         //
 
         container.registerComponentInstance(def);
-        container.registerComponentImplementation(def.loadClass());
+        container.registerComponentImplementation(type);
 
         // container.start() ?
 
@@ -156,6 +159,17 @@
 
             public Variables getVariables() {
                 return vars;
+            }
+
+            MessageSource messageSource;
+
+            public MessageSource getMessageSource() {
+                // Lazy init the messages, commands many not need them
+                if (messageSource == null) {
+                    messageSource = new MessageSourceImpl(type.getName() + "Messages");
+                }
+
+                return messageSource;
             }
         });
 

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/SetCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/SetCommand.java?rev=411473&r1=411472&r2=411473&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/SetCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/builtins/SetCommand.java
Sat Jun  3 14:45:25 2006
@@ -22,13 +22,14 @@
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.PosixParser;
+
 import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.command.Variables;
+import org.apache.geronimo.gshell.command.MessageSource;
 import org.apache.geronimo.gshell.console.IO;
 
 import java.util.Iterator;
-import java.util.ResourceBundle;
 
 /**
  * Set a variable or property.
@@ -51,12 +52,7 @@
     protected int doExecute(String[] args) throws Exception {
         assert args != null;
 
-        //
-        // TODO: Sub-class, create a Spring-like MessageSource, using Varags + printf
-        //       Add to commmand context interface
-        //
-
-        ResourceBundle resources = ResourceBundle.getBundle(getClass().getName() + "Messages");
+        MessageSource messages = getMessageSource();
 
         //
         // TODO: Optimize, move common code to CommandSupport
@@ -67,11 +63,11 @@
         Options options = new Options();
 
         options.addOption(OptionBuilder.withLongOpt("help")
-            .withDescription(resources.getString("cli.option.help"))
+            .withDescription(messages.getMessage("cli.option.help"))
             .create('h'));
 
         options.addOption(OptionBuilder.withLongOpt("property")
-            .withDescription(resources.getString("cli.option.property"))
+            .withDescription(messages.getMessage("cli.option.property"))
             .create('p'));
 
         //
@@ -88,7 +84,7 @@
         if (line.hasOption('h')) {
             io.out.print(getName());
             io.out.print(" -- ");
-            io.out.println(resources.getString("cli.usage.description"));
+            io.out.println(messages.getMessage("cli.usage.description"));
             io.out.println();
 
             HelpFormatter formatter = new HelpFormatter();
@@ -104,7 +100,7 @@
                 false); // auto usage
 
             io.out.println();
-            io.out.println(resources.getString("cli.usage.footer")); //
+            io.out.println(messages.getMessage("cli.usage.footer"));
             io.out.println();
 
             return Command.SUCCESS;

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/MessageSourceImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/MessageSourceImpl.java?rev=411473&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/MessageSourceImpl.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/MessageSourceImpl.java
Sat Jun  3 14:45:25 2006
@@ -0,0 +1,52 @@
+/*
+ * 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.ResourceBundle;
+import java.util.Formatter;
+
+/**
+ * ???
+ *
+ * @version $Id$
+ */
+public class MessageSourceImpl
+    implements MessageSource
+{
+    private final ResourceBundle bundle;
+
+    public MessageSourceImpl(final String name) {
+        assert name != null;
+
+        bundle = ResourceBundle.getBundle(name);
+    }
+
+    public String getMessage(final String code) {
+        return bundle.getString(code);
+    }
+
+    public String getMessage(final String code, final Object... args) {
+        String format = getMessage(code);
+
+        StringBuilder sb = new StringBuilder();
+        Formatter f = new Formatter(sb);
+        
+        f.format(format, args);
+
+        return sb.toString();
+    }
+}
\ No newline at end of file

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

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

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

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MessageSourceImplTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MessageSourceImplTest.java?rev=411473&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MessageSourceImplTest.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/MessageSourceImplTest.java
Sat Jun  3 14:45:25 2006
@@ -0,0 +1,44 @@
+/*
+ * 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 junit.framework.TestCase;
+
+/**
+ * Unit tests for the {@link MessageSourceImpl} class.
+ *
+ * @version $Id$
+ */
+public class MessageSourceImplTest
+    extends TestCase
+{
+    public void testLoadAndGetMessage() throws Exception {
+        MessageSourceImpl messages = new MessageSourceImpl(getClass().getName());
+
+        String a = messages.getMessage("a");
+        assertEquals("1", a);
+
+        String b = messages.getMessage("b");
+        assertEquals("2", b);
+
+        String c = messages.getMessage("c");
+        assertEquals("3", c);
+
+        String f = messages.getMessage("f", a, b, c);
+        assertEquals("1 2 3", f);
+    }
+}

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

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

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

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties?rev=411473&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties
Sat Jun  3 14:45:25 2006
@@ -0,0 +1,11 @@
+##
+## $Id$
+##
+
+a=1
+
+b=2
+
+c=3
+
+f=%s %s %s
\ No newline at end of file

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/resources/org/apache/geronimo/gshell/command/MessageSourceImplTest.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message