View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.service.importer.support.internal.util;
18  
19  import java.util.Dictionary;
20  import java.util.Map;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.osgi.framework.BundleContext;
25  import org.osgi.framework.ServiceReference;
26  import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
27  import org.springframework.osgi.util.OsgiServiceReferenceUtils;
28  import org.springframework.util.ObjectUtils;
29  
30  /**
31   * @author Costin Leau
32   * 
33   */
34  public abstract class OsgiServiceBindingUtils {
35  
36  	private static final Log log = LogFactory.getLog(OsgiServiceBindingUtils.class);
37  
38  
39  	public static void callListenersBind(BundleContext context, Object serviceProxy, ServiceReference reference,
40  			OsgiServiceLifecycleListener[] listeners) {
41  		if (!ObjectUtils.isEmpty(listeners)) {
42  			boolean debug = log.isDebugEnabled();
43  
44  			// get a Dictionary implementing a Map
45  			Dictionary properties = OsgiServiceReferenceUtils.getServicePropertiesSnapshot(reference);
46  			for (int i = 0; i < listeners.length; i++) {
47  				if (debug)
48  					log.debug("Calling bind on " + listeners[i] + " w/ reference " + reference);
49  				try {
50  					listeners[i].bind(serviceProxy, (Map) properties);
51  				}
52  				catch (Exception ex) {
53  					log.warn("Bind method on listener " + listeners[i] + " threw exception ", ex);
54  				}
55  				if (debug)
56  					log.debug("Called bind on " + listeners[i] + " w/ reference " + reference);
57  			}
58  		}
59  	}
60  
61  	public static void callListenersUnbind(BundleContext context, Object serviceProxy, ServiceReference reference,
62  			OsgiServiceLifecycleListener[] listeners) {
63  		if (!ObjectUtils.isEmpty(listeners)) {
64  			boolean debug = log.isDebugEnabled();
65  			// get a Dictionary implementing a Map
66  			Dictionary properties = OsgiServiceReferenceUtils.getServicePropertiesSnapshot(reference);
67  			for (int i = 0; i < listeners.length; i++) {
68  				if (debug)
69  					log.debug("Calling unbind on " + listeners[i] + " w/ reference " + reference);
70  				try {
71  					listeners[i].unbind(serviceProxy, (Map) properties);
72  				}
73  				catch (Exception ex) {
74  					log.warn("Unbind method on listener " + listeners[i] + " threw exception ", ex);
75  				}
76  				if (debug)
77  					log.debug("Called unbind on " + listeners[i] + " w/ reference " + reference);
78  			}
79  		}
80  	}
81  }