00001
00004 package org.openmobileis.bundle.osgi.synchro;
00005
00006 import java.io.ByteArrayOutputStream;
00007 import java.io.File;
00008 import java.io.FileInputStream;
00009 import java.io.InputStream;
00010 import java.io.InputStreamReader;
00011 import java.net.URL;
00012 import java.util.Properties;
00013
00014 import org.openmobileis.bundle.osgi.services.BunbleContextManager;
00015 import org.openmobileis.common.context.ApplicationContextManager;
00016 import org.openmobileis.common.util.collection.Array;
00017 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00018 import org.openmobileis.common.util.log.LogManager;
00019 import org.openmobileis.database.fastobjectdb.synchro.server.FODBOpenMSPSynchroTargetWrapper;
00020 import org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget;
00021 import org.openmobileis.module.core.ProfileModule;
00022 import org.openmobileis.module.core.ProfileModuleSynchroListener;
00023 import org.openmobileis.module.core.XmlModuleParser;
00024 import org.openmobileis.module.server.ModuleJarSynchroTarget;
00025 import org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPSynchroTargetListener;
00026 import org.openmobileis.synchro.openmsp.server.synctarget.SynchroTargerManager;
00027 import org.osgi.framework.BundleActivator;
00028 import org.osgi.framework.BundleContext;
00029 import org.osgi.framework.ServiceRegistration;
00030
00035 public final class SynchroBundleActivator implements BundleActivator {
00036 ApplicationContextManager manager = ApplicationContextManager.getManager();
00037
00038
00039
00040
00041
00042
00043 public void start(BundleContext bundleContext) throws Exception {
00044 System.out.println("SynchroBundleActivator start");
00045 try {
00046
00047 ClassLoader bundleLoader = this.getClass().getClassLoader();
00048 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
00049 Thread.currentThread().setContextClassLoader(bundleLoader);
00050 BunbleContextManager.getManager().setBundleContext(bundleContext);
00051
00052 System.out.println("SynchroBundleActivator initlog");
00053
00054 Properties props = new Properties();
00055 props.put("org.openmobileis.common.log.file", System.getProperty("user.dir") + "/" + bundleContext.getBundle().getSymbolicName()
00056 + "/WEB-INF/log/openmislog.txt");
00057 System.out.println("SynchroBundleActivator log file :"+ System.getProperty("user.dir") + "/" + bundleContext.getBundle().getSymbolicName()
00058 + "/WEB-INF/log/openmislog.txt");
00059 File file = new File(props.getProperty("org.openmobileis.common.log.file"));
00060
00061 boolean logInFile = false;
00062 if (!file.exists()) {
00063 try {
00064 FileInputStream infile = new FileInputStream(file);
00065 infile.close();
00066 logInFile = true;
00067 } catch (Throwable ex) {
00068
00069 }
00070 }
00071 if (logInFile)
00072 FileOpenCloseLogManager.registerLogManager(props);
00073 else
00074 LogManager.registerLogManager(null);
00075
00076 System.out.println("SynchroBundleActivator init moduledescriptor");
00077
00078 URL url = bundleContext.getBundle().getEntry("/moduledescriptor.xml");
00079 InputStream in = url.openStream();
00080 XmlModuleParser parser = new XmlModuleParser(new InputStreamReader(in));
00081 parser.parse();
00082
00083 ProfileModule module = parser.getModuletoload();
00084 LogManager.traceInfo(0, "Module Bundle activator Load module :" + module.getName());
00085
00086
00087 byte[] terminaljar = null;
00088 URL jarurl = bundleContext.getBundle().getEntry("/" + module.getTerminaljar());
00089 if (jarurl != null) {
00090 InputStream jarin = jarurl.openStream();
00091 byte[] buffer = new byte[1024];
00092 ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00093 int bytes_read;
00094 try {
00095 while ((bytes_read = jarin.read(buffer)) != -1)
00096
00097 stream.write(buffer, 0, bytes_read);
00098 } finally {
00099 stream.flush();
00100 stream.close();
00101 }
00102 terminaljar = stream.toByteArray();
00103 } else {
00104 LogManager.traceWarning(0, "No terminal jar for module :" + module.getName());
00105 }
00106
00107
00108 File modulejar = new File(System.getProperty("user.dir") + "/WEB-INF/modules/" + module.getServerjar());
00109 long filens = modulejar.lastModified();
00110
00111
00112 ModuleJarSynchroTarget modulelistener = new ModuleJarSynchroTarget(module);
00113 modulelistener.setTerminalJar(terminaljar, filens);
00114
00115
00116 SynchroTargerManager.getManager().addSynchroTargetListener(modulelistener, modulelistener.getVersion(), null);
00117
00118
00119
00120
00121
00122
00123
00124 Properties listenerprops = new Properties();
00125 listenerprops.put("version", module.getVersion());
00126 Array li = module.getSynchroListenerList();
00127 for (int j = 0; j < li.size(); j++) {
00128 ProfileModuleSynchroListener listener = (ProfileModuleSynchroListener) li.get(j);
00129 String targetClass = listener.targetClassName;
00130 Object target = bundleLoader.loadClass(targetClass).newInstance();
00131 OpenMSPSynchroTargetListener synchrolistener = null;
00132 if (target instanceof FODBSyncTarget) {
00133 synchrolistener = new FODBOpenMSPSynchroTargetWrapper((FODBSyncTarget) target);
00134 } else {
00135 synchrolistener = (OpenMSPSynchroTargetListener) target;
00136 }
00137 LogManager.traceDebug(0, "Module Bundle activator register target :" + synchrolistener.getTargetName() + " version :" + module.getVersion()
00138 + " object:" + synchrolistener);
00139
00140
00141
00142 ServiceRegistration reg = bundleContext.registerService(OpenMSPSynchroTargetListener.class.getName(), synchrolistener, listenerprops);
00143 }
00144
00145 Thread.currentThread().setContextClassLoader(oldLoader);
00146 } catch (Exception ex) {
00147 LogManager.traceError(0, "SynchroBundleActivator start error :" + ex.toString());
00148 LogManager.traceError(0, ex);
00149 }
00150 }
00151
00152
00153
00154
00155
00156
00157 public void stop(BundleContext bundleContext) throws Exception {
00158 }
00159
00160 }