00001 package org.openmobileis.oscar.bundlemanager;
00002
00003 import java.io.BufferedInputStream;
00004 import java.io.File;
00005 import java.io.FileInputStream;
00006 import java.io.IOException;
00007
00008 import org.openmobileis.common.util.collection.Array;
00009 import org.openmobileis.common.util.exception.ServiceException;
00010 import org.openmobileis.common.util.file.ZipUtilities;
00011 import org.openmobileis.modules.profils.embedded.data.ProfilManager;
00012 import org.openmobileis.modules.profils.embedded.data.ProfilModule;
00013 import org.openmobileis.oscar.OscarBundleUtils;
00014 import org.osgi.framework.BundleContext;
00015
00016 public class OscarBundleManager {
00017
00018 private static BundleContext context = null;
00019 private static OscarBundleManager manager = null;
00020 private static OscarBundleUtils utils;
00021
00022 private String packagesPath = null;
00023
00024 private OscarBundleManager() {
00025 utils = new OscarBundleUtils(context, System.getProperty("user.dir")+File.separator+"WEB-INF"+File.separator+"bundles"+File.separator);
00026 this.packagesPath = System.getProperty("user.dir")+File.separator+"synchro"+File.separator+"packages"+File.separator;
00027 }
00028
00029 public static OscarBundleManager getManager() {
00030 if (manager == null) {
00031 synchronized(OscarBundleManager.class) {
00032 if (manager == null) {
00033 manager = new OscarBundleManager();
00034 }
00035 }
00036
00037 }
00038 return manager;
00039 }
00040
00041 public static void setContext(BundleContext context) {
00042 OscarBundleManager.context = context;
00043 if (utils != null) {
00044 utils.setContext(context);
00045 } else {
00046 OscarBundleManager.getManager();
00047 }
00048 }
00049
00050 private void checkUtils() throws ServiceException {
00051 if (utils == null) {
00052 throw new ServiceException("OscarBundleManager: utils no loaded");
00053 }
00054
00055 }
00056
00057 public void startBundles() throws ServiceException {
00058 ServiceException firstException = null;
00059 Array modules = ProfilManager.getManager().getProfilModules();
00060 if (modules != null) {
00061 for (int i = 0; i < modules.size(); i++) {
00062 ProfilModule module = (ProfilModule)modules.get(i);
00063 try {
00064 System.out.println("********* DEBUG "+module.getName());
00065 if (isLoadablePackage(module.getName())) {
00066 this.loadPackage(module.getName());
00067 }
00068 this.install(module.getName());
00069 this.start(module.getName());
00070 } catch (ServiceException ex) {
00071 System.out.println("Unable to start bundle : "+module.getName());
00072 if (firstException == null) {
00073 firstException = ex;
00074 }
00075 }
00076 }
00077 }
00078
00079 if (firstException != null) {
00080 throw firstException;
00081 }
00082 }
00083
00084 public void install(String name) throws ServiceException {
00085 this.checkUtils();
00086 utils.installWithFileName(name);
00087 }
00088
00089 public void update(String name) throws ServiceException {
00090 this.checkUtils();
00091 utils.updateBundleWithFileName(name);
00092 }
00093
00094 public void uninstall(String name) throws ServiceException {
00095 this.checkUtils();
00096 utils.uninstallBundleWithFileName(name);
00097 }
00098
00099 public void start(String name) throws ServiceException {
00100 this.checkUtils();
00101 utils.startBundleWithFileName(name);
00102 }
00103
00104 public void stop(String name) throws ServiceException {
00105 this.checkUtils();
00106 utils.stopBundleWithFileName(name);
00107 }
00108
00109 public void restart(String name) throws ServiceException {
00110 this.checkUtils();
00111 utils.restartBundleWithFileName(name);
00112 }
00113
00114 public void refresh(String name) throws ServiceException {
00115 this.checkUtils();
00116 utils.refreshBundleWithFileName(name);
00117 }
00118
00119 public void shutdown() throws ServiceException {
00120 this.checkUtils();
00121 utils.shutdown();
00122 }
00123
00124 public void loadPackage(String packageName) throws ServiceException {
00125 File packageFile = new File(this.packagesPath+packageName+".pkg");
00126 if (packageFile.length() > Integer.MAX_VALUE) {
00127
00128 throw new ServiceException("File is too long : "+packageFile.length()+" bytes !!!");
00129 }
00130 try {
00131 FileInputStream instr = new FileInputStream(packageFile);
00132 BufferedInputStream bufstr = new BufferedInputStream(instr);
00133 ZipUtilities.unZipData(bufstr, System.getProperty("user.dir")+File.separator);
00134 packageFile.delete();
00135 } catch (IOException ex) {
00136 throw new ServiceException(ex);
00137 }
00138 }
00139
00140 public boolean isLoadablePackage(String packageName) {
00141 File packageFile = new File(this.packagesPath+packageName+".pkg");
00142 if (packageFile.exists() && packageFile.canRead() && packageFile.isFile()) {
00143 return true;
00144 }
00145 return false;
00146 }
00147
00148 public boolean isInstalledBundle(String name) {
00149 return false;
00150 }
00151
00152 }