karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anierb...@apache.org
Subject svn commit: r1057856 - in /karaf/trunk/shell/web/src/main: ./ java/ java/org/ java/org/apache/ java/org/apache/karaf/ java/org/apache/karaf/shell/ java/org/apache/karaf/shell/web/
Date Tue, 11 Jan 2011 20:35:32 GMT
Author: anierbeck
Date: Tue Jan 11 20:35:32 2011
New Revision: 1057856

URL: http://svn.apache.org/viewvc?rev=1057856&view=rev
Log:
[KARAF-211] - osgi:list to other command to display context-path for web applications or a
new war command for that
web:list implementation

Added:
    karaf/trunk/shell/web/src/main/
    karaf/trunk/shell/web/src/main/java/
    karaf/trunk/shell/web/src/main/java/org/
    karaf/trunk/shell/web/src/main/java/org/apache/
    karaf/trunk/shell/web/src/main/java/org/apache/karaf/
    karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/
    karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/
    karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebEventHandler.java  
(with props)
    karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebListCommand.java   (with
props)

Added: karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebEventHandler.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebEventHandler.java?rev=1057856&view=auto
==============================================================================
--- karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebEventHandler.java (added)
+++ karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebEventHandler.java Tue
Jan 11 20:35:32 2011
@@ -0,0 +1,36 @@
+/**
+ * 
+ */
+package org.apache.karaf.shell.web;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+
+/**
+ * @author Achim
+ *
+ */
+public class WebEventHandler implements EventHandler {
+	
+	private final Map<Long, String> bundleEvents = new HashMap<Long, String>();
+
+	/* (non-Javadoc)
+	 * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
+	 */
+	public void handleEvent(Event event) {
+		String topic = event.getTopic();
+		Long bundleID = (Long) event.getProperty("bundle.id");
+		getBundleEvents().put(bundleID, topic);
+	}
+
+	/**
+	 * @return the bundleEvents
+	 */
+	public Map<Long, String> getBundleEvents() {
+		return bundleEvents;
+	}
+
+}

