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.util.Properties;
00009 
00010 import org.openmobileis.bundle.osgi.services.BunbleContextManager;
00011 import org.openmobileis.bundle.osgi.services.TemplateRetrieverServiceImpl;
00012 import org.openmobileis.common.context.ApplicationContextManager;
00013 import org.openmobileis.common.context.SessionContextManager;
00014 import org.openmobileis.common.util.PropertiesManager;
00015 import org.openmobileis.common.util.collection.Array;
00016 import org.openmobileis.common.util.exception.ServiceException;
00017 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00018 import org.openmobileis.common.util.log.LogManager;
00019 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00020 import org.openmobileis.database.fastobjectdb.synchro.client.SynchroFastObjectDBManager;
00021 import org.openmobileis.embedded.webserver.WebServer;
00022 import org.openmobileis.embedded.webserver.templates.Freemarkerv1TemplateDelegate;
00023 import org.openmobileis.embedded.webserver.templates.TemplateManager;
00024 import org.openmobileis.embedded.webserver.templates.TemplateRetrieverManagerService;
00025 import org.openmobileis.services.Service;
00026 import org.openmobileis.services.common.ServiceManager;
00027 import org.osgi.framework.BundleActivator;
00028 import org.osgi.framework.BundleContext;
00029 import org.osgi.framework.ServiceRegistration;
00030 
00031 public class OpenMISBundleActivator implements BundleActivator {
00032         ApplicationContextManager manager = ApplicationContextManager.getManager();
00033         private ServiceRegistration regTemplate=null;
00034         private Array loadedServiceList;
00035         private WebServer server;
00036 
00037         class StartWebServertThread implements Runnable {
00038                 public void run()       {
00039             //start web server
00040                         try  {
00041                                 System.out.println("start WebServer");
00042 
00043                                 server = new WebServer();
00044                 server.serve();
00045                 } catch (IOException ex)        {}
00046                 }
00047         }
00048 
00049   /* (non-Javadoc)
00050    * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
00051    */
00052   public void start(BundleContext bundleContext) throws Exception {
00053           ClassLoader bundleLoader = this.getClass().getClassLoader();
00054           ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
00055           Thread.currentThread().setContextClassLoader(bundleLoader);
00056 
00057           BunbleContextManager.getManager().setBundleContext(bundleContext);
00058 
00059           //init log
00060             Properties  props = new Properties();
00061             props.put("org.openmobileis.common.log.file", System.getProperty("user.dir")+"/WEB-INF/log/openmislog.txt");
00062             File file = new File(System.getProperty("user.dir")+"/WEB-INF/log");
00063             if (file.exists())   FileOpenCloseLogManager.registerLogManager(props); //log to a file
00064             else LogManager.registerLogManager(null); //log to a file
00065 
00066             //init application context and plateform
00067             ApplicationContextManager.getManager();
00068             ApplicationContextManager.getManager().getApplicationContext().setBundleLoaded(true);
00069 
00070             //register property service.
00071             PropertiesManager.getManager().addProperty("test", "testpropoerty");
00072 
00073             //manage session
00074           SessionContextManager.getManager();
00075 
00076             //init FODB management
00077             PropertiesManager.getManager().addProperty("fastobjectdb.database.path", System.getProperty("user.dir")+"/database");
00078             PropertiesManager.getManager().addProperty("fastobjectdb.database.name", "db");
00079             //init synchro properties
00080             PropertiesManager.getManager().addProperty("org.openmobileis.synchro.client.openmsp.numsyncpropsfile",System.getProperty("user.dir") + "/WEB-INF/conf/numsync.txt");
00081             try {
00082                 FastObjectDBManager.registerManager(new SynchroFastObjectDBManager()); //init synchro db to be use as default db
00083                 FastObjectDBManager.getManager();
00084             } catch (Throwable ex)  {
00085               throw new ServiceException(ex);
00086             }
00087             
00088             // init service management
00089                   ServiceManager.getManager();
00090 
00091             //start seb server
00092             Thread thread = new Thread(new StartWebServertThread());
00093             thread.start();
00094 
00095             Thread.currentThread().sleep(2000);
00096             
00097             //init template management
00098             Freemarkerv1TemplateDelegate templateDelegate = new Freemarkerv1TemplateDelegate();
00099             TemplateManager.registerTemplateDelegate(templateDelegate);
00100                 String rootTemplatePath = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00101                 file = new File(rootTemplatePath);
00102                 if (!file.exists())     {
00103                         templateDelegate.registerTemplateRetriever(new TemplateRetrieverServiceImpl(bundleContext.getBundle().getSymbolicName(), rootTemplatePath));
00104                 }
00105                 regTemplate = bundleContext.registerService (TemplateRetrieverManagerService.class.getName(), templateDelegate, null);
00106 
00107                   loadedServiceList = ServiceManager.getManager().loadRubricLoader(new OSGIOpenMISRubricLoad());
00108 
00109 /*          NumSyncManagerDB.getManager();
00110             ModuleManager.getManager().registerModuleManagerListener(new OSGIOpenMSPSyncListener());
00111             ModuleManager.getManager().initModule(); */
00112                   Thread.currentThread().setContextClassLoader(oldLoader);
00113   }
00114 
00115   /* (non-Javadoc)
00116    * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
00117    */
00118   public void stop(BundleContext context) throws Exception {
00119           //unload service
00120           for (int i=0; i< loadedServiceList.size(); i++)       {
00121                 Service service = (Service)loadedServiceList.get(i);
00122                 ServiceManager.getManager().unloadService(service.getServiceUri());
00123           }
00124           server.stopServer();
00125           
00126                 if (regTemplate != null) regTemplate.unregister();
00127  }
00128 }

Generated on Tue May 22 23:01:11 2007 for OpenMobileIS by  doxygen 1.5.1-p1