You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2011/03/14 11:16:04 UTC

svn commit: r1081305 - in /karaf/trunk/shell/config/src: main/java/org/apache/karaf/shell/config/ main/resources/org/apache/karaf/shell/config/ test/java/org/apache/karaf/shell/config/

Author: gnodet
Date: Mon Mar 14 10:16:03 2011
New Revision: 1081305

URL: http://svn.apache.org/viewvc?rev=1081305&view=rev
Log:
[KARAF-508] The config:edit create does not allow the creation of factory configs

Added:
    karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/MockCommandSession.java
    karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/UpdateCommandTest.java
Modified:
    karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
    karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/UpdateCommand.java
    karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/config/edit.txt
    karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/EditCommandTest.java

Modified: karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java?rev=1081305&r1=1081304&r2=1081305&view=diff
==============================================================================
--- karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java (original)
+++ karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java Mon Mar 14 10:16:03 2011
@@ -46,7 +46,7 @@ public class EditCommand extends ConfigC
 	    Dictionary props;
 
 	    //User selected to use file instead.
-	    if (useFile) {
+        if (useFile) {
 		    Configuration configuration = this.findConfigurationByFileName(admin, pid);
 		    if(configuration == null) {
 			    System.err.println("Could not find configuration with file install property set to: " + pid);
@@ -55,11 +55,12 @@ public class EditCommand extends ConfigC
 		    props = configuration.getProperties();
 		    pid = configuration.getPid();
 	    } else {
-		    props = admin.getConfiguration(pid).getProperties();
-		    if (props == null) {
-			    props = new Properties();
-		    }
-	    }
+            Configuration configuration = admin.getConfiguration(pid, null);
+            props = configuration.getProperties();
+            if (props == null) {
+                props = new Properties();
+            }
+        }
         this.session.put(PROPERTY_CONFIG_PID, pid);
         this.session.put(PROPERTY_CONFIG_PROPS, props);
     }

