KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > JarWithModuleAttributes


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.SortedMap JavaDoc;
28 import java.util.TreeMap JavaDoc;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.taskdefs.Jar;
34 import org.apache.tools.ant.taskdefs.Manifest;
35
36 /**
37  * Task just like <jar> but predefines various module attributes.
38  * Cf. projectized.xml#jar
39  * @author Jesse Glick
40  */

41 public class JarWithModuleAttributes extends Jar {
42     
43     public JarWithModuleAttributes() {}
44     
45     private static final Pattern JavaDoc COMMA_SPACE = Pattern.compile(", *");
46     private static final Pattern JavaDoc IMPL_DEP = Pattern.compile(" *([a-zA-Z0-9_.]+)(/[0-9]+)? *= *(.+) *");
47
48     public void setManifest(File JavaDoc manifestFile) throws BuildException {
49         Manifest added = new Manifest();
50         try {
51             String JavaDoc pubPkgs = getProject().getProperty("public.packages");
52             if (pubPkgs == null) {
53                 throw new BuildException("Must have defined 'public.packages'", getLocation());
54             }
55             added.addConfiguredAttribute(new Manifest.Attribute("OpenIDE-Module-Public-Packages", pubPkgs));
56             String JavaDoc friends = getProject().getProperty("friends");
57             if (friends != null) {
58                 added.addConfiguredAttribute(new Manifest.Attribute("OpenIDE-Module-Friends", friends));
59             }
60             // #52354: define Class-Path in the manifest automatically.
61
String JavaDoc javahelpClassPathExtension = getProject().getProperty("javahelp.class.path.extension");
62             String JavaDoc classPathExtensions = getProject().getProperty("class.path.extensions");
63             String JavaDoc cp;
64             if (javahelpClassPathExtension != null) {
65                 if (classPathExtensions != null) {
66                     cp = classPathExtensions + " " + javahelpClassPathExtension;
67                 } else {
68                     cp = javahelpClassPathExtension;
69                 }
70             } else {
71                 if (classPathExtensions != null) {
72                     cp = classPathExtensions;
73                 } else {
74                     cp = null;
75                 }
76             }
77             if (cp != null) {
78                 added.addConfiguredAttribute(new Manifest.Attribute("Class-Path", cp));
79             }
80             String JavaDoc moduleDeps = getProject().getProperty("module.dependencies");
81             if (moduleDeps != null) {
82                 added.addConfiguredAttribute(new Manifest.Attribute("OpenIDE-Module-Module-Dependencies", moduleDeps));
83             }
84             String JavaDoc javaDep = getProject().getProperty("javac.target");
85             if (javaDep != null && javaDep.matches("[0-9]+(\\.[0-9]+)*")) {
86                 added.addConfiguredAttribute(new Manifest.Attribute("OpenIDE-Module-Java-Dependencies", "Java > " + javaDep));
87             }
88             // Check to see if OpenIDE-Module-Implementation-Version is already defined.
89
String JavaDoc implVers;
90             String JavaDoc ownCnb;
91             Manifest staticManifest;
92             InputStream JavaDoc is = new FileInputStream JavaDoc(manifestFile);
93             try {
94                 staticManifest = new Manifest(new InputStreamReader JavaDoc(is, "UTF-8"));
95                 Manifest.Section mainSection = staticManifest.getMainSection();
96                 implVers = mainSection.getAttributeValue("OpenIDE-Module-Implementation-Version");
97                 String JavaDoc myself = mainSection.getAttributeValue("OpenIDE-Module");
98                 int slash = myself.indexOf('/');
99                 if (slash == -1) {
100                     ownCnb = myself;
101                 } else {
102                     ownCnb = myself.substring(0, slash);
103                 }
104                 String JavaDoc cnbs = getProject().getProperty("code.name.base.slashes");
105                 String JavaDoc cnbDots = (cnbs != null) ? cnbs.replace('/', '.') : null;
106                 if (!ownCnb.equals(cnbDots)) {
107                     // #58248: make sure these stay in synch.
108
throw new BuildException("Mismatch in module code name base: manifest says " + ownCnb + " but project.xml says " + cnbDots, getLocation());
109                 }
110             } finally {
111                 is.close();
112             }
113             String JavaDoc buildNumber = getProject().getProperty("buildnumber");
114             if (buildNumber == null) {
115                 throw new BuildException("Must have defined 'buildnumber'", getLocation());
116             }
117             String JavaDoc attrToAdd = implVers != null ? "OpenIDE-Module-Build-Version" : "OpenIDE-Module-Implementation-Version";
118             added.addConfiguredAttribute(new Manifest.Attribute(attrToAdd, buildNumber));
119             // If spec.version.base is defined, use it, after tacking on any numeric impl deps (sorted by CNB of the dep for stability),
120
// and also using the implementation version of this module as well if it is numeric.
121
// This trick makes sure that if you have an impl dep on some module which changes its (numeric) impl version,
122
// your spec version will also change correspondingly, so e.g. Auto Update will see a new version of your module too.
123
String JavaDoc specVersBase = getProject().getProperty("spec.version.base");
124             if (specVersBase != null) {
125                 boolean edited = false;
126                 if (implVers != null) {
127                     try {
128                         Integer JavaDoc implVersI = new Integer JavaDoc(implVers);
129                         specVersBase += "." + implVersI;
130                         edited = true;
131                     } catch (NumberFormatException JavaDoc e) {
132                         // OK, ignore it, not numeric.
133
getProject().log(manifestFile + ": warning: use of spec.version.base with non-integer OpenIDE-Module-Implementation-Version (see http://wiki.netbeans.org/wiki/view/DevFaqImplementationDependency)", Project.MSG_WARN);
134                     }
135                 }
136                 SortedMap JavaDoc<String JavaDoc,Integer JavaDoc> additions = new TreeMap JavaDoc<String JavaDoc, Integer JavaDoc>();
137                 if (moduleDeps != null) {
138                     String JavaDoc[] individualDeps = COMMA_SPACE.split(moduleDeps);
139                     for (int j = 0; j < individualDeps.length; j++) {
140                         Matcher JavaDoc m = IMPL_DEP.matcher(individualDeps[j]);
141                         if (m.matches()) {
142                             String JavaDoc cnb = m.group(1);
143                             String JavaDoc version = m.group(3);
144                             try {
145                                 if (version.length() > 1 && version.charAt(0) == '0') {
146                                     // Could be interpreted as an integer, but not here - e.g. "050123" is a date.
147
throw new NumberFormatException JavaDoc(version);
148                                 }
149                                 Integer JavaDoc versionI = new Integer JavaDoc(version);
150                                 additions.put(cnb, versionI);
151                             } catch (NumberFormatException JavaDoc e) {
152                                 // OK, ignore this one, not numeric.
153
getProject().log("Warning: in " + ownCnb + ", use of spec.version.base with non-integer OpenIDE-Module-Implementation-Version from " + cnb + " (see http://wiki.netbeans.org/wiki/view/DevFaqImplementationDependency)", Project.MSG_WARN);
154                             }
155                         }
156                     }
157                 }
158                 Iterator JavaDoc versions = additions.values().iterator();
159                 while (versions.hasNext()) {
160                     Integer JavaDoc version = (Integer JavaDoc) versions.next();
161                     specVersBase += "." + version;
162                     edited = true;
163                 }
164                 if (!edited) {
165                     getProject().log("Warning: in " + ownCnb + ", using spec.version.base for no reason; could just use OpenIDE-Module-Specification-Version statically in the manifest (see http://wiki.netbeans.org/wiki/view/DevFaqImplementationDependency)", Project.MSG_WARN);
166                 }
167                 if (staticManifest.getMainSection().getAttributeValue("OpenIDE-Module-Specification-Version") != null) {
168                     getProject().log("Warning: in " + ownCnb + ", attempting to use spec.version.base while some OpenIDE-Module-Specification-Version is statically defined in manifest.mf; this cannot work (see http://wiki.netbeans.org/wiki/view/DevFaqImplementationDependency)", Project.MSG_WARN);
169                 } else {
170                     added.addConfiguredAttribute(new Manifest.Attribute("OpenIDE-Module-Specification-Version", specVersBase));
171                 }
172             } else if (moduleDeps != null && moduleDeps.indexOf('=') != -1) {
173                 getProject().log("Warning: in " + ownCnb + ", not using spec.version.base, yet declaring implementation dependencies; may lead to problems with Auto Update (see http://wiki.netbeans.org/wiki/view/DevFaqImplementationDependency)", Project.MSG_WARN);
174             } else if (implVers != null) {
175                 try {
176                     new Integer JavaDoc(implVers);
177                 } catch (NumberFormatException JavaDoc e) {
178                     getProject().log(manifestFile + ": warning: use of non-integer OpenIDE-Module-Implementation-Version may be problematic for clients trying to use spec.version.base (see http://wiki.netbeans.org/wiki/view/DevFaqImplementationDependency)", Project.MSG_WARN);
179                 }
180             }
181             // Now ask the regular <jar> task to add all this stuff to the regular manifest.mf.
182
added.merge(staticManifest);
183             if (!"lib".equals (getProject().getProperty("module.jar.dir"))) {
184                 // modules in lib cannot request this token
185
String JavaDoc key = "OpenIDE-Module-Requires";
186                 String JavaDoc token = "org.openide.modules.ModuleFormat1";
187                 String JavaDoc requires = staticManifest.getMainSection().getAttributeValue(key);
188                 String JavaDoc newRequires;
189                 if (requires != null) {
190                     // #59671: have to modify it, not just use super.setManifest(manifestFile).
191
added.getMainSection().removeAttribute(key);
192                     newRequires = requires + ", " + token;
193                 } else {
194                     newRequires = token;
195                 }
196                 added.addConfiguredAttribute(new Manifest.Attribute(key, newRequires));
197             }
198             addConfiguredManifest(added);
199         } catch (Exception JavaDoc e) {
200             throw new BuildException(e, getLocation());
201         }
202     }
203     
204 }
205
Popular Tags