Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f3dacc9e5a8378c97738fe3b9872f2a431576f5c (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//
//  ========================================================================
//  Copyright (c) 1995-2015 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.utils.internal;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.service.startlevel.StartLevel;

/**
 * PackageAdminServiceTracker
 * <p>
 * When the PackageAdmin service is activated we can look for the fragments
 * attached to this bundle and do a fake "activate" on them.
 * <p> 
 * See particularly the jetty-osgi-boot-jsp fragment bundle that uses this
 * facility.
 */
public class PackageAdminServiceTracker implements ServiceListener
{
    private BundleContext _context;

    private List<BundleActivator> _activatedFragments = new ArrayList<BundleActivator>();

    private boolean _fragmentsWereActivated = false;

    // Use the deprecated StartLevel to stay compatible with older versions of
    // OSGi.
    private StartLevel _startLevel;

    private int _maxStartLevel = 6;

    public static PackageAdminServiceTracker INSTANCE = null;

    public PackageAdminServiceTracker(BundleContext context)
    {
        INSTANCE = this;
        _context = context;
        if (!setup())
        {
            try
            {
                _context.addServiceListener(this, "(objectclass=" + PackageAdmin.class.getName() + ")");
            }
            catch (InvalidSyntaxException e)
            {
                e.printStackTrace(); // won't happen
            }
        }
    }

    /**
     * @return true if the fragments were activated by this method.
     */
    private boolean setup()
    {
        ServiceReference sr = _context.getServiceReference(PackageAdmin.class.getName());
        _fragmentsWereActivated = sr != null;
        if (sr != null) invokeFragmentActivators(sr);

        sr = _context.getServiceReference(StartLevel.class.getName());
        if (sr != null)
        {
            _startLevel = (StartLevel) _context.getService(sr);
            try
            {
                _maxStartLevel = Integer.parseInt(System.getProperty("osgi.startLevel", "6"));
            }
            catch (Exception e)
            {
                // nevermind default on the usual.
                _maxStartLevel = 6;
            }
        }
        return _fragmentsWereActivated;
    }

    /**
     * Invokes the optional BundleActivator in each fragment. By convention the
     * bundle activator for a fragment must be in the package that is defined by
     * the symbolic name of the fragment and the name of the class must be
     * 'FragmentActivator'.
     * 
     * @param event The <code>ServiceEvent</code> object.
     */
    public void serviceChanged(ServiceEvent event)
    {
        if (event.getType() == ServiceEvent.REGISTERED)
        {
            invokeFragmentActivators(event.getServiceReference());
        }
    }

    /**
     * Helper to access the PackageAdmin and return the fragments hosted by a
     * bundle. when we drop the support for the older versions of OSGi, we will
     * stop using the PackageAdmin service.
     * 
     * @param bundle the bundle
     * @return the bundle fragment list
     */
    public Bundle[] getFragments(Bundle bundle)
    {
        ServiceReference sr = _context.getServiceReference(PackageAdmin.class.getName());
        if (sr == null)
        {// we should never be here really.
            return null;
        }
        PackageAdmin admin = (PackageAdmin) _context.getService(sr);
        return admin.getFragments(bundle);
    }

    /**
     * Returns the fragments and the required-bundles of a bundle. Recursively
     * collect the required-bundles and fragment when the directive
     * visibility:=reexport is added to a required-bundle.
     * 
     * @param bundle the bundle
     * @return the bundle fragment and required list
     */
    public Bundle[] getFragmentsAndRequiredBundles(Bundle bundle)
    {
        ServiceReference sr = _context.getServiceReference(PackageAdmin.class.getName());
        if (sr == null)
        {// we should never be here really.
            return null;
        }
        PackageAdmin admin = (PackageAdmin) _context.getService(sr);
        LinkedHashMap<String, Bundle> deps = new LinkedHashMap<String, Bundle>();
        collectFragmentsAndRequiredBundles(bundle, admin, deps, false);
        return deps.values().toArray(new Bundle[deps.size()]);
    }

    /**
     * Returns the fragments and the required-bundles. Collects them
     * transitively when the directive 'visibility:=reexport' is added to a
     * required-bundle.
     * 
     * @param bundle the bundle
     * @param admin the admin package
     * @param deps The map of fragment and required bundles associated to the value of the
     *         jetty-web attribute.
     * @param onlyReexport true to collect resources and web-fragments
     *            transitively if and only if the directive visibility is
     *            reexport.
     */
    protected void collectFragmentsAndRequiredBundles(Bundle bundle, PackageAdmin admin, Map<String, Bundle> deps, boolean onlyReexport)
    {
        Bundle[] fragments = admin.getFragments(bundle);
        if (fragments != null)
        {
            // Also add the bundles required by the fragments.
            // this way we can inject onto an existing web-bundle a set of
            // bundles that extend it
            for (Bundle f : fragments)
            {
                if (!deps.keySet().contains(f.getSymbolicName()))
                {
                    deps.put(f.getSymbolicName(), f);
                    collectRequiredBundles(f, admin, deps, onlyReexport);
                }
            }
        }
        collectRequiredBundles(bundle, admin, deps, onlyReexport);
    }

    /**
     * A simplistic but good enough parser for the Require-Bundle header. Parses
     * the version range attribute and the visibility directive.
     * 
     * @param bundle the bundle
     * @param admin the admin package 
     * @param deps The map of required bundles associated to the value of the
     *         jetty-web attribute.
     * @param onlyReexport true to collect resources and web-fragments
     *            transitively if and only if the directive visibility is
     *            reexport.
     */
    protected void collectRequiredBundles(Bundle bundle, PackageAdmin admin, Map<String, Bundle> deps, boolean onlyReexport)
    {
        String requiredBundleHeader = (String) bundle.getHeaders().get("Require-Bundle");
        if (requiredBundleHeader == null) { return; }
        StringTokenizer tokenizer = new ManifestTokenizer(requiredBundleHeader);
        while (tokenizer.hasMoreTokens())
        {
            String tok = tokenizer.nextToken().trim();
            StringTokenizer tokenizer2 = new StringTokenizer(tok, ";");
            String symbolicName = tokenizer2.nextToken().trim();
            if (deps.keySet().contains(symbolicName))
            {
                // was already added. 2 dependencies pointing at the same
                // bundle.
                continue;
            }
            String versionRange = null;
            boolean reexport = false;
            while (tokenizer2.hasMoreTokens())
            {
                String next = tokenizer2.nextToken().trim();
                if (next.startsWith("bundle-version="))
                {
                    if (next.startsWith("bundle-version=\"") || next.startsWith("bundle-version='"))
                    {
                        versionRange = next.substring("bundle-version=\"".length(), next.length() - 1);
                    }
                    else
                    {
                        versionRange = next.substring("bundle-version=".length());
                    }
                }
                else if (next.equals("visibility:=reexport"))
                {
                    reexport = true;
                }
            }
            if (!reexport && onlyReexport) { return; }
            Bundle[] reqBundles = admin.getBundles(symbolicName, versionRange);
            if (reqBundles != null && reqBundles.length != 0)
            {
                Bundle reqBundle = null;
                for (Bundle b : reqBundles)
                {
                    if (b.getState() == Bundle.ACTIVE || b.getState() == Bundle.STARTING)
                    {
                        reqBundle = b;
                        break;
                    }
                }
                if (reqBundle == null)
                {
                    // strange? in OSGi with Require-Bundle,
                    // the dependent bundle is supposed to be active already
                    reqBundle = reqBundles[0];
                }
                deps.put(reqBundle.getSymbolicName(), reqBundle);
                collectFragmentsAndRequiredBundles(reqBundle, admin, deps, true);
            }
        }
    }

    private void invokeFragmentActivators(ServiceReference sr)
    {
        PackageAdmin admin = (PackageAdmin) _context.getService(sr);
        Bundle[] fragments = admin.getFragments(_context.getBundle());
        if (fragments == null) { return; }
        for (Bundle frag : fragments)
        {
            // find a convention to look for a class inside the fragment.
            try
            {
                String fragmentActivator = frag.getSymbolicName() + ".FragmentActivator";
                Class<?> c = Class.forName(fragmentActivator);
                if (c != null)
                {
                    BundleActivator bActivator = (BundleActivator) c.newInstance();
                    bActivator.start(_context);
                    _activatedFragments.add(bActivator);
                }
            }
            catch (NullPointerException e)
            {
                // e.printStackTrace();
            }
            catch (InstantiationException e)
            {
                // e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                // e.printStackTrace();
            }
            catch (ClassNotFoundException e)
            {
                // e.printStackTrace();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public void stop()
    {
        INSTANCE = null;
        for (BundleActivator fragAct : _activatedFragments)
        {
            try
            {
                fragAct.stop(_context);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * @return true if the framework has completed all the start levels.
     */
    public boolean frameworkHasCompletedAutostarts()
    {
        return _startLevel == null ? true : _startLevel.getStartLevel() >= _maxStartLevel;
    }

    private static class ManifestTokenizer extends StringTokenizer
    {

        public ManifestTokenizer(String header)
        {
            super(header, ",");
        }

        @Override
        public String nextToken()
        {
            String token = super.nextToken();

            while (hasOpenQuote(token) && hasMoreTokens())
            {
                token += "," + super.nextToken();
            }
            return token;
        }

        private boolean hasOpenQuote(String token)
        {
            int i = -1;
            do
            {
                int quote = getQuote(token, i + 1);
                if (quote < 0) { return false; }

                i = token.indexOf(quote, i + 1);
                i = token.indexOf(quote, i + 1);
            }
            while (i >= 0);
            return true;
        }

        private int getQuote(String token, int offset)
        {
            int i = token.indexOf('"', offset);
            int j = token.indexOf('\'', offset);
            if (i < 0)
            {
                if (j < 0)
                {
                    return -1;
                }
                else
                {
                    return '\'';
                }
            }
            if (j < 0) { return '"'; }
            if (i < j) { return '"'; }
            return '\'';
        }

    }

}

Back to the top