KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > ServiceRegistrationImpl


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar;
37
38 import java.security.AccessController JavaDoc;
39 import java.security.PrivilegedExceptionAction JavaDoc;
40 import java.util.*;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Dictionary JavaDoc;
43 import java.util.Enumeration JavaDoc;
44
45 import org.osgi.framework.*;
46 import org.ungoverned.oscar.util.CaseInsensitiveMap;
47
48 class ServiceRegistrationImpl implements ServiceRegistration
49 {
50     // Oscar framework.
51
private Oscar m_oscar = null;
52     // Bundle implementing the service.
53
private BundleImpl m_bundle = null;
54     // Interfaces associated with the service object.
55
private String JavaDoc[] m_classes = null;
56     // Service Id associated with the service object.
57
private Long JavaDoc m_serviceId = null;
58     // Service object.
59
private Object JavaDoc m_svcObj = null;
60     // Service factory interface.
61
private ServiceFactory m_factory = null;
62     // Associated property dictionary.
63
private Map m_propMap = null;
64     // Re-usable service reference.
65
private ServiceReferenceImpl m_ref = null;
66
67     public ServiceRegistrationImpl(
68         Oscar oscar, BundleImpl bundle,
69         String JavaDoc[] classes, Long JavaDoc serviceId,
70         Object JavaDoc svcObj, Dictionary JavaDoc dict)
71     {
72         m_oscar = oscar;
73         m_bundle = bundle;
74         m_classes = classes;
75         m_serviceId = serviceId;
76         m_svcObj = svcObj;
77         m_factory = (m_svcObj instanceof ServiceFactory)
78             ? (ServiceFactory) m_svcObj : null;
79
80         initializeProperties(dict);
81
82         // This reference is the "standard" reference for this
83
// service and will always be returned by getReference().
84
// Since all reference to this service are supposed to
85
// be equal, we use the hashcode of this reference for
86
// a references to this service in ServiceReference.
87
m_ref = new ServiceReferenceImpl(this, m_bundle);
88     }
89
90     protected boolean isValid()
91     {
92         return (m_svcObj != null);
93     }
94
95     public ServiceReference getReference()
96     {
97         return m_ref;
98     }
99
100     public void setProperties(Dictionary JavaDoc dict)
101     {
102         // Make sure registration is valid.
103
if (!isValid())
104         {
105             throw new IllegalStateException JavaDoc(
106                 "The service registration is no longer valid.");
107         }
108         // Set the properties.
109
initializeProperties(dict);
110         // Tell Oscar about it.
111
m_oscar.servicePropertiesModified(this);
112     }
113
114     private void initializeProperties(Dictionary JavaDoc dict)
115     {
116         // Create a case insensitive map.
117
if (m_propMap == null)
118         {
119             m_propMap = new CaseInsensitiveMap();
120         }
121         else
122         {
123             m_propMap.clear();
124         }
125
126         if (dict != null)
127         {
128             Enumeration JavaDoc keys = dict.keys();
129             while (keys.hasMoreElements())
130             {
131                 Object JavaDoc key = keys.nextElement();
132                 m_propMap.put(key, dict.get(key));
133             }
134         }
135
136         // Add the framework assigned properties.
137
m_propMap.put(Constants.OBJECTCLASS, m_classes);
138         m_propMap.put(Constants.SERVICE_ID, m_serviceId);
139     }
140
141     public void unregister()
142     {
143         Oscar.debug("ServiceRegistration.unregister()");
144         m_oscar.unregisterService(m_bundle, this);
145         m_svcObj = null;
146         m_factory = null;
147     }
148
149     /*
150      * Utility methods.
151      */

152
153     protected Object JavaDoc getService(Bundle acqBundle)
154     {
155         // If the service object is a service factory, then
156
// let it create the service object.
157
if (m_factory != null)
158         {
159             try {
160                 if (System.getSecurityManager() != null)
161                 {
162                     return AccessController.doPrivileged(
163                         new ServiceFactoryPrivileged(acqBundle, null));
164                 }
165                 else
166                 {
167                     return getFactoryUnchecked(acqBundle);
168                 }
169             } catch (Exception JavaDoc ex) {
170                 Oscar.error("ServiceRegistrationImpl: Error getting service.", ex);
171                 return null;
172             }
173         }
174         else
175         {
176             return m_svcObj;
177         }
178     }
179
180     protected void ungetService(Bundle relBundle, Object JavaDoc svcObj)
181     {
182         // If the service object is a service factory, then
183
// let is release the service object.
184
if (m_factory != null)
185         {
186             try {
187                 if (System.getSecurityManager() != null)
188                 {
189                     AccessController.doPrivileged(
190                         new ServiceFactoryPrivileged(relBundle, svcObj));
191                 }
192                 else
193                 {
194                     ungetFactoryUnchecked(relBundle, svcObj);
195                 }
196             } catch (Exception JavaDoc ex) {
197                 Oscar.error("ServiceRegistrationImpl: Error ungetting service.", ex);
198             }
199         }
200     }
201
202     protected Bundle[] getUsingBundles()
203     {
204         return m_oscar.getUsingBundles(m_ref);
205     }
206
207     protected Object JavaDoc getProperty(String JavaDoc key)
208     {
209         return m_propMap.get(key);
210     }
211
212     private transient ArrayList JavaDoc m_list = new ArrayList JavaDoc();
213
214     protected String JavaDoc[] getPropertyKeys()
215     {
216         synchronized (m_list)
217         {
218             m_list.clear();
219             Iterator i = m_propMap.entrySet().iterator();
220             while (i.hasNext())
221             {
222                 Map.Entry entry = (Map.Entry) i.next();
223                 m_list.add(entry.getKey());
224             }
225             return (String JavaDoc[]) m_list.toArray(new String JavaDoc[m_list.size()]);
226         }
227     }
228
229     private Object JavaDoc getFactoryUnchecked(Bundle bundle)
230     {
231         return m_factory.getService(bundle, this);
232     }
233
234     private void ungetFactoryUnchecked(Bundle bundle, Object JavaDoc svcObj)
235     {
236         m_factory.ungetService(bundle, this, svcObj);
237     }
238
239     /**
240      * This simple class is used to ensure that when a service factory
241      * is called, that no other classes on the call stack interferes
242      * with the permissions of the factory itself.
243     **/

244     private class ServiceFactoryPrivileged implements PrivilegedExceptionAction JavaDoc
245     {
246         private Bundle m_bundle = null;
247         private Object JavaDoc m_svcObj = null;
248
249         public ServiceFactoryPrivileged(Bundle bundle, Object JavaDoc svcObj)
250         {
251             m_bundle = bundle;
252             m_svcObj = svcObj;
253         }
254
255         public Object JavaDoc run() throws Exception JavaDoc
256         {
257             if (m_svcObj == null)
258             {
259                 return getFactoryUnchecked(m_bundle);
260             }
261             else
262             {
263                 ungetFactoryUnchecked(m_bundle, m_svcObj);
264             }
265             return null;
266         }
267     }
268 }
Free Books   Free Magazines  
Popular Tags