Modified: karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/UpdateCommand.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/UpdateCommand.java?rev=1081305&r1=1081304&r2=1081305&view=diff
==============================================================================
--- karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/UpdateCommand.java (original)
+++ karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/UpdateCommand.java Mon Mar 14 10:16:03 2011
@@ -49,11 +49,14 @@ public class UpdateCommand extends Confi
         Dictionary props = getEditedProps();
         if (props == null) {
             System.err.println("No configuration is being edited. Run the edit command first");
-        } else if (!bypassStorage && storage != null) {
-        	String pid = (String) this.session.get(PROPERTY_CONFIG_PID);
+            return;
+        }
+
+        String pid = (String) this.session.get(PROPERTY_CONFIG_PID);
+        if (!bypassStorage && storage != null) {
             File storageFile = new File(storage, pid + ".cfg");
             Configuration cfg = admin.getConfiguration(pid, null);
-            if (cfg != null) {
+            if (cfg != null && cfg.getProperties() != null) {
                 Object val = cfg.getProperties().get(FELIX_FILEINSTALL_FILENAME);
                 if (val instanceof String) {
                     if (((String) val).startsWith("file:")) {
@@ -74,11 +77,32 @@ public class UpdateCommand extends Confi
             storage.mkdirs();
             p.save();
         } else {
-            String pid = (String) this.session.get(PROPERTY_CONFIG_PID);
             Configuration cfg = admin.getConfiguration(pid, null);
+            if (cfg.getProperties() == null) {
+                String[] pids = parsePid(pid);
+                if (pids[1] != null) {
+                    cfg = admin.createFactoryConfiguration(pids[0], null);
+                }
+            }
+            if (cfg.getBundleLocation() != null) {
+                cfg.setBundleLocation(null);
+            }
             cfg.update(props);
         }
         this.session.put(PROPERTY_CONFIG_PID, null);
         this.session.put(PROPERTY_CONFIG_PROPS, null);
     }
+
+    private String[] parsePid(String pid) {
+        int n = pid.indexOf('-');
+        if (n > 0) {
+            String factoryPid = pid.substring(n + 1);
+            pid = pid.substring(0, n);
+            return new String[] { pid, factoryPid };
+        } else {
+            return new String[] { pid, null };
+        }
+    }
+
+
 }

Modified: karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/config/edit.txt
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/config/edit.txt?rev=1081305&r1=1081304&r2=1081305&view=diff
==============================================================================
--- karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/config/edit.txt (original)
+++ karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/config/edit.txt Mon Mar 14 10:16:03 2011
@@ -3,9 +3,11 @@ The edit command can be used to create o
 The command above will create a file etc/org.apache.karaf.sample.pid which corresponds to a configuration object with pid org.apache.karaf.sample.pid
 
 In case of Managed Service Factories the user can create or edit the configuration of a specific instance of a managed service.
- > config:edit org.apache.karaf.sample.service.factory.pid-instanceid
+  > config:edit org.apache.karaf.sample.service.factory.pid-instanceid
 The command above will create a file etc/org.apache.karaf.sample.service.factory.pid-instanceid but in this case the pid is generated, according to the specification.
 
 This makes further editing to the configuration difficult because the generated pid is not known to the user. To make things simple config:edit provides a special flag -f or --use-file which allows editing a configuration using the filename instead of the pid.
 So the user can simply edit again the configuration by typing:
-> config:edit org.apache.karaf.sample.service.factory.pid-instanceid.
+  > config:edit org.apache.karaf.sample.service.factory.pid-instanceid.
+
+The --factory option can be used to create a new configuration for a given factory pid.  The parameter will be used as the factory pid and the configuration pid will be generated.  Note that this option
\ No newline at end of file

Modified: karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/EditCommandTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/EditCommandTest.java?rev=1081305&r1=1081304&r2=1081305&view=diff
==============================================================================
--- karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/EditCommandTest.java (original)
+++ karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/EditCommandTest.java Mon Mar 14 10:16:03 2011
@@ -16,19 +16,10 @@
  */
 package org.apache.karaf.shell.config;
 
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-
-import java.io.InputStream;
-import java.io.PrintStream;
 import java.util.Dictionary;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.Properties;
 
 import junit.framework.TestCase;
-
 import org.apache.felix.service.command.CommandSession;
 import org.easymock.EasyMock;
 import org.osgi.framework.BundleContext;
@@ -36,6 +27,10 @@ import org.osgi.framework.ServiceReferen
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+
 /**
  * Test cases for {@link EditCommand}
  */
@@ -69,7 +64,7 @@ public class EditCommandTest extends Tes
     
     public void testExecuteOnExistingPid() throws Exception {        
         Configuration config = createMock(Configuration.class);
-        expect(admin.getConfiguration(PID)).andReturn(config);
+        expect(admin.getConfiguration(PID, null)).andReturn(config);
         replay(admin);
         
         // the ConfigAdmin service returns a Dictionary for an existing PID
@@ -90,7 +85,7 @@ public class EditCommandTest extends Tes
     @SuppressWarnings("unchecked")
     public void testExecuteOnNewPid() throws Exception {        
         Configuration config = createMock(Configuration.class);
-        expect(admin.getConfiguration(PID)).andReturn(config);
+        expect(admin.getConfiguration(PID, null)).andReturn(config);
         replay(admin);
         
         // the ConfigAdmin service does not return a Dictionary for a new PID
@@ -107,49 +102,5 @@ public class EditCommandTest extends Tes
         assertNotNull("Should have a Dictionary on the session", props);
         assertTrue("Should have an empty Dictionary on the session", props.isEmpty());
     }
-    
-    /*
-     * A mock CommandSession implementation that only cares about the properties set on the session
-     */
-    private class MockCommandSession implements CommandSession {
-        
-        private Map<String, Object> properties = new HashMap<String, Object>();
-
-        public void close() {
-            // not implemented
-        }
-
-        public Object convert(Class<?> type, Object instance) {
-            // not implemented
-            return null;
-        }
-
-        public Object execute(CharSequence commandline) throws Exception {
-            // not implemented
-            return null;
-        }
-
-        public CharSequence format(Object target, int level) {
-            // not implemented
-            return null;
-        }
-
-        public Object get(String name) {
-            return properties.get(name);
-        }
-
-        public PrintStream getConsole() {
-            // not implemented
-            return null;
-        }
-
-        public InputStream getKeyboard() {
-            // not implemented
-            return null;
-        }
-
-        public void put(String name, Object value) {
-            properties.put(name, value);
-        }
-    }
+
 }

Added: karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/MockCommandSession.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/MockCommandSession.java?rev=1081305&view=auto
==============================================================================
--- karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/MockCommandSession.java (added)
+++ karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/MockCommandSession.java Mon Mar 14 10:16:03 2011
@@ -0,0 +1,69 @@
+/*
+ * 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.karaf.shell.config;
+
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.service.command.CommandSession;
+
+/*
+ * A mock CommandSession implementation that only cares about the properties set on the session
+ */
+class MockCommandSession implements CommandSession {
+
+    private Map<String, Object> properties = new HashMap<String, Object>();
+
+    public void close() {
+        // not implemented
+    }
+
+    public Object convert(Class<?> type, Object instance) {
+        // not implemented
+        return null;
+    }
+
+    public Object execute(CharSequence commandline) throws Exception {
+        // not implemented
+        return null;
+    }
+
+    public CharSequence format(Object target, int level) {
+        // not implemented
+        return null;
+    }
+
+    public Object get(String name) {
+        return properties.get(name);
+    }
+
+    public PrintStream getConsole() {
+        // not implemented
+        return null;
+    }
+
+    public InputStream getKeyboard() {
+        // not implemented
+        return null;
+    }
+
+    public void put(String name, Object value) {
+        properties.put(name, value);
+    }
+}

Added: karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/UpdateCommandTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/UpdateCommandTest.java?rev=1081305&view=auto
==============================================================================
--- karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/UpdateCommandTest.java (added)
+++ karaf/trunk/shell/config/src/test/java/org/apache/karaf/shell/config/UpdateCommandTest.java Mon Mar 14 10:16:03 2011
@@ -0,0 +1,88 @@
+/*
+ * 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.karaf.shell.config;
+
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+import org.apache.felix.service.command.CommandSession;
+import org.easymock.EasyMock;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+
+/**
+ * Test cases for {@link EditCommand}
+ */
+public class UpdateCommandTest extends TestCase {
+
+    private static final String PID = "my.test.persistent.id-other";
+
+    private UpdateCommand command;
+    private BundleContext context;
+    private ConfigurationAdmin admin;
+    private CommandSession session;
+
+    @Override
+    protected void setUp() throws Exception {
+        command = new UpdateCommand();
+
+        context = EasyMock.createMock(BundleContext.class);
+        command.setBundleContext(context);
+
+        ServiceReference reference = createMock(ServiceReference.class);
+        expect(context.getServiceReference(ConfigurationAdmin.class.getName())).andReturn(reference);
+
+        admin = createMock(ConfigurationAdmin.class);
+        expect(context.getService(reference)).andReturn(admin);
+        expect(context.ungetService(reference)).andReturn(Boolean.TRUE);
+
+        replay(context);
+
+        session = new MockCommandSession();
+    }
+
+    public void testupdateOnNewFactoryPid() throws Exception {
+        Properties props = new Properties();
+
+        session.put(ConfigCommandSupport.PROPERTY_CONFIG_PID, PID);
+        session.put(ConfigCommandSupport.PROPERTY_CONFIG_PROPS, props);
+
+        Configuration configNew = createMock(Configuration.class);
+        expect(admin.getConfiguration(PID, null)).andReturn(configNew);
+        expect(configNew.getProperties()).andReturn(null);
+
+
+        Configuration configFac = createMock(Configuration.class);
+        expect(admin.createFactoryConfiguration(PID.substring(0, PID.indexOf('-')), null)).andReturn(configFac);
+        configFac.update(props);
+        expect(configFac.getBundleLocation()).andReturn(null);
+        replay(admin);
+        replay(configNew);
+        replay(configFac);
+
+        command.execute(session);
+
+    }
+
+}