KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.osgi.framework.ServiceReference;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.Constants;
32
33 import java.util.Dictionary;
34 import java.util.Hashtable;
35 import java.util.Enumeration;
36 import java.util.Vector;
37
38 // Implementation of the org.osgi.framework.ServiceReference interface
39
public class ServiceReferenceImpl implements ServiceReference {
40
41    private static long lastAssignedId = -1;
42
43    // the bundle that registered this service
44
private Bundle bundle;
45    // the class names under which this service was registered
46
private String[] objectClass;
47    // the properties this service was registered with
48
private long id;
49    private Hashtable properties;
50    // the list of bundle currently using this service
51
private Vector usingBundles;
52
53    public ServiceReferenceImpl( Bundle bundle, String[] objectClass, Dictionary props ) {
54       this.bundle = bundle;
55       this.objectClass = objectClass;
56
57       id = lastAssignedId + 1;
58       lastAssignedId = id;
59
60       setProperties( props );
61
62       usingBundles = new Vector();
63    }
64
65    // Return a property of this service.
66
// Keep returning properties even after the service is unregistered.
67
public Object getProperty( String key ) {
68       Util.checkNullParameter( key, getClass().getName(), "getProperty", "key" );
69
70       String k = getPropertyKey( key );
71       if ( k != null ) {
72          return properties.get( k );
73       }
74       return null;
75    }
76
77    // Return the property keys for this service.
78
// Keep returning the property keys even after the service is unregistered.
79
public String[] getPropertyKeys() {
80       String[] keys = new String[ properties.size() ];
81       Enumeration enum = properties.keys();
82       int i = 0;
83       while ( enum.hasMoreElements() ) {
84          keys[ i++ ] = (String)enum.nextElement();
85       }
86       return keys;
87    }
88
89    // Return the service that registered this bundle while the service is registered.
90
// Return null if the service has been unregistered.
91
public Bundle getBundle() {
92       // This class's bundle attribute is set by the constructor.
93
// When the service is unregistered, the service registration calls this class's
94
// serviceUnregistered() method, which sets the bundle attribute to null.
95
return bundle;
96    }
97
98    public Bundle[] getUsingBundles() {
99       Vector tmp = (Vector)usingBundles.clone();
100       Bundle[] ret = new Bundle[ tmp.size() ];
101       tmp.copyInto( ret );
102       return ret;
103    }
104
105 // *****************************************************************************
106

107    // This method is called by the service registration's set property method.
108
protected void setProperties( Dictionary props ) {
109       // Create a new hashtable of properties.
110
Hashtable newProperties = new Hashtable();
111       // Copy the values of the props argument (if any) into the hashtable
112
if ( props != null ) {
113          Enumeration propNames = props.keys();
114          String propName;
115          Object propValue;
116          while ( propNames.hasMoreElements() ) {
117             propName = (String)propNames.nextElement();
118             propValue = props.get( propName );
119             if ( propName.equalsIgnoreCase( Constants.SERVICE_ID ) ) {
120                // osgi_compliance
121
// should not normally allow setting service id
122
// but framework.property.tc1 osgi compliance test fails if we throw an
123
// exception here
124
//throw new IllegalArgumentException( propName + " is a reserved property" );
125
}
126             if ( propName.equalsIgnoreCase( Constants.SERVICE_RANKING ) ) {
127                if ( !( propValue instanceof Integer ) ) {
128                   if ( propValue instanceof String ) {
129                      try {
130                         Integer.valueOf( (String)propValue ).intValue();
131                      }
132                      catch ( NumberFormatException e ) {
133                         throw new IllegalArgumentException( "Value of property named '" + propName + "' must be an integer" );
134                      }
135                   }
136                   else {
137                      throw new IllegalArgumentException( "Value of property named '" + propName + "' must be an integer" );
138                   }
139                }
140             }
141             // if property exists with same key but different case, throw exception
142
String caseVariant = getPropertyKey( propName );
143             if ( caseVariant != null && !caseVariant.equals( propName ) ) {
144                throw new IllegalArgumentException( "Property with case-variant of the same key name already exists" );
145             }
146             newProperties.put( propName, propValue );
147          }
148       }
149       // Make the "objectClass" property equal to the array of class names
150
// under which this service was registered
151
newProperties.put( Constants.OBJECTCLASS, objectClass );
152       newProperties.put( Constants.SERVICE_ID, new Long( id ) );
153
154       // set new properties only if method ended without exceptions
155
properties = newProperties;
156    }
157
158    // This method is called by the service registration when the service is unregistered.
159
// The service reference's getBundle() method must return null after the service has been
160
// unregistered.
161
protected void serviceUnregistered() {
162       bundle = null;
163    }
164
165    // Utility method to get the "objectClass" property without going through the properties.
166
// The value that is returned by this method is the same as that returned by the
167
// getProperty() method with a property name equal to "objectClass".
168
protected String[] getObjectClass() {
169       return objectClass;
170    }
171
172    // This method should be called ONLY by ServiceRegistryEntry.addUser()
173
// If the using bundle uses this service multiple times simultaneously,
174
// this method is called only once when the service use count for the bundle
175
// reaches one
176
public void addUsingBundle( Bundle bundle ) {
177       // this check should not be necessary, just to make extra sure
178
// we don't add the bundle multiple times
179
if ( usingBundles.indexOf( bundle ) == -1 ) {
180          usingBundles.addElement( bundle );
181       }
182    }
183
184    // This method should be called ONLY by ServiceRegistryEntry.removeUser()
185
// If the using bundle uses this service multiple times simultaneously,
186
// this method is called only once when the service use count for the bundle
187
// reaches zero
188
public void removeUsingBundle( Bundle bundle ) {
189       usingBundles.removeElement( bundle );
190    }
191
192    // Return the property key in the case variant that it was registered
193
// (will be null if no such property key has been registered, or a string that
194
// is a case-variant of key argument).
195
private String getPropertyKey( String key ) {
196       // properties can be null the first time setProperties() is called
197
if ( properties == null ) {
198          return null;
199       }
200
201       Enumeration keys = properties.keys();
202       String k;
203       while ( keys.hasMoreElements() ) {
204          k = (String)keys.nextElement();
205          if ( k.equalsIgnoreCase( key ) ) {
206             return k;
207          }
208       }
209       return null;
210    }
211
212 }
Popular Tags