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.ProfileModuleRubric;
00024 import org.openmobileis.module.core.XmlModuleParser;
00025 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00026 import org.openmobileis.services.Service;
00027 import org.openmobileis.services.common.ServiceManager;
00028 import org.osgi.framework.BundleActivator;
00029 import org.osgi.framework.BundleContext;
00030 import org.osgi.framework.ServiceReference;
00031 
00036 public final class TestServiceClient 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                 //init log
00051                 Properties props = new Properties();
00052                 props.put("org.openmobileis.common.log.file", System.getProperty("user.dir") + "/WEB-INF/log/openmislog.txt");
00053                 File file = new File(System.getProperty("user.dir") + "/WEB-INF/log");
00054                 if (file.exists())
00055                         FileOpenCloseLogManager.registerLogManager(props); //log to a file
00056                 else
00057                         LogManager.registerLogManager(null); //log to a file
00058 
00059                 //joint webserver session if init made in another thread than the web server.
00060                 SessionContextManager.getManager().joinSessionContext("OPENMOBILEISSESSION");
00061                 
00062                 //init template management
00063                 try {
00064                         ServiceReference[] refs = bundleContext.getServiceReferences(TemplateRetrieverManagerService.class.getName(), null);
00065                         if (refs != null) {
00066                                 TemplateRetrieverManagerService templatemanager = (TemplateRetrieverManagerService) bundleContext.getService(refs[0]);
00067                         String rootTemplatePath = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00068                         TemplateRetrieverService retriever = new TemplateRetrieverServiceImpl(bundleContext.getBundle().getSymbolicName(), rootTemplatePath);
00069                                 templatemanager.registerTemplateRetriever(retriever);
00070                                 bundleContext.ungetService(refs[0]);
00071                                 refs[0] = null;
00072                         }
00073                 } catch (Throwable ex) {
00074                         LogManager.traceError(0, ex);
00075                 }
00076 
00077                 //init module
00078                 // get module XML
00079                 InputStream in = bundleLoader.getResourceAsStream("/"+bundleContext.getBundle().getSymbolicName()+".xml");
00080                 System.out.println("XML "+"/"+bundleContext.getBundle().getSymbolicName()+".xml in :"+in);
00081                 XmlModuleParser parser = new XmlModuleParser(new InputStreamReader(in));
00082                 parser.parse();
00083                 ProfileModule module = parser.getModuletoload();
00084                 this.notifyModuleLoading(module);
00085           Thread.currentThread().setContextClassLoader(oldLoader);
00086 
00087         }
00088 
00089         /* (non-Javadoc)
00090          * @see org.openmobileis.module.terminal.ModuleManagerListener#notifyModuleLoading(org.openmobileis.module.core.ProfileModule)
00091          */
00092         public void notifyModuleLoading(ProfileModule module) throws ServiceException {
00093                 Array rubList = module.getModuleRubrics();
00094                 for (int i=0; i<rubList.size(); i++)    {
00095                         ProfileModuleRubric rubric = (ProfileModuleRubric) rubList.get(i);
00096                         String loaderClassName = rubric.getLoaderClass();
00097                         if (loaderClassName != null)    {
00098                                 try     {
00099                                         RubricLoader loader = (RubricLoader) Thread.currentThread().getContextClassLoader().loadClass(loaderClassName).newInstance();
00100                                         this.loadRubricLoader(loader);
00101                                 } catch (Throwable ex)  {
00102                                         LogManager.traceError(0, "StaticModuleManagerListener notifyModuleLoading : Exception during module "+module.getName()+" loading :"+ex.getMessage());
00103                                         throw new ServiceException(ex);
00104                                 }
00105                         }
00106                 }
00107 
00108 
00109         }
00110 
00111   private void loadRubricLoader(RubricLoader rubricLoader) throws ServiceException  {
00112     rubricLoader.preLoadingInit();
00113     // load services
00114     Array serviceList = rubricLoader.loadService();
00115     registeredServiceList = new Array();
00116     for (int i=0; i<serviceList.size(); i++)  {
00117       Service service = (Service)serviceList.get(i);
00118       registeredServiceList.add(service.getServiceUri());
00119       ServiceManager.getManager().loadService(service);
00120     }
00121     // load listener
00122     rubricLoader.postLoadingInit();
00123   }
00124   
00125   private void unregisterServices()     {
00126     for (int i=0; i<registeredServiceList.size(); i++)  {
00127       String serviceURI = (String)registeredServiceList.get(i);
00128       ServiceManager.getManager().unloadService(serviceURI);
00129     }
00130   }
00131 
00132         /* (non-Javadoc)
00133          * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
00134          */
00135         public void stop(BundleContext bundleContext) throws Exception {
00136                 this.unregisterServices();
00137         }
00138 
00139 }

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