ModuleBundleActivator.java

00001 
00004 package org.openmobileis.bundle.osgi.terminal;
00005 
00006 import java.io.InputStream;
00007 import java.io.InputStreamReader;
00008 import java.net.URL;
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.LogManager;
00018 import org.openmobileis.embedded.webserver.templates.TemplateRetrieverManagerService;
00019 import org.openmobileis.embedded.webserver.templates.TemplateRetrieverService;
00020 import org.openmobileis.module.core.ProfileModule;
00021 import org.openmobileis.module.core.ProfileModuleSynchroListener;
00022 import org.openmobileis.module.core.XmlModuleParser;
00023 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00024 import org.openmobileis.services.Service;
00025 import org.openmobileis.services.common.ServiceManager;
00026 import org.openmobileis.synchro.openmsp.client.OpenMSPSyncListener;
00027 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00028 import org.osgi.framework.BundleActivator;
00029 import org.osgi.framework.BundleContext;
00030 import org.osgi.framework.ServiceReference;
00031 
00036 public final class ModuleBundleActivator implements BundleActivator {
00037         ApplicationContextManager manager = ApplicationContextManager.getManager();
00038         private Array registeredServiceList;
00039 
00040         /* (non-Javadoc)
00041          * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
00042          */
00043         public void start(BundleContext bundleContext) throws Exception {
00044           //init class loading
00045           ClassLoader bundleLoader = this.getClass().getClassLoader();
00046           ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
00047           Thread.currentThread().setContextClassLoader(bundleLoader);
00048           BunbleContextManager.getManager().setBundleContext(bundleContext);
00049 
00050           //TODO use log service.
00051                 //init log
00052         /*      Properties props = new Properties();
00053                 props.put("org.openmobileis.common.log.file", System.getProperty("user.dir") + "/WEB-INF/log/openmislog.txt");
00054                 File file = new File(System.getProperty("user.dir") + "/WEB-INF/log");
00055                 if (file.exists())
00056                         FileOpenCloseLogManager.registerLogManager(props); //log to a file
00057                 else
00058                         LogManager.registerLogManager(null); //log to the console */
00059 
00060                 //joint webserver session if init made in another thread than the web server.
00061                 SessionContextManager.getManager().joinSessionContext("OPENMOBILEISSESSION");
00062                 
00063                 //init template management
00064                 LogManager.traceDebug(0, "ModuleBundleActivator start before init template.");
00065                 try {
00066                         ServiceReference[] refs = bundleContext.getServiceReferences(TemplateRetrieverManagerService.class.getName(), null);
00067                         if (refs != null) {
00068                                 TemplateRetrieverManagerService templatemanager = (TemplateRetrieverManagerService) bundleContext.getService(refs[0]);
00069                         String rootTemplatePath = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00070                         TemplateRetrieverService retriever = new TemplateRetrieverServiceImpl(bundleContext.getBundle().getSymbolicName(), rootTemplatePath);
00071                                 templatemanager.registerTemplateRetriever(retriever);
00072                                 bundleContext.ungetService(refs[0]);
00073                                 refs[0] = null;
00074                         }
00075                 } catch (Throwable ex) {
00076                         LogManager.traceError(0, ex);
00077                 }
00078 
00079                 //init module
00080                 // get module XML
00081                 LogManager.traceDebug(0, "ModuleBundleActivator start before load descriptor.");
00082                 URL url = bundleContext.getBundle().getEntry("/moduledescriptor.xml");
00083                 InputStream in = url.openStream();
00084                 XmlModuleParser parser = new XmlModuleParser(new InputStreamReader(in));
00085                 parser.parse();
00086                 ProfileModule module = parser.getModuletoload();
00087                 this.loadModule(module, bundleLoader);
00088           Thread.currentThread().setContextClassLoader(oldLoader);
00089 
00090         }
00091 
00092         /* (non-Javadoc)
00093          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00094          */
00095         private void loadModule(ProfileModule module, ClassLoader bundleLoader) throws ServiceException {
00096                 String loaderClassName = module.getModuleLoaderClass();
00097                 if (loaderClassName != null)    {
00098                         try     {
00099                                 RubricLoader loader = (RubricLoader) Thread.currentThread().getContextClassLoader().loadClass(loaderClassName).newInstance();
00100                                 this.loadRubricLoader(loader);
00101                                 //load sync listener
00102                                 Array li = module.getSynchroListenerList();
00103                                 for (int j=0; j<li.size(); j++) {
00104                                         ProfileModuleSynchroListener profillistener = (ProfileModuleSynchroListener) li.get(j);
00105                                         String listenerClass = profillistener.listernerClassName;
00106                                         if (listenerClass != null)      {
00107                                                 OpenMSPSyncListener listener = (OpenMSPSyncListener)bundleLoader.loadClass(listenerClass).newInstance();
00108                                                 Object loadedlistener = OpenMSPSynchroManager.getManager().getListenerByName(listener.getSyncName());
00109                                                 LogManager.traceDebug(0, "ModuleBundleActivator loadModule loadedlistener :"+loadedlistener);
00110                                                 if (loadedlistener == null){
00111                                                         LogManager.traceDebug(0, "ModuleBundleActivator loadModule addListener :"+listener.getSyncName());
00112                                                         OpenMSPSynchroManager.getManager().addListener(listener, profillistener.dependsList);
00113                                                 }
00114                                         }
00115                                 }
00116                                 
00117                         } catch (Throwable ex)  {
00118                                 LogManager.traceError(0, "ModuleBundleActivator loadModule : Exception during module "+module.getName()+" loading :"+ex.getMessage());
00119                                 LogManager.traceError(0, ex);
00120                                 throw new ServiceException(ex);
00121                         }
00122                 }
00123         }
00124 
00125   private void loadRubricLoader(RubricLoader rubricLoader) throws ServiceException  {
00126     rubricLoader.preLoadingInit();
00127     // load services
00128     Array serviceList = rubricLoader.loadService();
00129     registeredServiceList = new Array();
00130     for (int i=0; i<serviceList.size(); i++)  {
00131       Service service = (Service)serviceList.get(i);
00132       registeredServiceList.add(service.getServiceUri());
00133       ServiceManager.getManager().loadService(service);
00134     }
00135     // load listener
00136     rubricLoader.postLoadingInit();
00137   }
00138   
00139   private void unregisterServices()     {
00140     for (int i=0; i<registeredServiceList.size(); i++)  {
00141       String serviceURI = (String)registeredServiceList.get(i);
00142       ServiceManager.getManager().unloadService(serviceURI);
00143     }
00144   }
00145 
00146         /* (non-Javadoc)
00147          * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
00148          */
00149         public void stop(BundleContext bundleContext) throws Exception {
00150                 LogManager.traceDebug(0, "ModuleBundleActivator stop.");
00151                 this.unregisterServices();
00152         }
00153 
00154 }

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