Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 74c910c525a3777c76182cb4f38839aa0178339d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// ========================================================================
// Copyright (c) 2009 Intalio, Inc.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at 
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses. 
// Contributors:
//    Hugues Malphettes - initial API and implementation
// ========================================================================
package org.eclipse.jetty.osgi.boot;

import java.util.Dictionary;
import java.util.Hashtable;

import org.eclipse.jetty.osgi.boot.internal.serverfactory.DefaultJettyAtJettyHomeHelper;
import org.eclipse.jetty.osgi.boot.internal.serverfactory.JettyServerServiceTracker;
import org.eclipse.jetty.osgi.boot.internal.webapp.IWebBundleDeployerHelper;
import org.eclipse.jetty.osgi.boot.internal.webapp.JettyContextHandlerServiceTracker;
import org.eclipse.jetty.osgi.boot.internal.webapp.WebBundleTrackerCustomizer;
import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.webapp.WebAppContext;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.BundleTracker;

/**
 * Experiment: bootstrap jetty's complete distrib from an OSGi bundle. Progress:
 * <ol>
 * <li>basic servlet [ok]</li>
 * <li>basic jetty.xml [ok]</li>
 * <li>basic jetty.xml and jetty-plus.xml [ok]</li>
 * <li>basic jsp [ok with modifications]
 * <ul>
 * <li>Needed to modify the headers of jdt.core-3.1.1 so that its dependency on
 * eclipse.runtime, eclipse.resources and eclipse.text are optional. Also we
 * should depend on the latest jdt.core from eclipse-3.5 not from eclipse-3.1.1
 * although that will require actual changes to jasper as some internal APIs of
 * jdt.core have changed.</li>
 * <li>Modifications to org.mortbay.jetty.jsp-2.1-glassfish: made all imports to
 * ant, xalan and sun packages optional.</li>
 * </ul>
 * </li>
 * <li>jsp with tag-libs [ok]</li>
 * <li>test-jndi with atomikos and derby inside ${jetty.home}/lib/ext [ok]</li>
 * </ul>
 */
public class JettyBootstrapActivator implements BundleActivator
{

    private static JettyBootstrapActivator INSTANCE = null;

    public static JettyBootstrapActivator getInstance()
    {
        return INSTANCE;
    }

    private ServiceRegistration _registeredServer;
    private Server _server;
    private JettyContextHandlerServiceTracker _jettyContextHandlerTracker;
    private PackageAdminServiceTracker _packageAdminServiceTracker;
    private BundleTracker _webBundleTracker;
    
//    private ServiceRegistration _jettyServerFactoryService;
    private JettyServerServiceTracker _jettyServerServiceTracker;
    

