servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r732301 - in /servicemix/smx4/kernel/trunk/gshell/gshell-osgi: ./ src/main/java/org/apache/servicemix/kernel/gshell/osgi/ src/main/resources/META-INF/spring/
Date Wed, 07 Jan 2009 11:06:37 GMT
Author: gnodet
Date: Wed Jan  7 03:06:37 2009
New Revision: 732301

URL: http://svn.apache.org/viewvc?rev=732301&view=rev
Log:
SMX4KNL-132: osgi/list should display the spring application state

Added:
    servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/SpringApplicationListener.java
Modified:
    servicemix/smx4/kernel/trunk/gshell/gshell-osgi/pom.xml
    servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/ListBundles.java
    servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/resources/META-INF/spring/gshell-osgi.xml

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-osgi/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-osgi/pom.xml?rev=732301&r1=732300&r2=732301&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-osgi/pom.xml (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-osgi/pom.xml Wed Jan  7 03:06:37 2009
@@ -53,6 +53,11 @@
             <groupId>org.springframework.osgi</groupId>
             <artifactId>spring-osgi-core</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>org.springframework.osgi</groupId>
+            <artifactId>spring-osgi-extender</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/ListBundles.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/ListBundles.java?rev=732301&r1=732300&r2=732301&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/ListBundles.java
(original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/ListBundles.java
Wed Jan  7 03:06:37 2009
@@ -34,6 +34,16 @@
     @Option(name = "-u", description = "Show update")
     boolean showUpdate;
 
+    private SpringApplicationListener springApplicationListener;
+
+    public SpringApplicationListener getSpringApplicationListener() {
+        return springApplicationListener;
+    }
+
+    public void setSpringApplicationListener(SpringApplicationListener springApplicationListener)
{
+        this.springApplicationListener = springApplicationListener;
+    }
+
     protected Object doExecute() throws Exception {
         ServiceReference ref = getBundleContext().getServiceReference(StartLevel.class.getName());
         StartLevel sl = null;
@@ -63,7 +73,7 @@
                    msg = " Update location";
                 }
                 String level = (sl == null) ? "" : "  Level ";
-                io.out.println("   ID " + "  State       " + level + msg);
+                io.out.println("   ID   State         Spring   " + level + msg);
                 for (int i = 0; i < bundles.length; i++) {
                     // Get the bundle name or location.
                     String name = (String) bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
@@ -103,7 +113,8 @@
                         id = " " + id;
                     }
                     io.out.println("[" + id + "] ["
-                        + getStateString(bundles[i].getState())
+                        + getStateString(bundles[i])
+                        + "] [" + getSpringStateString(bundles[i])
                         + "] [" + level + "] " + name);
                 }
             }
@@ -119,18 +130,34 @@
         }
     }
 
-    public String getStateString(int i)
+    public String getStateString(Bundle bundle)
     {
-        if (i == Bundle.ACTIVE)
+        int state = bundle.getState();
+        if (state == Bundle.ACTIVE) {
             return "Active     ";
-        else if (i == Bundle.INSTALLED)
+        } else if (state == Bundle.INSTALLED) {
             return "Installed  ";
-        else if (i == Bundle.RESOLVED)
+        } else if (state == Bundle.RESOLVED) {
             return "Resolved   ";
-        else if (i == Bundle.STARTING)
+        } else if (state == Bundle.STARTING) {
             return "Starting   ";
-        else if (i == Bundle.STOPPING)
+        } else if (state == Bundle.STOPPING) {
             return "Stopping   ";
-        return "Unknown    ";
+        } else {
+            return "Unknown    ";
+        }
+    }
+
+    public String getSpringStateString(Bundle bundle) {
+        SpringApplicationListener.SpringState state = springApplicationListener.getSpringState(bundle);
+        if (state == SpringApplicationListener.SpringState.Waiting) {
+            return "Waiting";
+        } else if (state == SpringApplicationListener.SpringState.Started) {
+            return "Started";
+        } else if (state == SpringApplicationListener.SpringState.Failed) {
+            return "Failed ";
+        } else {
+            return "       ";
+        }
     }
 }

