KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.util.Dictionary JavaDoc;
41
42 import org.osgi.framework.*;
43
44 class BundleContextImpl implements BundleContext
45 {
46     private Oscar m_oscar = null;
47     private BundleImpl m_bundle = null;
48
49     protected BundleContextImpl(Oscar oscar, BundleImpl bundle)
50     {
51         m_oscar = oscar;
52         m_bundle = bundle;
53     }
54
55     public String JavaDoc getProperty(String JavaDoc name)
56     {
57         return m_oscar.getProperty(name);
58     }
59
60     public Bundle getBundle()
61     {
62         return m_bundle;
63     }
64
65     public Filter createFilter(String JavaDoc expr)
66         throws InvalidSyntaxException
67     {
68         return new FilterImpl(expr);
69     }
70
71     public Bundle installBundle(String JavaDoc location)
72         throws BundleException
73     {
74         return installBundle(location, null);
75     }
76
77     public Bundle installBundle(String JavaDoc location, InputStream JavaDoc is)
78         throws BundleException
79     {
80         return m_oscar.installBundle(location, is);
81     }
82
83     public Bundle getBundle(long id)
84     {
85         return m_oscar.getBundle(id);
86     }
87
88     public Bundle[] getBundles()
89     {
90         return m_oscar.getBundles();
91     }
92
93     public void addBundleListener(BundleListener l)
94     {
95         m_oscar.addBundleListener(m_bundle, l);
96     }
97
98     public void removeBundleListener(BundleListener l)
99     {
100         m_oscar.removeBundleListener(l);
101     }
102
103     public void addServiceListener(ServiceListener l)
104     {
105         try {
106             addServiceListener(l, null);
107         } catch (InvalidSyntaxException ex) {
108             // This will not happen since the filter is null.
109
}
110     }
111
112     public void addServiceListener(ServiceListener l, String JavaDoc s)
113         throws InvalidSyntaxException
114     {
115         Oscar.debug("BundleContext.addServiceListener(l, \"" + s + "\")");
116         m_oscar.addServiceListener(m_bundle, l, s);
117     }
118
119     public void removeServiceListener(ServiceListener l)
120     {
121         m_oscar.removeServiceListener(l);
122     }
123
124     public void addFrameworkListener(FrameworkListener l)
125     {
126         m_oscar.addFrameworkListener(m_bundle, l);
127     }
128
129     public void removeFrameworkListener(FrameworkListener l)
130     {
131         m_oscar.removeFrameworkListener(l);
132     }
133
134     public ServiceRegistration registerService(
135         String JavaDoc clazz, Object JavaDoc svcObj, Dictionary JavaDoc dict)
136     {
137         return registerService(new String JavaDoc[] { clazz }, svcObj, dict);
138     }
139
140     public ServiceRegistration registerService(
141         String JavaDoc[] clazzes, Object JavaDoc svcObj, Dictionary JavaDoc dict)
142     {
143         return m_oscar.registerService(m_bundle, clazzes, svcObj, dict);
144     }
145
146     public ServiceReference getServiceReference(String JavaDoc clazz)
147     {
148         Oscar.debug("BundleContext.getServiceReference()");
149         try {
150             ServiceReference[] refs = getServiceReferences(clazz, null);
151             return getBestServiceReference(refs);
152         } catch (InvalidSyntaxException ex) {
153             Oscar.error("BundleContextImpl: " + ex);
154         }
155         return null;
156     }
157
158     private ServiceReference getBestServiceReference(ServiceReference[] refs)
159     {
160         if (refs == null)
161         {
162             return null;
163         }
164
165         if (refs.length == 1)
166         {
167             return refs[0];
168         }
169
170         // Loop through all service references and return
171
// the "best" one according to its rank and ID.
172
ServiceReference bestRef = null;
173         Integer JavaDoc bestRank = null;
174         Long JavaDoc bestId = null;
175         for (int i = 0; i < refs.length; i++)
176         {
177             ServiceReference ref = refs[i];
178
179             // The first time through the loop just
180
// assume that the first reference is best.
181
if (bestRef == null)
182             {
183                 bestRef = ref;
184                 bestRank = (Integer JavaDoc) bestRef.getProperty("service.ranking");
185                 // The spec says no ranking defaults to zero.
186
if (bestRank == null)
187                 {
188                     bestRank = new Integer JavaDoc(0);
189                 }
190                 bestId = (Long JavaDoc) bestRef.getProperty("service.id");
191             }
192
193             // Compare current and best references to see if
194
// the current reference is a better choice.
195
Integer JavaDoc rank = (Integer JavaDoc) ref.getProperty("service.ranking");
196
197             // The spec says no ranking defaults to zero.
198
if (rank == null)
199             {
200                 rank = new Integer JavaDoc(0);
201             }
202
203             // If the current reference ranking is greater than the
204
// best ranking, then keep the current reference.
205
if (bestRank.compareTo(rank) < 0)
206             {
207                 bestRef = ref;
208                 bestRank = rank;
209                 bestId = (Long JavaDoc) bestRef.getProperty("service.id");
210             }
211             // If rankings are equal, then compare IDs and
212
// keep the smallest.
213
else if (bestRank.compareTo(rank) == 0)
214             {
215                 Long JavaDoc id = (Long JavaDoc) ref.getProperty("service.id");
216                 // If either reference has a null ID, then keep
217
// the one with a non-null ID.
218
if ((bestId == null) || (id == null))
219                 {
220                     bestRef = (bestId == null) ? ref : bestRef;
221                     // bestRank = bestRank; // No need to update since they are equal.
222
bestId = (Long JavaDoc) bestRef.getProperty("service.id");
223                 }
224                 // Otherwise compare IDs.
225
else
226                 {
227                     // If the current reference ID is less than the
228
// best ID, then keep the current reference.
229
if (bestId.compareTo(id) > 0)
230                     {
231                         bestRef = ref;
232                         // bestRank = bestRank; // No need to update since they are equal.
233
bestId = (Long JavaDoc) bestRef.getProperty("service.id");
234                     }
235                 }
236             }
237         }
238
239         return bestRef;
240     }
241
242     public ServiceReference[] getServiceReferences(String JavaDoc clazz, String JavaDoc filter)
243         throws InvalidSyntaxException
244     {
245         Oscar.debug("BundleContext.getServiceReferences()");
246         return m_oscar.getServiceReferences(clazz, filter);
247     }
248
249     public Object JavaDoc getService(ServiceReference ref)
250     {
251         if (ref == null)
252         {
253             throw new NullPointerException JavaDoc("Specified service reference cannot be null.");
254         }
255         return m_oscar.getService(m_bundle, ref);
256     }
257
258     public boolean ungetService(ServiceReference ref)
259     {
260         if (ref == null)
261         {
262             throw new NullPointerException JavaDoc("Specified service reference cannot be null.");
263         }
264
265         // Unget the specified service.
266
Object JavaDoc svcObj = m_oscar.ungetService(m_bundle, ref);
267
268         // Return false if no service object was found.
269
return (svcObj != null);
270     }
271
272     public File JavaDoc getDataFile(String JavaDoc s)
273     {
274         Oscar.debug("BundleContext.getDataFile()");
275         return m_oscar.getBundleDataFile(m_bundle, s);
276     }
277 }
Popular Tags