00001
00002
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
00040 try {
00041 System.out.println("start WebServer");
00042
00043 server = new WebServer();
00044 server.serve();
00045 } catch (IOException ex) {}
00046 }
00047 }
00048
00049
00050
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
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);
00064 else LogManager.registerLogManager(null);
00065
00066
00067 ApplicationContextManager.getManager();
00068 ApplicationContextManager.getManager().getApplicationContext().setBundleLoaded(true);
00069
00070
00071 PropertiesManager.getManager().addProperty("test", "testpropoerty");
00072
00073
00074 SessionContextManager.getManager();
00075
00076
00077 PropertiesManager.getManager().addProperty("fastobjectdb.database.path", System.getProperty("user.dir")+"/database");
00078 PropertiesManager.getManager().addProperty("fastobjectdb.database.name", "db");
00079
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());
00083 FastObjectDBManager.getManager();
00084 } catch (Throwable ex) {
00085 throw new ServiceException(ex);
00086 }
00087
00088
00089 ServiceManager.getManager();
00090
00091
00092 Thread thread = new Thread(new StartWebServertThread());
00093 thread.start();
00094
00095 Thread.currentThread().sleep(2000);
00096
00097
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
00110
00111
00112 Thread.currentThread().setContextClassLoader(oldLoader);
00113 }
00114
00115
00116
00117
00118 public void stop(BundleContext context) throws Exception {
00119
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 }