OscarBundleUtils.java

00001 package org.openmobileis.oscar;
00002 
00003 import java.io.InputStream;
00004 import java.net.URL;
00005 
00006 import org.openmobileis.common.util.exception.ServiceException;
00007 import org.osgi.framework.Bundle;
00008 import org.osgi.framework.BundleContext;
00009 import org.osgi.framework.Constants;
00010 import org.osgi.framework.ServiceReference;
00011 import org.osgi.service.packageadmin.PackageAdmin;
00012 
00013 public class OscarBundleUtils {
00014         
00015         private BundleContext context;
00016         private String bundlePath = null;
00017         
00018         public OscarBundleUtils(BundleContext context, String path) {
00019                 this.context = context;
00020                 this.bundlePath = "file:"+path;
00021         }
00022         
00023         public void setContext(BundleContext context) {
00024                 this.context = context;
00025         }
00026         
00027         public void installWithFileName(String fileName) throws ServiceException {
00028                 if (fileName == null || fileName.trim().length() == 0) {
00029                         throw new ServiceException("OscarBundleManager::installWithFileName : received no filename");
00030                 }
00031                 
00032                 String location = bundlePath+fileName.trim();
00033                 if (!location.endsWith(".jar"));
00034                 location = location+".jar";
00035                 this.install(location);
00036         }
00037         
00038         private void install(String location) throws ServiceException {
00039                 try {
00040                         this.context.installBundle(location);
00041                 } catch (Exception ex) {
00042                         throw new ServiceException(ex);
00043                 }
00044         }
00045         
00046         public boolean isInstalledBundle(String bundleName) throws ServiceException {
00047                 return (this.getBundleIdFromName(bundleName) > 0)?true:false;
00048         }
00049         
00050         public void updateBundleWithName(String bundleName) throws ServiceException {
00051                 this.update(this.getBundleIdFromName(bundleName));
00052         }
00053         
00054         public void updateBundleWithFileName(String bundleFileName) throws ServiceException {
00055                 this.update(this.getBundleIdFromFileName(bundleFileName));
00056         }
00057         
00058         private void update(long bundleId) throws ServiceException {
00059                 this.update(bundleId, null);
00060         }
00061         
00062         public void updateBundleWithName(String bundleName, String location) throws ServiceException {
00063                 this.update(this.getBundleIdFromName(bundleName), location);
00064         }
00065         
00066         public void updateBundleWithFileName(String bundleFileName, String location) throws ServiceException {
00067                 this.update(this.getBundleIdFromFileName(bundleFileName), location);
00068         }
00069 
00070         private void update(long bundleId, String location) throws ServiceException {
00071                 Bundle bundle = this.context.getBundle(bundleId);
00072                 if (bundle != null) {
00073                         try {
00074                                 if (location != null && location.trim().length() > 0) {
00075                    InputStream is = new URL(location).openStream();
00076                    bundle.update(is);
00077                                 } else {
00078                                         bundle.update();
00079                                 }
00080                         } catch (Exception ex) {
00081                                 throw new ServiceException(ex);
00082                         }
00083                 } else {
00084                         throw new ServiceException("OscarBundleManager::update : Invalid Bundle id : "+bundleId);
00085                 }
00086         }
00087         
00088         public void uninstallBundleWithName(String bundleName) throws ServiceException {
00089                 this.uninstall(this.getBundleIdFromName(bundleName));
00090         }
00091         
00092         public void uninstallBundleWithFileName(String bundleFileName) throws ServiceException {
00093                 this.uninstall(this.getBundleIdFromFileName(bundleFileName));
00094         }
00095         
00096         private void uninstall(long bundleId) throws ServiceException {
00097                 Bundle bundle = this.context.getBundle(bundleId);
00098                 if (bundle != null) {
00099                         try {
00100                                 bundle.uninstall();
00101                         } catch (Exception ex) {
00102                                 throw new ServiceException(ex);
00103                         }
00104                 } else {
00105                         throw new ServiceException("OscarBundleManager::uninstall : Invalid Bundle id : "+bundleId);
00106                 }
00107         }
00108         
00109         public void startBundleWithName(String bundleName) throws ServiceException {
00110                 this.start(this.getBundleIdFromName(bundleName));
00111         }
00112         
00113         public void startBundleWithFileName(String bundleFileName) throws ServiceException {
00114                 this.start(this.getBundleIdFromFileName(bundleFileName));
00115         }
00116         
00117         private void start(long bundleId) throws ServiceException {
00118                 Bundle bundle = this.context.getBundle(bundleId);
00119                 if (bundle != null) {
00120                         try {
00121                                 bundle.start();
00122                         } catch (Exception ex) {
00123                                 throw new ServiceException(ex);
00124                         }
00125                 } else {
00126                         throw new ServiceException("OscarBundleManager::start : Invalid Bundle id : "+bundleId);
00127                 }               
00128         }
00129         
00130         public void stopBundleWithName(String bundleName) throws ServiceException {
00131                 this.stop(this.getBundleIdFromName(bundleName));
00132         }
00133         
00134         public void stopBundleWithFileName(String bundleFileName) throws ServiceException {
00135                 this.stop(this.getBundleIdFromFileName(bundleFileName));
00136         }
00137         
00138         private void stop(long bundleId) throws ServiceException {
00139                 Bundle bundle = this.context.getBundle(bundleId);
00140                 if (bundle != null) {
00141                         try {
00142                                 bundle.stop();
00143                         } catch (Exception ex) {
00144                                 throw new ServiceException(ex);
00145                         }
00146                 } else {
00147                         throw new ServiceException("OscarBundleManager::stop : Invalid Bundle id : "+bundleId);
00148                 }
00149         }
00150         
00151         public void restartBundleWithName(String bundleName) throws ServiceException {
00152                 this.restart(this.getBundleIdFromName(bundleName));
00153         }
00154         
00155         public void restartBundleWithFileName(String bundleFileName) throws ServiceException {
00156                 this.restart(this.getBundleIdFromFileName(bundleFileName));
00157         }
00158         
00159         private void restart(long bundleId) throws ServiceException {
00160                 this.stop(bundleId);
00161                 this.start(bundleId);
00162         }
00163         
00164         public void refreshBundleWithName(String bundleName) throws ServiceException {
00165                 if (bundleName == null) { // We want to refresh all bundles
00166                         this.refresh(-1);
00167                 } else {
00168                         this.refresh(this.getBundleIdFromName(bundleName));
00169                 }
00170         }
00171         
00172         public void refreshBundleWithFileName(String bundleFileName) throws ServiceException {
00173                 if (bundleFileName == null) { // We want to refresh all bundles
00174                         this.refresh(-1);
00175                 } else {
00176                         this.refresh(this.getBundleIdFromFileName(bundleFileName));
00177                 }
00178         }
00179         
00180         private void refresh(long bundleId) throws ServiceException {
00181         // Get package admin service.
00182         ServiceReference ref = this.context.getServiceReference(
00183             org.osgi.service.packageadmin.PackageAdmin.class.getName());
00184         if (ref == null)
00185         {
00186                 throw new ServiceException("OscarBundleManager::refresh : PackageAdmin service is unavailable.");
00187         }
00188 
00189         PackageAdmin pa = (PackageAdmin) this.context.getService(ref);
00190         if (pa == null)
00191         {
00192                 throw new ServiceException("OscarBundleManager::refresh : PackageAdmin service is unavailable.");
00193         }
00194 
00195         if (bundleId > 0) { // We want to refresh a specific bundle
00196                 Bundle bundle = this.context.getBundle(bundleId);
00197                 if (bundle != null) {
00198                         pa.refreshPackages(new Bundle[] {bundle});
00199                 } else {
00200                         throw new ServiceException("OscarBundleManager::refresh : Bundle not found : "+bundleId);
00201                 }
00202         } else {
00203                 pa.refreshPackages(null);
00204         }
00205         }
00206         
00207         private long getBundleIdFromName(String bundleName) throws ServiceException {
00208                 
00209                 if (bundleName == null || bundleName.trim().length() == 0) {
00210                         throw new ServiceException("OscarBundleManager::getBundleIdFormName : received empty bundle name");
00211                 }
00212                 
00213                 bundleName = bundleName.trim();
00214                 
00215                 Bundle[] bundles = this.context.getBundles();
00216                 
00217                 if (bundles == null) {
00218                         return -1;
00219                 }
00220                 
00221                 for (int i = 0; i < bundles.length; i++) {
00222             // Get the bundle name or location.
00223             String name = (String)
00224                 bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
00225             
00226             if (bundleName.equals(name)) {
00227                 return bundles[i].getBundleId();
00228             }
00229                 }
00230                 
00231                 // No bundle matched the name
00232                 return -1;
00233         }
00234         
00235         private long getBundleIdFromFileName(String bundleFileName) throws ServiceException {
00236                 
00237                 if (bundleFileName == null || bundleFileName.trim().length() == 0) {
00238                         throw new ServiceException("OscarBundleManager::getBundleIdFormName : received empty bundle name");
00239                 }
00240                 
00241                 bundleFileName = bundleFileName.trim();
00242                 
00243                 Bundle[] bundles = this.context.getBundles();
00244                 
00245                 if (bundles == null) {
00246                         return -1;
00247                 }
00248                 
00249                 for (int i = 0; i < bundles.length; i++) {
00250             // Get the bundle name or location.
00251             String fullName = (String)
00252                 bundles[i].getLocation();
00253             
00254             int slash = fullName.lastIndexOf("/");
00255             
00256             String fileNameWithExtension;
00257             if (slash != -1) {
00258                 fileNameWithExtension = fullName.substring(slash+1);
00259             } else {
00260                 fileNameWithExtension = fullName;
00261             }
00262             
00263             String fileName;
00264             int dot = fileNameWithExtension.indexOf(".");
00265             if (dot != -1) {
00266                 fileName = fileNameWithExtension.substring(0, dot);
00267             } else {
00268                 fileName = fileNameWithExtension;
00269             }
00270             
00271             if (bundleFileName.equals(fileName)) {
00272                 return bundles[i].getBundleId();
00273             }
00274                 }
00275                 
00276                 // No bundle matched the name
00277                 return -1;
00278         }
00279         
00280         public void shutdown() throws ServiceException {
00281                 Bundle bundle = this.context.getBundle(0);
00282                 try {
00283                         bundle.stop();
00284                 } catch (Exception ex) {
00285                         throw new ServiceException(ex);
00286                 }
00287         }
00288         
00289 }

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1