Author: davidb
Date: Mon Mar 30 10:08:43 2015
New Revision: 1670035

URL: http://svn.apache.org/r1670035
Log:
ARIES-1309 - Subsystem Commands

Donation of a small gogo command to control subsystems that I wrote some time 
ago and has received fairly wide use.

Added:
    aries/trunk/subsystem/subsystem-gogo-command/
    aries/trunk/subsystem/subsystem-gogo-command/pom.xml
    aries/trunk/subsystem/subsystem-gogo-command/src/
    aries/trunk/subsystem/subsystem-gogo-command/src/main/
    aries/trunk/subsystem/subsystem-gogo-command/src/main/java/
    aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/
    aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/
    aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/
    
aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/subsystem/
    
aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/subsystem/gogo/
    
aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/subsystem/gogo/Activator.java

Added: aries/trunk/subsystem/subsystem-gogo-command/pom.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-gogo-command/pom.xml?rev=1670035&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-gogo-command/pom.xml (added)
+++ aries/trunk/subsystem/subsystem-gogo-command/pom.xml Mon Mar 30 10:08:43 
2015
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.aries</groupId>
+    <artifactId>parent</artifactId>
+    <version>2.0.0</version>
+    <relativePath>../../parent/pom.xml</relativePath>
+  </parent>
+
+  <groupId>org.apache.aries.subsystem</groupId>
+  <artifactId>org.apache.aries.subsystem.gogo-command</artifactId>
+  <version>1.0.0-SNAPSHOT</version>
+  <name>Apache Aries Subsystem Gogo command</name>
+  <packaging>bundle</packaging>
+
+  <scm>
+    
<connection>scm:svn:http://svn.apache.org/repos/asf/aries/trunk/subsystem/subsystem-gogo-command</connection>
+    
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/aries/trunk/subsystem/subsystem-gogo-command</developerConnection>
+    
<url>http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-gogo-command</url>
+  </scm>
+
+  <properties>
+    <aries.osgi.activator>
+        org.apache.aries.subsystem.gogo.Activator
+    </aries.osgi.activator>
+    <aries.osgi.export.pkg />
+    <aries.osgi.import>
+        *
+    </aries.osgi.import>
+    <aries.osgi.private.pkg>
+      org.apache.aries.subsystem.gogo.*
+    </aries.osgi.private.pkg>
+    <!-- Move this item to 1.0.0 once released -->
+    <lastReleaseVersion>1.0.0-SNAPSHOT</lastReleaseVersion>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>5.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.enterprise</artifactId>
+      <version>5.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>  
+</project>

Added: 
aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/subsystem/gogo/Activator.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/subsystem/gogo/Activator.java?rev=1670035&view=auto
==============================================================================
--- 
aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/subsystem/gogo/Activator.java
 (added)
+++ 
aries/trunk/subsystem/subsystem-gogo-command/src/main/java/org/apache/aries/subsystem/gogo/Activator.java
 Mon Mar 30 10:08:43 2015
@@ -0,0 +1,91 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (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.aries.subsystem.gogo;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.subsystem.Subsystem;
+
+public class Activator implements BundleActivator {
+    private BundleContext bundleContext;
+
+    public void start(BundleContext context) throws Exception {
+        bundleContext = context;
+
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put("osgi.command.function", new String[] { "install", 
"uninstall", "start", "stop", "list" });
+        props.put("osgi.command.scope", "subsystem");
+        context.registerService(getClass().getName(), this, props);
+    }
+
+    public void install(String url) throws IOException {
+        Subsystem rootSubsystem = getSubsystem(0);
+        System.out.println("Installing subsystem: " + url);
+        Subsystem s = rootSubsystem.install(url, new URL(url).openStream());
+        System.out.println("Subsystem successfully installed: " + 
s.getSymbolicName() + "; id: " + s.getSubsystemId());
+    }
+
+    public void uninstall(long id) {
+        getSubsystem(id).uninstall();
+    }
+
+    public void start(long id) {
+        getSubsystem(id).start();
+    }
+
+    public void stop(long id) {
+        getSubsystem(id).stop();
+    }
+
+    public void list() throws InvalidSyntaxException {
+        Map<Long, String> subsystems = new TreeMap<Long, String>();
+
+        for (ServiceReference<Subsystem> ref : 
bundleContext.getServiceReferences(Subsystem.class, null)) {
+            Subsystem s = bundleContext.getService(ref);
+            if (s != null) {
+                subsystems.put(s.getSubsystemId(),
+                    String.format("%d\t%s\t%s %s", s.getSubsystemId(), 
s.getState(), s.getSymbolicName(), s.getVersion()));
+            }
+        }
+
+        for (String entry : subsystems.values()) {
+            System.out.println(entry);
+        }
+    }
+
+    private Subsystem getSubsystem(long id) {
+        try {
+            for (ServiceReference<Subsystem> ref :
+                    bundleContext.getServiceReferences(Subsystem.class, 
"(subsystem.id=" + id + ")")) {
+                Subsystem svc = bundleContext.getService(ref);
+                if (svc != null)
+                    return svc;
+            }
+        } catch (InvalidSyntaxException e) {
+            throw new RuntimeException(e);
+        }
+        throw new RuntimeException("Unable to find subsystem " + id);
+    }
+
+    public void stop(BundleContext context) throws Exception {
+    }
+}


Reply via email to