View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2011 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.osgi.equinoxtools;
14  
15  import javax.servlet.ServletException;
16  
17  import org.eclipse.jetty.osgi.equinoxtools.console.EquinoxChattingSupport;
18  import org.eclipse.jetty.osgi.equinoxtools.console.EquinoxConsoleContinuationServlet;
19  import org.eclipse.jetty.osgi.equinoxtools.console.EquinoxConsoleSyncServlet;
20  import org.eclipse.jetty.osgi.equinoxtools.console.EquinoxConsoleWebSocketServlet;
21  import org.eclipse.jetty.osgi.equinoxtools.console.WebConsoleSession;
22  import org.eclipse.jetty.util.log.Log;
23  import org.eclipse.jetty.util.log.Logger;
24  import org.eclipse.osgi.framework.console.ConsoleSession;
25  import org.osgi.framework.BundleActivator;
26  import org.osgi.framework.BundleContext;
27  import org.osgi.framework.ServiceReference;
28  import org.osgi.service.http.HttpService;
29  import org.osgi.service.http.NamespaceException;
30  import org.osgi.util.tracker.ServiceTracker;
31  import org.osgi.util.tracker.ServiceTrackerCustomizer;
32  
33  /**
34   * When started will register on the HttpService 3 servlets for 3 different styles of equinox consoles.
35   */
36  public class WebEquinoxToolsActivator implements BundleActivator
37  {
38      private static final Logger LOG = Log.getLogger(WebEquinoxToolsActivator.class);
39  
40  
41      private static BundleContext context;
42      public static BundleContext getContext()
43      {
44          return context;
45      }
46  
47      private HttpService _httpService;
48      private ServiceTracker _tracker;
49      private EquinoxChattingSupport _equinoxChattingSupport;
50      
51  
52      /*
53       * (non-Javadoc)
54       * 
55       * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
56       */
57      public void start(BundleContext bundleContext) throws Exception
58      {
59          WebEquinoxToolsActivator.context = bundleContext;
60                  
61          ServiceTrackerCustomizer httpServiceTrackerCustomizer = new ServiceTrackerCustomizer()
62          {
63              public void removedService(ServiceReference reference, Object service)
64              {
65                  _httpService = null;
66              }
67  
68              public void modifiedService(ServiceReference reference, Object service)
69              {
70                  _httpService = (HttpService)context.getService(reference);
71              }
72  
73              public Object addingService(ServiceReference reference)
74              {
75                  _httpService = (HttpService)context.getService(reference);
76                  try
77                  {
78                      //TODO; some effort to use the same console session on the 2 async console servlets?
79  
80                      //websocket:
81  //                    WebConsoleSession wsSession = new WebConsoleSession();
82  //                    WebEquinoxConsoleActivator.context.registerService(ConsoleSession.class.getName(), wsSession, null);
83  //                    EquinoxChattingSupport wsEquinoxChattingSupport = new EquinoxChattingSupport(wsSession);
84                      _httpService.registerResources("/equinoxconsole/ws/index.html","/equinoxconsole/ws/index.html",null);
85                      _httpService.registerServlet("/equinoxconsole/ws",new EquinoxConsoleWebSocketServlet(/*wsSession, wsEquinoxChattingSupport*/),null,null);
86                      
87                      //continuations:
88  //                    WebConsoleSession contSession = new WebConsoleSession();
89  //                    WebEquinoxConsoleActivator.context.registerService(ConsoleSession.class.getName(), contSession, null);
90  //                    EquinoxChattingSupport contEquinoxChattingSupport = new EquinoxChattingSupport(contSession);
91                      _httpService.registerResources("/equinoxconsole/index.html","/equinoxconsole/index.html",null);
92                      _httpService.registerServlet("/equinoxconsole",new EquinoxConsoleContinuationServlet(/*contSession, contEquinoxChattingSupport*/),null,null);
93                      
94                      //legacy synchroneous; keep it in a separate console session.
95                      WebConsoleSession syncSession = new WebConsoleSession();
96                      WebEquinoxToolsActivator.context.registerService(ConsoleSession.class.getName(), syncSession, null);
97                      _httpService.registerServlet("/equinoxconsole/sync",new EquinoxConsoleSyncServlet(syncSession),null,null);
98                  }
99                  catch (ServletException e)
100                 {
101                     LOG.warn(e);
102                 }
103                 catch (NamespaceException e)
104                 {
105                     LOG.warn(e);
106                 }
107                 return _httpService;
108             }
109         };
110 
111         _tracker = new ServiceTracker(context,HttpService.class.getName(),httpServiceTrackerCustomizer);
112         _tracker.open();
113     }
114 
115     /*
116      * (non-Javadoc)
117      * 
118      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
119      */
120     public void stop(BundleContext bundleContext) throws Exception
121     {
122         _tracker.close();
123         WebEquinoxToolsActivator.context = null;
124     }
125     
126 
127 }