Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | package org.apache.osgi.framework; |
| 18 | |
| 19 | import org.osgi.framework.Bundle; |
| 20 | import org.osgi.service.packageadmin.*; |
| 21 | |
| 22 | class PackageAdminImpl implements PackageAdmin, Runnable |
| 23 | { |
| 24 | private Felix m_felix = null; |
| 25 | private Bundle[][] m_reqBundles = null; |
| 26 | |
| 27 | public PackageAdminImpl(Felix felix) |
| 28 | { |
| 29 | m_felix = felix; |
| 30 | |
| 31 | // Start a thread to perform asynchronous package refreshes. |
| 32 | Thread t = new Thread(this, "FelixPackageAdmin"); |
| 33 | t.setDaemon(true); |
| 34 | t.start(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns the exported package associated with the specified |
| 39 | * package name. |
| 40 | * |
| 41 | * @param name the name of the exported package to find. |
| 42 | * @return the exported package or null if no matching package was found. |
| 43 | **/ |
| 44 | public ExportedPackage getExportedPackage(String name) |
| 45 | { |
| 46 | return m_felix.getExportedPackage(name); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the packages exported by the specified bundle. |
| 51 | * |
| 52 | * @param bundle the bundle whose exported packages are to be returned. |
| 53 | * @return an array of packages exported by the bundle or null if the |
| 54 | * bundle does not export any packages. |
| 55 | **/ |
| 56 | public ExportedPackage[] getExportedPackages(Bundle b) |
| 57 | { |
| 58 | return m_felix.getExportedPackages(b); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * The OSGi specification states that refreshing packages is |
| 63 | * asynchronous; this method simply notifies the package admin |
| 64 | * thread to do a refresh. |
| 65 | * @param bundles array of bundles to refresh or <tt>null</tt> to refresh |
| 66 | * any bundles in need of refreshing. |
| 67 | **/ |
| 68 | public synchronized void refreshPackages(Bundle[] bundles) |
| 69 | throws SecurityException |
| 70 | { |
| 71 | // Save our request parameters and notify all. |
| 72 | if (m_reqBundles == null) |
| 73 | { |
| 74 | m_reqBundles = new Bundle[][] { bundles }; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | Bundle[][] newReqBundles = new Bundle[m_reqBundles.length + 1][]; |
| 79 | System.arraycopy(m_reqBundles, 0, |
| 80 | newReqBundles, 0, m_reqBundles.length); |
| 81 | newReqBundles[m_reqBundles.length] = bundles; |
| 82 | m_reqBundles = newReqBundles; |
| 83 | } |
| 84 | notifyAll(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The OSGi specification states that package refreshes happen |
| 89 | * asynchronously; this is the run() method for the package |
| 90 | * refreshing thread. |
| 91 | **/ |
| 92 | public void run() |
| 93 | { |
| 94 | // This thread loops forever, thus it should |
| 95 | // be a daemon thread. |
| 96 | Bundle[] bundles = null; |
| 97 | while (true) |
| 98 | { |
| 99 | synchronized (this) |
| 100 | { |
| 101 | // Wait for a refresh request. |
| 102 | while (m_reqBundles == null) |
| 103 | { |
| 104 | try |
| 105 | { |
| 106 | wait(); |
| 107 | } |
| 108 | catch (InterruptedException ex) |
| 109 | { |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Get the bundles parameter for the current |
| 114 | // refresh request. |
| 115 | if (m_reqBundles != null) |
| 116 | { |
| 117 | bundles = m_reqBundles[0]; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Perform refresh. |
| 122 | m_felix.refreshPackages(bundles); |
| 123 | |
| 124 | // Remove the first request since it is now completed. |
| 125 | synchronized (this) |
| 126 | { |
| 127 | if (m_reqBundles.length == 1) |
| 128 | { |
| 129 | m_reqBundles = null; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | Bundle[][] newReqBundles = new Bundle[m_reqBundles.length - 1][]; |
| 134 | System.arraycopy(m_reqBundles, 1, |
| 135 | newReqBundles, 0, m_reqBundles.length - 1); |
| 136 | m_reqBundles = newReqBundles; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | public ExportedPackage[] getExportedPackages(String name) |
| 143 | { |
| 144 | // TODO: Implement PackageAdmin.getExportedPackages() |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | public boolean resolveBundles(Bundle[] bundles) |
| 149 | { |
| 150 | // TODO: Implement PackageAdmin.resolveBundles() |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | public RequiredBundle[] getRequiredBundles(String symbolicName) |
| 155 | { |
| 156 | // TODO: Implement PackageAdmin.getRequiredBundles() |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | public Bundle[] getBundles(String symbolicName, String versionRange) |
| 161 | { |
| 162 | // TODO: Implement PackageAdmin.getBundles() |
| 163 | return null; |
| 164 | } |
| 165 | |
| 166 | public Bundle[] getFragments(Bundle bundle) |
| 167 | { |
| 168 | // TODO: Implement PackageAdmin.getFragments() |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | public Bundle[] getHosts(Bundle bundle) |
| 173 | { |
| 174 | // TODO: Implement PackageAdmin.getHosts() |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | public Bundle getBundle(Class clazz) |
| 179 | { |
| 180 | // TODO: Implement PackageAdmin.getBundle() |
| 181 | return null; |
| 182 | } |
| 183 | |
| 184 | public int getBundleType(Bundle bundle) |
| 185 | { |
| 186 | // TODO: Implement PackageAdmin.getBundleType() |
| 187 | return 0; |
| 188 | } |
| 189 | } |