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  package org.springframework.osgi.service.exporter.support;
17  
18  import java.util.Dictionary;
19  import java.util.Map;
20  
21  import org.osgi.framework.ServiceReference;
22  import org.osgi.framework.ServiceRegistration;
23  import org.springframework.osgi.service.exporter.OsgiServiceRegistrationListener;
24  import org.springframework.osgi.util.OsgiServiceReferenceUtils;
25  import org.springframework.util.Assert;
26  
27  /**
28   * Decorator class for {@link ServiceReference} which add notification for
29   * {@link ServiceRegistration#unregister()} method when dealing with listeners.
30   * 
31   * @author Costin Leau
32   */
33  class ServiceRegistrationDecorator implements ServiceRegistration {
34  
35  	/** actual service registration */
36  	private final ServiceRegistration delegate;
37  
38  	private final OsgiServiceRegistrationListener[] listeners;
39  
40  	private final Object service;
41  
42  	public ServiceRegistrationDecorator(Object service, ServiceRegistration registration,
43  			OsgiServiceRegistrationListener[] listeners) {
44  		Assert.notNull(registration);
45  		this.delegate = registration;
46  		this.listeners = (listeners == null ? new OsgiServiceRegistrationListener[0] : listeners);
47  		this.service = service;
48  	}
49  
50  	public ServiceReference getReference() {
51  		return delegate.getReference();
52  	}
53  
54  	public void setProperties(Dictionary properties) {
55  		delegate.setProperties(properties);
56  	}
57  
58  	// call unregister on the actual service but inform also listeners
59  	public void unregister() {
60  		// if the delegate is unregistered then an exception will be thrown
61  		Map properties = (Map) OsgiServiceReferenceUtils.getServicePropertiesSnapshot(delegate.getReference());
62  
63  		delegate.unregister();
64  
65  		// if no exception has been thrown (i.e. the delegate is properly
66  		// unregistered), the listeners will be informed
67  		for (int i = 0; i < listeners.length; i++) {
68  			if (listeners[i] != null) {
69  				try {
70  					listeners[i].unregistered(service, properties);
71  				}
72  				catch (Exception ex) {
73  					// no need to log exceptions, the wrapper already does this
74  				}
75  			}
76  		}
77  	}
78  
79  	public String toString() {
80  		return "ServiceWrapper for " + delegate.toString();
81  	}
82  
83  }