KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.osgi.framework.Bundle;
39 import org.osgi.service.packageadmin.ExportedPackage;
40 import org.osgi.service.packageadmin.PackageAdmin;
41
42 class PackageAdminImpl implements PackageAdmin, Runnable JavaDoc
43 {
44     private Oscar m_oscar = null;
45     private Bundle[][] m_reqBundles = null;
46
47     public PackageAdminImpl(Oscar oscar)
48     {
49         m_oscar = oscar;
50
51         // Start a thread to perform asynchronous package refreshes.
52
Thread JavaDoc t = new Thread JavaDoc(this, "OscarPackageAdmin");
53         t.setDaemon(true);
54         t.start();
55     }
56
57     /**
58      * Returns the exported package associated with the specified
59      * package name.
60      *
61      * @param name the name of the exported package to find.
62      * @return the exported package or null if no matching package was found.
63     **/

64     public ExportedPackage getExportedPackage(String JavaDoc name)
65     {
66         return m_oscar.getExportedPackage(name);
67     }
68
69     /**
70      * Returns the packages exported by the specified bundle.
71      *
72      * @param bundle the bundle whose exported packages are to be returned.
73      * @return an array of packages exported by the bundle or null if the
74      * bundle does not export any packages.
75     **/

76     public ExportedPackage[] getExportedPackages(Bundle b)
77     {
78         return m_oscar.getExportedPackages(b);
79     }
80
81     /**
82      * The OSGi specification states that refreshing packages is
83      * asynchronous; this method simply notifies the package admin
84      * thread to do a refresh.
85      * @param bundles array of bundles to refresh or <tt>null</tt> to refresh
86      * any bundles in need of refreshing.
87     **/

88     public synchronized void refreshPackages(Bundle[] bundles)
89         throws SecurityException JavaDoc
90     {
91         // Save our request parameters and notify all.
92
if (m_reqBundles == null)
93         {
94             m_reqBundles = new Bundle[][] { bundles };
95         }
96         else
97         {
98             Bundle[][] newReqBundles = new Bundle[m_reqBundles.length + 1][];
99             System.arraycopy(m_reqBundles, 0,
100                 newReqBundles, 0, m_reqBundles.length);
101             newReqBundles[m_reqBundles.length] = bundles;
102             m_reqBundles = newReqBundles;
103         }
104         notifyAll();
105     }
106
107     /**
108      * The OSGi specification states that package refreshes happen
109      * asynchronously; this is the run() method for the package
110      * refreshing thread.
111     **/

112     public void run()
113     {
114         // This thread loops forever, thus it should
115
// be a daemon thread.
116
Bundle[] bundles = null;
117         while (true)
118         {
119             synchronized (this)
120             {
121                 // Wait for a refresh request.
122
while (m_reqBundles == null)
123                 {
124                     try
125                     {
126                         wait();
127                     }
128                     catch (InterruptedException JavaDoc ex)
129                     {
130                     }
131                 }
132
133                 // Get the bundles parameter for the current
134
// refresh request.
135
if (m_reqBundles != null)
136                 {
137                     bundles = m_reqBundles[0];
138                 }
139             }
140
141             // Perform refresh.
142
m_oscar.refreshPackages(bundles);
143
144             // Remove the first request since it is now completed.
145
synchronized (this)
146             {
147                 if (m_reqBundles.length == 1)
148                 {
149                     m_reqBundles = null;
150                 }
151                 else
152                 {
153                     Bundle[][] newReqBundles = new Bundle[m_reqBundles.length - 1][];
154                     System.arraycopy(m_reqBundles, 1,
155                         newReqBundles, 0, m_reqBundles.length - 1);
156                     m_reqBundles = newReqBundles;
157                 }
158             }
159         }
160     }
161 }
Popular Tags