Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8763ece7b9d7ab89e6d4d3479fff52e400bd7222 (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
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  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.
//  ========================================================================
//

package org.eclipse.jetty.osgi.boot;

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.BundleWatcher;
import org.eclipse.jetty.osgi.boot.internal.webapp.ServiceWatcher;
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.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.BundleTracker;

/**
 * JettyBootstrapActivator
 * 
 * Bootstrap jetty and publish a default Server instance as an OSGi service.
 * 
 * Listen for other Server instances to be published as services and support them as deployment targets.
 * 
 * Listen for Bundles to be activated, and deploy those that represent webapps/ContextHandlers to one of the known Server instances.
 * 
 */
public class JettyBootstrapActivator implements BundleActivator
{
    private static final Logger LOG = Log.getLogger(JettyBootstrapActivator.class);
    
    private static JettyBootstrapActivator INSTANCE = null;

    public static JettyBootstrapActivator getInstance()
    {
        return INSTANCE;
    }

    private ServiceRegistration _registeredServer;

    private ServiceWatcher _jettyContextHandlerTracker;

    private PackageAdminServiceTracker _packageAdminServiceTracker;

    private BundleTracker _webBundleTracker;

    private BundleContext _bundleContext;

    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(final BundleContext context) throws Exception
    {
        INSTANCE = this;
        _bundleContext = context;

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

        // track jetty Server instances that we should support as deployment targets
        _jettyServerServiceTracker = new JettyServerServiceTracker();
        context.addServiceListener(_jettyServerServiceTracker, "(objectclass=" + Server.class.getName() + ")");

        // track ContextHandler class instances and deploy them to one of the known Servers
        _jettyContextHandlerTracker = new ServiceWatcher();
        context.addServiceListener(_jettyContextHandlerTracker, "(objectclass=" + ContextHandler.class.getName() + ")");

        // Create a default jetty instance right now.
        Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);

        //Create a bundle tracker to help deploy webapps and ContextHandlers
        BundleWatcher bundleTrackerCustomizer = new BundleWatcher();
        bundleTrackerCustomizer.setWaitForDefaultServer(defaultServer != null);
        _webBundleTracker =  new BundleTracker(context, Bundle.ACTIVE | Bundle.STOPPING, bundleTrackerCustomizer);
        bundleTrackerCustomizer.setBundleTracker(_webBundleTracker);
        bundleTrackerCustomizer.open();
    }
    
    
    
    /* ------------------------------------------------------------ */
    /**
     * Stop the activator.
     * 
     * @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)
            {
                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;
                }
            }
        }
        finally
        {
            INSTANCE = null;
        }
    }
}

Back to the top