You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2011/01/06 18:35:20 UTC

svn commit: r1055952 - in /karaf/trunk/admin: command/src/main/java/org/apache/karaf/admin/command/ command/src/main/resources/OSGI-INF/blueprint/ core/src/main/java/org/apache/karaf/admin/ core/src/main/java/org/apache/karaf/admin/internal/ core/src/t...

Author: jbonofre
Date: Thu Jan  6 17:35:19 2011
New Revision: 1055952

URL: http://svn.apache.org/viewvc?rev=1055952&view=rev
Log:
[KARAF-348] Add admin:rename command.

Added:
    karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/RenameCommand.java
Modified:
    karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml
    karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java
    karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/Instance.java
    karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
    karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/InstanceImpl.java
    karaf/trunk/admin/core/src/test/java/org/apache/karaf/admin/internal/AdminServiceImplTest.java

Added: karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/RenameCommand.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/RenameCommand.java?rev=1055952&view=auto
==============================================================================
--- karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/RenameCommand.java (added)
+++ karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/RenameCommand.java Thu Jan  6 17:35:19 2011
@@ -0,0 +1,43 @@
+/*
+ * 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.admin.command;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+
+/**
+ * <p>
+ * Rename an existing Karaf container instance.
+ * </p>
+ *
+ * @author jbonofre
+ */
+@Command(scope = "admin", name = "rename", description = "Renames an existing container instance.")
+public class RenameCommand extends AdminCommandSupport {
+
+    @Argument(index = 0, name = "name", description = "The name of the container instance to rename", required = true, multiValued = false)
+    String instance = null;
+
+    @Argument(index = 1, name = "new-name", description = "The new name of the container instance", required = true, multiValued = false)
+    String newName = null;
+
+    protected Object doExecute() throws Exception {
+        getAdminService().renameInstance(instance, newName);
+        return null;
+    }
+
+}

Modified: karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml?rev=1055952&r1=1055951&r2=1055952&view=diff
==============================================================================
--- karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml (original)
+++ karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml Thu Jan  6 17:35:19 2011
@@ -68,6 +68,15 @@
                 <null/>
             </completers>
         </command>
+        <command name="admin/rename">
+            <action class="org.apache.karaf.admin.command.RenameCommand">
+                <property name="adminService" ref="adminService" />
+            </action>
+            <completers>
+                <ref component-id="instanceCompleter" />
+                <null />
+            </completers>
+        </command>
         <command name="admin/change-ssh-port">
             <action class="org.apache.karaf.admin.command.ChangeSshPortCommand">
                 <property name="adminService" ref="adminService" />

