1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.util.Iterator ; 27 import java.util.SortedMap ; 28 import java.util.TreeMap ; 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 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 41 public class JarWithModuleAttributes extends Jar { 42 43 public JarWithModuleAttributes() {} 44 45 private static final Pattern COMMA_SPACE = Pattern.compile(", *"); 46 private static final Pattern IMPL_DEP = Pattern.compile(" *([a-zA-Z0-9_.]+)(/[0-9]+)? *= *(.+) *"); 47 48 public void setManifest(File manifestFile) throws BuildException { 49 Manifest added = new Manifest(); 50 try { 51 String 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 friends = getProject().getProperty("friends"); 57 if (friends != null) { 58 added.addConfiguredAttribute(new Manifest.Attribute("OpenIDE-Module-Friends", friends)); 59 } 60 String javahelpClassPathExtension = getProject().getProperty("javahelp.class.path.extension"); 62 String classPathExtensions = getProject().getProperty("class.path.extensions"); 63 String 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 moduleDeps = getProject().getProperty("module.dependencies"); 81 if (moduleDeps != null) { 82 added.addConfiguredAttribute(new Manifest.Attribute("OpenIDE-Module-Module-Dependencies", moduleDeps)); 83 } 84 String 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 String implVers; 90 String ownCnb; 91 Manifest staticManifest; 92 InputStream is = new FileInputStream (manifestFile); 93 try { 94 staticManifest = new Manifest(new InputStreamReader (is, "UTF-8")); 95 Manifest.Section mainSection = staticManifest.getMainSection(); 96 implVers = mainSection.getAttributeValue("OpenIDE-Module-Implementation-Version"); 97 String 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 cnbs = getProject().getProperty("code.name.base.slashes"); 105 String cnbDots = (cnbs != null) ? cnbs.replace('/', '.') : null; 106 if (!ownCnb.equals(cnbDots)) { 107 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 buildNumber = getProject().getProperty("buildnumber"); 114 if (buildNumber == null) { 115 throw new BuildException("Must have defined 'buildnumber'", getLocation()); 116 } 117 String attrToAdd = implVers != null ? "OpenIDE-Module-Build-Version" : "OpenIDE-Module-Implementation-Version"; 118 added.addConfiguredAttribute(new Manifest.Attribute(attrToAdd, buildNumber)); 119 String specVersBase = getProject().getProperty("spec.version.base"); 124 if (specVersBase != null) { 125 boolean edited = false; 126 if (implVers != null) { 127 try { 128 Integer implVersI = new Integer (implVers); 129 specVersBase += "." + implVersI; 130 edited = true; 131 } catch (NumberFormatException e) { 132 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 <String ,Integer > additions = new TreeMap <String , Integer >(); 137 if (moduleDeps != null) { 138 String [] individualDeps = COMMA_SPACE.split(moduleDeps); 139 for (int j = 0; j < individualDeps.length; j++) { 140 Matcher m = IMPL_DEP.matcher(individualDeps[j]); 141 if (m.matches()) { 142 String cnb = m.group(1); 143 String version = m.group(3); 144 try { 145 if (version.length() > 1 && version.charAt(0) == '0') { 146 throw new NumberFormatException (version); 148 } 149 Integer versionI = new Integer (version); 150 additions.put(cnb, versionI); 151 } catch (NumberFormatException e) { 152 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 versions = additions.values().iterator(); 159 while (versions.hasNext()) { 160 Integer version = (Integer ) 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 (implVers); 177 } catch (NumberFormatException 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 added.merge(staticManifest); 183 if (!"lib".equals (getProject().getProperty("module.jar.dir"))) { 184 String key = "OpenIDE-Module-Requires"; 186 String token = "org.openide.modules.ModuleFormat1"; 187 String requires = staticManifest.getMainSection().getAttributeValue(key); 188 String newRequires; 189 if (requires != null) { 190 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 e) { 200 throw new BuildException(e, getLocation()); 201 } 202 } 203 204 } 205 | Popular Tags |