Skip to main content
summaryrefslogtreecommitdiffstats
blob: d5940adf47487b2f6183cc8807a1564959e55e7f (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//
//  ========================================================================
//  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.
//  ========================================================================
//

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.TreeMap;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import org.eclipse.equinox.servletbridge.FrameworkLauncher;

/**
 * Extend the servletbridge FrameworkLauncher to support launching an equinox
 * installation made by p2director.
 */
public class FrameworkLauncherExtended extends FrameworkLauncher
{

    /**
     * if the OSGI_INSTALL_AREA installed area is specified as a sytem property
     * and matches a Folder on the file system, we don't copy the whole eclipse
     * installation instead we use that folder as it is
     */
    private static final String DEPLOY_IN_PLACE_WHEN_INSTALL_AREA_IS_FOLDER = "org.eclipse.equinox.servletbridge.deployinplace"; //$NON-NLS-1$

    public static final String FRAMEWORK_BOOTDELEGATION = "org.osgi.framework.bootdelegation";

    private boolean deployedInPlace = false;

    private URL resourceBaseAsURL = null;

    protected static Boolean ASYNCH_START_IN_PROGRESS;

    protected static Throwable ASYNCH_START_FAILURE = null;

    /**
     * If the start is asynch we do it in a different thread and return
     * immediately.
     */
    @Override
    public synchronized void start()
    {
        if (ASYNCH_START_IN_PROGRESS == null && "true".equals(super.config.getInitParameter("asyncStart")))
        {
            final ClassLoader webappCl = Thread.currentThread().getContextClassLoader();
            Thread th = new Thread()
            {
                public void run()
                {
                    Thread.currentThread().setContextClassLoader(webappCl);
                    System.out.println("Jetty-Nested: Starting equinox asynchronously.");
                    FrameworkLauncherExtended.this.start();
                    System.out.println("Jetty-Nested: Finished starting equinox asynchronously.");
                }
            };
            ASYNCH_START_IN_PROGRESS = true;
            try
            {
                th.start();
            }
            catch (Throwable t)
            {
                ASYNCH_START_FAILURE = t;
                if (t instanceof RuntimeException)
                {
                    throw (RuntimeException) t;
                }
                else
                {
                    throw new RuntimeException("Equinox failed to start", t);
                }
            }
            finally
            {
                ASYNCH_START_IN_PROGRESS = false;
            }
        }
        else
        {
            System.out.println("Jetty-Nested: Starting equinox synchronously.");
            super.start();
            System.out.println("Jetty-Nested: Finished starting equinox synchronously.");
        }
    }

    /**
     * try to find the resource base for this webapp by looking for the launcher
     * initialization file.
     */
    protected void initResourceBase()
    {
        try
        {
            String resourceBaseStr = System.getProperty(OSGI_INSTALL_AREA);
            if (resourceBaseStr == null || resourceBaseStr.length() == 0)
            {
                resourceBaseStr = config.getInitParameter(OSGI_INSTALL_AREA);
            }
            if (resourceBaseStr != null && resourceBaseStr.length() != 0)
            {
                // If the path starts with a reference to a system property,
                // resolve it.
                resourceBaseStr = resolveSystemProperty(resourceBaseStr);
                if (resourceBaseStr.startsWith("/WEB-INF/"))
                {
                    String rpath = context.getRealPath(resourceBaseStr);
                    if (rpath != null)
                    {
                        File rpathFile = new File(rpath);
                        if (rpathFile.exists() && rpathFile.isDirectory() && rpathFile.canWrite())
                        {
                            resourceBaseStr = rpath;
                        }
                    }
                }

                if (resourceBaseStr.startsWith("file://"))
                {
                    resourceBaseAsURL = new URL(resourceBaseStr.replace(" ", "%20")); //$NON-NLS-1$ //$NON-NLS-2$
                }
                else if (new File(resourceBaseStr).exists())
                {
                    resourceBaseAsURL = new URL("file://" + new File(resourceBaseStr).getCanonicalPath().replace(" ", "%20")); //$NON-NLS-1$ //$NON-NLS-2$
                }
                else
                {
                    resourceBaseAsURL = context.getResource(resourceBaseStr);
                }
            }
            else
            {
                if (context.getResource(RESOURCE_BASE + ECLIPSE) != null)
                {
                    resourceBase = RESOURCE_BASE + ECLIPSE;
                }
                else
                {
                    super.initResourceBase();
                }
                resourceBaseAsURL = context.getResource(resourceBase);
            }
        }
        catch (MalformedURLException e)
        {
            // ignore
        }
        catch (IOException e)
        {
            // ignore
        }
        if (resourceBaseAsURL != null && resourceBaseAsURL.getProtocol().equals("file"))
        {
            File resBase = new File(resourceBaseAsURL.getPath());
            if (resBase.exists() && resBase.isDirectory()
                    && !Boolean.FALSE.toString().equalsIgnoreCase(System.getProperty(DEPLOY_IN_PLACE_WHEN_INSTALL_AREA_IS_FOLDER)))
            {
                __setPlatformDirectory(resBase);
                deployedInPlace = true;
            }
        }
    }

    /**
     * Override this method to be able to set default system properties computed
     * on the fly depending on the environment where equinox and jetty-osgi are
     * deployed.
     * 
     * @param resource - The target to read properties from
     * @return the properties
     */
    protected Properties loadProperties(String resource)
    {
        Properties props = super.loadProperties(resource);
        if (resource.equals(resourceBase + LAUNCH_INI) && deployedInPlace)
        {
            String osgiInstall = props.getProperty(OSGI_INSTALL_AREA);
            if (osgiInstall == null)
            {
                // compute the osgi install dynamically.
                props.put(OSGI_INSTALL_AREA, getPlatformDirectory().getAbsolutePath());
            }
            String osgiFramework = props.getProperty(OSGI_FRAMEWORK);
            File pluginsFolder = null;
            if (osgiFramework == null && getPlatformDirectory() != null)
            {
                File osgiFrameworkF = findOsgiFramework(getPlatformDirectory());
                pluginsFolder = osgiFrameworkF.getParentFile();
                props.put(OSGI_FRAMEWORK, osgiFrameworkF.getAbsoluteFile().getAbsolutePath());
            }
            String osgiFrameworkExtensions = props.getProperty(OSGI_FRAMEWORK_EXTENSIONS);
            if (osgiFrameworkExtensions == null)
            {
                // this bundle will make the javax.servlet and
                // javax.servlet.http packages passed from
                // the bootstrap classloader into equinox
                osgiFrameworkExtensions = "org.eclipse.equinox.servletbridge.extensionbundle";
            }
            File configIni = new File(getPlatformDirectory(), "configuration/config.ini");
            Properties configIniProps = new Properties();
            if (configIni.exists())
            {
                System.out.println("Got the " + configIni.getAbsolutePath());
                InputStream configIniStream = null;
                try
                {
                    configIniStream = new FileInputStream(configIni);
                    configIniProps.load(configIniStream);
                }
                catch (IOException ioe)
                {

                }
                finally
                {
                    try
                    {
                        configIniStream.close();
                    }
                    catch (IOException ioe2)
                    {
                    }
                }
                String confIniFrameworkExt = configIniProps.getProperty(OSGI_FRAMEWORK_EXTENSIONS);
                if (confIniFrameworkExt != null)
                {
                    osgiFrameworkExtensions = osgiFrameworkExtensions + "," + confIniFrameworkExt;
                }
            }
            else
            {
                System.out.println("Unable to locate the " + configIni.getAbsolutePath());
            }
            props.setProperty(OSGI_FRAMEWORK_EXTENSIONS, osgiFrameworkExtensions);
            // __deployExtensionBundle(pluginsFolder);
            deployExtensionBundle(pluginsFolder, true);

            String bootDeleg = props.getProperty(FRAMEWORK_BOOTDELEGATION);
            if (bootDeleg == null)
            {
                bootDeleg = configIniProps.getProperty(FRAMEWORK_BOOTDELEGATION);
            }
            if (bootDeleg == null || bootDeleg.indexOf("javax.servlet.http") == -1)
            {
                String add = "javax.servlet,javax.servlet.http,javax.servlet.resources";
                if (bootDeleg != null)
                {
                    bootDeleg += add;
                }
                else
                {
                    bootDeleg = add;
                }
                props.setProperty(FRAMEWORK_BOOTDELEGATION, bootDeleg);
            }

            String jettyHome = System.getProperty("jetty.home");
            if (jettyHome == null)
            {
                jettyHome = getPlatformDirectory().getAbsolutePath();
                System.setProperty("jetty.home", jettyHome);
                props.setProperty("jetty.home", jettyHome);
            }
            else
            {
                jettyHome = resolveSystemProperty(jettyHome);
            }
            String etcJettyXml = System.getProperty("jetty.etc.config.urls");
            if (etcJettyXml == null)
            {
                etcJettyXml = "etc/jetty.xml";
                if (new File(jettyHome, "etc/jetty-nested.xml").exists())
                {
                    etcJettyXml += ",etc/jetty-nested.xml";
                }
                System.setProperty("jetty.etc.config.urls", etcJettyXml);
                props.setProperty("jetty.etc.config.urls", etcJettyXml);
            }
            String startLevel = System.getProperty("osgi.startLevel");
            if (startLevel == null)
            {
                startLevel = props.getProperty("osgi.startLevel");
                if (startLevel == null)
                {
                    startLevel = configIniProps.getProperty("osgi.startLevel");
                }
                if (startLevel != null)
                {
                    props.setProperty("osgi.startLevel", startLevel);
                    System.setProperty("osgi.startLevel", startLevel);
                }
            }
            String logback = System.getProperty("logback.configurationFile");
            if (logback == null)
            {
                File etcLogback = new File(jettyHome, "etc/logback-nested.xml");
                if (!etcLogback.exists())
                {
                    etcLogback = new File(jettyHome, "etc/logback.xml");
                }
                if (etcLogback.exists())
                {
                    System.setProperty("logback.configurationFile", etcLogback.getAbsolutePath());
                    props.setProperty("logback.configurationFile", etcLogback.getAbsolutePath());
                }
            }
            else
            {
                logback = resolveSystemProperty(logback);
            }
            System.out.println("sysout: logback.configurationFile=" + System.getProperty("logback.configurationFile"));
        }
        return props;
    }

    /**
     * Look for the eclipse.ini file. or any *.ini Search for the argument
     * -startup The next line is a relative path to the launcher osgi bundle:
     * ../bundlepool/plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar
     * Get that file, get the parent folder. This is where the plugins are
     * located. In that folder look for the
     * 
     * @param installFolder
     * @return The osgi framework bundle.
     */
    private File findOsgiFramework(File installFolder)
    {
        File[] fs = installFolder.listFiles();
        for (int i = 0; i < fs.length; i++)
        {
            File f = fs[i];
            if (f.isFile() && f.getName().endsWith(".ini") && !f.getName().equals(LAUNCH_INI))
            {
                BufferedReader br = null;
                try
                {
                    br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
                    String line = null;
                    String pathToLauncherJar = null;
                    boolean gotStartArg = false;
                    while ((line = br.readLine()) != null)
                    {
                        if (gotStartArg)
                        {
                            pathToLauncherJar = line.trim();
                            if (pathToLauncherJar.length() == 0)
                            {
                                continue;
                            }
                            break;
                        }
                        else if (line.trim().equals("-startup"))
                        {
                            gotStartArg = true;
                        }
                    }
                    if (pathToLauncherJar != null)
                    {
                        File currFolder = getPlatformDirectory();
                        String oriStartup = pathToLauncherJar;
                        while (pathToLauncherJar.startsWith("../"))
                        {
                            currFolder = currFolder.getParentFile();
                            pathToLauncherJar = pathToLauncherJar.substring(3);
                        }
                        File pluginsfolder = new File(currFolder, pathToLauncherJar).getParentFile();
                        // System.err.println("Got the pluginsfolder " +
                        // pluginsfolder);
                        if (!pluginsfolder.exists()) { throw new IllegalStateException("The -startup argument in " + f.getPath()
                                                                                       + " is "
                                                                                       + oriStartup
                                                                                       + ". It points to "
                                                                                       + pluginsfolder.getPath()
                                                                                       + " plugins directory that does not exists."); }
                        TreeMap osgis = new TreeMap();
                        File[] plugins = pluginsfolder.listFiles();
                        for (int j = 0; j < plugins.length; j++)
                        {
                            File b = plugins[j];
                            if (b.isFile() && b.getName().startsWith(FRAMEWORK_BUNDLE_NAME + "_") && b.getName().endsWith(".jar"))
                            {
                                osgis.put(b.getName(), b);
                            }
                        }
                        if (osgis.isEmpty()) { throw new IllegalStateException("The -startup argument in " + f.getPath()
                                                                               + " is "
                                                                               + oriStartup
                                                                               + ". It points to "
                                                                               + pluginsfolder.getPath()
                                                                               + " plugins directory but there is no org.eclipse.osgi.*.jar files there."); }
                        File osgiFramework = (File) osgis.values().iterator().next();
                        String path = osgiFramework.getPath();
                        System.err.println("Using " + path + " for the osgi framework.");
                        return osgiFramework;
                    }
                }
                catch (IOException ioe)
                {
                    //
                }
                finally
                {
                    if (br != null) try
                    {
                        br.close();
                    }
                    catch (IOException ii)
                    {
                    }
                }

            }
        }
        return null;
    }

    /**
     * recursively substitute the ${sysprop} by their actual system property.
     * ${sysprop,defaultvalue} will use 'defaultvalue' as the value if no
     * sysprop is defined. Not the most efficient code but we are shooting for
     * simplicity and speed of development here.
     * 
     * @param value
     * @return
     */
    public static String resolveSystemProperty(String value)
    {
        int ind = value.indexOf("${");
        if (ind == -1) { return value; }
        int ind2 = value.indexOf('}', ind);
        if (ind2 == -1) { return value; }
        String sysprop = value.substring(ind + 2, ind2);
        String defaultValue = null;
        int comma = sysprop.indexOf(',');
        if (comma != -1 && comma + 1 != sysprop.length())
        {
            defaultValue = sysprop.substring(comma + 1);
            defaultValue = resolveSystemProperty(defaultValue);
            sysprop = sysprop.substring(0, comma);
        }
        else
        {
            defaultValue = "${" + sysprop + "}";
        }

        String v = System.getProperty(sysprop);

        String reminder = value.length() > ind2 + 1 ? value.substring(ind2 + 1) : "";
        reminder = resolveSystemProperty(reminder);
        if (v != null)
        {
            return value.substring(0, ind) + v + reminder;
        }
        else
        {
            return value.substring(0, ind) + defaultValue + reminder;
        }
    }

    // introspection trick to be able to set the private field platformDirectory
    private static Field _field;

    void __setPlatformDirectory(File platformDirectory)
    {
        try
        {
            if (_field == null)
            {
                _field = org.eclipse.equinox.servletbridge.FrameworkLauncher.class.getDeclaredField("platformDirectory");
                _field.setAccessible(true);
            }
            _field.set(this, platformDirectory);
        }
        catch (SecurityException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }

    // introspection trick to invoke the generateExtensionBundle method
    private static Method _deployExtensionBundleMethod;

    private void __deployExtensionBundle(File plugins)
    {
        // look for the extensionbundle
        // if it is already there no need to do something:
        for (String file : plugins.list())
        {
            if (file.startsWith("org.eclipse.equinox.servletbridge.extensionbundle"))// EXTENSIONBUNDLE_DEFAULT_BSN
            { return; }
        }

        try
        {
            // invoke deployExtensionBundle(File plugins)
            if (_deployExtensionBundleMethod == null)
            { 
                _deployExtensionBundleMethod = FrameworkLauncher.class.getDeclaredMethod("deployExtensionBundle", File.class);
                _deployExtensionBundleMethod.setAccessible(true);
            }
            _deployExtensionBundleMethod.invoke(this, plugins);
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }

    // --end of introspection to invoke deployExtensionBundle

    // from Framework with support for the equinox hook
    private static final String EXTENSIONBUNDLE_DEFAULT_BSN = "org.eclipse.equinox.servletbridge.extensionbundle"; //$NON-NLS-1$

    private static final String EXTENSIONBUNDLE_DEFAULT_VERSION = "1.2.0"; //$NON-NLS-1$

    private static final String MANIFEST_VERSION = "Manifest-Version"; //$NON-NLS-1$

    private static final String BUNDLE_MANIFEST_VERSION = "Bundle-ManifestVersion"; //$NON-NLS-1$

    private static final String BUNDLE_NAME = "Bundle-Name"; //$NON-NLS-1$

    private static final String BUNDLE_SYMBOLIC_NAME = "Bundle-SymbolicName"; //$NON-NLS-1$

    private static final String BUNDLE_VERSION = "Bundle-Version"; //$NON-NLS-1$

    private static final String FRAGMENT_HOST = "Fragment-Host"; //$NON-NLS-1$

    private static final String EXPORT_PACKAGE = "Export-Package"; //$NON-NLS-1$

    private static final String CONFIG_EXTENDED_FRAMEWORK_EXPORTS = "extendedFrameworkExports"; //$NON-NLS-1$

    private void deployExtensionBundle(File plugins, boolean configureEquinoxHook)
    {
        // we might want to parameterize the extension bundle BSN in the future
        final String extensionBundleBSN = EXTENSIONBUNDLE_DEFAULT_BSN;
        File extensionBundleFile = findExtensionBundleFile(plugins, extensionBundleBSN);

        if (extensionBundleFile == null)
            generateExtensionBundle(plugins, extensionBundleBSN, EXTENSIONBUNDLE_DEFAULT_VERSION, configureEquinoxHook);
        else
            /*
             * if (Boolean.valueOf(config.getInitParameter(
             * CONFIG_OVERRIDE_AND_REPLACE_EXTENSION_BUNDLE)).booleanValue())
             */{
            String extensionBundleVersion = findExtensionBundleVersion(extensionBundleFile, extensionBundleBSN);
            if (extensionBundleFile.isDirectory())
            {
                deleteDirectory(extensionBundleFile);
            }
            else
            {
                extensionBundleFile.delete();
            }
            generateExtensionBundle(plugins, extensionBundleBSN, extensionBundleVersion, true);
            // } else {
            // processExtensionBundle(extensionBundleFile);
        }
    }

    private File findExtensionBundleFile(File plugins, final String extensionBundleBSN)
    {
        FileFilter extensionBundleFilter = new FileFilter()
        {
            public boolean accept(File candidate)
            {
                return candidate.getName().startsWith(extensionBundleBSN + "_"); //$NON-NLS-1$
            }
        };
        File[] extensionBundles = plugins.listFiles(extensionBundleFilter);
        if (extensionBundles.length == 0) return null;

        if (extensionBundles.length > 1)
        {
            for (int i = 1; i < extensionBundles.length; i++)
            {
                if (extensionBundles[i].isDirectory())
                {
                    deleteDirectory(extensionBundles[i]);
                }
                else
                {
                    extensionBundles[i].delete();
                }
            }
        }
        return extensionBundles[0];
    }

    private String findExtensionBundleVersion(File extensionBundleFile, String extensionBundleBSN)
    {
        String fileName = extensionBundleFile.getName();
        if (fileName.endsWith(".jar")) { return fileName.substring(extensionBundleBSN.length() + 1, fileName.length() - ".jar".length()); }
        return fileName.substring(extensionBundleBSN.length() + 1);
    }

    private void generateExtensionBundle(File plugins, String extensionBundleBSN, String extensionBundleVersion, boolean configureEquinoxHook)
    {
        Manifest mf = new Manifest();
        Attributes attribs = mf.getMainAttributes();
        attribs.putValue(MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
        attribs.putValue(BUNDLE_MANIFEST_VERSION, "2"); //$NON-NLS-1$
        attribs.putValue(BUNDLE_NAME, "Servletbridge Extension Bundle"); //$NON-NLS-1$
        attribs.putValue(BUNDLE_SYMBOLIC_NAME, extensionBundleBSN);
        attribs.putValue(BUNDLE_VERSION, extensionBundleVersion);
        attribs.putValue(FRAGMENT_HOST, "system.bundle; extension:=framework"); //$NON-NLS-1$

        String servletVersion = context.getMajorVersion() + "." + context.getMinorVersion(); //$NON-NLS-1$
        String packageExports = "org.eclipse.equinox.servletbridge; version=1.1"
                                 + ", javax.servlet; version="
                                 + servletVersion
                                 + ", javax.servlet.http; version="
                                 + servletVersion
                                 + ", javax.servlet.resources; version="
                                 + servletVersion; 

        String extendedExports = config.getInitParameter(CONFIG_EXTENDED_FRAMEWORK_EXPORTS);
        if (extendedExports != null && extendedExports.trim().length() != 0) packageExports += ", " + extendedExports; //$NON-NLS-1$

        attribs.putValue(EXPORT_PACKAGE, packageExports);
        writeJarFile(new File(plugins, extensionBundleBSN + "_" + extensionBundleVersion + ".jar"), mf, configureEquinoxHook); //$NON-NLS-1$
    }

    private void writeJarFile(File jarFile, Manifest mf, boolean configureEquinoxHook)
    {
        try
        {
            JarOutputStream jos = null;
            try
            {
                jos = new JarOutputStream(new FileOutputStream(jarFile), mf);

                if (configureEquinoxHook)
                {
                    // hook configurator properties:
                    ZipEntry e = new ZipEntry("hookconfigurators.properties");
                    jos.putNextEntry(e);
                    Properties props = new Properties();
                    props.put("hook.configurators", "org.eclipse.jetty.osgi.servletbridge.hook.ServletBridgeClassLoaderDelegateHook");
                    props.store(jos, "");
                    jos.closeEntry();

                    // the hook class
                    e = new ZipEntry("org/eclipse/jetty/osgi/servletbridge/hook/ServletBridgeClassLoaderDelegateHook.class");
                    jos.putNextEntry(e);
                    InputStream in = getClass().getResourceAsStream("/org/eclipse/jetty/osgi/servletbridge/hook/ServletBridgeClassLoaderDelegateHook.class");

                    byte[] buffer = new byte[512];
                    try
                    {
                        int n;
                        while ((n = in.read(buffer)) != -1)
                        {
                            jos.write(buffer, 0, n);
                        }
                    }
                    finally
                    {
                        in.close();
                    }
                    jos.closeEntry();
                }

                jos.finish();
            }
            finally
            {
                if (jos != null) jos.close();
            }
        }
        catch (IOException e)
        {
            context.log("Error writing extension bundle", e); //$NON-NLS-1$
        }
    }
    // --from Framework with support for the equinox hook

}

Back to the top