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.osgi.framework.console.ConsoleSession;
24  import org.osgi.framework.BundleActivator;
25  import org.osgi.framework.BundleContext;
26  import org.osgi.framework.ServiceReference;
27  import org.osgi.service.http.HttpService;
28  import org.osgi.service.http.NamespaceException;
29  import org.osgi.util.tracker.ServiceTracker;
30  import org.osgi.util.tracker.ServiceTrackerCustomizer;
31  
32  /**
33   * When started will register on the HttpService 3 servlets for 3 different styles of equinox consoles.
34   */
35  public class WebEquinoxToolsActivator implements BundleActivator
36  {
37  
38      private static BundleContext context;
39      public static BundleContext getContext()
40      {
41          return context;
42      }
43  
44      private HttpService _httpService;
45      private ServiceTracker _tracker;
46      private EquinoxChattingSupport _equinoxChattingSupport;
47      
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
53       */
54      public void start(BundleContext bundleContext) throws Exception
55      {
56          WebEquinoxToolsActivator.context = bundleContext;
57                  
58          ServiceTrackerCustomizer httpServiceTrackerCustomizer = new ServiceTrackerCustomizer()
59          {
60              @Override
61              public void removedService(ServiceReference reference, Object service)
62              {
63                  _httpService = null;
64              }
65  
66              @Override
67              public void modifiedService(ServiceReference reference, Object service)
68              {
69                  _httpService = (HttpService)context.getService(reference);
70              }
71  
72              @Override
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 }