OpenMISBundleActivator.java

00001 /*
00002  * Created on Sun Feb 18 21:49:19 CET 2007
00003  */
00004 package org.openmobileis.bundle.osgi.terminal;
00005 
00006 import java.io.File;
00007 import java.io.IOException;
00008 import java.io.InputStream;
00009 import java.io.InputStreamReader;
00010 import java.util.Properties;
00011 
00012 import org.openmobileis.bundle.osgi.services.BunbleContextManager;
00013 import org.openmobileis.bundle.osgi.services.TemplateRetrieverServiceImpl;
00014 import org.openmobileis.common.context.ApplicationContextManager;
00015 import org.openmobileis.common.context.SessionContextManager;
00016 import org.openmobileis.common.util.PropertiesManager;
00017 import org.openmobileis.common.util.collection.Array;
00018 import org.openmobileis.common.util.exception.ServiceException;
00019 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00020 import org.openmobileis.common.util.log.LogManager;
00021 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00022 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDBManager;
00023 import org.openmobileis.embedded.webserver.WebServer;
00024 import org.openmobileis.embedded.webserver.templates.Freemarkerv1TemplateDelegate;
00025 import org.openmobileis.embedded.webserver.templates.TemplateManager;
00026 import org.openmobileis.embedded.webserver.templates.TemplateRetrieverManagerService;
00027 import org.openmobileis.module.core.ProfileModule;
00028 import org.openmobileis.module.core.XmlModuleParser;
00029 import org.openmobileis.module.terminal.ModuleManager;
00030 import org.openmobileis.modules.common.log.terminal.LogsSynchroListener;
00031 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00032 import org.openmobileis.services.Service;
00033 import org.openmobileis.services.common.ServiceManager;
00034 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00035 import org.osgi.framework.BundleActivator;
00036 import org.osgi.framework.BundleContext;
00037 import org.osgi.framework.ServiceRegistration;
00038 
00039 public class OpenMISBundleActivator implements BundleActivator {
00040         ApplicationContextManager manager = ApplicationContextManager.getManager();
00041         private ServiceRegistration regTemplate=null;
00042 //      private Array loadedServiceList;
00043         private WebServer server;
00044         private OSGIModuleManagerListener listener = null;
00045 
00046         class StartWebServertThread implements Runnable {
00047                 public void run()       {
00048             //start web server
00049                         try  {
00050                                 System.out.println("start WebServer");
00051 
00052                                 server = new WebServer();
00053                 server.serve();
00054                 } catch (IOException ex)        {}
00055                 }
00056         }
00057 
00058   /* (non-Javadoc)
00059    * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
00060    */
00061   public void start(BundleContext bundleContext) throws Exception {
00062           ClassLoader bundleLoader = this.getClass().getClassLoader();
00063           ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
00064           Thread.currentThread().setContextClassLoader(bundleLoader);
00065 
00066           BunbleContextManager.getManager().setBundleContext(bundleContext);
00067 
00068     //init synchro properties
00069     PropertiesManager.getManager().addProperty("org.openmobileis.synchro.client.openmsp.numsyncpropsfile",System.getProperty("user.dir") + "/WEB-INF/conf/numsync.txt");
00070 
00071             //init application context and plateform
00072             ApplicationContextManager.getManager();
00073             ApplicationContextManager.getManager().getApplicationContext().setBundleLoaded(true);
00074 
00075             //register property service.
00076 //          PropertiesManager.getManager().addProperty("test", "testpropoerty");
00077 
00078             //manage session
00079           SessionContextManager.getManager();
00080 
00081             //init FODB management
00082             PropertiesManager.getManager().addProperty("fastobjectdb.database.path", System.getProperty("user.dir")+"/database");
00083             PropertiesManager.getManager().addProperty("fastobjectdb.database.name", "db");
00084             try {
00085                 FastObjectDBManager.registerManager(new SynchroFastObjectDBManager()); //init synchro db to be use as default db
00086                 FastObjectDBManager.getManager();
00087             } catch (Throwable ex)  {
00088               throw new ServiceException(ex);
00089             }
00090             
00091             // init service management
00092                   ServiceManager.getManager();
00093 
00094             //start seb server
00095             Thread thread = new Thread(new StartWebServertThread());
00096             thread.setContextClassLoader(bundleLoader);
00097             thread.start();
00098 
00099             Thread.currentThread().sleep(2000); //TODO synchronize properties loading and server loading.
00100 
00101                   //init log
00102             Properties  props = new Properties();
00103             props.put("org.openmobileis.common.log.file", System.getProperty("user.dir")+"/WEB-INF/log/openmislog.txt");
00104             File file = new File(System.getProperty("user.dir")+"/WEB-INF/log");
00105             if (file.exists()){
00106                 FileOpenCloseLogManager.registerLogManager(props); //log to a file
00107                         OpenMSPSynchroManager.getManager().addListener(new LogsSynchroListener(), null);
00108                         LogManager.traceDebug(0, "LOG LISTENER REGISTERED.");
00109             }
00110             else LogManager.registerLogManager(null); //log to a file
00111             
00112             //init template management
00113             Freemarkerv1TemplateDelegate templateDelegate = new Freemarkerv1TemplateDelegate();
00114             TemplateManager.registerTemplateDelegate(templateDelegate);
00115                 String rootTemplatePath = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00116                 file = new File(rootTemplatePath);
00117                 if (!file.exists())     {
00118                         templateDelegate.registerTemplateRetriever(new TemplateRetrieverServiceImpl(bundleContext.getBundle().getSymbolicName(), rootTemplatePath));
00119                 }
00120                 regTemplate = bundleContext.registerService (TemplateRetrieverManagerService.class.getName(), templateDelegate, null);
00121 
00122 //                loadedServiceList = ServiceManager.getManager().loadRubricLoader(new OSGIOpenMISRubricLoad());
00123                 //load Open Mobile IS module xml descrptor
00124                         InputStream in = bundleLoader.getResourceAsStream("/moduledescriptor.xml");
00125                         XmlModuleParser parser = new XmlModuleParser(new InputStreamReader(in));
00126                         parser.parse();
00127                         ProfileModule module = parser.getModuletoload();
00128                         this.loadModule(module);
00129 
00130 
00131 /*          NumSyncManagerDB.getManager();
00132             ModuleManager.getManager().registerModuleManagerListener(new OSGIOpenMSPSyncListener());
00133             ModuleManager.getManager().initModule(); */
00134                   
00135                   //init module 
00136                   listener = new OSGIModuleManagerListener(bundleContext);
00137                   ModuleManager.getManager().registerModuleManagerListener(listener);
00138                   ModuleManager.getManager().initModule();
00139                   
00140                   
00141                   Thread.currentThread().setContextClassLoader(oldLoader);
00142   }
00143 
00144         /* (non-Javadoc)
00145          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00146          */
00147         private void loadModule(ProfileModule module) throws ServiceException {
00148                 String loaderClassName = module.getModuleLoaderClass();
00149                 if (loaderClassName != null)    {
00150                         try     {
00151                                 RubricLoader loader = (RubricLoader) Thread.currentThread().getContextClassLoader().loadClass(loaderClassName).newInstance();
00152                                 this.loadRubricLoader(loader);
00153                         } catch (Throwable ex)  {
00154                                 LogManager.traceError(0, "StaticModuleManagerListener notifyModuleLoading : Exception during module "+module.getName()+" loading :"+ex.getMessage());
00155                                 throw new ServiceException(ex);
00156                         }
00157                 }
00158         }
00159 
00160   private void loadRubricLoader(RubricLoader rubricLoader) throws ServiceException  {
00161     rubricLoader.preLoadingInit();
00162     // load services
00163     Array serviceList = rubricLoader.loadService();
00164     for (int i=0; i<serviceList.size(); i++)  {
00165       Service service = (Service)serviceList.get(i);
00166       ServiceManager.getManager().loadService(service);
00167     }
00168     // load listener
00169     rubricLoader.postLoadingInit();
00170   }
00171   /* (non-Javadoc)
00172    * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
00173    */
00174   public void stop(BundleContext context) throws Exception {
00175           //unload service
00176 /*        for (int i=0; i< loadedServiceList.size(); i++)       {
00177                 Service service = (Service)loadedServiceList.get(i);
00178                 ServiceManager.getManager().unloadService(service.getServiceUri());
00179           } */
00180           server.stopServer();
00181           
00182                 if (regTemplate != null) regTemplate.unregister();
00183  }
00184 }

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