KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > ServiceRegistrationImpl


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube;
28
29 import com.opensugar.cube.serviceRegistry.ServiceRegistryEntry;
30
31 import org.osgi.framework.ServiceRegistration;
32 import org.osgi.framework.ServiceReference;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.ServiceEvent;
35
36 import java.util.Dictionary JavaDoc;
37
38 // Implementation of the org.osgi.framework.ServiceRegistration interface
39
public class ServiceRegistrationImpl implements ServiceRegistration {
40
41    // the sercice reference for this service
42
private ServiceReferenceImpl reference;
43    // a flag that indicates if this service is still registered
44
private boolean registered;
45
46    public ServiceRegistrationImpl( Bundle bundle, String JavaDoc[] classNames, Dictionary JavaDoc properties ) {
47       reference = new ServiceReferenceImpl( bundle, classNames, properties );
48       registered = true;
49    }
50
51    // Return the reference for this service if the service is still registered.
52
// If the service has been unregistered, throw an IllegalStateException.
53
public ServiceReference getReference() {
54       // Check if the service is still registered and throw an IllegalStateException if it is not.
55
checkStillRegistered();
56
57       return reference;
58    }
59
60    // Set this service's properties if the service is still registered.
61
// If the service has been unregistered, throw an IllegalStateException.
62
public void setProperties( Dictionary JavaDoc properties ) {
63       // null properties argument is allowed
64

65       // Check if the service is still registered and throw an IllegalStateException if it is not.
66
checkStillRegistered();
67
68       // Set the service properties.
69
reference.setProperties( properties );
70
71       // Fire a service modified event
72
( (BundleImpl)reference.getBundle() ).getCube().fireServiceEvent( ServiceEvent.MODIFIED, reference );
73    }
74
75    public void unregister() {
76       // Check if the service is still registered and throw an IllegalStateException if it is not.
77
checkStillRegistered();
78
79       registered = false;
80       // Do not remove service registration from service registry because service properties
81
// and property keys still need to be accessible after service is unregistered
82

83       // Fire service unregistering event
84
BundleImpl bundle = (BundleImpl)reference.getBundle();
85       Object JavaDoc serviceClass = reference.getProperty( "objectClass" );
86       if ( serviceClass instanceof Object JavaDoc[] ) {
87          serviceClass = ( (Object JavaDoc[])serviceClass )[ 0 ];
88       }
89       ( bundle.getCube() ).log( AbstractCube.LOG_DEBUG, bundle.getLocationWithoutAdminPermissionCheck() + " unregistering " + serviceClass );
90       bundle.getCube().fireServiceEvent( ServiceEvent.UNREGISTERING, reference );
91
92       // Let service reference know the service has been unregistered so that it knows
93
// to return null when its getBundle method is called
94
reference.serviceUnregistered();
95
96       // For each bundle whose use count for this service is greater than zero,
97
// 1. set the bundle's use count for this service to zero
98
// 2. if the service was registered with a service factory, call the factory's
99
// unget method to release the service object for the bundle
100
ServiceRegistryEntry serviceRegistryEntry = bundle.getCube().getServiceRegistry().getServiceRegistryEntry( this );
101       serviceRegistryEntry.serviceUnregistered();
102    }
103
104 // *****************************************************************************
105

106    // Check if this service is still registered.
107
private void checkStillRegistered() {
108       if ( !registered ) {
109          throw new IllegalStateException JavaDoc( "Service has already been unregistered" );
110       }
111    }
112
113 }
Popular Tags