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.exporter.support;
18  
19  import java.util.Map;
20  
21  import org.osgi.framework.ServiceRegistration;
22  import org.springframework.beans.factory.DisposableBean;
23  import org.springframework.osgi.service.exporter.OsgiServiceRegistrationListener;
24  
25  /**
26   * Base exporter class providing common functionality for registering (also
27   * known as exporting) Spring beans as OSGi services.
28   * 
29   * @author Costin Leau
30   */
31  abstract class AbstractOsgiServiceExporter implements DisposableBean {
32  
33  	/** listeners */
34  	private OsgiServiceRegistrationListener[] listeners = new OsgiServiceRegistrationListener[0];
35  
36  
37  	/**
38  	 * Takes care of notifying the listeners on both startup and shutdown (by
39  	 * wrapping the service registration).
40  	 * 
41  	 * @param service object published as OSGi service
42  	 * @param properties exported OSGi service properties
43  	 * @param registration original service registration
44  	 * @return
45  	 */
46  	ServiceRegistration notifyListeners(Object service, Map properties, ServiceRegistration registration) {
47  		// notify listeners
48  		callRegisteredOnListeners(service, properties);
49  		// wrap registration to be notified of unregistration
50  		return new ServiceRegistrationDecorator(service, registration, listeners);
51  	}
52  
53  	/**
54  	 * Call registration on listeners.
55  	 * 
56  	 * @param properties
57  	 */
58  	private void callRegisteredOnListeners(Object service, Map properties) {
59  		for (int i = 0; i < listeners.length; i++) {
60  			if (listeners[i] != null) {
61  				try {
62  					listeners[i].registered(service, properties);
63  				}
64  				catch (Exception ex) {
65  					// no need to log exceptions, the listener wrapper already
66  					// does this for us
67  				}
68  			}
69  		}
70  	}
71  
72  	/**
73  	 * Sets the listeners interested in registration and unregistration events.
74  	 * 
75  	 * @param listeners registration/unregistration listeners.
76  	 */
77  	public void setListeners(OsgiServiceRegistrationListener[] listeners) {
78  		if (listeners != null)
79  			this.listeners = listeners;
80  	}
81  
82  	public void destroy() {
83  		unregisterService();
84  	}
85  
86  	/**
87  	 * Registers/Exports the OSGi service.
88  	 */
89  	abstract void registerService();
90  
91  	/**
92  	 * Unregisters/de-exports the OSGi service.
93  	 */
94  	abstract void unregisterService();
95  }