You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-commits@ws.apache.org by da...@apache.org on 2007/03/24 14:10:49 UTC

svn commit: r522016 [3/3] - in /webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi: ./ descriptor/ internal/ routing/ util/

Modified: webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/OSGiResourceRouter.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/OSGiResourceRouter.java?view=diff&rev=522016&r1=522015&r2=522016
==============================================================================
--- webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/OSGiResourceRouter.java (original)
+++ webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/OSGiResourceRouter.java Sat Mar 24 06:10:47 2007
@@ -1,484 +1,487 @@
-/*=============================================================================*
- *  Copyright 2006 The Apache Software Foundation
- *
- *  Licensed 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.muse.core.platform.osgi.routing;
-
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-
-import org.apache.muse.core.Environment;
-import org.apache.muse.core.Resource;
-import org.apache.muse.core.ResourceManager;
-import org.apache.muse.core.descriptor.PersistenceDefinition;
-import org.apache.muse.core.descriptor.ResourceDefinition;
-import org.apache.muse.core.descriptor.RouterDefinition;
-import org.apache.muse.core.descriptor.SerializerDefinition;
-import org.apache.muse.core.descriptor.WsdlConfig;
-import org.apache.muse.core.platform.osgi.internal.OSGiResourceManager;
-import org.apache.muse.core.platform.osgi.OSGiEnvironment;
-import org.apache.muse.core.platform.osgi.OSGiEnvironmentImpl;
-import org.apache.muse.core.platform.osgi.OSGiPlatformConstants;
-import org.apache.muse.core.platform.osgi.ResourceManagementProvider;
-import org.apache.muse.core.platform.osgi.descriptor.OSGiDeploymentDescriptor;
-import org.apache.muse.core.platform.osgi.util.BundleRootHelper;
-import org.apache.muse.core.platform.osgi.util.OSGiReflectUtilHelper;
-import org.apache.muse.core.routing.ResourceRouter;
-import org.apache.muse.core.routing.RouterPersistence;
-import org.apache.muse.core.routing.SimpleResourceRouter;
-import org.apache.muse.core.serializer.ArraySerializer;
-import org.apache.muse.core.serializer.Serializer;
-import org.apache.muse.core.serializer.SerializerRegistry;
-import org.apache.muse.util.ReflectUtils;
-import org.apache.muse.util.xml.XmlUtils;
-import org.apache.muse.ws.addressing.EndpointReference;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.BundleListener;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-
-/**
- *
- * OSGiResourceRouter is an extension of the SimpleResourceRouter that supports
- * a delegation model for additional Muse-enabled Bundles. This class is intended 
- * for use by <code>ResourceManagementProvider</code> implementations. It is not 
- * intended for use by normal Muse-enabled bundles.
- *  
- * @see ResourceManagementProvider
- * @author Joel Hawkins (joelh)
- *
- */
-
-public class OSGiResourceRouter extends SimpleResourceRouter implements BundleListener{
-	
-	private static HashMap bundleToDefinitionMap = new HashMap();
-	private static HashMap delegateMap = new HashMap();
-	private static HashMap deferredResources = new HashMap();
-	private static HashMap bundleToContextMap = new HashMap();
-	private static HashMap contextToBundleMap = new HashMap();
-	
-	private static ResourceManagementProvider provider;
-	
-	private static OSGiResourceRouter _router = new OSGiResourceRouter();
-
-	public static OSGiResourceRouter getDefault(){ return _router;}
-	
-	protected ResourceManager createResourceManager() {
-		OSGiResourceManager manager = new OSGiResourceManager();
-		return manager;
-	}
-	
-	public Bundle getDelegateBundle(){
-        Environment env = getEnvironment();
-        EndpointReference epr = env.getAddressingContext().getToAddress();
-        String address = epr.getAddress().toString();
-        
-        Iterator keys = contextToBundleMap.keySet().iterator();
-        while(keys.hasNext()){
-        	String currentKey = (String)keys.next();
-        	if(address.endsWith(currentKey)){
-        		return (Bundle)contextToBundleMap.get(currentKey);
-        	}
-        }
-        
-        return null;
-	}
-
-
-	public Element invoke(Element soapBody){
-		Resource resource = null;
-		try {
-			Bundle bundle = getDelegateBundle();			
-			ResourceRouter delegateRouter = null;
-			if(bundle != null){
-				delegateRouter = (ResourceRouter)delegateMap.get(bundle);
-			}
-	        ((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(bundle);
-		return delegateRouter.invoke(soapBody);
-        } finally {
-            ((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(null);
-        }
-	}
-	
-	
-	public void setEnvironment(Environment environment) {
-		super.setEnvironment(environment);
-		//set the environment of all the delegates
-		if (!deferredResources.isEmpty()) {
-			synchronized (deferredResources) {
-				Iterator iter = deferredResources.keySet().iterator();
-				while (iter.hasNext()) {
-					Bundle bundle = (Bundle) iter.next();
-					try {
-						String target = (String) bundle.getHeaders().get(
-								OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
-						URL resourceURL = bundle.getResource(target);
-						((OSGiEnvironment) this.getEnvironment())
-								.setThreadLocalBundle(bundle);
-						OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(bundle);
-						addResourceDefinitions(bundle, resourceURL);
-					} catch (Throwable t) {
-						t.printStackTrace();
-					} finally {
-						((OSGiEnvironment) this.getEnvironment())
-								.setThreadLocalBundle(null);
-						OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(null);
-					}
-				}
-				//TODO I think we just set these to FALSE
-				deferredResources.clear();
-			}
-		}
-	}
-	
-	
-	public void initialize(BundleContext context) {
-		context.addBundleListener(this);
-		Bundle bundles[] = context.getBundles();
-		OSGiEnvironment env = new OSGiEnvironmentImpl(false);
-		for (int i = 0; i < bundles.length; i++) {
-			switch (bundles[i].getState()) {
-			case Bundle.ACTIVE:
-			case Bundle.RESOLVED:
-			case Bundle.STARTING:
-				String target = (String) bundles[i].getHeaders().get(
-						OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
-				String contextPath = (String) bundles[i].getHeaders().get(
-						OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
-				if (target != null) {
-					registerBundleResources(bundles[i], contextPath, target);
-				}
-				break;
-			default:
-				break;
-			}
-		}
-	}
-	
-	private class ContextInfo {
-		public ContextInfo(String contextPath, String wsdlPath){
-			this.contextPath = contextPath;
-			this.wsdlPath = wsdlPath;
-		}
-		
-		public String getContextPath(){
-			return contextPath;
-		}
-		
-		private String getWsdlPath(){
-			return wsdlPath;
-		}
-		
-		private String contextPath;
-		private String wsdlPath;
-	}
-	
-	private Collection getContextPathsForBundle(Bundle bundle, String target) {
-		ArrayList list = new ArrayList();
-		OSGiEnvironment env = new OSGiEnvironmentImpl(false);
-		env.setThreadLocalBundle(bundle);
-		OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(bundle);
-		try {
-			Document ddXML = env.getDocument(target);
-			OSGiDeploymentDescriptor dd = new OSGiDeploymentDescriptor();
-			Collection resourceDefs = dd.loadContextPaths(ddXML, env);
-			Iterator paths = resourceDefs.iterator();
-			
-			String contextPath = (String) bundle.getHeaders().get(
-					OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
-			while (paths.hasNext()) {
-				String context = (String) paths.next(); 
-				String wsdlPath = dd.getWsdlPathForContext(ddXML, context);
-				//FIXME - this will work for Axis - not sure about others
-				contextToBundleMap.put(contextPath + "/services/" + context, bundle);
-				list.add(new ContextInfo(context, wsdlPath));
-			}
-
-			this.bundleToContextMap.put(bundle, new ArrayList(list));
-
-		} catch (Throwable t) {
-			t.printStackTrace();
-		}
-		return list;
-
-	}
-	
-	private void deployManagementServices(Bundle bundle, String contextName, String target){
-				
-		try {
-			provider.intializeContext(bundle, contextName);
-		} catch(Exception e){
-			e.printStackTrace();
-		}
-		
-		Collection contextPaths = getContextPathsForBundle(bundle, target);
-		Iterator i = contextPaths.iterator();
-		while (i.hasNext()) {
-			ContextInfo info = (ContextInfo)i.next();
-			String path = info.getContextPath();
-			String wsdl = info.getWsdlPath();
-			String trimmedPath = path;
-			if (trimmedPath.startsWith("/"))
-				trimmedPath = trimmedPath.substring(1);
-			try {
-				provider.deployManagementService(bundle, contextName, trimmedPath, wsdl);
-				BundleRootHelper.registerContext(bundle,contextName);
-			} catch (Exception e) {
-				// TODO requeue?
-				e.printStackTrace();
-			}
-		}
-	}
-	
-	
-	public void attachManagementProvider(ResourceManagementProvider provider) {
-		this.provider = provider;
-		synchronized(deferredResources){
-			Iterator i = deferredResources.keySet().iterator();
-			while(i.hasNext()){
-				Bundle bundle = (Bundle)i.next();
-				Boolean resource = (Boolean)deferredResources.get(bundle);
-				if(resource != null && resource.booleanValue()){
-					String target = (String) bundle.getHeaders().get(
-							OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
-					String contextPath = (String) bundle.getHeaders().get(
-							OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
-					deployManagementServices(bundle, contextPath, target);
-					deferredResources.put(bundle, Boolean.FALSE);
-				}
-			}
-		}
-	}
-
-	public void releaseManagementProvider() {
-		provider = null;
-	}
-	
-	
-	
-	protected void addResourceDefinitions(Bundle bundle, Collection definitions) {
-		if (definitions.isEmpty())
-			return;
-		synchronized (bundleToDefinitionMap) {
-			bundleToDefinitionMap.put(bundle, definitions);
-		}
-	}
-	
-	public Document getDocument(Bundle bundle, URL resourceConfig) {
-		try {
-			return XmlUtils.createDocument(resourceConfig.openStream());
-		}
-
-		catch (Exception error) {
-			error.printStackTrace();
-			return null;
-		}
-	}
-	
-	protected void deferBundleResources(Bundle bundle, String contextName,
-			String target) {
-		// Register the resource contexts with the soap provider
-		synchronized(deferredResources){
-			if (provider != null) {
-				if(Boolean.TRUE.equals(deferredResources.get(bundle))){
-					deployManagementServices(bundle, contextName, target);
-					deferredResources.put(bundle, Boolean.FALSE);
-				}
-			} else {
-				deferredResources.put(bundle, Boolean.TRUE);
-			}
-		}
-
-	}
-	
-	protected void addResourceDefinitions(Bundle bundle, URL resourceConfig) {
-
-		OSGiDeploymentDescriptor dd = new OSGiDeploymentDescriptor();
-
-		Document ddXML = getDocument(bundle, resourceConfig);
-		if (ddXML == null)
-			return;
-		try {
-			dd.setBundle(bundle);
-			dd.load(ddXML, this.getEnvironment());
-
-			//
-			// put all custom serializers in the serializer registry
-			//
-			Collection serializerDefinitions = dd.getSerializerDefinitions();
-			Iterator i = serializerDefinitions.iterator();
-
-			SerializerRegistry registry = SerializerRegistry.getInstance();
-
-			while (i.hasNext()) {
-				SerializerDefinition next = (SerializerDefinition) i.next();
-
-				Serializer ser = next.create();
-				Class type = ser.getSerializableType();
-
-				Class arrayType = ReflectUtils.getArrayClassFromClass(type);
-				Serializer arraySer = new ArraySerializer(arrayType, ser);
-
-				registry.registerSerializer(type, ser);
-				registry.registerSerializer(arrayType, arraySer);
-			}
-
-			//
-			// check for any specified router - this may be used
-			// as a delegate when trying to resolve a reference
-			// to a resource
-			//
-			RouterDefinition rd = dd.getRouterDefinition();
-			Class delegateRouter = rd.getRouterClass();
-			ResourceRouter delegate = null;
-			
-			//initialize persistence for router
-			PersistenceDefinition persistenceDef = rd.getPersistenceDefinition();
-			if(delegateRouter != null){
-				delegate = rd.newInstance();
-			
-				if(persistenceDef != null){
-		            RouterPersistence persistence = (RouterPersistence)persistenceDef.newInstance();
-		            delegate.setPersistence(persistence);
-				}				
-				delegate.initialize();
-			}
-
-			Collection resourceDefs = dd.getResourceDefinitions();
-
-			this.addResourceDefinitions(bundle, resourceDefs);
-
-			if(delegate != null){
-				delegateMap.put(bundle, delegate);
-				Iterator iter = resourceDefs.iterator();
-				while(iter.hasNext()){
-					ResourceDefinition def = (ResourceDefinition)iter.next();
-					String contextPath = (String) bundle.getHeaders().get(
-							OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
-					def.getContextPath();
-					contextToBundleMap.put(contextPath + "/services/" + def.getContextPath(), bundle);
-				}
-			}
-		} catch (Throwable t) {
-			// Log the error somewhere
-			t.printStackTrace();
-		}
-
-	}
-	
-	protected synchronized void registerBundleResources(Bundle bundle, String contextPath,
-			String target) {
-		OSGiEnvironment env = (OSGiEnvironment) this.getEnvironment();
-		if (env != null) {
-			URL resourceURL = bundle.getResource(target);
-			try {
-				env.setThreadLocalBundle(bundle);
-				OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(bundle);
-				addResourceDefinitions(bundle, resourceURL);
-			} catch (Throwable t) {
-				// TODO JOEL log this somewhere.
-				t.printStackTrace();
-			} finally {
-				((OSGiEnvironment) this.getEnvironment())
-						.setThreadLocalBundle(null);
-				OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(null);
-
-			}
-		} else {
-			deferBundleResources(bundle, contextPath, target);
-		}
-	}
-	
-	public void setDelegateBundle(Bundle bundle, String contextPath, String context){
-		synchronized(contextToBundleMap){
-			contextToBundleMap.put(contextPath + "/services/" + context, bundle);
-		}
-	}
-		
-	protected void removeBundleResources(Bundle bundle){
-		//FIXME cleanup
-	}
-	
-	public void bundleChanged(BundleEvent event) {
-		String target;
-		Bundle bundle;
-		switch (event.getType()) {
-		case BundleEvent.INSTALLED:
-			break;
-		case BundleEvent.RESOLVED:
-			// Check for SOA deployment information
-			target = (String) event.getBundle().getHeaders().get(
-					OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
-			String contextPath = (String) event.getBundle().getHeaders().get(
-					OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
-			bundle = event.getBundle();
-			if (target != null) {
-				if (this.getEnvironment() != null) {
-					URL resourceURL = bundle.getResource(target);
-					addResourceDefinitions(bundle, resourceURL);
-				} else {
-					registerBundleResources(bundle, contextPath, target);
-				}
-			}
-			break;
-		case BundleEvent.STARTING:
-			break;
-		case BundleEvent.STARTED:
-			// Check for SOA deployment information
-			target = (String) event.getBundle().getHeaders().get(
-					OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
-			contextPath = (String) event.getBundle().getHeaders().get(
-					OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
-			bundle = event.getBundle();
-			if (target != null) {
-				// process SOA artifacts - expecting a set of comma-delimited
-				// names.
-				// The lookup process is as follows - look in configuration
-				// repository,
-				// then look in bundle classpath
-				if (getEnvironment() != null) {
-					((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(event.getBundle());
-					URL resourceURL = bundle.getResource(target);
-					addResourceDefinitions(bundle, resourceURL);
-					((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(null);
-				} else {
-					registerBundleResources(bundle, contextPath, target);
-				}
-			}
-			break;
-		case BundleEvent.STOPPING:
-			break;
-		case BundleEvent.STOPPED:
-			removeBundleResources(event.getBundle());
-			break;
-		case BundleEvent.UNINSTALLED:
-			break;
-		case BundleEvent.UNRESOLVED:
-			break;
-		case BundleEvent.UPDATED:
-			break;
-		default:
-			break;
-		}
-	}
-	
-
-}
+/* 
+ * 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.muse.core.platform.osgi.routing;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import org.apache.muse.core.Environment;
+import org.apache.muse.core.Resource;
+import org.apache.muse.core.ResourceManager;
+import org.apache.muse.core.descriptor.PersistenceDefinition;
+import org.apache.muse.core.descriptor.ResourceDefinition;
+import org.apache.muse.core.descriptor.RouterDefinition;
+import org.apache.muse.core.descriptor.SerializerDefinition;
+import org.apache.muse.core.descriptor.WsdlConfig;
+import org.apache.muse.core.platform.osgi.internal.OSGiResourceManager;
+import org.apache.muse.core.platform.osgi.OSGiEnvironment;
+import org.apache.muse.core.platform.osgi.OSGiEnvironmentImpl;
+import org.apache.muse.core.platform.osgi.OSGiPlatformConstants;
+import org.apache.muse.core.platform.osgi.ResourceManagementProvider;
+import org.apache.muse.core.platform.osgi.descriptor.OSGiDeploymentDescriptor;
+import org.apache.muse.core.platform.osgi.util.BundleRootHelper;
+import org.apache.muse.core.platform.osgi.util.OSGiReflectUtilHelper;
+import org.apache.muse.core.routing.ResourceRouter;
+import org.apache.muse.core.routing.RouterPersistence;
+import org.apache.muse.core.routing.SimpleResourceRouter;
+import org.apache.muse.core.serializer.ArraySerializer;
+import org.apache.muse.core.serializer.Serializer;
+import org.apache.muse.core.serializer.SerializerRegistry;
+import org.apache.muse.util.ReflectUtils;
+import org.apache.muse.util.xml.XmlUtils;
+import org.apache.muse.ws.addressing.EndpointReference;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ *
+ * OSGiResourceRouter is an extension of the SimpleResourceRouter that supports
+ * a delegation model for additional Muse-enabled Bundles. This class is intended 
+ * for use by <code>ResourceManagementProvider</code> implementations. It is not 
+ * intended for use by normal Muse-enabled bundles.
+ *  
+ * @see ResourceManagementProvider
+ * @author Joel Hawkins (joelh)
+ *
+ */
+
+public class OSGiResourceRouter extends SimpleResourceRouter implements BundleListener{
+	
+	private static HashMap bundleToDefinitionMap = new HashMap();
+	private static HashMap delegateMap = new HashMap();
+	private static HashMap deferredResources = new HashMap();
+	private static HashMap bundleToContextMap = new HashMap();
+	private static HashMap contextToBundleMap = new HashMap();
+	
+	private static ResourceManagementProvider provider;
+	
+	private static OSGiResourceRouter _router = new OSGiResourceRouter();
+
+	public static OSGiResourceRouter getDefault(){ return _router;}
+	
+	protected ResourceManager createResourceManager() {
+		OSGiResourceManager manager = new OSGiResourceManager();
+		return manager;
+	}
+	
+	public Bundle getDelegateBundle(){
+        Environment env = getEnvironment();
+        EndpointReference epr = env.getAddressingContext().getToAddress();
+        String address = epr.getAddress().toString();
+        
+        Iterator keys = contextToBundleMap.keySet().iterator();
+        while(keys.hasNext()){
+        	String currentKey = (String)keys.next();
+        	if(address.endsWith(currentKey)){
+        		return (Bundle)contextToBundleMap.get(currentKey);
+        	}
+        }
+        
+        return null;
+	}
+
+
+	public Element invoke(Element soapBody){
+		Resource resource = null;
+		try {
+			Bundle bundle = getDelegateBundle();			
+			ResourceRouter delegateRouter = null;
+			if(bundle != null){
+				delegateRouter = (ResourceRouter)delegateMap.get(bundle);
+			}
+	        ((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(bundle);
+		return delegateRouter.invoke(soapBody);
+        } finally {
+            ((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(null);
+        }
+	}
+	
+	
+	public void setEnvironment(Environment environment) {
+		super.setEnvironment(environment);
+		//set the environment of all the delegates
+		if (!deferredResources.isEmpty()) {
+			synchronized (deferredResources) {
+				Iterator iter = deferredResources.keySet().iterator();
+				while (iter.hasNext()) {
+					Bundle bundle = (Bundle) iter.next();
+					try {
+						String target = (String) bundle.getHeaders().get(
+								OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
+						URL resourceURL = bundle.getResource(target);
+						((OSGiEnvironment) this.getEnvironment())
+								.setThreadLocalBundle(bundle);
+						OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(bundle);
+						addResourceDefinitions(bundle, resourceURL);
+					} catch (Throwable t) {
+						t.printStackTrace();
+					} finally {
+						((OSGiEnvironment) this.getEnvironment())
+								.setThreadLocalBundle(null);
+						OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(null);
+					}
+				}
+				//TODO I think we just set these to FALSE
+				deferredResources.clear();
+			}
+		}
+	}
+	
+	
+	public void initialize(BundleContext context) {
+		context.addBundleListener(this);
+		Bundle bundles[] = context.getBundles();
+		OSGiEnvironment env = new OSGiEnvironmentImpl(false);
+		for (int i = 0; i < bundles.length; i++) {
+			switch (bundles[i].getState()) {
+			case Bundle.ACTIVE:
+			case Bundle.RESOLVED:
+			case Bundle.STARTING:
+				String target = (String) bundles[i].getHeaders().get(
+						OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
+				String contextPath = (String) bundles[i].getHeaders().get(
+						OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
+				if (target != null) {
+					registerBundleResources(bundles[i], contextPath, target);
+				}
+				break;
+			default:
+				break;
+			}
+		}
+	}
+	
+	private class ContextInfo {
+		public ContextInfo(String contextPath, String wsdlPath){
+			this.contextPath = contextPath;
+			this.wsdlPath = wsdlPath;
+		}
+		
+		public String getContextPath(){
+			return contextPath;
+		}
+		
+		private String getWsdlPath(){
+			return wsdlPath;
+		}
+		
+		private String contextPath;
+		private String wsdlPath;
+	}
+	
+	private Collection getContextPathsForBundle(Bundle bundle, String target) {
+		ArrayList list = new ArrayList();
+		OSGiEnvironment env = new OSGiEnvironmentImpl(false);
+		env.setThreadLocalBundle(bundle);
+		OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(bundle);
+		try {
+			Document ddXML = env.getDocument(target);
+			OSGiDeploymentDescriptor dd = new OSGiDeploymentDescriptor();
+			Collection resourceDefs = dd.loadContextPaths(ddXML, env);
+			Iterator paths = resourceDefs.iterator();
+			
+			String contextPath = (String) bundle.getHeaders().get(
+					OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
+			while (paths.hasNext()) {
+				String context = (String) paths.next(); 
+				String wsdlPath = dd.getWsdlPathForContext(ddXML, context);
+				//FIXME - this will work for Axis - not sure about others
+				contextToBundleMap.put(contextPath + "/services/" + context, bundle);
+				list.add(new ContextInfo(context, wsdlPath));
+			}
+
+			this.bundleToContextMap.put(bundle, new ArrayList(list));
+
+		} catch (Throwable t) {
+			t.printStackTrace();
+		}
+		return list;
+
+	}
+	
+	private void deployManagementServices(Bundle bundle, String contextName, String target){
+				
+		try {
+			provider.intializeContext(bundle, contextName);
+		} catch(Exception e){
+			e.printStackTrace();
+		}
+		
+		Collection contextPaths = getContextPathsForBundle(bundle, target);
+		Iterator i = contextPaths.iterator();
+		while (i.hasNext()) {
+			ContextInfo info = (ContextInfo)i.next();
+			String path = info.getContextPath();
+			String wsdl = info.getWsdlPath();
+			String trimmedPath = path;
+			if (trimmedPath.startsWith("/"))
+				trimmedPath = trimmedPath.substring(1);
+			try {
+				provider.deployManagementService(bundle, contextName, trimmedPath, wsdl);
+				BundleRootHelper.registerContext(bundle,contextName);
+			} catch (Exception e) {
+				// TODO requeue?
+				e.printStackTrace();
+			}
+		}
+	}
+	
+	
+	public void attachManagementProvider(ResourceManagementProvider provider) {
+		this.provider = provider;
+		synchronized(deferredResources){
+			Iterator i = deferredResources.keySet().iterator();
+			while(i.hasNext()){
+				Bundle bundle = (Bundle)i.next();
+				Boolean resource = (Boolean)deferredResources.get(bundle);
+				if(resource != null && resource.booleanValue()){
+					String target = (String) bundle.getHeaders().get(
+							OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
+					String contextPath = (String) bundle.getHeaders().get(
+							OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
+					deployManagementServices(bundle, contextPath, target);
+					deferredResources.put(bundle, Boolean.FALSE);
+				}
+			}
+		}
+	}
+
+	public void releaseManagementProvider() {
+		provider = null;
+	}
+	
+	
+	
+	protected void addResourceDefinitions(Bundle bundle, Collection definitions) {
+		if (definitions.isEmpty())
+			return;
+		synchronized (bundleToDefinitionMap) {
+			bundleToDefinitionMap.put(bundle, definitions);
+		}
+	}
+	
+	public Document getDocument(Bundle bundle, URL resourceConfig) {
+		try {
+			return XmlUtils.createDocument(resourceConfig.openStream());
+		}
+
+		catch (Exception error) {
+			error.printStackTrace();
+			return null;
+		}
+	}
+	
+	protected void deferBundleResources(Bundle bundle, String contextName,
+			String target) {
+		// Register the resource contexts with the soap provider
+		synchronized(deferredResources){
+			if (provider != null) {
+				if(Boolean.TRUE.equals(deferredResources.get(bundle))){
+					deployManagementServices(bundle, contextName, target);
+					deferredResources.put(bundle, Boolean.FALSE);
+				}
+			} else {
+				deferredResources.put(bundle, Boolean.TRUE);
+			}
+		}
+
+	}
+	
+	protected void addResourceDefinitions(Bundle bundle, URL resourceConfig) {
+
+		OSGiDeploymentDescriptor dd = new OSGiDeploymentDescriptor();
+
+		Document ddXML = getDocument(bundle, resourceConfig);
+		if (ddXML == null)
+			return;
+		try {
+			dd.setBundle(bundle);
+			dd.load(ddXML, this.getEnvironment());
+
+			//
+			// put all custom serializers in the serializer registry
+			//
+			Collection serializerDefinitions = dd.getSerializerDefinitions();
+			Iterator i = serializerDefinitions.iterator();
+
+			SerializerRegistry registry = SerializerRegistry.getInstance();
+
+			while (i.hasNext()) {
+				SerializerDefinition next = (SerializerDefinition) i.next();
+
+				Serializer ser = next.create();
+				Class type = ser.getSerializableType();
+
+				Class arrayType = ReflectUtils.getArrayClassFromClass(type);
+				Serializer arraySer = new ArraySerializer(arrayType, ser);
+
+				registry.registerSerializer(type, ser);
+				registry.registerSerializer(arrayType, arraySer);
+			}
+
+			//
+			// check for any specified router - this may be used
+			// as a delegate when trying to resolve a reference
+			// to a resource
+			//
+			RouterDefinition rd = dd.getRouterDefinition();
+			Class delegateRouter = rd.getRouterClass();
+			ResourceRouter delegate = null;
+			
+			//initialize persistence for router
+			PersistenceDefinition persistenceDef = rd.getPersistenceDefinition();
+			if(delegateRouter != null){
+				delegate = rd.newInstance();
+			
+				if(persistenceDef != null){
+		            RouterPersistence persistence = (RouterPersistence)persistenceDef.newInstance();
+		            delegate.setPersistence(persistence);
+				}				
+				delegate.initialize();
+			}
+
+			Collection resourceDefs = dd.getResourceDefinitions();
+
+			this.addResourceDefinitions(bundle, resourceDefs);
+
+			if(delegate != null){
+				delegateMap.put(bundle, delegate);
+				Iterator iter = resourceDefs.iterator();
+				while(iter.hasNext()){
+					ResourceDefinition def = (ResourceDefinition)iter.next();
+					String contextPath = (String) bundle.getHeaders().get(
+							OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
+					def.getContextPath();
+					contextToBundleMap.put(contextPath + "/services/" + def.getContextPath(), bundle);
+				}
+			}
+		} catch (Throwable t) {
+			// Log the error somewhere
+			t.printStackTrace();
+		}
+
+	}
+	
+	protected synchronized void registerBundleResources(Bundle bundle, String contextPath,
+			String target) {
+		OSGiEnvironment env = (OSGiEnvironment) this.getEnvironment();
+		if (env != null) {
+			URL resourceURL = bundle.getResource(target);
+			try {
+				env.setThreadLocalBundle(bundle);
+				OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(bundle);
+				addResourceDefinitions(bundle, resourceURL);
+			} catch (Throwable t) {
+				// TODO JOEL log this somewhere.
+				t.printStackTrace();
+			} finally {
+				((OSGiEnvironment) this.getEnvironment())
+						.setThreadLocalBundle(null);
+				OSGiReflectUtilHelper.getDefault().setThreadLocalBundle(null);
+
+			}
+		} else {
+			deferBundleResources(bundle, contextPath, target);
+		}
+	}
+	
+	public void setDelegateBundle(Bundle bundle, String contextPath, String context){
+		synchronized(contextToBundleMap){
+			contextToBundleMap.put(contextPath + "/services/" + context, bundle);
+		}
+	}
+		
+	protected void removeBundleResources(Bundle bundle){
+		//FIXME cleanup
+	}
+	
+	public void bundleChanged(BundleEvent event) {
+		String target;
+		Bundle bundle;
+		switch (event.getType()) {
+		case BundleEvent.INSTALLED:
+			break;
+		case BundleEvent.RESOLVED:
+			// Check for SOA deployment information
+			target = (String) event.getBundle().getHeaders().get(
+					OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
+			String contextPath = (String) event.getBundle().getHeaders().get(
+					OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
+			bundle = event.getBundle();
+			if (target != null) {
+				if (this.getEnvironment() != null) {
+					URL resourceURL = bundle.getResource(target);
+					addResourceDefinitions(bundle, resourceURL);
+				} else {
+					registerBundleResources(bundle, contextPath, target);
+				}
+			}
+			break;
+		case BundleEvent.STARTING:
+			break;
+		case BundleEvent.STARTED:
+			// Check for SOA deployment information
+			target = (String) event.getBundle().getHeaders().get(
+					OSGiPlatformConstants.MANAGEMENT_MANIFEST_HEADER);
+			contextPath = (String) event.getBundle().getHeaders().get(
+					OSGiPlatformConstants.MANAGEMENT_CONTEXT_HEADER);
+			bundle = event.getBundle();
+			if (target != null) {
+				// process SOA artifacts - expecting a set of comma-delimited
+				// names.
+				// The lookup process is as follows - look in configuration
+				// repository,
+				// then look in bundle classpath
+				if (getEnvironment() != null) {
+					((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(event.getBundle());
+					URL resourceURL = bundle.getResource(target);
+					addResourceDefinitions(bundle, resourceURL);
+					((OSGiEnvironment)getEnvironment()).setThreadLocalBundle(null);
+				} else {
+					registerBundleResources(bundle, contextPath, target);
+				}
+			}
+			break;
+		case BundleEvent.STOPPING:
+			break;
+		case BundleEvent.STOPPED:
+			removeBundleResources(event.getBundle());
+			break;
+		case BundleEvent.UNINSTALLED:
+			break;
+		case BundleEvent.UNRESOLVED:
+			break;
+		case BundleEvent.UPDATED:
+			break;
+		default:
+			break;
+		}
+	}
+	
+
+}

Modified: webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/ResourceRouterDelegate.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/ResourceRouterDelegate.java?view=diff&rev=522016&r1=522015&r2=522016
==============================================================================
--- webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/ResourceRouterDelegate.java (original)
+++ webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/routing/ResourceRouterDelegate.java Sat Mar 24 06:10:47 2007
@@ -1,41 +1,44 @@
-/*=============================================================================*
- *  Copyright 2006 The Apache Software Foundation
- *
- *  Licensed 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.muse.core.platform.osgi.routing;
-
-import org.apache.muse.core.Environment;
-import org.apache.muse.core.Resource;
-import org.apache.muse.core.ResourceManager;
-import org.apache.muse.core.routing.ResourceRouter;
-import org.apache.muse.ws.addressing.EndpointReference;
-import org.apache.muse.ws.addressing.soap.SoapFault;
-import org.apache.muse.core.platform.osgi.EnvironmentChangeListener;
-
-/**
- *
- * ResourceRouterDelegate is ...
- *
- * @author Joel Hawkins (joelh)
- *
- */
-
-public interface ResourceRouterDelegate {
-	Resource getResource(EndpointReference epr) throws SoapFault;
-	void initialize(ResourceManager manager, ResourceRouter parent);
-	void addEnvironmentChangeListener(EnvironmentChangeListener listener);
-	public void setEnvironment(Environment environment);
-}
+/* 
+ * 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.muse.core.platform.osgi.routing;
+
+import org.apache.muse.core.Environment;
+import org.apache.muse.core.Resource;
+import org.apache.muse.core.ResourceManager;
+import org.apache.muse.core.routing.ResourceRouter;
+import org.apache.muse.ws.addressing.EndpointReference;
+import org.apache.muse.ws.addressing.soap.SoapFault;
+import org.apache.muse.core.platform.osgi.EnvironmentChangeListener;
+
+/**
+ *
+ * ResourceRouterDelegate is ...
+ *
+ * @author Joel Hawkins (joelh)
+ *
+ */
+
+public interface ResourceRouterDelegate {
+	Resource getResource(EndpointReference epr) throws SoapFault;
+	void initialize(ResourceManager manager, ResourceRouter parent);
+	void addEnvironmentChangeListener(EnvironmentChangeListener listener);
+	public void setEnvironment(Environment environment);
+}

Modified: webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/BundleRootHelper.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/BundleRootHelper.java?view=diff&rev=522016&r1=522015&r2=522016
==============================================================================
--- webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/BundleRootHelper.java (original)
+++ webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/BundleRootHelper.java Sat Mar 24 06:10:47 2007
@@ -1,81 +1,85 @@
-/*=============================================================================*
- *  Copyright 2006 The Apache Software Foundation
- *
- *  Licensed 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.muse.core.platform.osgi.util;
-
-import java.io.File;
-import java.util.HashMap;
-import org.osgi.framework.Bundle;
-
-/**
- * BundleRootHelper allows bundles to specify a file system root for use
- * in loading persistence data, etc. 
- * 
- * @author Joel Hawkins (joelh)
- *
- */
-public class BundleRootHelper {
-
-	private static HashMap bundleToRootMap = new HashMap();
-	private static HashMap bundleToContextMap = new HashMap();
-	
-	/**
-	 * registers a File System root for a particular bundle
-	 * @param bundle
-	 * @param root
-	 */
-	public static void registerRoot(Bundle bundle, File root){
-		synchronized(bundleToRootMap){
-			bundleToRootMap.put(bundle, root);
-		}
-	}
-	
-	/**
-	 * registers a Muse context (from the manifest)for a particular bundle
-	 * @param bundle
-	 * @param root
-	 */
-	public static void registerContext(Bundle bundle, String context){
-		synchronized(bundleToContextMap){
-			bundleToContextMap.put(bundle, context);
-		}
-	}
-
-	/**
-	 * returns the registerd file system root 
-	 * @param bundle
-	 * @return the root File registered for the specified bundle, or null if the 
-	 * bundle did not register a file system root
-	 * @see org.apache.muse.core.platform.osgi.axis2.Axis2Environment#getRealDirectory()
-	 */
-	public static File getRootForBundle(Bundle bundle){
-		synchronized(bundleToRootMap){
-			return (File) bundleToRootMap.get(bundle);
-		}
-	}
-
-	/**
-	 * returns the registerd file system root 
-	 * @param bundle
-	 * @return the context registered for the specified bundle, or null if the 
-	 * bundle did not register a context
-	 */
-	public static String getContextForBundle(Bundle bundle){
-		synchronized(bundleToContextMap){
-			return (String) bundleToContextMap.get(bundle);
-		}
-	}
-}
+/* 
+ * 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.muse.core.platform.osgi.util;
+
+import java.io.File;
+import java.util.HashMap;
+import org.osgi.framework.Bundle;
+
+/**
+ * BundleRootHelper allows bundles to specify a file system root for use
+ * in loading persistence data, etc. 
+ * 
+ * @author Joel Hawkins (joelh)
+ *
+ */
+public class BundleRootHelper {
+
+	private static HashMap bundleToRootMap = new HashMap();
+	private static HashMap bundleToContextMap = new HashMap();
+	
+	/**
+	 * registers a File System root for a particular bundle
+	 * @param bundle
+	 * @param root
+	 */
+	public static void registerRoot(Bundle bundle, File root){
+		synchronized(bundleToRootMap){
+			bundleToRootMap.put(bundle, root);
+		}
+	}
+	
+	/**
+	 * registers a Muse context (from the manifest)for a particular bundle
+	 * @param bundle
+	 * @param root
+	 */
+	public static void registerContext(Bundle bundle, String context){
+		synchronized(bundleToContextMap){
+			bundleToContextMap.put(bundle, context);
+		}
+	}
+
+	/**
+	 * returns the registerd file system root 
+	 * @param bundle
+	 * @return the root File registered for the specified bundle, or null if the 
+	 * bundle did not register a file system root
+	 * @see org.apache.muse.core.platform.osgi.axis2.Axis2Environment#getRealDirectory()
+	 */
+	public static File getRootForBundle(Bundle bundle){
+		synchronized(bundleToRootMap){
+			return (File) bundleToRootMap.get(bundle);
+		}
+	}
+
+	/**
+	 * returns the registerd file system root 
+	 * @param bundle
+	 * @return the context registered for the specified bundle, or null if the 
+	 * bundle did not register a context
+	 */
+	public static String getContextForBundle(Bundle bundle){
+		synchronized(bundleToContextMap){
+			return (String) bundleToContextMap.get(bundle);
+		}
+	}
+}

Modified: webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/OSGiReflectUtilHelper.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/OSGiReflectUtilHelper.java?view=diff&rev=522016&r1=522015&r2=522016
==============================================================================
--- webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/OSGiReflectUtilHelper.java (original)
+++ webservices/muse/trunk/modules/muse-osgi-core/src/org/apache/muse/core/platform/osgi/util/OSGiReflectUtilHelper.java Sat Mar 24 06:10:47 2007
@@ -1,294 +1,297 @@
-/*=============================================================================*
- *  Copyright 2006 The Apache Software Foundation
- *
- *  Licensed 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.muse.core.platform.osgi.util;
-
-import java.io.InputStream;
-import java.net.URL;
-import java.util.HashMap;
-
-import org.apache.muse.util.ReflectUtilHelper;
-import org.apache.muse.util.osgi.CapabilityPair;
-import org.apache.muse.util.osgi.ResourceManagementImplementation;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
-import org.osgi.framework.ServiceReference;
-
-/**
- *
- * OSGiReflectUtilHelper is an implementation of the <code>ReflectUtilHelper</code> interface,
- * which allows platforms to extend the behavior of the ReflectUtils class.
- *
- * Additonally, this class acts as a repository for Resource and Capability implementations
- * that may be shared between bundles - simplifying the management of bundle dependencies when
- * using default capability and resource implementations provide by Muse.
- *
- * The repository is initialized by locating instance of ResourceManagementImplementation services
- * exported by bundles with contributions to make to the helper class. Most of the muse-*-impl or
- * muse-*-api bundle activators are contribution providers.
- *
- * @see ReflectUtilHelper
- * @see org.apache.muse.util.ReflectUtils
- * @see org.apache.muse.util.osgi.ResourceManagementImplementation
- * @author Joel Hawkins (joelh)
- *
- */
-
-public class OSGiReflectUtilHelper implements ReflectUtilHelper, ServiceListener {
-
-	private ThreadLocal localEnvironmentContext = new ThreadLocal();
-
-	private static OSGiReflectUtilHelper _helper = new OSGiReflectUtilHelper();
-
-	private BundleContext context;
-
-	private HashMap contributorMap = new HashMap(); //Maps impl name to service
-	private HashMap serviceMap = new HashMap(); //Maps reference to impl name
-
-	//Map contributions to bundles
-	private HashMap spec2BundleMap = new HashMap();
-	private HashMap resourceImpl2BundleMap = new HashMap();
-	private HashMap capabilityImpl2BundleMap = new HashMap();
-
-	private static final String MANAGEMENT_IMPLEMENTATION_FILTER = "(objectclass=org.apache.muse.util.osgi.ResourceManagementImplementation)";
-
-	private static class CapabilityEntry {
-
-		public CapabilityEntry(String uri, String implName, Bundle bundle){
-			this.uri = uri;
-			this.implName = implName;
-			this.bundle = bundle;
-		}
-
-		public String uri;
-		public String implName;
-		public Bundle bundle;
-	}
-
-	/**
-	 * setter for the thread local bundle for the executing thread. This bundle will be
-	 * used for reflection requests delagated to this helper by the executing thread.
-	 * @param bundle
-	 */
-	public void setThreadLocalBundle(Bundle bundle) {
-		localEnvironmentContext.set(bundle);
-	}
-
-	/**
-	 * getter for the thread local bundle of the executing thread.
-	 * @return the thread local Bundle for the executing thread.
-	 */
-	public Bundle getThreadLocalBundle() {
-		return (Bundle)localEnvironmentContext.get();
-	}
-
-	/**
-	 * access to the singleton instance of the OSGiReflectUtilHelper
-	 * @return
-	 */
-	public static OSGiReflectUtilHelper getDefault() {
-		return _helper;
-	}
-
-	/**
-	 * initializes the helper class by locating all instances of <code>ResourceManagementImplementation</code>
-	 * services and loading their contributions into the helper class.
-	 * @param context
-	 */
-	public void initialize(BundleContext context){
-		this.context = context;
-		try {
-			context.addServiceListener(this,MANAGEMENT_IMPLEMENTATION_FILTER);
-			ServiceReference[] refs = context.getServiceReferences("org.apache.muse.util.osgi.ResourceManagementImplementation", MANAGEMENT_IMPLEMENTATION_FILTER);
-			if(refs != null){
-				for(int i=0;i<refs.length;i++){
-					ResourceManagementImplementation impl = (ResourceManagementImplementation)context.getService(refs[i]);
-					if(impl != null) loadContributions(impl);
-				}
-			}
-		} catch (InvalidSyntaxException e) {
-			//This should never happen as the filter is not dynamic
-		}
-	}
-
-	private OSGiReflectUtilHelper() {
-	}
-
-	/**
-	 * existence check for a class
-	 * @param className the name of the class in question
-	 * return <code>true</code> if the class exists in the executing thread's bundle context,
-	 * otherwise <code>false</code>
-	 */
-	public boolean exists(String className) {
-		Bundle bundle = (Bundle) localEnvironmentContext.get();
-		if (bundle == null)
-			return false;
-		try {
-			bundle.loadClass(className);
-			return true;
-		} catch (Throwable t) {
-			try{
-				OSGiReflectUtilHelper.class.getClassLoader().loadClass(className);
-				return true;
-			} catch(Throwable cnfe){
-				return false;
-			}
-		}
-	}
-
-	private Class getClassFromContributions(String className){
-		Bundle contributionBundle = null;
-		contributionBundle = (Bundle) resourceImpl2BundleMap.get(className);
-		if(contributionBundle == null){
-			CapabilityEntry entry = (CapabilityEntry)capabilityImpl2BundleMap.get(className);
-			if(entry != null) contributionBundle = entry.bundle;
-		}
-		if(contributionBundle == null) return null;
-		try{
-			return contributionBundle.loadClass(className);
-		}catch(ClassNotFoundException cnfe){
-			return null;
-		}
-		catch(Throwable t){
-			return null;
-		}
-	}
-
-	/**
-	 * attempts to load a Class from the executing thread's bundle context.
-	 * If the class is not found, an attempt is made to resolve the class based
-	 * on contributions made by other bundes.
-	 *
-	 * @param className the name of the class in question
-	 * @return the <code>Class</code> if the class exists in the executing thread's bundle context,
-	 * otherwise <code>null</code>
-	 */
-	public Class getClass(String className) {
-		Bundle bundle = (Bundle) localEnvironmentContext.get();
-		if (bundle == null)
-			return getClassFromContributions(className);
-		try {
-			return bundle.loadClass(className);
-		} catch (Throwable t) {
-			try{
-				return OSGiReflectUtilHelper.class.getClassLoader().loadClass(className);
-			} catch(Throwable cnfe){
-				return getClassFromContributions(className);
-			}
-		}
-	}
-
-	/**
-	 * attempts to resolve a resource from contributions made by other bundles. These resources
-	 * are typically wsdl or schema files.
-	 * @param name
-	 * @return
-	 */
-	public InputStream getResource(String name){
-		//Check against contributions -
-		Bundle bundle = null;
-		String trimmedName = name;
-		synchronized(spec2BundleMap){
-			bundle = (Bundle)spec2BundleMap.get(name);
-			if(bundle == null && name.lastIndexOf("/") != -1){
-				trimmedName = name.substring(name.lastIndexOf("/")+1);
-				bundle = (Bundle)spec2BundleMap.get(trimmedName);
-			}
-		}
-		if(bundle != null){
-			try{
-				URL url = bundle.getResource("/" + trimmedName);
-				if(url != null) return url.openStream();
-			}catch(Throwable t){
-				//fall through
-			}
-		}
-		return null;
-	}
-
-	private void loadContributions(ResourceManagementImplementation impl) {
-
-		Bundle bundle = impl.getBundle();
-
-		String implName = impl.getImplementationName();
-
-		String[] resourceImplNames = impl.getResourceImplementationNames();
-		String[] specNames = impl.getResourceSpecNames();
-		CapabilityPair capabilities[] = impl.getResourceCapabilityMappings();
-		int i;
-
-		if(resourceImplNames != null){
-			synchronized(resourceImpl2BundleMap){
-				for(i=0;i<resourceImplNames.length;i++){
-					resourceImpl2BundleMap.put(resourceImplNames[i], bundle);
-				}
-			}
-		}
-		if(specNames != null){
-			synchronized(spec2BundleMap){
-				for(i=0;i<specNames.length;i++){
-					spec2BundleMap.put(specNames[i], bundle);
-				}
-			}
-		}
-		if(capabilities != null){
-			synchronized(capabilityImpl2BundleMap){
-				for(i=0;i<capabilities.length;i++){
-					String uri = (String)capabilities[i].capabilityURI;
-					String capImpl = (String)capabilities[i].capabilityImplementation;
-					CapabilityEntry entry = new CapabilityEntry(uri,capImpl,bundle);
-					capabilityImpl2BundleMap.put(capImpl, entry);
-				}
-			}
-		}
-	}
-
-
-	/**
-	 * ServiceListener method for dealing with ResourceManagementImplementation
-	 * service lifecycle events.
-	 */
-	public synchronized void serviceChanged(ServiceEvent event) {
-		ServiceReference ref = event.getServiceReference();
-		ResourceManagementImplementation impl = (ResourceManagementImplementation)context.getService(ref);
-		String name = impl.getImplementationName();
-		switch (event.getType()) {
-		case ServiceEvent.REGISTERED:
-			if(contributorMap.get(name) == null){
-				contributorMap.put(name, impl);
-				serviceMap.put(ref, name);
-				loadContributions(impl);
-			}
-			break;
-		case ServiceEvent.MODIFIED:
-			break;
-		case ServiceEvent.UNREGISTERING:
-			if (serviceMap.get(ref) != null) {
-				contributorMap.remove(serviceMap.get(ref));
-				serviceMap.remove(ref);
-				//TODO clean up contributions
-			}
-			break;
-		}
-
-	}
-
-}
+/* 
+ * 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.muse.core.platform.osgi.util;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+
+import org.apache.muse.util.ReflectUtilHelper;
+import org.apache.muse.util.osgi.CapabilityPair;
+import org.apache.muse.util.osgi.ResourceManagementImplementation;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ * OSGiReflectUtilHelper is an implementation of the <code>ReflectUtilHelper</code> interface,
+ * which allows platforms to extend the behavior of the ReflectUtils class.
+ *
+ * Additonally, this class acts as a repository for Resource and Capability implementations
+ * that may be shared between bundles - simplifying the management of bundle dependencies when
+ * using default capability and resource implementations provide by Muse.
+ *
+ * The repository is initialized by locating instance of ResourceManagementImplementation services
+ * exported by bundles with contributions to make to the helper class. Most of the muse-*-impl or
+ * muse-*-api bundle activators are contribution providers.
+ *
+ * @see ReflectUtilHelper
+ * @see org.apache.muse.util.ReflectUtils
+ * @see org.apache.muse.util.osgi.ResourceManagementImplementation
+ * @author Joel Hawkins (joelh)
+ *
+ */
+
+public class OSGiReflectUtilHelper implements ReflectUtilHelper, ServiceListener {
+
+	private ThreadLocal localEnvironmentContext = new ThreadLocal();
+
+	private static OSGiReflectUtilHelper _helper = new OSGiReflectUtilHelper();
+
+	private BundleContext context;
+
+	private HashMap contributorMap = new HashMap(); //Maps impl name to service
+	private HashMap serviceMap = new HashMap(); //Maps reference to impl name
+
+	//Map contributions to bundles
+	private HashMap spec2BundleMap = new HashMap();
+	private HashMap resourceImpl2BundleMap = new HashMap();
+	private HashMap capabilityImpl2BundleMap = new HashMap();
+
+	private static final String MANAGEMENT_IMPLEMENTATION_FILTER = "(objectclass=org.apache.muse.util.osgi.ResourceManagementImplementation)";
+
+	private static class CapabilityEntry {
+
+		public CapabilityEntry(String uri, String implName, Bundle bundle){
+			this.uri = uri;
+			this.implName = implName;
+			this.bundle = bundle;
+		}
+
+		public String uri;
+		public String implName;
+		public Bundle bundle;
+	}
+
+	/**
+	 * setter for the thread local bundle for the executing thread. This bundle will be
+	 * used for reflection requests delagated to this helper by the executing thread.
+	 * @param bundle
+	 */
+	public void setThreadLocalBundle(Bundle bundle) {
+		localEnvironmentContext.set(bundle);
+	}
+
+	/**
+	 * getter for the thread local bundle of the executing thread.
+	 * @return the thread local Bundle for the executing thread.
+	 */
+	public Bundle getThreadLocalBundle() {
+		return (Bundle)localEnvironmentContext.get();
+	}
+
+	/**
+	 * access to the singleton instance of the OSGiReflectUtilHelper
+	 * @return
+	 */
+	public static OSGiReflectUtilHelper getDefault() {
+		return _helper;
+	}
+
+	/**
+	 * initializes the helper class by locating all instances of <code>ResourceManagementImplementation</code>
+	 * services and loading their contributions into the helper class.
+	 * @param context
+	 */
+	public void initialize(BundleContext context){
+		this.context = context;
+		try {
+			context.addServiceListener(this,MANAGEMENT_IMPLEMENTATION_FILTER);
+			ServiceReference[] refs = context.getServiceReferences("org.apache.muse.util.osgi.ResourceManagementImplementation", MANAGEMENT_IMPLEMENTATION_FILTER);
+			if(refs != null){
+				for(int i=0;i<refs.length;i++){
+					ResourceManagementImplementation impl = (ResourceManagementImplementation)context.getService(refs[i]);
+					if(impl != null) loadContributions(impl);
+				}
+			}
+		} catch (InvalidSyntaxException e) {
+			//This should never happen as the filter is not dynamic
+		}
+	}
+
+	private OSGiReflectUtilHelper() {
+	}
+
+	/**
+	 * existence check for a class
+	 * @param className the name of the class in question
+	 * return <code>true</code> if the class exists in the executing thread's bundle context,
+	 * otherwise <code>false</code>
+	 */
+	public boolean exists(String className) {
+		Bundle bundle = (Bundle) localEnvironmentContext.get();
+		if (bundle == null)
+			return false;
+		try {
+			bundle.loadClass(className);
+			return true;
+		} catch (Throwable t) {
+			try{
+				OSGiReflectUtilHelper.class.getClassLoader().loadClass(className);
+				return true;
+			} catch(Throwable cnfe){
+				return false;
+			}
+		}
+	}
+
+	private Class getClassFromContributions(String className){
+		Bundle contributionBundle = null;
+		contributionBundle = (Bundle) resourceImpl2BundleMap.get(className);
+		if(contributionBundle == null){
+			CapabilityEntry entry = (CapabilityEntry)capabilityImpl2BundleMap.get(className);
+			if(entry != null) contributionBundle = entry.bundle;
+		}
+		if(contributionBundle == null) return null;
+		try{
+			return contributionBundle.loadClass(className);
+		}catch(ClassNotFoundException cnfe){
+			return null;
+		}
+		catch(Throwable t){
+			return null;
+		}
+	}
+
+	/**
+	 * attempts to load a Class from the executing thread's bundle context.
+	 * If the class is not found, an attempt is made to resolve the class based
+	 * on contributions made by other bundes.
+	 *
+	 * @param className the name of the class in question
+	 * @return the <code>Class</code> if the class exists in the executing thread's bundle context,
+	 * otherwise <code>null</code>
+	 */
+	public Class getClass(String className) {
+		Bundle bundle = (Bundle) localEnvironmentContext.get();
+		if (bundle == null)
+			return getClassFromContributions(className);
+		try {
+			return bundle.loadClass(className);
+		} catch (Throwable t) {
+			try{
+				return OSGiReflectUtilHelper.class.getClassLoader().loadClass(className);
+			} catch(Throwable cnfe){
+				return getClassFromContributions(className);
+			}
+		}
+	}
+
+	/**
+	 * attempts to resolve a resource from contributions made by other bundles. These resources
+	 * are typically wsdl or schema files.
+	 * @param name
+	 * @return
+	 */
+	public InputStream getResource(String name){
+		//Check against contributions -
+		Bundle bundle = null;
+		String trimmedName = name;
+		synchronized(spec2BundleMap){
+			bundle = (Bundle)spec2BundleMap.get(name);
+			if(bundle == null && name.lastIndexOf("/") != -1){
+				trimmedName = name.substring(name.lastIndexOf("/")+1);
+				bundle = (Bundle)spec2BundleMap.get(trimmedName);
+			}
+		}
+		if(bundle != null){
+			try{
+				URL url = bundle.getResource("/" + trimmedName);
+				if(url != null) return url.openStream();
+			}catch(Throwable t){
+				//fall through
+			}
+		}
+		return null;
+	}
+
+	private void loadContributions(ResourceManagementImplementation impl) {
+
+		Bundle bundle = impl.getBundle();
+
+		String implName = impl.getImplementationName();
+
+		String[] resourceImplNames = impl.getResourceImplementationNames();
+		String[] specNames = impl.getResourceSpecNames();
+		CapabilityPair capabilities[] = impl.getResourceCapabilityMappings();
+		int i;
+
+		if(resourceImplNames != null){
+			synchronized(resourceImpl2BundleMap){
+				for(i=0;i<resourceImplNames.length;i++){
+					resourceImpl2BundleMap.put(resourceImplNames[i], bundle);
+				}
+			}
+		}
+		if(specNames != null){
+			synchronized(spec2BundleMap){
+				for(i=0;i<specNames.length;i++){
+					spec2BundleMap.put(specNames[i], bundle);
+				}
+			}
+		}
+		if(capabilities != null){
+			synchronized(capabilityImpl2BundleMap){
+				for(i=0;i<capabilities.length;i++){
+					String uri = (String)capabilities[i].capabilityURI;
+					String capImpl = (String)capabilities[i].capabilityImplementation;
+					CapabilityEntry entry = new CapabilityEntry(uri,capImpl,bundle);
+					capabilityImpl2BundleMap.put(capImpl, entry);
+				}
+			}
+		}
+	}
+
+
+	/**
+	 * ServiceListener method for dealing with ResourceManagementImplementation
+	 * service lifecycle events.
+	 */
+	public synchronized void serviceChanged(ServiceEvent event) {
+		ServiceReference ref = event.getServiceReference();
+		ResourceManagementImplementation impl = (ResourceManagementImplementation)context.getService(ref);
+		String name = impl.getImplementationName();
+		switch (event.getType()) {
+		case ServiceEvent.REGISTERED:
+			if(contributorMap.get(name) == null){
+				contributorMap.put(name, impl);
+				serviceMap.put(ref, name);
+				loadContributions(impl);
+			}
+			break;
+		case ServiceEvent.MODIFIED:
+			break;
+		case ServiceEvent.UNREGISTERING:
+			if (serviceMap.get(ref) != null) {
+				contributorMap.remove(serviceMap.get(ref));
+				serviceMap.remove(ref);
+				//TODO clean up contributions
+			}
+			break;
+		}
+
+	}
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: muse-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: muse-commits-help@ws.apache.org