This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 179473
Collapse All | Expand All

(-)a/apisupport.harness/nbproject/project.properties (+1 lines)
Lines 84-89 Link Here
84
    org/netbeans/nbbuild/MakeListOfNBM*.class,\
84
    org/netbeans/nbbuild/MakeListOfNBM*.class,\
85
    org/netbeans/nbbuild/MakeMasterJNLP*.class,\
85
    org/netbeans/nbbuild/MakeMasterJNLP*.class,\
86
    org/netbeans/nbbuild/MakeNBM*.class,\
86
    org/netbeans/nbbuild/MakeNBM*.class,\
87
    org/netbeans/nbbuild/MakeOSGi*.class,\
87
    org/netbeans/nbbuild/MakeUpdateDesc*.class,\
88
    org/netbeans/nbbuild/MakeUpdateDesc*.class,\
88
    org/netbeans/nbbuild/ModuleListParser*.class,\
89
    org/netbeans/nbbuild/ModuleListParser*.class,\
89
    org/netbeans/nbbuild/ModuleSelector*.class,\
90
    org/netbeans/nbbuild/ModuleSelector*.class,\
(-)a/apisupport.harness/release/osgi.xml (+107 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!--
3
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
5
Copyright 2010 Sun Microsystems, Inc. All rights reserved.
6
7
The contents of this file are subject to the terms of either the GNU
8
General Public License Version 2 only ("GPL") or the Common
9
Development and Distribution License("CDDL") (collectively, the
10
"License"). You may not use this file except in compliance with the
11
License. You can obtain a copy of the License at
12
http://www.netbeans.org/cddl-gplv2.html
13
or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
14
specific language governing permissions and limitations under the
15
License.  When distributing the software, include this License Header
16
Notice in each file and include the License file at
17
nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
18
particular file as subject to the "Classpath" exception as provided
19
by Sun in the GPL Version 2 section of the License file that
20
accompanied this code. If applicable, add the following below the
21
License Header, with the fields enclosed by brackets [] replaced by
22
your own identifying information:
23
"Portions Copyrighted [year] [name of copyright owner]"
24
25
Contributor(s):
26
27
The Original Software is NetBeans. The Initial Developer of the Original
28
Software is Sun Microsystems, Inc. Portions Copyright 2010 Sun
29
Microsystems, Inc. All Rights Reserved.
30
31
If you wish your version of this file to be governed by only the CDDL
32
or only the GPL Version 2, indicate your decision by adding
33
"[Contributor] elects to include this software in this distribution
34
under the [CDDL or GPL Version 2] license." If you do not indicate a
35
single choice of license, a recipient has the option to distribute
36
your version of this file under either the CDDL, the GPL Version 2 or
37
to extend the choice of license to its licensees as provided above.
38
However, if you add GPL Version 2 code and therefore, elected the GPL
39
Version 2 license, then the option applies only if the new code is
40
made subject to such option by the copyright holder.
41
-->
42
<project name="suite-osgi" basedir=".">
43
44
    <target name="init">
45
        <property name="osgi.dest.dir" location="build/osgi"/>
46
        <property name="osgi.cache.dir" location="build/osgi-cache"/>
47
        <property name="disabled.modules" value=""/>
48
        <property name="enabled.clusters" value=""/>
49
        <property name="disabled.clusters" value=""/>
50
    </target>
51
52
    <target name="build" depends="init">
53
        <mkdir dir="${osgi.dest.dir}"/>
54
        <makeosgi destdir="${osgi.dest.dir}">
55
            <pathfileset>
56
                <path refid="cluster.path.id"/>
57
                <and>
58
                    <or>
59
                        <filename name="modules/*.jar"/>
60
                        <filename name="lib/*.jar"/>
61
                        <filename name="core/*.jar"/>
62
                    </or>
63
                    <custom classpath="${harness.dir}/tasks.jar" classname="org.netbeans.nbbuild.ModuleSelector">
64
                        <param name="excludeModules" value="${disabled.modules}"/>
65
                        <param name="includeClusters" value="${enabled.clusters}"/>
66
                        <param name="excludeClusters" value="${disabled.clusters}"/>
67
                    </custom>
68
                </and>
69
            </pathfileset>
70
            <fileset dir="${cluster}">
71
                <include name="modules/*.jar"/>
72
                <include name="lib/*.jar"/>
73
                <include name="core/*.jar"/>
74
            </fileset>
75
        </makeosgi>
76
    </target>
77
78
    <target name="run" depends="init">
79
        <!-- XXX Felix cache does not seem to work correctly, uses stale JARs; and org.osgi.framework.storage.clean=onFirstInit broken too: -->
80
        <delete dir="${osgi.cache.dir}"/>
81
        <mkdir dir="${osgi.cache.dir}"/>
82
        <!-- From felix/main/src/main/resources/config.properties; XXX is there a better way to grab these? -->
83
        <property name="felix-sys-prop.felix.auto.deploy.action" value="install,start"/>
84
        <property name="felix-sys-prop.felix.log.level" value="4"/>
85
        <property name="felix-sys-prop.org.osgi.service.http.port" value="8080"/>
86
        <property name="felix-sys-prop.obr.repository.url" value="http://felix.apache.org/obr/releases.xml"/>
87
        <java fork="true" classname="org.apache.felix.main.Main">
88
            <classpath>
89
                <pathfileset>
90
                    <path refid="cluster.path.id"/>
91
                    <or>
92
                        <filename name="core/osgi*.jar"/>
93
                        <filename name="modules/ext/felix*.jar"/>
94
                    </or>
95
                </pathfileset>
96
            </classpath>
97
            <arg value="-b"/>
98
            <arg file="${osgi.dest.dir}"/>
99
            <arg file="${osgi.cache.dir}"/>
100
            <syspropertyset>
101
                <propertyref prefix="felix-sys-prop."/>
102
                <mapper type="glob" from="felix-sys-prop.*" to="*"/>
103
            </syspropertyset>
104
        </java>
105
    </target>
106
107
</project>
(-)a/apisupport.harness/release/suite.xml (-1 / +13 lines)
Lines 510-516 Link Here
510
            <property name="jnlp.generate.versions" value="false"/>  <!-- versioning is not enabled w/o app server-->
510
            <property name="jnlp.generate.versions" value="false"/>  <!-- versioning is not enabled w/o app server-->
511
        </ant>
511
        </ant>
512
    </target>
512
    </target>
513
    
513
514
    <target name="build-osgi" depends="build,-jdk-init" description="Converts this suite into OSGi-compatible bundles.">
515
        <ant antfile="${harness.dir}/osgi.xml" target="build">
516
            <reference refid="cluster.path.id"/>
517
        </ant>
518
    </target>
519
520
    <target name="run-osgi" depends="build-osgi" description="Runs this suite in an OSGi container.">
521
        <ant antfile="${harness.dir}/osgi.xml" target="run">
522
            <reference refid="cluster.path.id"/>
523
        </ant>
524
    </target>
525
514
    <target name="test" depends="build" description="Runs tests for all modules in the suite.">
526
    <target name="test" depends="build" description="Runs tests for all modules in the suite.">
515
        <sortsuitemodules unsortedmodules="${modules}" sortedmodulesproperty="modules.test.sorted" sorttests="true"/>
527
        <sortsuitemodules unsortedmodules="${modules}" sortedmodulesproperty="modules.test.sorted" sorttests="true"/>
516
        <property name="continue.after.failing.tests" value="true"/> <!-- fallback -->
528
        <property name="continue.after.failing.tests" value="true"/> <!-- fallback -->
(-)a/apisupport.harness/taskdefs.properties (+1 lines)
Lines 43-48 Link Here
43
makenbm=org.netbeans.nbbuild.MakeNBM
43
makenbm=org.netbeans.nbbuild.MakeNBM
44
makejnlp=org.netbeans.nbbuild.MakeJNLP
44
makejnlp=org.netbeans.nbbuild.MakeJNLP
45
makemasterjnlp=org.netbeans.nbbuild.MakeMasterJNLP
45
makemasterjnlp=org.netbeans.nbbuild.MakeMasterJNLP
46
makeosgi=org.netbeans.nbbuild.MakeOSGi
46
parseprojectxml=org.netbeans.nbbuild.ParseProjectXml
47
parseprojectxml=org.netbeans.nbbuild.ParseProjectXml
47
genlist=org.netbeans.nbbuild.MakeListOfNBM
48
genlist=org.netbeans.nbbuild.MakeListOfNBM
48
arch=org.netbeans.nbbuild.Arch
49
arch=org.netbeans.nbbuild.Arch
(-)a/apisupport.project/src/org/netbeans/modules/apisupport/project/queries/GlobalSourceForBinaryImpl.java (+1 lines)
Lines 111-116 Link Here
111
                    }