Added: servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/SpringApplicationListener.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/SpringApplicationListener.java?rev=732301&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/SpringApplicationListener.java
(added)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/java/org/apache/servicemix/kernel/gshell/osgi/SpringApplicationListener.java
Wed Jan  7 03:06:37 2009
@@ -0,0 +1,102 @@
+/*
+ * 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.servicemix.kernel.gshell.osgi;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.springframework.osgi.context.event.OsgiBundleApplicationContextListener;
+import org.springframework.osgi.context.event.OsgiBundleApplicationContextEvent;
+import org.springframework.osgi.context.event.OsgiBundleContextFailedEvent;
+import org.springframework.osgi.context.event.OsgiBundleContextRefreshedEvent;
+import org.springframework.osgi.context.BundleContextAware;
+import org.springframework.osgi.extender.event.BootstrappingDependencyEvent;
+import org.springframework.osgi.service.importer.event.OsgiServiceDependencyEvent;
+import org.springframework.osgi.service.importer.event.OsgiServiceDependencyWaitStartingEvent;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.DisposableBean;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class SpringApplicationListener implements OsgiBundleApplicationContextListener,
+                                                  BundleListener, BundleContextAware,
+                                                  InitializingBean, DisposableBean {
+
+    public static enum SpringState {
+        Unknown,
+        Waiting,
+        Started,
+        Failed,
+    }
+
+    private static final Log LOG = LogFactory.getLog(SpringApplicationListener.class);
+
+    private final Map<Long, SpringState> states;
+    private BundleContext bundleContext;
+
+    public SpringApplicationListener() {
+        this.states = new ConcurrentHashMap<Long, SpringState>();
+    }
+
+    public SpringState getSpringState(Bundle bundle) {
+        SpringState state = states.get(bundle.getBundleId());
+        if (state == null) {
+            state = SpringState.Unknown;
+        }
+        return state;
+    }
+
+    public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) {
+        SpringState state = null;
+        if (event instanceof BootstrappingDependencyEvent) {
+            OsgiServiceDependencyEvent de = ((BootstrappingDependencyEvent) event).getDependencyEvent();
+            if (de instanceof OsgiServiceDependencyWaitStartingEvent) {
+                state = SpringState.Waiting;
+            }
+        } else if (event instanceof OsgiBundleContextFailedEvent) {
+            state = SpringState.Failed;
+        } else if (event instanceof OsgiBundleContextRefreshedEvent) {
+            state = SpringState.Started;
+        }
+        if (state != null) {
+            LOG.debug("Spring app state changed to " + state + " for bundle " + event.getBundle().getBundleId());
+            states.put(event.getBundle().getBundleId(), state);
+        }
+    }
+
+    public void bundleChanged(BundleEvent event) {
+        if (event.getType() == BundleEvent.UNINSTALLED) {
+            states.remove(event.getBundle().getBundleId());
+        }
+    }
+
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+
+    public void afterPropertiesSet() throws Exception {
+        bundleContext.addBundleListener(this);
+    }
+
+    public void destroy() throws Exception {
+        bundleContext.removeBundleListener(this);
+    }
+}

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/resources/META-INF/spring/gshell-osgi.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/resources/META-INF/spring/gshell-osgi.xml?rev=732301&r1=732300&r2=732301&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/resources/META-INF/spring/gshell-osgi.xml
(original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-osgi/src/main/resources/META-INF/spring/gshell-osgi.xml
Wed Jan  7 03:06:37 2009
@@ -45,7 +45,9 @@
             <gshell:action class="org.apache.servicemix.kernel.gshell.osgi.InstallBundle"
/>
         </gshell:command>
         <gshell:command name="osgi/list">
-            <gshell:action class="org.apache.servicemix.kernel.gshell.osgi.ListBundles"
/>
+            <gshell:action class="org.apache.servicemix.kernel.gshell.osgi.ListBundles">
+                <property name="springApplicationListener" ref="springApplicationListener"
/>
+            </gshell:action>
         </gshell:command>
         <gshell:command name="osgi/ls">
             <gshell:action class="org.apache.servicemix.kernel.gshell.osgi.ListServices"
/>
@@ -76,4 +78,8 @@
         </gshell:command>
     </gshell:command-bundle>
 
+    <bean id="springApplicationListener" class="org.apache.servicemix.kernel.gshell.osgi.SpringApplicationListener"
/>
+
+    <osgi:service ref="springApplicationListener" interface="org.springframework.osgi.context.event.OsgiBundleApplicationContextListener"
/>
+
 </beans>
\ No newline at end of file



Mime
View raw message