TestServiceClient.java

00001 
00004 package org.openmobileis.bundle.osgi.terminal.test;
00005 
00006 import java.io.File;
00007 import java.io.InputStream;
00008 import java.io.InputStreamReader;
00009 import java.util.Properties;
00010 
00011 import org.openmobileis.bundle.osgi.services.BunbleContextManager;
00012 import org.openmobileis.bundle.osgi.services.TemplateRetrieverServiceImpl;
00013 import org.openmobileis.common.context.ApplicationContextManager;
00014 import org.openmobileis.common.context.SessionContextManager;
00015 import org.openmobileis.common.util.PropertiesManager;
00016 import org.openmobileis.common.util.collection.Array;
00017 import org.openmobileis.common.util.exception.ServiceException;
00018 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00019 import org.openmobileis.common.util.log.LogManager;
00020 import org.openmobileis.embedded.webserver.templates.TemplateRetrieverManagerService;
00021 import org.openmobileis.embedded.webserver.templates.TemplateRetrieverService;
00022 import org.openmobileis.module.core.ProfileModule;
00023 import org.openmobileis.module.core.XmlModuleParser;
00024 import org.openmobileis.modules.profiles.terminal.RubricLoader;
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.ServiceReference;
00030 
00035 public final class TestServiceClient implements BundleActivator {
00036         ApplicationContextManager manager = ApplicationContextManager.getManager();
00037         private Array registeredServiceList;
00038 
00039         /* (non-Javadoc)
00040          * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
00041          */
00042         public void start(BundleContext bundleContext) throws Exception {
00043           //init class loading
00044           ClassLoader bundleLoader = this.getClass().getClassLoader();
00045           ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
00046           Thread.currentThread().setContextClassLoader(bundleLoader);
00047           BunbleContextManager.getManager().setBundleContext(bundleContext);
00048 
00049                 //init log
00050                 Properties props = new Properties();
00051                 props.put("org.openmobileis.common.log.file", System.getProperty("user.dir") + "/WEB-INF/log/openmislog.txt");
00052                 File file = new File(System.getProperty("user.dir") + "/WEB-INF/log");
00053                 if (file.exists())
00054                         FileOpenCloseLogManager.registerLogManager(props); //log to a file
00055                 else
00056                         LogManager.registerLogManager(null); //log to a file
00057 
00058                 //joint webserver session if init made in another thread than the web server.
00059                 SessionContextManager.getManager().joinSessionContext("OPENMOBILEISSESSION");
00060                 
00061                 //init template management
00062                 try {
00063                         ServiceReference[] refs = bundleContext.getServiceReferences(TemplateRetrieverManagerService.class.getName(), null);
00064                         if (refs != null) {
00065                                 TemplateRetrieverManagerService templatemanager = (TemplateRetrieverManagerService) bundleContext.getService(refs[0]);
00066                         String rootTemplatePath = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00067                         TemplateRetrieverService retriever = new TemplateRetrieverServiceImpl(bundleContext.getBundle().getSymbolicName(), rootTemplatePath);
00068                                 templatemanager.registerTemplateRetriever(retriever);
00069                                 bundleContext.ungetService(refs[0]);
00070                                 refs[0] = null;
00071                         }
00072                 } catch (Throwable ex) {
00073                         LogManager.traceError(0, ex);
00074                 }
00075 
00076                 //init module
00077                 // get module XML
00078                 InputStream in = bundleLoader.getResourceAsStream("/"+bundleContext.getBundle().getSymbolicName()+".xml");
00079                 System.out.println("XML "+"/"+bundleContext.getBundle().getSymbolicName()+".xml in :"+in);
00080                 XmlModuleParser parser = new XmlModuleParser(new InputStreamReader(in));
00081                 parser.parse();
00082                 ProfileModule module = parser.getModuletoload();
00083                 this.notifyModuleLoading(module);
00084           Thread.currentThread().setContextClassLoader(oldLoader);
00085 
00086         }
00087 
00088         /* (non-Javadoc)
00089          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00090          */
00091         public void notifyModuleLoading(ProfileModule module) throws ServiceException {
00092                 String loaderClassName = module.getModuleLoaderClass();
00093                 if (loaderClassName != null)    {
00094                         try     {
00095                                 RubricLoader loader = (RubricLoader) Thread.currentThread().getContextClassLoader().loadClass(loaderClassName).newInstance();
00096                                 this.loadRubricLoader(loader);
00097                         } catch (Throwable ex)  {
00098                                 LogManager.traceError(0, "StaticModuleManagerListener notifyModuleLoading : Exception during module "+module.getName()+" loading :"+ex.getMessage());
00099                                 throw new ServiceException(ex);
00100                         }
00101                 }
00102 
00103 
00104         }
00105 
00106   private void loadRubricLoader(RubricLoader rubricLoader) throws ServiceException  {
00107     rubricLoader.preLoadingInit();
00108     // load services
00109     Array serviceList = rubricLoader.loadService();
00110     registeredServiceList = new Array();
00111     for (int i=0; i<serviceList.size(); i++)  {
00112       Service service = (Service)serviceList.get(i);
00113       registeredServiceList.add(service.getServiceUri());
00114       ServiceManager.getManager().loadService(service);
00115     }
00116     // load listener
00117     rubricLoader.postLoadingInit();
00118   }
00119   
00120   private void unregisterServices()     {
00121     for (int i=0; i<registeredServiceList.size(); i++)  {
00122       String serviceURI = (String)registeredServiceList.get(i);
00123       ServiceManager.getManager().unloadService(serviceURI);
00124     }
00125   }
00126 
00127         /* (non-Javadoc)
00128          * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
00129          */
00130         public void stop(BundleContext bundleContext) throws Exception {
00131                 this.unregisterServices();
00132         }
00133 
00134 }

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