File: BuildTask.java

package info (click to toggle)
libjackson-json-java 1.5.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,392 kB
  • ctags: 7,846
  • sloc: java: 46,768; xml: 938; makefile: 21
file content (385 lines) | stat: -rw-r--r-- 13,805 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2006, 2007 luminis
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the Luminis nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
*/
package net.luminis.build.plugin.bnd;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Manifest;
import org.apache.tools.ant.taskdefs.Manifest.Attribute;
import org.apache.tools.ant.taskdefs.Manifest.Section;
import org.apache.tools.ant.types.Path;

import aQute.lib.osgi.Analyzer;
import aQute.lib.osgi.Builder;
import aQute.lib.osgi.Jar;

public class BuildTask extends Task {
    Properties m_instructions = new Properties();
    File m_target;
    File m_outputDir;
    Path m_classpath;
    String m_filename;

    @SuppressWarnings("unchecked")
    public void execute() {
        try {
            Properties properties = new Properties();

            if (m_outputDir == null) {
                m_outputDir = (m_target == null) ? getProject().getBaseDir() : m_target.getParentFile().getAbsoluteFile();
            }

            String version = m_instructions.getProperty(Analyzer.BUNDLE_VERSION);

            if (version != null) {
                Pattern P_VERSION = Pattern.compile("([0-9]+(\\.[0-9])*)-(.*)");

                Matcher m = P_VERSION.matcher(version);

                if (m.matches()) {
                    version = m.group(1) + "." + m.group(3);
                }

                m_instructions.put(Analyzer.BUNDLE_VERSION, version);
                version = "-" + version;
            }
            else {
                version = "";
            }
            
            // if target is set: do not change the target
            if (m_target == null) {
                // if a filename is specified: use it, otherwise ignore and generate a filename
                if (m_filename != null) {
                    m_target = new File(m_outputDir, m_filename + ".jar");
                } else {
                    m_target = new File(m_outputDir, m_instructions.getProperty(Analyzer.BUNDLE_SYMBOLICNAME, m_instructions.getProperty(Analyzer.BUNDLE_NAME).replace(" ", "").trim()) + version + ".jar");
                }
            }

            properties.put(Analyzer.IMPORT_PACKAGE, "*");

            if (!m_instructions.containsKey(Analyzer.PRIVATE_PACKAGE)) {
                properties.put(Analyzer.EXPORT_PACKAGE, m_instructions.getProperty(Analyzer.BUNDLE_SYMBOLICNAME) + ".*");
            }

            properties.putAll(m_instructions);

            if (m_classpath == null)
                throw new BuildException("Classpath not set");
            if (m_classpath.list().length == 0)
                throw new BuildException("Classpath is empty");
            
            List<Jar> classpath = new ArrayList<Jar>(m_classpath.list().length);
            String classpathString = null; 
            for (String cpElement: m_classpath.list()) {
                File f = new File(cpElement);
                if (f.exists()) {
                    classpath.add(new Jar(f.getName(), f));
                    if (classpathString == null)
                        classpathString = f.toString();
                    else
                        classpathString += ":" + f;
                }
                else {
                    log("classpath element '" + cpElement + "' does not exist; ignored.", Project.MSG_WARN);
                }
            }
            // Log classpath only when in verbose mode
            log("Classpath: " + classpathString, Project.MSG_VERBOSE);

            Builder builder = new Builder();
            builder.setBase(getProject().getBaseDir());
            builder.setProperties(properties);
            builder.setClasspath(classpath.toArray(new Jar[classpath.size()]));

            builder.build();
            Jar jar = builder.getJar();
            jar.setName(m_target.getName());

            List errors = builder.getErrors();
            List warnings = builder.getWarnings();

            if (errors.size() > 0) {
                m_target.delete();
                for (Iterator e = errors.iterator(); e.hasNext();) {
                    String msg = (String) e.next();
                    getProject().log(msg);
                }
                throw new BuildException("Found errors, see log");
            }
            else {
                boolean exists = m_target.exists();
                boolean hasChanged = m_target.lastModified() <= jar.lastModified();
                if (!exists || hasChanged) {
                    jar.write(m_target);
                    if (exists) {
                        getProject().log("modified " + jar.getName());
                    }
                    else {
                        getProject().log("created " + jar.getName());
                    }
                }
            }
            for (Iterator w = warnings.iterator(); w.hasNext();) {
                String msg = (String) w.next();
                getProject().log(msg);
            }

        }
        catch (BuildException ex) {
            throw ex;
        }
        catch (Exception e) {
            throw new BuildException("Unknown error occurred", e);
        }
    }

    public void setClasspathRefId(String value) {
        if (value == null) {
            throw new BuildException("ClasspathRefId value can not be null");
        }
        else if (getProject().getReference(value) == null) {
            throw new BuildException("ClasspathRefId value [" + value + "] not found in project");
        }
        m_classpath = ((Path) getProject().getReference(value));
    }

    /**
     * Adds a path to the classpath.
     * @return a class path to be configured
     */
    public Path createClasspath() {
        if (m_classpath == null) {
            m_classpath = new Path(getProject());
        }
        return m_classpath.createPath();
    }
    
    /**
     * Set the classpath to be used for this compilation.
     *
     * @param classpath an Ant Path object containing the compilation classpath.
     */
    public void setClasspath(Path classpath) {
        if (m_classpath == null) {
            m_classpath = classpath;
        } else {
            m_classpath.append(classpath);
        }
    }

    public void setTarget(File value) {
        m_target = value;
    }

    public void setOutputDir(File value) {
        m_outputDir = value;
    }