    /**
     * Setup a new jetty Server, registers it as a service. Setup the Service
     * tracker for the jetty ContextHandlers that are in charge of deploying the
     * webapps. Setup the BundleListener that supports the extender pattern for
     * the jetty ContextHandler.
     * 
     * @param context
     */
    public void start(BundleContext context) throws Exception
    {
        INSTANCE = this;

        // track other bundles and fragments attached to this bundle that we
        // should activate.
        _packageAdminServiceTracker = new PackageAdminServiceTracker(context);

    	_jettyServerServiceTracker = new JettyServerServiceTracker();
        context.addServiceListener(_jettyServerServiceTracker,"(objectclass=" + Server.class.getName() + ")");

        //Register the Jetty Server Factory as a ManagedServiceFactory:
//          Properties jettyServerMgdFactoryServiceProps = new Properties(); 
//          jettyServerMgdFactoryServiceProps.put("pid", OSGiWebappConstants.MANAGED_JETTY_SERVER_FACTORY_PID);
//          _jettyServerFactoryService = context.registerService(
//          		ManagedServiceFactory.class.getName(),  new JettyServersManagedFactory(),
//          		jettyServerMgdFactoryServiceProps);
    
        _jettyContextHandlerTracker = new JettyContextHandlerServiceTracker(_jettyServerServiceTracker);

        // the tracker in charge of the actual deployment
        // and that will configure and start the jetty server.
        context.addServiceListener(_jettyContextHandlerTracker,"(objectclass=" + ContextHandler.class.getName() + ")");

        //see if we shoult start a default jetty instance right now.
        DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
        
        // now ready to support the Extender pattern:        
        _webBundleTracker = new BundleTracker(context,
        		Bundle.ACTIVE | Bundle.STOPPING, new WebBundleTrackerCustomizer());
        _webBundleTracker.open();
        
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception
    {
        try
        {
        	
        	if (_webBundleTracker != null)
        	{
        		_webBundleTracker.close();
        		_webBundleTracker = null;
        	}
            if (_jettyContextHandlerTracker != null)
            {
                _jettyContextHandlerTracker.stop();
                context.removeServiceListener(_jettyContextHandlerTracker);
                _jettyContextHandlerTracker = null;
            }
            if (_jettyServerServiceTracker != null)
            {
            	_jettyServerServiceTracker.stop();
                context.removeServiceListener(_jettyServerServiceTracker);
                _jettyServerServiceTracker = null;
            }
            if (_packageAdminServiceTracker != null)
            {
                _packageAdminServiceTracker.stop();
                context.removeServiceListener(_packageAdminServiceTracker);
                _packageAdminServiceTracker = null;
            }
            if (_registeredServer != null)
            {
                try
                {
                    _registeredServer.unregister();
                }
                catch (IllegalArgumentException ill)
                {
                    // already unregistered.
                }
                finally
                {
                	_registeredServer = null;
                }
            }
//        	if (_jettyServerFactoryService != null)
//        	{
//                try
//                {
//                	_jettyServerFactoryService.unregister();
//                }
//                catch (IllegalArgumentException ill)
//                {
//                    // already unregistered.
//                }
//                finally
//                {
//                	_jettyServerFactoryService = null;
//                }
//        	}

        }
        finally
        {
            if (_server != null)
            {
            	_server.stop();
            }
            INSTANCE = null;
        }
    }

    /**
     * Helper method that creates a new org.jetty.webapp.WebAppContext and
     * registers it as an OSGi service. The tracker
     * {@link JettyContextHandlerServiceTracker} will do the actual deployment.
     * 
     * @param contributor
     *            The bundle
     * @param webappFolderPath
     *            The path to the root of the webapp. Must be a path relative to
     *            bundle; either an absolute path.
     * @param contextPath
     *            The context path. Must start with "/"
     * @throws Exception
     */
    public static void registerWebapplication(Bundle contributor, String webappFolderPath, String contextPath) throws Exception
    {
    	checkBundleActivated();
    	WebAppContext contextHandler = new WebAppContext();
        Dictionary dic = new Hashtable();
        dic.put(OSGiWebappConstants.SERVICE_PROP_WAR,webappFolderPath);
        dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH,contextPath);
        String requireTldBundle = (String)contributor.getHeaders().get(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
        if (requireTldBundle != null) {
        	dic.put(OSGiWebappConstants.SERVICE_PROP_REQUIRE_TLD_BUNDLE, requireTldBundle);
        }
        contributor.getBundleContext().registerService(ContextHandler.class.getName(),contextHandler,dic);
    }

    /**
     * Helper method that creates a new org.jetty.webapp.WebAppContext and
     * registers it as an OSGi service. The tracker
     * {@link JettyContextHandlerServiceTracker} will do the actual deployment.
     * 
     * @param contributor
     *            The bundle
     * @param webappFolderPath
     *            The path to the root of the webapp. Must be a path relative to
     *            bundle; either an absolute path.
     * @param contextPath
     *            The context path. Must start with "/"
     * @param dic
     *        TODO: parameter description
     * @throws Exception
     */
    public static void registerWebapplication(Bundle contributor, String webappFolderPath, String contextPath, Dictionary<String, String> dic) throws Exception
    {
    	checkBundleActivated();
        WebAppContext contextHandler = new WebAppContext();
        dic.put(OSGiWebappConstants.SERVICE_PROP_WAR,webappFolderPath);
        dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH,contextPath);
        contributor.getBundleContext().registerService(ContextHandler.class.getName(),contextHandler,dic);
    }

    /**
     * Helper method that creates a new skeleton of a ContextHandler and
     * registers it as an OSGi service. The tracker
     * {@link JettyContextHandlerServiceTracker} will do the actual deployment.
     * 
     * @param contributor
     *            The bundle that registers a new context
     * @param contextFilePath
     *            The path to the file inside the bundle that defines the
     *            context.
     * @throws Exception
     */
    public static void registerContext(Bundle contributor, String contextFilePath) throws Exception
    {
        registerContext(contributor,contextFilePath,new Hashtable<String, String>());
    }

    /**
     * Helper method that creates a new skeleton of a ContextHandler and
     * registers it as an OSGi service. The tracker
     * {@link JettyContextHandlerServiceTracker} will do the actual deployment.
     * 
     * @param contributor
     *            The bundle that registers a new context
     * @param contextFilePath
     *            The path to the file inside the bundle that defines the
     *            context.
     * @param dic
     *          TODO: parameter description
     * @throws Exception
     */
    public static void registerContext(Bundle contributor, String contextFilePath, Dictionary<String, String> dic) throws Exception
    {
    	checkBundleActivated();
        ContextHandler contextHandler = new ContextHandler();
        dic.put(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH,contextFilePath);
        dic.put(IWebBundleDeployerHelper.INTERNAL_SERVICE_PROP_UNKNOWN_CONTEXT_HANDLER_TYPE,Boolean.TRUE.toString());
        contributor.getBundleContext().registerService(ContextHandler.class.getName(),contextHandler,dic);
    }

    public static void unregister(String contextPath)
    {
        // todo
    }
    
    /**
     * Since org.eclipse.jetty.osgi.boot does not have a lazy activation policy
     * when one fo the static methods to register a webapp is called we should make sure that
     * the bundle is started.
     */
    private static void checkBundleActivated()
    {
    	if (INSTANCE == null) {
    		Bundle thisBundle = FrameworkUtil.getBundle(JettyBootstrapActivator.class);
    		try
    		{
				thisBundle.start();
			}
    		catch (BundleException e)
			{
				//nevermind.
			}
    	}
    }
    

}

Back to the top