summaryrefslogtreecommitdiffstats
path: root/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleAggregatorMojo.java
blob: 9c36ae34a2957bf3f53b82ed18b183279d2360bb (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.maven.bundle.plugin;

import static org.apache.tuscany.maven.bundle.plugin.AggregatedBundleActivator.BUNDLE_ACTIVATOR_LIST;
import static org.apache.tuscany.maven.bundle.plugin.HeaderParser.merge;
import static org.osgi.framework.Constants.BUNDLE_ACTIVATOR;
import static org.osgi.framework.Constants.BUNDLE_CLASSPATH;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.tuscany.maven.bundle.plugin.HeaderParser.HeaderClause;

/**
 * @version $Rev$ $Date$
 * @goal aggregate-modules
 * @phase process-resources
 * @requiresDependencyResolution test
 * @description Generate an aggregated bundle that contains all the modules and 3rd party jars
 */
public class BundleAggregatorMojo extends AbstractMojo {
    /**
     * Root directory.
     *
     *  @parameter expression="${project.build.directory}/modules"
     */
    private File rootDirectory;

    /**
     * Aggregated bundle
     *
     *  @parameter expression="${project.build.directory}/singlebundle/tuscany-bundle.jar"
     */
    private File targetBundleFile;

    /**
     * @parameter default-value== "org.apache.tuscany.sca.bundle";
     */
    private String bundleName = "org.apache.tuscany.sca.bundle";

    /**
     * @parameter default-value== "2.0.0";
     */
    private String bundleVersion = "2.0.0";

    // private static final Logger logger = Logger.getLogger(BundleAggregatorMojo.class.getName());

    public static void aggregateBundles(Log log,
                                        File root,
                                        File[] files,
                                        File targetBundleFile,
                                        String bundleName,
                                        String bundleVersion) throws Exception {
        targetBundleFile.getParentFile().mkdirs();
        Set<File> jarFiles = new HashSet<File>();
        List<Manifest> manifests = new ArrayList<Manifest>();
        for (File child : files) {
            try {
                Manifest manifest = null;
                if (child.isDirectory()) {
                    File mf = new File(child, "META-INF/MANIFEST.MF");
                    if (mf.isFile()) {
                        FileInputStream is = new FileInputStream(mf);
                        manifest = new Manifest(is);
                        is.close();
                        if (manifest != null) {
                            String classpath = manifest.getMainAttributes().getValue("Bundle-ClassPath");
                            if (classpath != null) {
                                for (HeaderClause clause : HeaderParser.parse(classpath)) {
                                    if (clause.getValue().equals(".")) {
                                        continue;
                                    } else {
                                        jarFiles.add(new File(child, clause.getValue()));
                                    }
                                }
                            } else {
                                // 
                            }
                        }
                    }
                } else if (child.getName().endsWith(".jar")) {
                    JarFile jar = new JarFile(child);
                    manifest = jar.getManifest();
                    jar.close();
                    if (manifest != null) {
                        String id = manifest.getMainAttributes().getValue("Bundle-SymbolicName");
                        if (id != null && (id.startsWith("org.eclipse.") || id
                            .startsWith("org.apache.tuscany.sca.gateway"))) {
                            manifest = null;
                        } else {
                            jarFiles.add(child);
                        }
                    }
                }
                if (manifest == null) {
                    continue;
                }

                log.debug("Bundle file: " + child);
                manifests.add(manifest);
            } catch (Exception e) {
                throw e;
            }
        }
        Manifest merged = new Manifest();
        Attributes attributes = merged.getMainAttributes();
        attributes.putValue("Manifest-Version", "1.0");
        attributes.putValue("Bundle-ManifestVersion", "2");
        attributes.putValue("Bundle-License", "http://www.apache.org/licenses/LICENSE-2.0.txt");
        attributes.putValue("Bundle-DocURL", "http://www.apache.org/");
        attributes.putValue("Bundle-RequiredExecutionEnvironment", "J2SE-1.5,JavaSE-1.6");
        attributes.putValue("Bundle-Vendor", "The Apache Software Foundation");
        attributes.putValue("Bundle-Version", bundleVersion);
        attributes.putValue("Bundle-SymbolicName", bundleName);
        attributes.putValue("SCA-Version", "1.1");
        attributes.putValue("Bundle-Name", bundleName);
        // attributes.putValue("Bundle-ActivationPolicy", "lazy");
        for (Manifest mf : manifests) {
            for (Map.Entry<Object, Object> e : mf.getMainAttributes().entrySet()) {
                Attributes.Name key = (Attributes.Name)e.getKey();
                String name = key.toString();
                String oldValue = attributes.getValue(name);
                String value = (String)e.getValue();
                if (name.equals("Export-Package") || name.equals("Import-Package")
                    || name.equals("Require-Bundle")
                    || name.equals("DynamicImport-Package")
                    || name.equals("Bundle-ClassPath")
                    || name.equals("Private-Package")
                    || name.equals("Bundle-Description")) {
                    attributes.putValue(name, merge(oldValue, value));
                } else if (name.equals(BUNDLE_ACTIVATOR)) {
                    oldValue = attributes.getValue(BUNDLE_ACTIVATOR_LIST);
                    attributes.putValue(BUNDLE_ACTIVATOR_LIST, merge(oldValue, value));
                } else if (name.equals("Main-Class") || name.startsWith("Eclipse-") || name.startsWith("Bundle-")) {
                    // Ignore
                } else {
                    // Ignore
                    // attributes.putValue(name, value);
                }
            }
        }
        log.info("Generating " + targetBundleFile);
        attributes.putValue(BUNDLE_ACTIVATOR, AggregatedBundleActivator.class.getName());
        String bundleClassPath = attributes.getValue(BUNDLE_CLASSPATH);
        bundleClassPath = merge(bundleClassPath, ".");
        for (File f : jarFiles) {
            bundleClassPath = merge(bundleClassPath, f.getName());
        }
        attributes.putValue(BUNDLE_CLASSPATH, bundleClassPath);

        FileOutputStream fos = new FileOutputStream(targetBundleFile);
        JarOutputStream bundle = new JarOutputStream(fos, merged);

        for (File file : jarFiles) {
            log.info("Adding " + file);
            addEntry(bundle, file.getName(), file);
        }

        String classFile = AggregatedBundleActivator.class.getName().replace(".", "/") + ".class";
        InputStream classStream = BundleAggregatorMojo.class.getClassLoader().getResourceAsStream(classFile);
        addEntry(bundle, classFile, classStream);
        bundle.close();
    }

    private static void addDir(JarOutputStream jos, File root, File dir) throws IOException, FileNotFoundException {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                addDir(jos, root, file);
            } else if (file.isFile()) {
                // getLog().info(file.toString());
                String uri = root.toURI().relativize(file.toURI()).toString();
                if ("META-INF/MANIFEST.MF".equals(uri)) {
                    continue;
                }
                addEntry(jos, uri, file);
            }
        }
    }

    private static void addEntry(JarOutputStream jos, String name, File file) throws IOException {
        FileInputStream in = new FileInputStream(file);
        addEntry(jos, name, in);
    }

    private static final byte[] buf = new byte[4096];

    private static void addEntry(JarOutputStream jos, String name, InputStream in) throws IOException {
        ZipEntry entry = new ZipEntry(name);
        jos.putNextEntry(entry);
        for (;;) {
            int len = in.read(buf);
            if (len > 0) {
                jos.write(buf, 0, len);
            } else {
                break;
            }
        }
        in.close();
        jos.closeEntry();
    }

    private static void generateJar(File root, File jar, Manifest mf) throws IOException {
        FileOutputStream fos = new FileOutputStream(jar);
        JarOutputStream jos = mf != null ? new JarOutputStream(fos, mf) : new JarOutputStream(fos);
        addDir(jos, root, root);
        jos.close();
    }

    public void execute() throws MojoExecutionException, MojoFailureException {
        try {
            Log log = getLog();
            if (!rootDirectory.isDirectory()) {
                log.warn(rootDirectory + " is not a directory");
                return;
            }
            File[] files = rootDirectory.listFiles();
            aggregateBundles(log, rootDirectory, files, targetBundleFile, bundleName, bundleVersion);
        } catch (Exception e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }

    }
}