Modified: karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java?rev=1055952&r1=1055951&r2=1055952&view=diff
==============================================================================
--- karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java (original)
+++ karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java Thu Jan  6 17:35:19 2011
@@ -20,6 +20,8 @@ public interface AdminService {
 
     Instance createInstance(String name, InstanceSettings settings) throws Exception;
 
+    void renameInstance(String name, String newName) throws Exception;
+
     Instance[] getInstances();
 
     Instance getInstance(String name);    

Modified: karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/Instance.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/Instance.java?rev=1055952&r1=1055951&r2=1055952&view=diff
==============================================================================
--- karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/Instance.java (original)
+++ karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/Instance.java Thu Jan  6 17:35:19 2011
@@ -24,11 +24,15 @@ public interface Instance {
     String ERROR = "Error";
 
     String getName();
+
+    void setName(String name);
     
     boolean isRoot();
 
     String getLocation();
 
+    void setLocation(String location);
+
     int getPid();
 
     int getSshPort();

Modified: karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java?rev=1055952&r1=1055951&r2=1055952&view=diff
==============================================================================
--- karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java (original)
+++ karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java Thu Jan  6 17:35:19 2011
@@ -242,6 +242,40 @@ public class AdminServiceImpl implements
         instances.remove(name);
     }
 
+    public synchronized void renameInstance(String name, String newName) throws Exception {
+        if (instances.get(newName) != null) {
+            throw new IllegalArgumentException("Instance " + newName + " already exists");
+        }
+        Instance instance = instances.get(name);
+        if (instance == null) {
+            throw new IllegalArgumentException("Instance " + name + " not found");
+        }
+
+        println(Ansi.ansi().a("Renaming instance ").a(Ansi.Attribute.INTENSITY_BOLD).a(name).a(Ansi.Attribute.RESET).a(" to ").a(Ansi.Attribute.INTENSITY_BOLD).a(newName).toString());
+        // remote the old instance
+        instances.remove(name);
+        // update instance name
+        instance.setName(newName);
+        // rename directory
+        File currentLocation = new File(instance.getLocation());
+        String basedir = currentLocation.getParent();
+        File newLocation = new File(basedir, newName);
+        currentLocation.renameTo(newLocation);
+        // update the instance location
+        instance.setLocation(newLocation.getPath());
+        // load the etc/system.properties
+        // TODO use Karaf util Properties to preserve the comment and format of the original properties file
+        Properties systemProperties = new Properties();
+        systemProperties.load(new FileInputStream(new File(newLocation, "etc/system.properties")));
+        systemProperties.setProperty("karaf.name", newName);
+        systemProperties.store(new FileOutputStream(new File(newLocation, "etc/system.properties")), null);
+        // TODO update the bin/karaf, bin/start and bin/stop scripts (and/or corresponding .bat scripts)
+        // add the renamed instances
+        instances.put(newName, instance);
+        // save instance definition in the instances.properties
+        saveState();
+    }
+
     synchronized void saveState() throws IOException {
         Properties storage = new Properties();
         Instance[] data = getInstances();

Modified: karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/InstanceImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/InstanceImpl.java?rev=1055952&r1=1055951&r2=1055952&view=diff
==============================================================================
--- karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/InstanceImpl.java (original)
+++ karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/InstanceImpl.java Thu Jan  6 17:35:19 2011
@@ -28,6 +28,7 @@ import org.apache.karaf.admin.Instance;
 import org.apache.karaf.jpm.Process;
 import org.apache.karaf.jpm.ProcessBuilderFactory;
 import org.apache.karaf.jpm.impl.ScriptUtils;
+import org.fusesource.jansi.Ansi;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -79,6 +80,10 @@ public class InstanceImpl implements Ins
     public String getName() {
         return this.name;
     }
+
+    public void setName(String name) {
+        this.name = name;
+    }
     
     public boolean isRoot() {
         return root;
@@ -88,6 +93,10 @@ public class InstanceImpl implements Ins
         return location;
     }
 
+    public void setLocation(String location) {
+        this.location = location;
+    }
+
     public boolean exists() {
         return new File(location).isDirectory();
     }
@@ -259,7 +268,6 @@ public class InstanceImpl implements Ins
         this.service.saveState();
     }
 
-
     public synchronized String getState() {
         int port = getSshPort();
         if (!exists() || port <= 0) {

Modified: karaf/trunk/admin/core/src/test/java/org/apache/karaf/admin/internal/AdminServiceImplTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/core/src/test/java/org/apache/karaf/admin/internal/AdminServiceImplTest.java?rev=1055952&r1=1055951&r2=1055952&view=diff
==============================================================================
--- karaf/trunk/admin/core/src/test/java/org/apache/karaf/admin/internal/AdminServiceImplTest.java (original)
+++ karaf/trunk/admin/core/src/test/java/org/apache/karaf/admin/internal/AdminServiceImplTest.java Thu Jan  6 17:35:19 2011
@@ -88,6 +88,22 @@ public class AdminServiceImplTest extend
         assertFileExists(instance.getLocation(), "etc/org.ops4j.pax.url.mvn.cfg");
     }
 
+    /**
+     * <p>
+     * Test the renaming of an existing instance.
+     * </p>
+     */
+    public void testRenameInstance() throws Exception {
+        AdminServiceImpl service = new AdminServiceImpl();
+        service.setStorageLocation(new File("target/instances/" + System.currentTimeMillis()));
+
+        InstanceSettings settings = new InstanceSettings(8122, 1122, getName(), null, null, null);
+        Instance instance = service.createInstance(getName(), settings);
+
+        service.renameInstance(getName(), getName() + "b");
+        assertNotNull(service.getInstance(getName() + "b"));
+    }
+
     private void assertFileExists(String path, String name) throws IOException {
         File file = new File(path, name);
         assertTrue("Expected " + file.getCanonicalPath() + " to exist",