BaseBundleLoaderActivator.java

00001 
00004 package org.openmobileis.bundle.osgi.synchro;
00005 
00006 import java.io.File;
00007 import java.io.FileInputStream;
00008 import java.io.FilenameFilter;
00009 import java.util.Properties;
00010 
00011 import org.openmobileis.bundle.osgi.services.BunbleContextManager;
00012 import org.openmobileis.common.context.ApplicationContextManager;
00013 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00014 import org.openmobileis.common.util.log.LogManager;
00015 import org.openmobileis.module.profiles.server.ProfilManagerSyncTarget;
00016 import org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPSynchroTargetListener;
00017 import org.openmobileis.synchro.openmsp.server.synctarget.SynchroTargerManager;
00018 import org.openmobileis.synchro.openmsp.server.synctarget.proxy.GenericProxySynchroTarget;
00019 import org.openmobileis.synchro.openmsp.server.synctarget.proxy.ProxySyncroTarget;
00020 import org.osgi.framework.Bundle;
00021 import org.osgi.framework.InvalidSyntaxException;
00022 import org.osgi.framework.ServiceEvent;
00023 import org.osgi.framework.ServiceListener;
00024 import org.osgi.framework.ServiceReference;
00025 import org.osgi.framework.BundleActivator;
00026 import org.osgi.framework.BundleContext;
00027 import org.osgi.util.tracker.ServiceTracker;
00028 
00029 //TODO Add profil synchro after normal listener to the application. Test if profile is synchronized. OK
00030 //TODO load profile module and module terminal jar synchronisation HERE
00031 //TODO build terminal bundle 1 et 2 and load add them on the server side.
00032 //TODO load terminal module bundle when synchronized.
00033 //TODO do synchronization and depending on the profil update module v1 or V2.
00034 
00039 public final class BaseBundleLoaderActivator implements BundleActivator {
00040         ApplicationContextManager manager = ApplicationContextManager.getManager();
00041           
00042         // private ServiceRegistration synchroManagerRegistration;  
00043         private ServiceTracker servicetracker;
00044         private BundleContext bc;
00045 
00046         /* (non-Javadoc)
00047          * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
00048          */
00049         public void start(BundleContext bundleContext) throws Exception {
00050           //init class loading
00051                 this.bc = bundleContext;
00052           ClassLoader bundleLoader = this.getClass().getClassLoader();
00053           ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
00054           Thread.currentThread().setContextClassLoader(bundleLoader);
00055           BunbleContextManager.getManager().setBundleContext(bundleContext);
00056 
00057                 //init log
00058                 Properties props = new Properties();
00059                 props.put("org.openmobileis.common.log.file", System.getProperty("user.dir")+ "/"+bundleContext.getBundle().getSymbolicName() + "/WEB-INF/log/openmislog.txt");
00060                 File file = new File(props.getProperty("org.openmobileis.common.log.file"));
00061                 //try to create the log file to see if we can log in it.
00062                 boolean logInFile = false;
00063                 if (!file.exists()) {
00064                         try     {
00065                                 FileInputStream infile = new FileInputStream(file);
00066                                 infile.close();
00067                                 logInFile = true;
00068                         } catch (Throwable ex){
00069                                 
00070                         }
00071                 }
00072                 if (logInFile)
00073                         FileOpenCloseLogManager.registerLogManager(props); //log to a file
00074                 else
00075                         LogManager.registerLogManager(null); //log to a file
00076                 
00077                 //Load all bundle in bundle repository
00078                 File moduleDir =  new File(System.getProperty("user.dir")+ "/WEB-INF/modules");
00079                 if (moduleDir.exists()){
00080                         File[] listFile = moduleDir.listFiles(new FilenameFilter()      {
00081                                 public boolean accept(File dir, String name)    {
00082                                         if (name.endsWith(".jar"))      {
00083                                                 return true;
00084                                         }
00085                                         return false;
00086                                 }
00087                         });
00088                         
00089                         //register synchro proxy service
00090                         ProxySyncroTarget proxytarget = new GenericProxySynchroTarget();
00091       SynchroTargerManager.getManager().registerProxyTargetForTerminal(proxytarget, null);
00092       
00093       //add profil synchronization target
00094       SynchroTargerManager.getManager().addSynchroTargetListener(new ProfilManagerSyncTarget(), null, null);
00095 
00096       //register SynchroTargetTraker and listen for synchro target
00097       //all target are a service that register to with its bundle. 
00098       // they are call during synchro by registering them to the synchroManager.
00099       // Version is a property of the target.
00100       servicetracker = new ServiceTracker(bundleContext, OpenMSPSynchroTargetListener.class.getName(), null);
00101       servicetracker.open();    
00102       ServiceListener sl = new ServiceListener() {
00103         public void serviceChanged(ServiceEvent ev) {
00104           ServiceReference sr = ev.getServiceReference();
00105           switch(ev.getType()) {
00106           case ServiceEvent.REGISTERED:
00107             {
00108                     registerSynchroTarget(sr);
00109             }
00110             break;
00111           case ServiceEvent.UNREGISTERING:
00112             {
00113                     unregisterSynchroTarget(sr);
00114             }
00115             break;
00116           }
00117         }
00118       };
00119       String filter = "(objectclass=" + OpenMSPSynchroTargetListener.class.getName() + ")";
00120       try {
00121         bundleContext.addServiceListener(sl, filter);
00122         ServiceReference[] srl = bundleContext.getServiceReferences(null, filter);
00123         for(int i = 0; srl != null && i < srl.length; i++) {
00124                 sl.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
00125                                            srl[i]));
00126         }
00127       } catch (InvalidSyntaxException ex) { 
00128         LogManager.traceError(0, ex); 
00129       }
00130     
00131       
00132       
00133                         //install all bundle in module
00134                         for(int i=0; i<listFile.length; i++)    {
00135                                 String name = listFile[i].getName();
00136                                 try     {
00137                                         Bundle syncBundle = bundleContext.installBundle(name, new FileInputStream(listFile[i]));
00138                                         syncBundle.start();     
00139                                         LogManager.traceDebug(0, "start bundle :"+syncBundle.getLocation());
00140                                 } catch (Throwable ex){
00141                                         LogManager.traceError(0, "Error : could not start bundle :"+name+" .Catch Exception :"+ex.toString());
00142                                         LogManager.traceError(0, ex);
00143                                 }
00144                         }
00145                         
00146                 }
00147 
00148                 Thread.currentThread().setContextClassLoader(oldLoader);
00149         }
00150 
00151         /* (non-Javadoc)
00152          * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
00153          */
00154         public void stop(BundleContext bundleContext) throws Exception {
00155         }
00156 
00157         private  void registerSynchroTarget(ServiceReference sr) {
00158                 OpenMSPSynchroTargetListener service = (OpenMSPSynchroTargetListener) bc.getService(sr);
00159                 String version = (String)sr.getProperty("version");
00160                 LogManager.traceDebug(0, "BaseBundleLoaderActivator addingService register synchroTarget target :"+service.getTargetName()+" version :"+version+ " object:"+service);
00161                 SynchroTargerManager.getManager().addSynchroTargetListener(service, version, null);
00162         }
00163 
00164         private  void unregisterSynchroTarget(ServiceReference sr) {
00165                 //TODO Not supported
00166                 //to update a target the server must be restarted.
00167                 OpenMSPSynchroTargetListener service = (OpenMSPSynchroTargetListener) bc.getService(sr);
00168                 LogManager.traceDebug(0, "BaseBundleLoaderActivator removeService register synchroTarget target :"+service.getTargetName());
00169         }
00170                  
00171 }

Generated on Mon Jan 11 21:19:13 2010 for OpenMobileIS by  doxygen 1.5.4