From 1511c6e9ccd2595beeb0ddecfeef8ad1e5942fcb Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Sat, 19 Jun 2010 21:32:03 +0000 Subject: [PATCH] refactor the BundleListner into a BundleTracker as it is the best practice git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2027 7e9141cc-0065-0410-87d8-b60c137991c4 --- ...r.java => WebBundleTrackerCustomizer.java} | 125 ++++++++++++------ 1 file changed, 86 insertions(+), 39 deletions(-) rename jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/{JettyContextHandlerExtender.java => WebBundleTrackerCustomizer.java} (64%) diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/JettyContextHandlerExtender.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleTrackerCustomizer.java similarity index 64% rename from jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/JettyContextHandlerExtender.java rename to jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleTrackerCustomizer.java index b82bd290507..65c1059b9a4 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/JettyContextHandlerExtender.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleTrackerCustomizer.java @@ -1,5 +1,5 @@ // ======================================================================== -// Copyright (c) 2009 Intalio, Inc. +// Copyright (c) 2009-2010 Intalio, Inc. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 @@ -18,9 +18,10 @@ import java.util.Dictionary; import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator; import org.eclipse.jetty.osgi.boot.OSGiWebappConstants; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; -import org.osgi.framework.BundleListener; +import org.osgi.util.tracker.BundleTracker; +import org.osgi.util.tracker.BundleTrackerCustomizer; + /** * Support bundles that declare the webapp directly through headers in their @@ -46,44 +47,82 @@ import org.osgi.framework.BundleListener; * * @author hmalphettes */ -public class JettyContextHandlerExtender implements BundleListener -{ +public class WebBundleTrackerCustomizer implements BundleTrackerCustomizer { + - /** - * Receives notification that a bundle has had a lifecycle change. - * - * @param event - * The BundleEvent. - */ - public void bundleChanged(BundleEvent event) - { - switch (event.getType()) - { - case BundleEvent.STARTED: - register(event.getBundle()); - break; - case BundleEvent.STOPPING: - unregister(event.getBundle()); - break; - } - } - - /** + /** + * A bundle is being added to the BundleTracker. * + *

+ * This method is called before a bundle which matched the search parameters + * of the BundleTracker is added to the + * BundleTracker. This method should return the object to be + * tracked for the specified Bundle. The returned object is + * stored in the BundleTracker and is available from the + * {@link BundleTracker#getObject(Bundle) getObject} method. + * + * @param bundle The Bundle being added to the + * BundleTracker. + * @param event The bundle event which caused this customizer method to be + * called or null if there is no bundle event associated + * with the call to this method. + * @return The object to be tracked for the specified Bundle + * object or null if the specified Bundle + * object should not be tracked. */ - public void init(BundleContext context) - { - Bundle bundles[] = context.getBundles(); - for (int i = 0; i < bundles.length; i++) - { - if ((bundles[i].getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0) - { - register(bundles[i]); - } - } - } + public Object addingBundle(Bundle bundle, BundleEvent event) + { + + boolean isWebBundle = register(bundle); + return isWebBundle ? bundle : null; + } - private void register(Bundle bundle) + /** + * A bundle tracked by the BundleTracker has been modified. + * + *

+ * This method is called when a bundle being tracked by the + * BundleTracker has had its state modified. + * + * @param bundle The Bundle whose state has been modified. + * @param event The bundle event which caused this customizer method to be + * called or null if there is no bundle event associated + * with the call to this method. + * @param object The tracked object for the specified bundle. + */ + public void modifiedBundle(Bundle bundle, BundleEvent event, + Object object) + { + //nothing the web-bundle was already track. something changed. + //we only reload the webapps if the bundle is stopped and restarted. +// System.err.println(bundle.getSymbolicName()); + } + + /** + * A bundle tracked by the BundleTracker has been removed. + * + *

+ * This method is called after a bundle is no longer being tracked by the + * BundleTracker. + * + * @param bundle The Bundle that has been removed. + * @param event The bundle event which caused this customizer method to be + * called or null if there is no bundle event associated + * with the call to this method. + * @param object The tracked object for the specified bundle. + */ + public void removedBundle(Bundle bundle, BundleEvent event, + Object object) + { + unregister(bundle); + } + + + /** + * @param bundle + * @return true if this bundle in indeed a web-bundle. + */ + private boolean register(Bundle bundle) { Dictionary dic = bundle.getHeaders(); String warFolderRelativePath = (String)dic.get(OSGiWebappConstants.JETTY_WAR_FOLDER_PATH); @@ -99,11 +138,13 @@ public class JettyContextHandlerExtender implements BundleListener try { JettyBootstrapActivator.registerWebapplication(bundle,warFolderRelativePath,contextPath); + return true; } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); + return true;//maybe it did not work maybe it did. safer to track this bundle. } } else if (dic.get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH) != null) @@ -112,7 +153,7 @@ public class JettyContextHandlerExtender implements BundleListener if (contextFileRelativePath == null) { // nothing to register here. - return; + return false; } // support for multiple webapps in the same bundle: String[] pathes = contextFileRelativePath.split(",;"); @@ -128,6 +169,7 @@ public class JettyContextHandlerExtender implements BundleListener e.printStackTrace(); } } + return true; } else { @@ -137,7 +179,7 @@ public class JettyContextHandlerExtender implements BundleListener URL rfc66Webxml = bundle.getEntry("/WEB-INF/web.xml"); if (rfc66Webxml == null) { - return;// no webapp in here + return false;// no webapp in here } // this is risky: should we make sure that there is no classes and // jars directly available @@ -151,11 +193,13 @@ public class JettyContextHandlerExtender implements BundleListener try { JettyBootstrapActivator.registerWebapplication(bundle,".",rfc66ContextPath); + return true; } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); + return true;//maybe it did not work maybe it did. safer to track this bundle. } } } @@ -195,4 +239,7 @@ public class JettyContextHandlerExtender implements BundleListener // webapps registered in that bundle. } + + + }