KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
39
40 import org.osgi.framework.*;
41 import org.ungoverned.moduleloader.Module;
42
43 class BundleInfo
44 {
45     private BundleArchive m_archive = null;
46     private Module[] m_modules = null;
47     private int m_state = 0;
48     private BundleActivator m_activator = null;
49     private BundleContext m_context = null;
50     // Indicates that the bundle was either updated
51
// or uninstalled and is waiting to be removed or refreshed.
52
private boolean m_removalPending = false;
53     // List of service registrations for this bundle; these are
54
// the services that the bundle has registered.
55
private List m_svcRegList = null;
56     // Maps service references to usage count objects; these are
57
// the services used by the bundle.
58
private Map m_svcUsageMap = null;
59
60     protected BundleInfo(BundleArchive archive, Module module)
61         throws Exception JavaDoc
62     {
63         m_archive = archive;
64         m_modules = (module == null) ? new Module[0] : new Module[] { module };
65
66         m_state = Bundle.INSTALLED;
67         m_removalPending = false;
68         m_activator = null;
69         m_context = null;
70
71         m_svcRegList = new ArrayList();
72         m_svcUsageMap = new HashMap();
73     }
74
75     /**
76      * Returns the bundle archive associated with this bundle.
77      * @return the bundle archive associated with this bundle.
78     **/

79     public BundleArchive getArchive()
80     {
81         return m_archive;
82     }
83
84     /**
85      * Returns an array of all modules associated with the bundle represented by
86      * this <tt>BundleInfo</tt> object. A module in the array corresponds to a
87      * revision of the bundle's JAR file and is ordered from oldest to newest.
88      * Multiple revisions of a bundle JAR file might exist if a bundle is
89      * updated, without refreshing the framework. In this case, exports from
90      * the prior revisions of the bundle JAR file are still offered; the
91      * current revision will be bound to packages from the prior revision,
92      * unless the packages were not offered by the prior revision. There is
93      * no limit on the potential number of bundle JAR file revisions.
94      * @return array of modules corresponding to the bundle JAR file revisions.
95     **/

96     public Module[] getModules()
97     {
98         return m_modules;
99     }
100
101     /**
102      * Determines if the specified module is associated with this bundle.
103      * @param module the module to determine if it is associate with this bundle.
104      * @return <tt>true</tt> if the specified module is in the array of modules
105      * associated with this bundle, <tt>false</tt> otherwise.
106     **/

107     public boolean hasModule(Module module)
108     {
109         for (int i = 0; i < m_modules.length; i++)
110         {
111             if (m_modules[i] == module)
112             {
113                 return true;
114             }
115         }
116         return false;
117     }
118
119     /**
120      * Returns the newest module, which corresponds to the last module
121      * in the module array.
122      * @return the newest module.
123     **/

124     public Module getCurrentModule()
125     {
126         return m_modules[m_modules.length - 1];
127     }
128     
129     /**
130      * Add a module that corresponds to a new bundle JAR file revision for
131      * the bundle associated with this <tt>BundleInfo</tt> object.
132      * @param module the module to add.
133     **/

134     public void addModule(Module module)
135     {
136         Module[] dest = new Module[m_modules.length + 1];
137         System.arraycopy(m_modules, 0, dest, 0, m_modules.length);
138         dest[m_modules.length] = module;
139         m_modules = dest;
140     }
141
142     public long getBundleId()
143     {
144         return m_archive.getId();
145     }
146     
147     public String JavaDoc getLocation()
148     {
149         try
150         {
151             return m_archive.getLocation();
152         }
153         catch (Exception JavaDoc ex)
154         {
155             Oscar.error("Unable to read location.", ex);
156             return null;
157         }
158     }
159
160     public int getStartLevel(int defaultLevel)
161     {
162         try
163         {
164             return m_archive.getStartLevel();
165         }
166         catch (Exception JavaDoc ex)
167         {
168             Oscar.error("Unable to read start level.", ex);
169             return defaultLevel;
170         }
171     }
172
173     public void setStartLevel(int i)
174     {
175         try
176         {
177             m_archive.setStartLevel(i);
178         }
179         catch (Exception JavaDoc ex)
180         {
181             Oscar.error("Error writing to bundle archive.", ex);
182         }
183     }
184
185     public Map getCurrentHeader()
186     {
187         try
188         {
189             // Return the header for the most recent bundle revision only,
190
// since we shouldn't ever need access to older revisions.
191
return m_archive.getManifestHeader(m_archive.getRevisionCount() - 1);
192         }
193         catch (Exception JavaDoc ex)
194         {
195             return null;
196         }
197     }
198
199     public int getState()
200     {
201         return m_state;
202     }
203
204     public void setState(int i)
205     {
206         m_state = i;
207     }
208
209     public int getPersistentState()
210     {
211         try
212         {
213             return m_archive.getPersistentState();
214         }
215         catch (Exception JavaDoc ex)
216         {
217             Oscar.error("Error reading bundle archive.", ex);
218             return Bundle.INSTALLED;
219         }
220     }
221
222     public void setPersistentStateInactive()
223     {
224         try
225         {
226             m_archive.setPersistentState(Bundle.INSTALLED);
227         }
228         catch (Exception JavaDoc ex)
229         {
230             Oscar.error("Error writing to bundle archive.", ex);
231         }
232     }
233
234     public void setPersistentStateActive()
235     {
236         try
237         {
238             m_archive.setPersistentState(Bundle.ACTIVE);
239         }
240         catch (Exception JavaDoc ex)
241         {
242             Oscar.error("Error writing to bundle archive.", ex);
243         }
244     }
245
246     public void setPersistentStateUninstalled()
247     {
248         try
249         {
250             m_archive.setPersistentState(Bundle.UNINSTALLED);
251         }
252         catch (Exception JavaDoc ex)
253         {
254             Oscar.error("Error writing to bundle archive.", ex);
255         }
256     }
257
258     public BundleContext getContext()
259     {
260         return m_context;
261     }
262
263     public void setContext(BundleContext context)
264     {
265         m_context = context;
266     }
267
268     public BundleActivator getActivator()
269     {
270         return m_activator;
271     }
272
273     public void setActivator(BundleActivator activator)
274     {
275         m_activator = activator;
276     }
277
278     public boolean isRemovalPending()
279     {
280         return m_removalPending;
281     }
282
283     public void setRemovalPending()
284     {
285         m_removalPending = true;
286     }
287
288     public int getServiceRegistrationCount()
289     {
290         return m_svcRegList.size();
291     }
292
293     public ServiceRegistrationImpl getServiceRegistration(int i)
294     {
295         try
296         {
297             return (ServiceRegistrationImpl) m_svcRegList.get(i);
298         }
299         catch (IndexOutOfBoundsException JavaDoc exc)
300         {
301             // Ignore and return null.
302
}
303         return null;
304     }
305
306     public void addServiceRegistration(ServiceRegistrationImpl reg)
307     {
308         m_svcRegList.add(reg);
309     }
310
311     public void removeServiceRegistration(ServiceRegistrationImpl reg)
312     {
313         m_svcRegList.remove(reg);
314     }
315
316 // TODO: Would we be better off putting "used services" into a
317
// list, like registered services above...for consistency?
318
public Iterator getServiceUsageCounters()
319     {
320         return m_svcUsageMap.keySet().iterator();
321     }
322
323     public UsageCounter getServiceUsageCounter(ServiceReference ref)
324     {
325         return (UsageCounter) m_svcUsageMap.get(ref);
326     }
327
328     public void putServiceUsageCounter(ServiceReference ref, UsageCounter usage)
329     {
330         m_svcUsageMap.put(ref, usage);
331     }
332
333     public void removeServiceUsageCounter(ServiceReference ref)
334     {
335         m_svcUsageMap.remove(ref);
336     }
337
338     /**
339      * Converts a module identifier to a bundle identifier. Module IDs
340      * are typically <tt>&lt;bundle-id&gt;.&lt;revision&gt;</tt>; this
341      * method returns only the portion corresponding to the bundle ID.
342     **/

343     protected static long getBundleIdFromModuleId(String JavaDoc id)
344     {
345         try
346         {
347             String JavaDoc bundleId = (id.indexOf('.') >= 0)
348                 ? id.substring(0, id.indexOf('.')) : id;
349             return Long.parseLong(bundleId);
350         }
351         catch (NumberFormatException JavaDoc ex)
352         {
353             return -1;
354         }
355     }
356
357     /**
358      * Converts a module identifier to a bundle identifier. Module IDs
359      * are typically <tt>&lt;bundle-id&gt;.&lt;revision&gt;</tt>; this
360      * method returns only the portion corresponding to the revision.
361     **/

362     protected static int getModuleRevisionFromModuleId(String JavaDoc id)
363     {
364         try
365         {
366             String JavaDoc rev = (id.indexOf('.') >= 0)
367                 ? id.substring(id.indexOf('.') + 1) : id;
368             return Integer.parseInt(rev);
369         }
370         catch (NumberFormatException JavaDoc ex)
371         {
372             return -1;
373         }
374     }
375
376     static class UsageCounter
377     {
378         public int m_count = 0;
379         public Object JavaDoc m_svcObj = null;
380         public Object JavaDoc[] m_svcObjArray = null;
381     }
382 }
Popular Tags