    public void setBundleClassPath(String value) {
        m_instructions.put(Analyzer.BUNDLE_CLASSPATH, value);
    }

    public void setBundleCopyright(String value) {
        m_instructions.put(Analyzer.BUNDLE_COPYRIGHT, value);
    }

    public void setBundleDescription(String value) {
        m_instructions.put(Analyzer.BUNDLE_DESCRIPTION, value);
    }

    public void setBundleName(String value) {
        m_instructions.put(Analyzer.BUNDLE_NAME, value);
    }

    public void setBundleNativeCode(String value) {
        m_instructions.put(Analyzer.BUNDLE_NATIVECODE, value);
    }

    public void setExportPackage(String value) {
        m_instructions.put(Analyzer.EXPORT_PACKAGE, value);
    }

    public void setExportService(String value) {
        m_instructions.put(Analyzer.EXPORT_SERVICE, value);
    }

    public void setImportPackage(String value) {
        m_instructions.put(Analyzer.IMPORT_PACKAGE, value);
    }

    public void setDynamicImportPackage(String value) {
        m_instructions.put(Analyzer.DYNAMICIMPORT_PACKAGE, value);
    }

    public void setImportService(String value) {
        m_instructions.put(Analyzer.IMPORT_SERVICE, value);
    }

    public void setBundleVendor(String value) {
        m_instructions.put(Analyzer.BUNDLE_VENDOR, value);
    }

    public void setBundleVersion(String value) {
        m_instructions.put(Analyzer.BUNDLE_VERSION, value);
    }

    public void setBundleDocURL(String value) {
        m_instructions.put(Analyzer.BUNDLE_DOCURL, value);
    }

    public void setBundleContactAddress(String value) {
        m_instructions.put(Analyzer.BUNDLE_CONTACTADDRESS, value);
    }

    public void setBundleActivator(String value) {
        m_instructions.put(Analyzer.BUNDLE_ACTIVATOR, value);
    }

    public void setBundleRequiredExecutionEnvironment(String value) {
        m_instructions.put(Analyzer.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, value);
    }

    public void setBundleSymbolicName(String value) {
        m_instructions.put(Analyzer.BUNDLE_SYMBOLICNAME, value);
    }

    public void setBundleLocalization(String value) {
        m_instructions.put(Analyzer.BUNDLE_LOCALIZATION, value);
    }

    public void setRequireBundle(String value) {
        m_instructions.put(Analyzer.REQUIRE_BUNDLE, value);
    }

    public void setFragmentHost(String value) {
        m_instructions.put(Analyzer.FRAGMENT_HOST, value);
    }

    /**
     * @deprecated	with no replacement: Bnd always uses Bundle-ManifestVersion 2
     */
    public void setBundleManifestVersion(String value) {
        log("attribute bundleManifestVersion is deprecated and ignored (bundle manifest version is always '2')");
    }

    public void setServiceComponent(String value) {
        m_instructions.put(Analyzer.SERVICE_COMPONENT, value);
    }

    public void setBundleLicense(String value) {
        m_instructions.put(Analyzer.BUNDLE_LICENSE, value);
    }

    public void setPrivatePackage(String value) {
        m_instructions.put(Analyzer.PRIVATE_PACKAGE, value);
    }

    public void setIgnorePackage(String value) {
        m_instructions.put(Analyzer.IGNORE_PACKAGE, value);
    }

    public void setIncludeResource(String value) {
        if (value == null) {
            throw new BuildException("Resource to include can not be null");
        }
        else if (value.length() == 0) {
            log("empty string passed to the include resource, ignored as BND uses this to include the currect directory", Project.MSG_VERBOSE);
            return;
        }
        else {
            m_instructions.put(Analyzer.INCLUDE_RESOURCE, value);
        }
    }

    /**
     * Set whether to include the "Include-Resource" header in the manifest. Default is true.
     * Note that this header is quite useless, and as it might confuse people, especially when
     * used with a local path that differs from the include path, it can be suppressed by this
     * task attribute.
     */
    public void setIncludeIncludeResourceHeader(boolean include) {
        if (! include)
            m_instructions.put(Analyzer.REMOVEHEADERS, Analyzer.INCLUDE_RESOURCE);
        // default is do include
    }
    
    public void setAdditionalManifest(File value) {
        m_instructions.put(Analyzer.INCLUDE, value.getAbsolutePath());
    }
    
    /**
     * Allows inline manifest declaration to provide extra manifest entries to bnd.
     * This simplifies the ant call as the user does not have create tempfiles in Ant. Only a
     * manifest ant element needs to be specified.
     * 
     * Note that only main attributes can be passed.
     *
     * @param newManifest an embedded manifest element
     * @throws IOException if an error occurs during the parsing of the manifest
     */
    @SuppressWarnings("unchecked")
    public void addConfiguredManifest(Manifest newManifest) throws IOException {
        if (newManifest == null) {
            throw new BuildException("manifest can not be null");
        }
        // Create temp file.
        File temp = File.createTempFile("bnd", ".tmp");
    
        // Delete temp file when program exits.
        temp.deleteOnExit();

        PrintWriter tempWriter = new PrintWriter(temp);
        Section main = newManifest.getMainSection();
        Enumeration e = main.getAttributeKeys();
        while (e.hasMoreElements()) {
            Attribute attr = main.getAttribute((String) e.nextElement());
            tempWriter.println(attr.getName() + "=" + attr.getValue());
        }
        tempWriter.close();
        setAdditionalManifest(temp);
    }
    
    public void setFilename(String filename) {
        m_filename = filename;
    }
}