00001 package org.openmobileis.bundle.osgi.terminal;
00002
00003 import java.io.File;
00004 import java.io.FileInputStream;
00005
00006 import org.openmobileis.common.util.exception.ServiceException;
00007 import org.openmobileis.common.util.log.LogManager;
00008 import org.openmobileis.module.core.ProfileModule;
00009 import org.openmobileis.module.terminal.ModuleManager;
00010 import org.openmobileis.module.terminal.ModuleManagerListener;
00011 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00012 import org.osgi.framework.Bundle;
00013 import org.osgi.framework.BundleContext;
00014 import org.osgi.service.packageadmin.PackageAdmin;
00015 import org.osgi.framework.ServiceReference;
00016
00017
00018 public final class OSGIModuleManagerListener implements ModuleManagerListener {
00019 private BundleContext bundleContext;
00020
00021 private PackageAdmin packageAdmin = null;
00022
00023 public OSGIModuleManagerListener(BundleContext bundleContext) {
00024 this.bundleContext = bundleContext;
00025 ServiceReference sr = bundleContext.getServiceReference(PackageAdmin.class.getName());
00026 if (sr != null) {
00027 packageAdmin = (PackageAdmin) bundleContext.getService(sr);
00028 }
00029 LogManager.traceDebug(0, "OSGIModuleManagerListener init packageAdmin:"+packageAdmin);
00030 }
00031
00032 public String getJarInstallPath() {
00033 return System.getProperty("user.dir")+"/WEB-INF/module";
00034 }
00035
00036 public String getXMLInstallPath() {
00037 return System.getProperty("user.dir")+"/WEB-INF/module";
00038 }
00039
00040 public void initManager() throws ServiceException {
00041 }
00042
00043 public void notifyModuleLoading(ProfileModule module) throws ServiceException {
00044 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleLoading :"+module.getName());
00045
00046 Bundle[] bundles = packageAdmin.getBundles(module.getName(), null);
00047 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleLoading found bundle :"+bundles);
00048 if (bundles != null) {
00049 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleLoading found bundles size:"+bundles.length);
00050 return;
00051 }
00052
00053 String filename = ModuleManager.getManager().getModuleRepositoryPath()+File.separator+ module.getName()+".jar";
00054 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleLoading filename :"+filename);
00055 File file = new File(filename);
00056 if (file.exists()) {
00057 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleLoading load module.");
00058 try {
00059 Bundle syncBundle = this.bundleContext.installBundle(module.getName(), new FileInputStream(filename));
00060 LogManager.traceDebug(0, "OSGIModuleManagerListener start bundle BEGIN :"+syncBundle.getLocation());
00061 syncBundle.start();
00062 OpenMSPSynchroManager.getManager().restartSynchro();
00063 LogManager.traceDebug(0, "OSGIModuleManagerListener start bundle END :"+syncBundle.getLocation());
00064 } catch (Throwable ex){
00065 LogManager.traceError(0, "Error : could not start bundle :"+module.getName()+" .Catch Exception :"+ex.toString());
00066 LogManager.traceError(0, ex);
00067 }
00068 }
00069 }
00070
00071 public void notifyModuleUpdate(ProfileModule module) throws ServiceException {
00072 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleUpdate :"+module.getName());
00073
00074 if (module.getName().equals(ModuleManager.OpenMobileISModuleName)) {
00075 return;
00076 }
00077
00078 Bundle[] bundles = packageAdmin.getBundles(module.getName(), null);
00079 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleUpdate found bundle :"+bundles);
00080 if (bundles != null) {
00081 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleUpdate found bundles size:"+bundles.length);
00082 if (bundles.length >0){
00083 for (int i=0; i<bundles.length;i++){
00084 try {
00085 String filename = ModuleManager.getManager().getModuleRepositoryPath()+File.separator+ module.getName()+".jar";
00086 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleUpdate filename :"+filename);
00087 File file = new File(filename);
00088 if (file.exists()) {
00089 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleUpdate load module.");
00090 bundles[i].update( new FileInputStream(filename));
00091 }
00092 } catch (Throwable ex){
00093 LogManager.traceDebug(0, "OSGIModuleManagerListener notifyModuleUpdate error during uninstall bundle :"+module.getName()+" ->"+ex.toString());
00094 LogManager.traceError(0, ex);
00095 }
00096 }
00097 }
00098 } else {
00099
00100 this.notifyModuleLoading(module);
00101 }
00102 OpenMSPSynchroManager.getManager().restartSynchro();
00103 }
00104
00105 }