/* * 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.
*/
/** Generates JNLP files for signed versions of the module JAR files. * * @author Jaroslav Tulach, Jesse Glick
*/ publicclass MakeJNLP extends Task { /** the files to work on */ private ResourceCollection files; private SignJar signTask;
private String verifyExcludes; /** Comma separated list of allowed excluded names of files during verify * phase.
*/ publicvoid setVerifyExcludes(String s) { this.verifyExcludes = s;
}
private String permissions = ""; /** * XML fragment pasted into the security part of the .jnlp file. * Should default to "<all-permissions/>"
*/ publicvoid setPermissions(String s) {
permissions = s;
}
private FileSet indirectJars; /** * Other JARs which should be copied into the destination directory and referred to as resources, * even though they are not listed as Class-Path extensions of the module and would not normally * be in its effective classpath. The basedir of the fileset should be a cluster root; for each * such JAR, a file META-INF/clusterpath/$relpath will be inserted in the JAR, where $relpath is the * relative path within the cluster. This permits the JAR to be located at runtime in a flat classpath, * using ClassLoader.getResource.
*/ publicvoid addIndirectJars(FileSet fs) {
indirectJars = fs;
}
private FileSet indirectFiles; /** * Other non-JAR files which should be made available to InstalledFileLocator. * The basedir of the fileset should be a cluster root; each * such file will be packed into a ZIP entry META-INF/files/$relpath * where the JAR will be available at runtime in a flat classpath, * using ClassLoader.getResource.
*/ publicvoid addIndirectFiles(FileSet fs) {
indirectFiles = fs;
}
privateboolean signJars = true; /** * Whether the final jars should be signed or not. Defaults to true * (if not supplied).
*/ publicvoid setSignJars(boolean s) { this.signJars = s;
}
privateboolean processJarVersions = false; /** * Whether to add versions and sizes of jars into jnlp files. Defaults to false * (if not supplied). * @param b
*/ publicvoid setProcessJarVersions(boolean b) { this.processJarVersions = b;
}
private Map<String, String> jarVersions; /** * Explicit definition of jar file versions (for jars without versions specified * in manifest file) * @param jarVersions
*/ publicvoid setJarVersions(Map<String, String> jarVersions) { this.jarVersions = jarVersions;
}
File ut = new File(updateTracking, dashcnb + ".xml"); if (!ut.exists()) { thrownew BuildException("The file " + ut + " for module " + codebasename + " cannot be found");
}
if (verifyExcludes != null) {
StringTokenizer tok = new StringTokenizer(verifyExcludes, ", "); while(tok.hasMoreElements()) {
removeWithLocales(fileToOwningModule, tok.nextToken(), clusterRoot, localizedFiles);
}
}
if (verify) { if (!fileToOwningModule.isEmpty()) { thrownew BuildException( "Cannot build JNLP for module " + f + " as these files are in " + "module's NBM, but are not referenced from any path (see harness/README for properties you can define to fix):\n" + fileToOwningModule.keySet()
);
}
}
return localizedFiles;
}
privatestaticvoid removeWithLocales(Map<String,String> removeFrom, String removeWhat, File clusterRoot, Map<String,List<File>> recordLocales) { if (removeFrom.remove(removeWhat) != null && removeWhat.endsWith(".jar")) { int basedir = removeWhat.lastIndexOf('/');
String base = basedir == -1 ? "" : removeWhat.substring(0, basedir);
String name = removeWhat.substring(basedir + 1, removeWhat.length() - 4);
Pattern p = Pattern.compile(base + "/locale/" + name + "(|_[a-zA-Z0-9_]+)\\.jar");
Iterator<String> it = removeFrom.keySet().iterator(); while (it.hasNext()) {
String s = it.next();
Matcher m = p.matcher(s); if (m.matches()) {
String locale = m.group(1).substring(1);
List<File> l = recordLocales.get(locale); if (l == null) {
l = new ArrayList<>();
recordLocales.put(locale, l);
}
l.add(new File(clusterRoot, s.replace('/', File.separatorChar)));
it.remove();
}
}
}
}
privatevoid processIndirectFiles(Writer fileWriter, String dashcnb) throws IOException, BuildException { if (indirectFiles == null) { return;
}
DirectoryScanner scan = indirectFiles.getDirectoryScanner(getProject());
Map<String,File> entries = new LinkedHashMap<>(); for (String f : scan.getIncludedFiles()) {
entries.put(f.replace(File.separatorChar, '/'), new File(scan.getBasedir(), f));
} if (entries.isEmpty()) { return;
}
File ext = new File(new File(targetFile, dashcnb), "extra-files.jar");
Zip jartask = (Zip) getProject().createTask("jar");
jartask.setDestFile(ext); for (Map.Entry<String,File> entry : entries.entrySet()) {
ZipFileSet zfs = new ZipFileSet();
zfs.setFile(entry.getValue());
zfs.setFullpath("META-INF/files/" + entry.getKey());
jartask.addZipfileset(zfs);
}
jartask.execute();
fileWriter.write(constructJarHref(ext, dashcnb));
signOrCopy(ext, null);
}
private String relative(File file, File root) {
String sfile = file.toString().replace(File.separatorChar, '/');
String sroot = (root.toString() + File.separator).replace(File.separatorChar, '/'); if (sfile.startsWith(sroot)) { try {
String result = new URI(null, sfile.substring(sroot.length()), null).normalize().getPath(); return result;
} catch (URISyntaxException x) { thrownew BuildException(x, getLocation()); // or just ignore?
}
} return sfile;
}
/** return alias if signed, or null if not */ privatestatic String isSigned(File f) throws IOException { try (JarFile jar = new JarFile(f)) {
Enumeration<JarEntry> en = jar.entries(); while (en.hasMoreElements()) {
Matcher m = SF.matcher(en.nextElement().getName()); if (m.matches()) { return m.group(1);
}
} returnnull;
}
} privatestaticfinal Pattern SF = Pattern.compile("META-INF/(.+)\\.SF");
/** * returns version of jar file depending on manifest.mf or explicitly specified * @param jar * @return * @throws IOException
*/ private String getJarVersion(JarFile jar) throws IOException {
String version = jar.getManifest().getMainAttributes().getValue("OpenIDE-Module-Specification-Version"); if (version == null) {
version = jar.getManifest().getMainAttributes().getValue("Specification-Version");
} if (version == null && jarVersions != null) {
version = jarVersions.get(jar.getName());
} return version;
}
/** * Constructs jar or nativelib tag for jars * @param f * @param dashcnb * @return * @throws IOException
*/ private String constructJarHref(File f, String dashcnb) throws IOException { return constructJarHref(f, dashcnb, f.getName());
}
/** * Constructs jar or nativelib tag for jars using custom name * @param f * @param dashcnb * @return * @throws IOException
*/ private String constructJarHref(File f, String dashcnb, String name) throws IOException {
String tag = "jar"; if (nativeLibraries != null && nativeLibraries.contains(name)) {
tag = "nativelib";
} if (processJarVersions) { if (!f.exists()) { thrownew BuildException("JAR file " + f + " does not exist, cannot extract required versioning info.");
}
JarFile extJar = new JarFile(f);
String version = getJarVersion(extJar); if (version != null) { return" <" + tag + " href='" + dashcnb + '/' + name + "' version='" + version + "' size='" + f.length() + "'/>\n";
}
} return" <" + tag + " href='" + dashcnb + '/' + name + "'/>\n";
}
privatevoid generateVersionXMLFiles() throws IOException {
FileSet fs = new FileSet();
fs.setIncludes("**/*.jar"); for (File directory : jarDirectories) {
fs.setDir(directory);
DirectoryScanner scan = fs.getDirectoryScanner(getProject());
StringWriter writeVersionXML = new StringWriter();
writeVersionXML.append("1.0\" encoding=\"UTF-8\"?>\n");
writeVersionXML.append("\n"); for (String jarName : scan.getIncludedFiles()) {
File jar = new File(scan.getBasedir(), jarName);
JarFile jarFile = new JarFile(jar);
String version = getJarVersion(jarFile); if (version != null) {
writeVersionXML.append(" \n \n ");
writeVersionXML.append(jar.getName());
writeVersionXML.append("\n ");
writeVersionXML.append(version);
writeVersionXML.append("\n \n ");
writeVersionXML.append(jar.getName());
writeVersionXML.append("\n \n");
} else {
writeVersionXML.append(" \n");
}
}
writeVersionXML.append("\n");
writeVersionXML.close();
File versionXML = new File(directory, "version.xml");
FileWriter w = new FileWriter(versionXML);
w.write(writeVersionXML.toString());
w.close();
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.