/src/server/servicefacade/src/keymind/keywatch/bundles/servicefacade/Activator.java
http://keywatch.googlecode.com/ · Java · 261 lines · 148 code · 35 blank · 78 comment · 8 complexity · 86047bb63f9fc235c5ae9f50e2f31e5e MD5 · raw file
- /**
- * -----------------------------------------------------------------------------------------------
- * File: Activator.java
- *
- * Copyright (c) 2007 by Keymind Computing as.
- * All rights reserved.
- *
- * This file is subject to the terms and conditions of the Apache Licence 2.0.
- * See the file LICENCE in the main directory of the Keywatch distribution for more details.
- *
- * Revision History (use svn log or TortoiseSVN):
- * $URL: http://keywatch.googlecode.com/svn/trunk/src/server/servicefacade/src/keymind/keywatch/bundles/servicefacade/Activator.java $
- * $Date: 2009-04-16 15:23:28 +0200 (Thu, 16 Apr 2009) $, $Rev: 1208 $
- * -----------------------------------------------------------------------------------------------
- */
- package keymind.keywatch.bundles.servicefacade;
-
- import org.osgi.framework.*;
- import org.mortbay.jetty.Server;
- import org.mortbay.jetty.servlet.WebApplicationContext;
- import org.mortbay.http.SocketListener;
- import keymind.keywatch.common.IOSGIServiceAccess;
- import keymind.keywatch.common.Log;
-
- import java.io.InputStream;
- import java.io.FileOutputStream;
- import java.io.File;
-
- /**
- * Bundle for OSGI. Uses Jetty as web container and offers an interface for accessing
- * bundle services.
- */
- public class Activator implements BundleActivator, IOSGIServiceAccess, FrameworkListener, BundleListener
- {
- public static BundleContext bundleCtx = null;
-
- private Server servletserver = null;
- private SocketListener listener = null;
- private WebApplicationContext srvctx = null;
- private static IOSGIServiceAccess serviceAccess;
-
- static final String HTTP_PORT = "http.port";
- static final String HTTP_DEFAULT_PORT = "8082";
-
- static final String SERVERBUNDLE_STOPPING = "Serverbundle stopping...";
- static final String SERVERBUNDLE_STOPPED = "Serverbundle stopped";
- static final String RUNTIME_BUILDER = "RuntimeBuilder";
- static final String WAR_UPDATING = "Service facade is updating WAR file...";
- static final String WAR_UPDATED = "WAR file successfully updated";
- static final String MISSING_FRAMEWORK = "Service facade: framework.war not found. Assuming it's in ./webapps/framework.war";
-
- /**
- * Called when the bundle starts
- *
- * @param bundleContext Bundle context
- * @throws Exception Exception
- */
- public void start(BundleContext bundleContext) throws Exception
- {
- Log.initialize(bundleContext);
-
- Activator.bundleCtx = bundleContext;
- serviceAccess = this;
-
- bundleCtx.addFrameworkListener(this);
- bundleCtx.addBundleListener(this);
-
- // Set classloader such that Jetty (and the servlet) can look up external services
- // (domainmodel etc from the server bundle)
-
- ClassLoader bundleLoader = this.getClass().getClassLoader();
- ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
- Thread.currentThread().setContextClassLoader(bundleLoader);
-
- // Initialize the servlet-container
- servletserver = new Server();
- listener = new SocketListener();
- listener.setPort(Integer.parseInt(System.getProperty(HTTP_PORT, HTTP_DEFAULT_PORT)));
- servletserver.addListener(listener);
-
- // Add context
- srvctx = new WebApplicationContext();
-
- // Add the web application, load the servlet
- srvctx.setContextPath("");
- srvctx.setClassLoaderJava2Compliant(true);
-
- InputStream is = srvctx.getClass().getResourceAsStream("/framework.war");
- if (is == null)
- {
- System.out.println(MISSING_FRAMEWORK);
- }
- else
- {
- File dir = new File("webapps");
- dir.mkdirs();
-
- File war = new File("webapps/framework.war");
- war.delete();
-
- FileOutputStream fos = new FileOutputStream("webapps/framework.war");
- int b = is.read();
- while (b != -1)
- {
- fos.write(b);
- b = is.read();
- }
-
- fos.close();
- }
-
- // This is required for hot redeployment
- try
- {
- srvctx.setExtractWAR(true);
- srvctx.setWAR("./webapps/framework.war");
-
- servletserver.addContext(srvctx);
- servletserver.start();
-
- //Thread.currentThread().setContextClassLoader(oldLoader);
- }
- catch (Exception ex)
- {
- Log.error(ex.toString());
- }
-
- }
-
- /**
- * Called when the bundle stops
- *
- * @param bundleContext Bundle context
- * @throws Exception Exception
- */
- public void stop(BundleContext bundleContext) throws Exception
- {
- bundleLog(SERVERBUNDLE_STOPPING);
-
- // Stop jetty gracefully
- servletserver.stop();
- servletserver.destroy();
- srvctx.stop();
- srvctx.destroy();
-
- bundleLog(SERVERBUNDLE_STOPPED);
- }
-
- /**
- * Implements the ServiceAccessFactory - returns an implementation of ServiceAccess-interface
- * Could return a separate bundle handling the interface, but for now, this is ok
- *
- * @return an implementation of ServiceAccess-interface
- */
- public static IOSGIServiceAccess getServiceAccess()
- {
- return serviceAccess;
- }
-
- /**
- * Get static bundle context
- *
- * @return the bundle context
- */
- public static synchronized BundleContext getBundleContext()
- {
- return bundleCtx;
- }
-
- /**
- * @see IOSGIServiceAccess#bundleLog(String)
- */
- public void bundleLog(String logString)
- {
- Log.info(logString);
- }
-
-
- /**
- * Used in development to run differnt tests, from local callback to
- * end to end test.
- * Input desides about the test, result indicates result
- *
- * @param type Type of test to run
- * @return resultstatus
- */
- public int test(int type)
- {
- return 0;
- }
-
- /**
- * Implements the OSGiServiceAccess interface to give the "client" access to services
- * even if it is not a bundle
- *
- * @param serviceName Service name
- * @return the service or null if not found
- */
- public Object getService(String serviceName)
- {
- synchronized (this)
- {
- try
- {
- return bundleCtx.getService(bundleCtx.getServiceReference(serviceName));
- }
- catch (Exception ignored)
- {
- bundleLog("Serviceaccess could not look up " + serviceName);
- return null;
- }
- }
- }
-
- /**
- * Reload WAR file when refreshing packages
- *
- * @param frameworkEvent Framework event
- */
- public void frameworkEvent(FrameworkEvent frameworkEvent)
- {
- try
- {
- if (frameworkEvent.getType() == FrameworkEvent.PACKAGES_REFRESHED)
- {
- srvctx.stop();
- srvctx.start();
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
- /**
- * Reload WAR file when RuntimeBuilder stops, since this indicates a client rebuild
- *
- * @param bundleEvent Bundle event
- */
- public void bundleChanged(BundleEvent bundleEvent)
- {
- if (bundleEvent.getType() == BundleEvent.STOPPED &&
- bundleEvent.getBundle().getSymbolicName().equals(RUNTIME_BUILDER))
- {
- try
- {
- Log.info(WAR_UPDATING);
-
- stop(bundleCtx);
- start(bundleCtx);
-
- Log.info(WAR_UPDATED);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }