View Javadoc

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