Propchange: karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebEventHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebListCommand.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebListCommand.java?rev=1057856&view=auto
==============================================================================
--- karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebListCommand.java (added)
+++ karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebListCommand.java Tue
Jan 11 20:35:32 2011
@@ -0,0 +1,156 @@
+/**
+ * 
+ */
+package org.apache.karaf.shell.web;
+
+import java.util.Map;
+
+import org.apache.felix.gogo.commands.Command;
+import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.service.startlevel.StartLevel;
+
+@Command(scope = "web", name = "list", description = "Lists details for war bundles")
+public class WebListCommand extends OsgiCommandSupport {
+	
+	private StartLevel startLevelService;
+	
+	private WebEventHandler eventHandler;
+
+	/* (non-Javadoc)
+	 * @see org.apache.karaf.shell.war.WarCommandSupport#doExecute(org.osgi.service.packageadmin.PackageAdmin)
+	 */
+	@Override
+	protected Object doExecute() {
+		Bundle[] bundles = getBundleContext().getBundles();
+		Map<Long, String> bundleEvents = eventHandler.getBundleEvents();
+		if (bundles != null) {
+			String level = (startLevelService == null) ? "" : "  Level ";
+			String webState = (bundleEvents == null || bundleEvents.isEmpty()) ? "" : "  Web-State
    ";
+			String headers = "   ID   State       ";
+			headers += webState + level + "  Web-ContextPath           Name";
+            System.out.println(headers);
+            for (int i = 0; i < bundles.length; i++) {
+            	//First check if this bundle contains  a webapp ctxt
+            	String webappctxt = (String) bundles[i].getHeaders().get("Web-ContextPath");
+            	if (webappctxt == null)
+            		webappctxt = (String) bundles[i].getHeaders().get("Webapp-Context");//this
one is used by pax-web but is deprecated.
+            	
+            	if (webappctxt == null)
+            		continue; //only list war archives. 
+            	
+            	webappctxt.trim();
+            	
+            	// Get the bundle name or location.
+                String name = (String) bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
+                // If there is no name, then default to symbolic name.
+                name = (name == null) ? bundles[i].getSymbolicName() : name;
+                // If there is no symbolic name, resort to location.
+                name = (name == null) ? bundles[i].getLocation() : name;
+                // Show bundle version if not showing location.
+                String version = (String) bundles[i].getHeaders().get(Constants.BUNDLE_VERSION);
+                name = ((version != null)) ? name + " (" + version + ")" : name;
+                long l = bundles[i].getBundleId();
+                String id = String.valueOf(l);
+                if (startLevelService == null) {
+                    level = "";
+                }
+                else {
+                    level = String.valueOf(startLevelService.getBundleStartLevel(bundles[i]));
+                }
+                while (level.length() < 5) {
+                    level = " " + level;
+                }
+                while (id.length() < 4) {
+                    id = " " + id;
+                }
+                
+                //prepend ctxt with slash (looks better)
+                if (!webappctxt.startsWith("/"))
+                	webappctxt = "/" + webappctxt;
+                
+                while (webappctxt.length() < 24) {
+                	webappctxt += " ";
+                }
+                
+                String line = "[" + id + "] [" + getStateString(bundles[i]) + "]";
+                if (bundleEvents != null && !bundleEvents.isEmpty())
+                	line += " ["+ getWebStateString(bundles[i]) +"] ";
+                line += " [" + level + "] [" + webappctxt + "] " + name;
+                System.out.println(line);
+            }
+		}
+		return null;
+
+	}
+
+    public String getStateString(Bundle bundle)
+    {
+        int state = bundle.getState();
+        if (state == Bundle.ACTIVE) {
+            return "Active     ";
+        } else if (state == Bundle.INSTALLED) {
+            return "Installed  ";
+        } else if (state == Bundle.RESOLVED) {
+            return "Resolved   ";
+        } else if (state == Bundle.STARTING) {
+            return "Starting   ";
+        } else if (state == Bundle.STOPPING) {
+            return "Stopping   ";
+        } else {
+            return "Unknown    ";
+        }
+    }
+    
+    public String getWebStateString(Bundle bundle) {
+    	
+    	long bundleId = bundle.getBundleId();
+    	Map<Long, String> bundleEvents = eventHandler.getBundleEvents();
+		if (bundleEvents.containsKey(bundleId)) {
+    		String topic = bundleEvents.get(bundleId);
+    		topic = topic.substring(21);
+    		if ("UNDEPLOYING".equalsIgnoreCase(topic))
+    			return "Undeploying ";
+    		else if ("DEPLOYING".equalsIgnoreCase(topic))
+    			return "Deploying   ";
+    		else if ("DEPLOYED".equalsIgnoreCase(topic))
+    			return "Deployed    ";
+    		else if ("FAILED".equalsIgnoreCase(topic))
+    			return "Failed      ";
+    		else if ("UNDEPLOYED".equalsIgnoreCase(topic))
+    			return "Undeployed  ";
+    	}
+    	
+    	return "Unknown     ";
+    }
+
+	/**
+	 * @param startLevelService the startLevelService to set
+	 */
+	public void setStartLevelService(StartLevel startLevelService) {
+		this.startLevelService = startLevelService;
+	}
+
+	/**
+	 * @return the startLevelService
+	 */
+	public StartLevel getStartLevelService() {
+		return startLevelService;
+	}
+
+	/**
+	 * @param eventHandler the eventHandler to set
+	 */
+	public void setEventHandler(WebEventHandler eventHandler) {
+		this.eventHandler = eventHandler;
+	}
+
+	/**
+	 * @return the eventHandler
+	 */
+	public WebEventHandler getEventHandler() {
+		return eventHandler;
+	}
+
+}

Propchange: karaf/trunk/shell/web/src/main/java/org/apache/karaf/shell/web/WebListCommand.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message