111
                    }
112
                }
112
                }
113
            }
113
            }
114
            // XXX $suite/build/osgi/$cnb-*.jar => $cnb in $suite
114
            NbPlatform supposedPlaf = null;
115
            NbPlatform supposedPlaf = null;
115
            for (NbPlatform plaf : NbPlatform.getPlatforms()) {
116
            for (NbPlatform plaf : NbPlatform.getPlatforms()) {
116
                // XXX more robust condition?
117
                // XXX more robust condition?
(-)a/core.netigso/manifest.mf (-1 / +1 lines)
Lines 5-9 Link Here
5
OpenIDE-Module-Specification-Version: 1.1
5
OpenIDE-Module-Specification-Version: 1.1
6
OpenIDE-Module-Recommends: org.osgi.framework.launch.FrameworkFactory
6
OpenIDE-Module-Recommends: org.osgi.framework.launch.FrameworkFactory
7
AutoUpdate-Essential-Module: true
7
AutoUpdate-Essential-Module: true
8
X-Comment: various OSGi headers are set by the MakeOSGi task
8
9
9
(-)a/core.netigso/nbproject/project.xml (+8 lines)
Lines 73-78 Link Here
73
                    </run-dependency>
73
                    </run-dependency>
74
                </dependency>
74
                </dependency>
75
                <dependency>
75
                <dependency>
76
                    <code-name-base>org.openide.filesystems</code-name-base>
77
                    <build-prerequisite/>
78
                    <compile-dependency/>
79
                    <run-dependency>
80
                        <specification-version>7.33</specification-version>
81
                    </run-dependency>
82
                </dependency>
83
                <dependency>
76
                    <code-name-base>org.openide.modules</code-name-base>
84
                    <code-name-base>org.openide.modules</code-name-base>
77
                    <build-prerequisite/>
85
                    <build-prerequisite/>
78
                    <compile-dependency/>
86
                    <compile-dependency/>
(-)a/core.netigso/src/org/netbeans/core/osgi/Activator.java (+164 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.core.osgi;
41
42
import java.net.URL;
43
import java.util.ArrayList;
44
import java.util.List;
45
import java.util.logging.Level;
46
import java.util.logging.Logger;
47
import org.openide.modules.ModuleInstall;
48
import org.openide.util.SharedClassObject;
49
import org.osgi.framework.Bundle;
50
import org.osgi.framework.BundleActivator;
51
import org.osgi.framework.BundleContext;
52
import org.osgi.framework.BundleEvent;
53
import org.osgi.framework.BundleListener;
54
import org.osgi.framework.Constants;
55
import org.osgi.framework.FrameworkEvent;
56
import org.osgi.framework.FrameworkListener;
57
58
/**
59
 * Initializes critical NetBeans infrastructure inside an OSGi container.
60
 */
61
public class Activator implements BundleActivator, BundleListener, FrameworkListener {
62
63
    public Activator() {}
64
65
    public @Override void start(BundleContext context) throws Exception {
66
        if (System.getProperty("netbeans.home") != null) {
67
            throw new IllegalStateException("Should not be run from inside regular NetBeans module system");
68
        }
69
        String storage = context.getProperty(Constants.FRAMEWORK_STORAGE);
70
        if (storage != null) {
71
            System.setProperty("netbeans.user", storage);
72
        }
73
        OSGiMainLookup.initialize(context);
74
        context.addFrameworkListener(this);
75
        context.addBundleListener(this);
76
        System.err.println("XXX processing already loaded bundles...");
77
        for (Bundle b : context.getBundles()) {
78
            switch (b.getState()) {
79
            case Bundle.ACTIVE:
80
                // XXX coalesce these layer events
81
                bundleChanged(new BundleEvent(BundleEvent.RESOLVED, b));
82
                // XXX should resolve all first, then start all:
83
                bundleChanged(new BundleEvent(BundleEvent.STARTED, b));
84
                break;
85
            case Bundle.RESOLVED:
86
            case Bundle.STARTING:
87
            case Bundle.STOPPING:
88
                bundleChanged(new BundleEvent(BundleEvent.RESOLVED, b));
89
                break;
90
            }
91
        }
92
        System.err.println("XXX done processing already loaded bundles.");
93
    }
94
95
    public @Override void stop(BundleContext context) throws Exception {}
96
97
    public @Override void bundleChanged(BundleEvent event) {
98
        Bundle bundle = event.getBundle();
99
        switch (event.getType()) {
100
        case BundleEvent.RESOLVED:
101
            System.err.println("XXX resolved " + bundle.getSymbolicName());
102
            OSGiMainLookup.bundleResolved(bundle);
103
            OSGiRepository.DEFAULT.addLayers(layersFor(bundle));
104
            // XXX install URLStreamHandlerService (OSGi spec 11.4); delegate to META-INF/namedservices/<protocol>/java.net.URLStreamHandler
105
            break;
106
        case BundleEvent.UNRESOLVED:
107
            System.err.println("XXX unresolved " + bundle.getSymbolicName());
108
            OSGiMainLookup.bundleUnresolved(bundle);
109
            OSGiRepository.DEFAULT.removeLayers(layersFor(bundle));
110
            break;
111
        case BundleEvent.STARTED:
112
            System.err.println("XXX started " + bundle.getSymbolicName());
113
            ModuleInstall mi = installerFor(bundle);
114
            if (mi != null) {
115
                System.err.println("XXX running " + mi.getClass().getName() + ".restored()");
116
                mi.restored();
117
            }
118
            // XXX if o.n.core (or o.n.m.settings?) tell OSGiMainLookup to use CoreBridge.getDefault().lookupCacheLoad()
119
            break;
120
        case BundleEvent.STOPPED:
121
            System.err.println("XXX stopped " + bundle.getSymbolicName());
122
            mi = installerFor(bundle);
123
            if (mi != null) {
124
                System.err.println("XXX running " + mi.getClass().getName() + ".uninstalled()");
125
                mi.uninstalled();
126
            }
127
            break;
128
        }
129
    }
130
131
    public @Override void frameworkEvent(FrameworkEvent event) {
132
        System.err.println("XXX framework event " + event.getType() + " on " + event.getBundle().getSymbolicName());
133
        // XXX perhaps defer processing various things until the framework is started
134
    }
135
136
    private static URL[] layersFor(Bundle b) {
137
        List<URL> layers = new ArrayList<URL>(2);
138
        String explicit = (String) b.getHeaders().get("OpenIDE-Module-Layer");
139
        if (explicit != null) {
140
            layers.add(b.getResource(explicit));
141
            // XXX could also add localized/branded variants
142
        }
143
        URL generated = b.getResource("META-INF/generated-layer.xml");
144
        if (generated != null) {
145
            layers.add(generated);
146
        }
147
        return layers.toArray(new URL[layers.size()]);
148
    }
149
150
    private static ModuleInstall installerFor(Bundle b) {
151
        String respath = (String) b.getHeaders().get("OpenIDE-Module-Install");
152
        if (respath != null) {
153
            String fqn = respath.replaceFirst("[.]class$", "").replace('/', '.');
154
            try {
155
                return SharedClassObject.findObject(((Class<?>) b.loadClass(fqn)).asSubclass(ModuleInstall.class), true);
156
            } catch (Exception x) { // CNFE, CCE, ...
157
                Logger.getLogger(Activator.class.getName()).log(Level.WARNING, "Could not load " + fqn, x);
158
                return null;
159
            }
160
        }
161
        return null;
162
    }
163
164
}
(-)a/core.netigso/src/org/netbeans/core/osgi/OSGiClassLoader.java (+110 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.core.osgi;
41
42
import java.io.IOException;
43
import java.net.URL;
44
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.Enumeration;
47
import java.util.List;
48
import org.openide.util.Enumerations;
49
import org.openide.util.NbCollections;
50
import org.osgi.framework.Bundle;
51
import org.osgi.framework.BundleContext;
52
53
/**
54
 * Delegates to all loaded bundles.
55
 */
56
class OSGiClassLoader extends ClassLoader {
57
58
    private final BundleContext context;
59
60
    public OSGiClassLoader(BundleContext context) {
61
        this.context = context;
62
    }
63
64
    protected @Override Class<?> findClass(String name) throws ClassNotFoundException {
65
        for (Bundle b : context.getBundles()) {
66
            if (b.getState() == Bundle.INSTALLED) {
67
                continue;
68
            }
69
            try {
70
                return b.loadClass(name);
71
            } catch (ClassNotFoundException x) {
72
                // normal, try next one
73
            }
74
        }
75
        return super.findClass(name);
76
    }
77
78
    protected @Override URL findResource(String name) {
79
        for (Bundle b : context.getBundles()) {
80
            if (b.getState() == Bundle.INSTALLED) {
81
                continue;
82
            }
83
            URL resource = b.getResource(name);
84
            if (resource != null) {
85
                return resource;
86
            }
87
        }
88
        return super.findResource(name);
89
    }
90
91
    protected @Override Enumeration<URL> findResources(String name) throws IOException {
92
        List<Enumeration<URL>> resourcess = new ArrayList<Enumeration<URL>>();
93
        for (Bundle b : context.getBundles()) {
94
            if (b.getState() == Bundle.INSTALLED) {
95
                continue;
96
            }
97
            Enumeration resourcesRaw = b.getResources(name);
98
            if (resourcesRaw == null) {
99
                // Oddly, this is permitted.
100
                continue;
101
            }
102
            Enumeration<URL> resources = NbCollections.checkedEnumerationByFilter(resourcesRaw, URL.class, true);
103
            if (resources != null) {
104
                resourcess.add(resources);
105
            }
106
        }
107
        return Enumerations.concat(Collections.enumeration(resourcess));
108
    }
109
110
}
(-)a/core.netigso/src/org/netbeans/core/osgi/OSGiMainLookup.java (+111 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.core.osgi;
41
42
import java.util.ArrayList;
43
import java.util.List;
44
import org.openide.util.Lookup;
45
import org.openide.util.lookup.Lookups;
46
import org.openide.util.lookup.ProxyLookup;
47
import org.osgi.framework.Bundle;
48
import org.osgi.framework.BundleContext;
49
50
/**
51
 * Default lookup when running inside an OSGi container.
52
 */
53
public class OSGiMainLookup extends ProxyLookup {
54
55
    private static BundleContext context;
56
57
    private static OSGiMainLookup get() {
58
        return (OSGiMainLookup) Lookup.getDefault();
59
    }
60
61
    public static void initialize(BundleContext _context) throws Exception {
62
        System.setProperty(Lookup.class.getName(), OSGiMainLookup.class.getName());
63
        context = _context;
64
        OSGiMainLookup lkp;
65
        ClassLoader oldCCL = Thread.currentThread().getContextClassLoader();
66
        Thread.currentThread().setContextClassLoader(OSGiMainLookup.class.getClassLoader());
67
        try {
68
            lkp = get();
69
        } finally {
70
            Thread.currentThread().setContextClassLoader(oldCCL);
71
        }
72
        lkp.postInit();
73
    }
74
75
    static void bundleResolved(Bundle bundle) {
76
        // XXX extend existing classLoader
77
        get().setClassLoader();
78
    }
79
80
    static void bundleUnresolved(Bundle bundle) {
81
        get().setClassLoader();
82
    }
83
84
    private ClassLoader classLoader;
85
    private final List<Lookup> nonClassLoaderDelegates = new ArrayList<Lookup>();
86
87
    public OSGiMainLookup() {}
88
89
    private void postInit() {
90
        nonClassLoaderDelegates.add(Lookups.forPath("Services/"));
91
        nonClassLoaderDelegates.add(Lookups.fixed(OSGiRepository.DEFAULT));
92
        // XXX add ModuleInfo objects corresponding to bundles
93
        // XXX InstalledFileLocator impl for OSGI-INF/files/*
94
        setClassLoader();
95
    }
96
97
    private void setClassLoader() {
98
        classLoader = new OSGiClassLoader(context);
99
        // XXX should it be set as thread CCL? would help some NB APIs, but might break OSGi conventions
100
        setDelegates();
101
    }
102
103
    private void setDelegates() {
104
        Lookup[] delegates = new Lookup[nonClassLoaderDelegates.size() + 2];
105
        nonClassLoaderDelegates.toArray(delegates);
106
        delegates[delegates.length - 2] = Lookups.metaInfServices(classLoader);
107
        delegates[delegates.length - 1] = Lookups.singleton(classLoader);
108
        setLookups(delegates);
109
    }
110
111
}
(-)a/core.netigso/src/org/netbeans/core/osgi/OSGiRepository.java (+118 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.core.osgi;
41
42
import java.net.URL;
43
import java.util.Arrays;
44
import java.util.HashMap;
45
import java.util.Map;
46
import java.util.logging.Level;
47
import java.util.logging.Logger;
48
import org.openide.filesystems.FileSystem;
49
import org.openide.filesystems.MultiFileSystem;
50
import org.openide.filesystems.Repository;
51
import org.openide.filesystems.XMLFileSystem;
52
import org.xml.sax.SAXException;
53
54
/**
55
 * Default repository to which layers can be added or removed.
56
 */
57
class OSGiRepository extends Repository {
58
59
    public static final OSGiRepository DEFAULT = new OSGiRepository();
60
61
    private final LayerFS fs;
62
63
    private OSGiRepository() {
64
        this(new LayerFS());
65
    }
66
67
    private OSGiRepository(LayerFS fs) {
68
        super(fs);
69
        this.fs = fs;
70
    }
71
72
    public void addLayers(URL... resources) {
73
        if (resources.length > 0) {
74
            System.err.println("XXX adding layers: " + Arrays.toString(resources));
75
            fs.addLayers(resources);
76
        }
77
    }
78
79
    public void removeLayers(URL... resources) {
80
        if (resources.length > 0) {
81
            System.err.println("XXX removing layers: " + Arrays.toString(resources));
82
            fs.removeLayers(resources);
83
        }
84
    }
85
86
    private static final class LayerFS extends MultiFileSystem {
87
88
        private final Map<URL,FileSystem> fss = new HashMap<URL,FileSystem>();
89
90
        LayerFS() {
91
            reset();
92
        }
93
94
        private synchronized void addLayers(URL... resources) {
95
            for (URL resource : resources) {
96
                try {
97
                    fss.put(resource, new XMLFileSystem(resource));
98
                } catch (SAXException x) {
99
                    Logger.getLogger(OSGiRepository.class.getName()).log(Level.WARNING, "Could not parse layer: " + resource, x);
100
                }
101
            }
102
            reset();
103
        }
104
105
        private synchronized void removeLayers(URL... resources) {
106
            fss.keySet().removeAll(Arrays.asList(resources));
107
            reset();
108
        }
109
110
        private void reset() {
111
            setDelegates(fss.values().toArray(new FileSystem[fss.size()]));
112
            // XXX add ${netbeans.user}/config as a writable layer
113
            // XXX also include FileSystem's from lookup
114
        }
115
116
    }
117
118
}
(-)a/core.netigso/src/org/netbeans/core/osgi/package-info.java (+44 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
38
 */
39
40
/**
41
 * Runtime services for use by NetBeans modules translated to bundles
42
 * running inside an OSGi container.
43
 */
44
package org.netbeans.core.osgi;
(-)a/libs.felix/external/binaries-list (+1 lines)
Line 1 Link Here
1
8F69CD29825A30B6AA774BD79576C909AEC07668 felix-2.0.3.jar
1
8F69CD29825A30B6AA774BD79576C909AEC07668 felix-2.0.3.jar
2
BD5615C6A15497B60A0AAA9A04D4F05E2BC42D07 felix-main-2.0.2.jar
(-)a/libs.felix/external/felix-2.0.3-license.txt (+1 lines)
Lines 3-8 Link Here
3
Description: Apache Felix OSGi container (minus org.osgi.** packages).
3
Description: Apache Felix OSGi container (minus org.osgi.** packages).
4
License: Apache-2.0
4
License: Apache-2.0
5
OSR: 13360
5
OSR: 13360
6
Files: felix-2.0.3.jar felix-main-2.0.2.jar
6
Origin: http://archive.apache.org/dist/felix/
7
Origin: http://archive.apache.org/dist/felix/
7
8
8
                                 Apache License
9
                                 Apache License
(-)a/libs.felix/nbproject/project.properties (+1 lines)
Lines 38-43 Link Here
38
# made subject to such option by the copyright holder.
38
# made subject to such option by the copyright holder.
39
39
40
release.external/felix-2.0.3.jar=modules/ext/felix-2.0.3.jar
40
release.external/felix-2.0.3.jar=modules/ext/felix-2.0.3.jar
41
release.external/felix-main-2.0.2.jar=modules/ext/felix-main-2.0.2.jar
41
42
42
javac.source=1.6
43
javac.source=1.6
43
javac.compilerargs=-Xlint -Xlint:-serial
44
javac.compilerargs=-Xlint -Xlint:-serial
(-)a/nbbuild/antsrc/org/netbeans/nbbuild/MakeOSGi.java (+378 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.nbbuild;
41
42
import java.io.ByteArrayOutputStream;
43
import java.io.File;
44
import java.io.FileInputStream;
45
import java.io.FileOutputStream;
46
import java.io.IOException;
47
import java.io.InputStream;
48
import java.io.OutputStream;
49
import java.net.URI;
50
import java.util.ArrayList;
51
import java.util.Enumeration;
52
import java.util.HashMap;
53
import java.util.HashSet;
54
import java.util.Iterator;
55
import java.util.List;
56
import java.util.Map;
57
import java.util.Properties;
58
import java.util.Set;
59
import java.util.jar.Attributes;
60
import java.util.jar.JarFile;
61
import java.util.jar.JarOutputStream;
62
import java.util.jar.Manifest;
63
import java.util.regex.Matcher;
64
import java.util.regex.Pattern;
65
import java.util.zip.CRC32;
66
import java.util.zip.ZipEntry;
67
import java.util.zip.ZipOutputStream;
68
import org.apache.tools.ant.BuildException;
69
import org.apache.tools.ant.Project;
70
import org.apache.tools.ant.Task;
71
import org.apache.tools.ant.types.ResourceCollection;
72
import org.apache.tools.ant.types.resources.FileResource;
73
import org.w3c.dom.Document;
74
import org.w3c.dom.Element;
75
import org.w3c.dom.NodeList;
76
import org.xml.sax.InputSource;
77
78
/**
79
 * Converts a set of NetBeans modules into OSGi bundles.
80
 */
81
public class MakeOSGi extends Task {
82
    
83
    private File destdir;
84
    private List<ResourceCollection> modules = new ArrayList<ResourceCollection>();
85
    
86
    /**
87
     * Mandatory destination directory. Bundles will be created here.
88
     */
89
    public void setDestdir(File destdir) {
90
        this.destdir = destdir;
91
    }
92
    
93
    /**
94
     * Adds a set of module JARs.
95
     * It is permitted for them to be JARs anywhere on disk,
96
     * but it is best if they are in a cluster structure
97
     * with ../update_tracking/*.xml present
98
     * so that associated files can be included in the bundle.
99
     */
100
    public void add(ResourceCollection modules) {
101
        this.modules.add(modules);
102
    }
103
104
    public @Override void execute() throws BuildException {
105
        if (destdir == null) {
106
            throw new BuildException("missing destdir");
107
        }
108
        for (ResourceCollection rc : modules) {
109
            Iterator it = rc.iterator();
110
            while (it.hasNext()) {
111
                File jar = ((FileResource) it.next()).getFile();
112
                try {
113
                    process(jar);
114
                } catch (Exception x) {
115
                    throw new BuildException("Could not process " + jar + ": " + x, x, getLocation());
116
                }
117
            }
118
        }
119
    }
120
121
    private void process(File module) throws Exception {
122
        JarFile jar = new JarFile(module);
123
        try {
124
            Manifest netbeans = jar.getManifest();
125
            Manifest osgi = new Manifest();
126
            if (netbeans.getMainAttributes().getValue("Bundle-SymbolicName") != null) {
127
                // XXX copy it as is!
128
            }
129
            osgi.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug
130
            osgi.getMainAttributes().putValue("Bundle-ManifestVersion", "2");
131
            String codename = netbeans.getMainAttributes().getValue("OpenIDE-Module");
132
            String cnb = codename.replaceFirst("/\\d+$", "");
133
            if (cnb.equals("org.netbeans.core.netigso")) {
134
                // special handling...
135
                netbeans.getMainAttributes().remove(new Attributes.Name("OpenIDE-Module-Install"));
136
                netbeans.getMainAttributes().remove(new Attributes.Name("Class-Path"));
137
                osgi.getMainAttributes().putValue("Bundle-Activator", "org.netbeans.core.osgi.Activator");
138
                osgi.getMainAttributes().putValue("Import-Package", "org.osgi.framework");
139
            }
140
            osgi.getMainAttributes().putValue("Bundle-SymbolicName", cnb);
141
            String spec = netbeans.getMainAttributes().getValue("OpenIDE-Module-Specification-Version");
142
            String bundleVersion = null;
143
            if (spec != null) {
144
                bundleVersion = threeDotsWithMajor(spec, codename);
145
                String buildVersion = netbeans.getMainAttributes().getValue("OpenIDE-Module-Build-Version");
146
                if (buildVersion == null) {
147
                    buildVersion = netbeans.getMainAttributes().getValue("OpenIDE-Module-Implementation-Version");
148
                }
149
                if (buildVersion != null) {
150
                    bundleVersion += "." + buildVersion.replaceAll("[^a-zA-Z0-9_-]", "_");
151
                }
152
                osgi.getMainAttributes().putValue("Bundle-Version", bundleVersion);
153
            }
154
            File bundleFile = new File(destdir, cnb + (bundleVersion != null ? "-" + bundleVersion : "") + ".jar");
155
            if (bundleFile.lastModified() > module.lastModified()) {
156
                log("Skipping " + module + " since " + bundleFile + " is newer", Project.MSG_VERBOSE);
157
                return;
158
            }
159
            log("Processing " + module + " into " + bundleFile);
160
            String pp = netbeans.getMainAttributes().getValue("OpenIDE-Module-Public-Packages");
161
            if (pp != null && !pp.equals("-")) {
162
                // XXX handle .** (subpackages)
163
                // XXX if have an integer OpenIDE-Module-Specification-Version, export all packages
164
                osgi.getMainAttributes().putValue("Export-Package", pp.replaceAll("\\.\\*", ""));
165
                // OpenIDE-Module-Friends is ignored since OSGi has no apparent equivalent
166
            }
167
            for (String attrToCopy : new String[] {"OpenIDE-Module-Layer", "OpenIDE-Module-Install"}) {
168
                String val = netbeans.getMainAttributes().getValue(attrToCopy);
169
                if (val != null) {
170
                    osgi.getMainAttributes().putValue(attrToCopy, val);
171
                }
172
            }
173
            StringBuilder requireBundles = new StringBuilder();
174
            /* XXX does not work, perhaps because of cyclic dependencies:
175
            // do not need to import any API, just need it to be started:
176
            requireBundles.append("org.netbeans.core.netigso");
177
             */
178
            String dependencies = netbeans.getMainAttributes().getValue("OpenIDE-Module-Module-Dependencies");
179
            if (dependencies != null) {
180
                for (String dependency : dependencies.split(" *, *")) {
181
                    if (requireBundles.length() > 0) {
182
                        requireBundles.append(", ");
183
                    }
184
                    translateDependency(requireBundles, dependency);
185
                }
186
            }
187
            if (requireBundles.length() > 0) {
188
                osgi.getMainAttributes().putValue("Require-Bundle", requireBundles.toString());
189
            }
190
            // XXX OpenIDE-Module-Java-Dependencies => Bundle-RequiredExecutionEnvironment: JavaSE-1.6
191
            // XXX OpenIDE-Module-Package-Dependencies => Import-Package
192
            // OpenIDE-Module-{Provides,Requires,Needs} are ignored since OSGi has no apparent equivalent
193
            // (achievable by exposing generic provide/require mechanisms of Felix and/or Equinox,
194
            // but this would not be part of the OSGi R4.1 spec, but could be raised as an issue for R5 since it was discussed for R4)
195
            // autoload, eager status are ignored since OSGi has no apparent equivalent
196
            Properties localizedStrings = new Properties();
197
            String locbundle = netbeans.getMainAttributes().getValue("OpenIDE-Module-Localizing-Bundle");
198
            if (locbundle != null) {
199
                InputStream is = jar.getInputStream(jar.getEntry(locbundle));
200
                try {
201
                    localizedStrings.load(is);
202
                } finally {
203
                    is.close();
204
                }
205
                osgi.getMainAttributes().putValue("Bundle-Localization", locbundle.replaceFirst("[.]properties$", ""));
206
            }
207
            handleDisplayAttribute(localizedStrings, netbeans.getMainAttributes(), osgi.getMainAttributes(),
208
                    "OpenIDE-Module-Name", "Bundle-Name");
209
            handleDisplayAttribute(localizedStrings, netbeans.getMainAttributes(), osgi.getMainAttributes(),
210
                    "OpenIDE-Module-Display-Category", "Bundle-Category");
211
            handleDisplayAttribute(localizedStrings, netbeans.getMainAttributes(), osgi.getMainAttributes(),
212
                    "OpenIDE-Module-Short-Description", "Bundle-Description");
213
            Map<String,File> bundledFiles = findBundledFiles(module, cnb);
214
            // XXX any use for OpenIDE-Module-Long-Description?
215
            String classPath = netbeans.getMainAttributes().getValue("Class-Path");
216
            if (classPath != null) {
217
                StringBuilder bundleCP = new StringBuilder();
218
                for (String entry : classPath.split("[, ]+")) {
219
                    String clusterPath = new URI(module.getParentFile().getName() + "/" + entry).normalize().toString();
220
                    if (bundledFiles.containsKey(clusterPath)) {
221
                        bundleCP.append("/OSGI-INF/files/").append(clusterPath).append(",");
222
                    } else {
223
                        log("Class-Path entry " + entry + " from " + module + " does not correspond to any apparent cluster file", Project.MSG_WARN);
224
                    }
225
                }
226
                osgi.getMainAttributes().putValue("Bundle-Classpath", bundleCP + ".");
227
            }
228
            // XXX modules/lib/*.dll/so => Bundle-NativeCode (but syntax is rather complex)
229
            OutputStream bundle = new FileOutputStream(bundleFile);
230
            try {
231
                ZipOutputStream zos = new JarOutputStream(bundle, osgi);
232
                Set<String> parents = new HashSet<String>();
233
                Enumeration<? extends ZipEntry> entries = jar.entries();
234
                while (entries.hasMoreElements()) {
235
                    ZipEntry entry = entries.nextElement();
236
                    String path = entry.getName();
237
                    if (path.endsWith("/") || path.equals("META-INF/MANIFEST.MF")) {
238
                        continue;
239
                    }
240
                    InputStream is = jar.getInputStream(entry);
241
                    try {
242
                        writeEntry(zos, path, is, parents);
243
                    } finally {
244
                        is.close();
245
                    }
246
                }
247
                for (Map.Entry<String,File> bundledFile : bundledFiles.entrySet()) {
248
                    InputStream is = new FileInputStream(bundledFile.getValue());
249
                    try {
250
                        // XXX need matching IFL impl in netigso
251
                        writeEntry(zos, "OSGI-INF/files/" + bundledFile.getKey(), is, parents);
252
                    } finally {
253
                        is.close();
254
                    }
255
                }
256
                zos.finish();
257
                zos.close();
258
            } finally {
259
                bundle.close();
260
            }
261
        } finally {
262
            jar.close();
263
        }
264
    }
265
266
    private static void writeEntry(ZipOutputStream zos, String path, InputStream data, Set<String> parents) throws IOException {
267
        int size = Math.max(data.available(), 100);
268
        ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
269
        byte[] buf = new byte[size];
270
        int read;
271
        while ((read = data.read(buf)) != -1) {
272
            baos.write(buf, 0, read);
273
        }
274
        writeEntry(zos, path, baos.toByteArray(), parents);
275
    }
276
    private static void writeEntry(ZipOutputStream zos, String path, byte[] data, Set<String> parents) throws IOException {
277
        assert path.length() > 0 && !path.endsWith("/") && !path.startsWith("/") && path.indexOf("//") == -1 : path;
278
        for (int i = 0; i < path.length(); i++) {
279
            if (path.charAt(i) == '/') {
280
                String parent = path.substring(0, i + 1);
281
                if (parents.add(parent)) {
282
                    ZipEntry ze = new ZipEntry(parent);
283
                    ze.setMethod(ZipEntry.STORED);
284
                    ze.setSize(0);
285
                    ze.setCrc(0);
286
                    zos.putNextEntry(ze);
287
                    zos.closeEntry();
288
                }
289
            }
290
        }
291
        ZipEntry ze = new ZipEntry(path);
292
        ze.setMethod(ZipEntry.STORED);
293
        ze.setSize(data.length);
294
        CRC32 crc = new CRC32();
295
        crc.update(data);
296
        ze.setCrc(crc.getValue());
297
        zos.putNextEntry(ze);
298
        zos.write(data, 0, data.length);
299
        zos.closeEntry();
300
    }
301
302
    // copied from NetigsoModuleFactory
303
    private static String threeDotsWithMajor(String version, String withMajor) {
304
        int indx = withMajor.indexOf('/');
305
        int major = 0;
306
        if (indx > 0) {
307
            major = Integer.parseInt(withMajor.substring(indx + 1));
308
        }
309
        String[] segments = (version + ".0.0.0").split("\\.");
310
        assert segments.length >= 3 && segments[0].length() > 0;
311
        return (Integer.parseInt(segments[0]) + major * 100) + "."  + segments[1] + "." + segments[2];
312
    }
313
314
    static void translateDependency(StringBuilder b, String dependency) throws IOException {
315
        Matcher m = Pattern.compile("([^/ >=]+)(?:/(\\d+)(?:-(\\d+))?)? *(?:(=|>) *(.+))?").matcher(dependency);
316
        if (!m.matches()) {
317
            throw new IOException("bad dep: " + dependency);
318
        }
319
        String depCnb = m.group(1);
320
        String depMajLo = m.group(2);
321
        String depMajHi = m.group(3);
322
        String comparison = m.group(4);
323
        String version = m.group(5);
324
        b.append(depCnb);
325
        if (!"=".equals(comparison)) {
326
            if (version == null) {
327
                version = "0";
328
            }
329
            String targetVersion = threeDotsWithMajor(version, depMajLo == null ? "" : "x/" + depMajLo);
330
            b.append(";bundle-version=\"[").append(targetVersion).append(",");
331
            b.append(100 * (Integer.parseInt(targetVersion.replaceFirst("[.].+", "")) / 100 + 1));
332
            b.append(")\"");
333
        }
334
    }
335
336
    private void handleDisplayAttribute(Properties props, Attributes netbeans, Attributes osgi, String netbeansHeader, String osgiHeader) throws IOException {
337
        String val = netbeans.getValue(netbeansHeader);
338
        if (val != null) {
339
            osgi.putValue(osgiHeader, val);
340
        } else if (props.containsKey(netbeansHeader)) {
341
            osgi.putValue(osgiHeader, "%" + netbeansHeader);
342
        }
343
    }
344
345
    private Map<String,File> findBundledFiles(File module, String cnb) throws Exception {
346
        Map<String,File> result = new HashMap<String,File>();
347
        if (module.getParentFile().getName().matches("modules|core|lib")) {
348
            File cluster = module.getParentFile().getParentFile();
349
            File updateTracking = new File(new File(cluster, "update_tracking"), cnb.replace('.', '-') + ".xml");
350
            if (updateTracking.isFile()) {
351
                Document doc = XMLUtil.parse(new InputSource(updateTracking.toURI().toString()), false, false, null, null);
352
                NodeList nl = doc.getElementsByTagName("file");
353
                for (int i = 0; i < nl.getLength(); i++) {
354
                    String path = ((Element) nl.item(i)).getAttribute("name");
355
                    if (path.matches("config/Modules/.+[.]xml")) {
356
                        continue;
357
                    }
358
                    File f = new File(cluster, path);
359
                    if (f.equals(module)) {
360
                        continue;
361
                    }
362
                    // XXX exclude lib/nbexec{,.dll,.exe}, core/*felix*.jar
363
                    if (f.isFile()) {
364
                        result.put(path, f);
365
                    } else {
366
                        log("did not find " + f + " specified in " + updateTracking, Project.MSG_WARN);
367
                    }
368
                }
369
            } else {
370
                log("did not find expected " + updateTracking, Project.MSG_WARN);
371
            }
372
        } else {
373
            log("JAR " + module + " not found in expected cluster layout", Project.MSG_WARN);
374
        }
375
        return result;
376
    }
377
378
}
(-)a/nbbuild/test/unit/src/org/netbeans/nbbuild/MakeOSGiTest.java (+64 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2010 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.nbbuild;
41
42
import org.netbeans.junit.NbTestCase;
43
44
public class MakeOSGiTest extends NbTestCase {
45
46
    public MakeOSGiTest(String n) {
47
        super(n);
48
    }
49
50
    public void testTranslateDependency() throws Exception {
51
        assertTranslateDependency("org.openide.util;bundle-version=\"[8.0.0,100)\"", "org.openide.util > 8.0");
52
        assertTranslateDependency("org.netbeans.modules.lexer;bundle-version=\"[201.4.0,300)\"", "org.netbeans.modules.lexer/2 > 1.4");
53
        assertTranslateDependency("what.ever;bundle-version=\"[0.0.0,100)\"", "what.ever");
54
        assertTranslateDependency("org.netbeans.modules.java.sourceui", "org.netbeans.modules.java.sourceui = 15");
55
        // XXX interpret depMajHi, e.g. /0-1 > 1.7 => [1.7.0,200)
56
        // XXX 3 or more items in sequence
57
    }
58
    private void assertTranslateDependency(String expected, String dependency) throws Exception {
59
        StringBuilder b = new StringBuilder();
60
        MakeOSGi.translateDependency(b, dependency);
61
        assertEquals(expected, b.toString());
62
    }
63
64
}

Return to bug 179473