KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > serviceRegistry > ServiceRegistry


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.serviceRegistry;
28
29 import com.opensugar.cube.AbstractCube;
30 import com.opensugar.cube.BundleImpl;
31 import com.opensugar.cube.ServiceRegistrationImpl;
32 import com.opensugar.cube.ServiceReferenceImpl;
33 import com.opensugar.cube.UnreachableCodeException;
34 import com.opensugar.cube.ldap.LDAPFilter;
35
36 import org.osgi.framework.ServiceReference;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.framework.InvalidSyntaxException;
39 import org.osgi.service.packageadmin.PackageAdmin;
40 import org.osgi.service.permissionadmin.PermissionAdmin;
41
42 import java.util.Dictionary;
43 import java.util.Hashtable;
44 import java.util.Vector;
45 import java.util.Enumeration;
46
47 public class ServiceRegistry {
48
49    private AbstractCube cube;
50
51    // list of services in the registry provider bundle --> vector of service registry entries
52
private Hashtable bundle_services;
53
54    public ServiceRegistry( AbstractCube cube ) {
55       this.cube = cube;
56       bundle_services = new Hashtable();
57    }
58
59    // Register a service in the registry.
60
public ServiceRegistrationImpl registerService( BundleImpl serviceProvider, String[] classNames, Object service, Dictionary properties ) {
61       cube.log( cube.LOG_DEBUG, serviceProvider.getLocationWithoutAdminPermissionCheck() + " registering service: " + classNames[ 0 ] );
62
63       // no bundle other than system bundle may register PackageAdmin or PermissionAdmin services
64
if ( serviceProvider.getBundleId() != 0 ) {
65          for ( int i = 0; i < classNames.length; i++ ) {
66             if ( classNames[ i ].equals( PackageAdmin.class.getName() ) ) {
67                throw new IllegalArgumentException( "Only system bundle may register PackageAdmin service" );
68             }
69             else if ( classNames[ i ].equals( PermissionAdmin.class.getName() ) ) {
70                throw new IllegalArgumentException( "Only system bundle may register PermissionAdmin service" );
71             }
72          }
73       }
74
75       ServiceRegistrationImpl serviceRegistration = new ServiceRegistrationImpl( serviceProvider, classNames, properties );
76       ServiceReferenceImpl serviceReference = (ServiceReferenceImpl)serviceRegistration.getReference();
77       ServiceRegistryEntry serviceRegistryEntry = new ServiceRegistryEntry( serviceRegistration, serviceReference, service );
78       getServiceRegistryEntries( serviceProvider, true ).addElement( serviceRegistryEntry );
79       return serviceRegistration;
80    }
81
82    // Return the services that implement the specified class and that pass the specified filter.
83
public ServiceReferenceImpl[] getServiceReferences( String className, LDAPFilter filter ) {
84       Vector serviceReferences = new Vector();
85       Enumeration enum = bundle_services.elements();
86       Vector serviceRegistryEntries;
87       ServiceRegistryEntry serviceRegistryEntry;
88       while ( enum.hasMoreElements() ) {
89          serviceRegistryEntries = (Vector)enum.nextElement();
90          for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) {
91             serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i );
92             // Return only services that are still registered and that pass the filter (if a filter is specified)
93
if ( serviceRegistryEntry.isServiceStillRegistered() && doFilter( serviceRegistryEntry, filter ) && serviceRegistryEntry.isImplemented( className ) ) {
94                serviceReferences.addElement( serviceRegistryEntry.getReference() );
95             }
96          }
97       }
98
99       if ( serviceReferences.size() == 0 ) {
100          return null;
101       }
102
103       ServiceReferenceImpl[] ret = new ServiceReferenceImpl[ serviceReferences.size() ];
104       serviceReferences.copyInto( ret );
105       return ret;
106    }
107
108    // Return the service object for the specified user bundle.
109
// This method throws an IllegalStateException if the service of the ServiceReference has been unregistered.
110
// This method throws an ServiceRegistryException if the service created by the service factory (when a
111
// service factory is used) is not an instance of all the classes named when the service was registered,
112
// if the service factory throws an exception while creating the service instance, or if any other problem
113
// occurs while creating a new service instance.
114
public Object getService( ServiceReference reference, BundleImpl userBundle ) throws ServiceRegistryException {
115       Object serviceClass = reference.getProperty( "objectClass" );
116       if ( serviceClass instanceof Object[] ) {
117          serviceClass = ( (Object[])serviceClass )[ 0 ];
118       }
119       cube.log( cube.LOG_DEBUG, userBundle.getLocationWithoutAdminPermissionCheck() + " getting service: " + serviceClass );
120
121       Enumeration enum = bundle_services.elements();
122       Vector serviceRegistryEntries;
123       ServiceRegistryEntry serviceRegistryEntry;
124       while ( enum.hasMoreElements() ) {
125          serviceRegistryEntries = (Vector)enum.nextElement();
126          for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) {
127             serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i );
128             // throw an IllegalStateException if service is unregistered
129
if ( serviceRegistryEntry.getReference().equals( reference ) ) {
130                // serviceRegistry.getService() throws an IllegalStateException if the service has been
131
// unregistered
132
// serviceRegistryEntry.getService() throws a ServiceRegistryException if a problem occurs
133
// while creating the service instance using the service factory
134
return serviceRegistryEntry.getService( userBundle );
135             }
136          }
137       }
138       return null;
139    }
140
141    // Register that the specified bundle is releasing the service.
142
public boolean ungetService( ServiceReference reference, BundleImpl userBundle ) {
143       Object serviceClass = reference.getProperty( "objectClass" );
144       if ( serviceClass instanceof Object[] ) {
145          serviceClass = ( (Object[])serviceClass )[ 0 ];
146       }
147       cube.log( cube.LOG_DEBUG, userBundle.getLocationWithoutAdminPermissionCheck() + " ungetting service: " + serviceClass );
148
149       Enumeration enum = bundle_services.elements();
150       Vector serviceRegistryEntries;
151       ServiceRegistryEntry serviceRegistryEntry;
152       while ( enum.hasMoreElements() ) {
153          serviceRegistryEntries = (Vector)enum.nextElement();
154          for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) {
155             serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i );
156             if ( serviceRegistryEntry.getReference().equals( reference ) ) {
157                return serviceRegistryEntry.ungetService( userBundle );
158             }
159          }
160       }
161
162       // This should never happen
163
// ServiceReference instances are all created by this class and immediately stored in
164
// a service registry entry. So any reference passed to this method should be found when
165
// looking through all service registry entries
166
throw new UnreachableCodeException();
167    }
168
169    // Return the list of services registered by the specified bundle.
170
// Only include services that are still registered.
171
public ServiceRegistrationImpl[] getServicesRegisteredBy( BundleImpl bundle ) {
172       Vector serviceRegistrations = new Vector();
173       Enumeration enum = bundle_services.elements();
174       Vector serviceRegistryEntries;
175       ServiceRegistryEntry serviceRegistryEntry;
176       while ( enum.hasMoreElements() ) {
177          serviceRegistryEntries = (Vector)enum.nextElement();
178          for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) {
179             serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i );
180             // Only return services that are still registered.
181
if ( serviceRegistryEntry.isServiceStillRegistered() && serviceRegistryEntry.isProvidedBy( bundle ) ) {
182                serviceRegistrations.addElement( serviceRegistryEntry.getRegistration() );
183             }
184          }
185       }
186
187       if ( serviceRegistrations.size() == 0 ) {
188          return null;
189       }
190
191       ServiceRegistrationImpl[] ret = new ServiceRegistrationImpl[ serviceRegistrations.size() ];
192       serviceRegistrations.copyInto( ret );
193       return ret;
194    }
195
196    // Return list of services used by the specified bundle.
197
// Include used services even if they are unregistered.
198
public ServiceReferenceImpl[] getServicesUsedBy( BundleImpl bundle ) {
199       Vector serviceReferences = new Vector();
200       Enumeration enum = bundle_services.elements();
201       Vector serviceRegistryEntries;
202       ServiceRegistryEntry serviceRegistryEntry;
203       while ( enum.hasMoreElements() ) {
204          serviceRegistryEntries = (Vector)enum.nextElement();
205          for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) {
206             serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i );
207             // Return service even if they have been unregistered
208
if ( serviceRegistryEntry.isUsedBy( bundle ) ) {
209                serviceReferences.addElement( serviceRegistryEntry.getReference() );
210             }
211          }
212       }
213
214       if ( serviceReferences.size() == 0 ) {
215          return null;
216       }
217
218       ServiceReferenceImpl[] ret = new ServiceReferenceImpl[ serviceReferences.size() ];
219       serviceReferences.copyInto( ret );
220       return ret;
221    }
222
223    public ServiceRegistryEntry getServiceRegistryEntry( ServiceRegistration serviceRegistration ) {
224       Vector serviceReferences = new Vector();
225       Enumeration enum = bundle_services.elements();
226       Vector serviceRegistryEntries;
227       ServiceRegistryEntry serviceRegistryEntry;
228       while ( enum.hasMoreElements() ) {
229          serviceRegistryEntries = (Vector)enum.nextElement();
230          for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) {
231             serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i );
232             if ( serviceRegistryEntry.getRegistration().equals( serviceRegistration ) ) {
233                return serviceRegistryEntry;
234             }
235          }
236       }
237       // This should never happen, or it means we asked for a service registry entry that does
238
// not exist. (ServiceRegistration is the only object that calls this method, and it should
239
// never call this method with a service registration that does not correspond to an
240
// existing service registry entry.
241
throw new UnreachableCodeException();
242    }
243
244
245 // *****************************************************************************
246

247    // Return the list of service registry entries provided by the specified bundle.
248
// If no list exists for the specified bundle and createIfNull is true, then create a new
249
// empty list of services for the bundle.
250
private Vector getServiceRegistryEntries( BundleImpl serviceProvider, boolean createIfNull ) {
251       if ( bundle_services.get( serviceProvider ) == null && createIfNull ) {
252          bundle_services.put( serviceProvider, new Vector() );
253       }
254       return (Vector)bundle_services.get( serviceProvider );
255    }
256
257    // Determine if the specified service registry entry passses the filter or not.
258
// Return true if the filter is null.
259
private boolean doFilter( ServiceRegistryEntry serviceRegistryEntry, LDAPFilter filter ) {
260       if ( filter != null ) {
261          try {
262             LDAPFilter caseInsensitiveFilter = new LDAPFilter( filter.getFilterString(), false );
263             return caseInsensitiveFilter.match( serviceRegistryEntry.getReference() );
264          }
265          catch ( InvalidSyntaxException e ) {
266             // creating a filter out of the filter string of another filter
267
// so filter string syntax must already have been checked
268
throw new UnreachableCodeException();
269          }
270       }
271       return true;
272    }
273
274 }
Popular Tags