private void copy(File workspaceDir, InputStream in, Pattern glob, boolean overwrite) throws Exception { Jar jar = new Jar("dot", in); try { for (Entry<String, Resource> e : jar.getResources().entrySet()) { String path = e.getKey(); bnd.trace("path %s", path); if (glob != null && !glob.matcher(path).matches()) continue; Resource r = e.getValue(); File dest = Processor.getFile(workspaceDir, path); if (overwrite || !dest.isFile() || dest.lastModified() < r.lastModified() || r.lastModified() <= 0) { bnd.trace("copy %s to %s", path, dest); File dp = dest.getParentFile(); if (!dp.exists() && !dp.mkdirs()) { throw new IOException("Could not create directory " + dp); } IO.copy(r.openInputStream(), dest); } } } finally { jar.close(); } }
/** @throws Exception */ File[] getBundleClasspathFiles() throws Exception { if (this.bundleClasspathExpansion != null) return bundleClasspathExpansion; File file = getFile(); Manifest m = getManifest(); String bundleClassPath; if (m == null || (bundleClassPath = m.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH)) == null) { this.bundleClasspathExpansion = new File[] {file}; } else { File bundleClasspathDirectory = IO.getFile(file.getParentFile(), "." + file.getName() + "-bcp"); Parameters header = new Parameters(bundleClassPath); this.bundleClasspathExpansion = new File[header.size()]; bundleClasspathDirectory.mkdir(); int n = 0; Jar jar = null; try { for (Map.Entry<String, Attrs> entry : header.entrySet()) { if (".".equals(entry.getKey())) { this.bundleClasspathExpansion[n] = file; } else { File member = new File(bundleClasspathDirectory, n + "-" + toName(entry.getKey())); if (!isCurrent(file, member)) { if (jar == null) { jar = new Jar(file); } Resource resource = jar.getResource(entry.getKey()); if (resource == null) { warning += "Invalid bcp entry: " + entry.getKey() + "\n"; } else { IO.copy(resource.openInputStream(), member); member.setLastModified(file.lastModified()); } } this.bundleClasspathExpansion[n] = member; } n++; } } finally { if (jar != null) jar.close(); } } return this.bundleClasspathExpansion; }
@SuppressWarnings("deprecation") private void tryJMXDeploy(String jmx, String bsn) { JMXBundleDeployer jmxBundleDeployer = null; int port = -1; try { port = Integer.parseInt(jmx); } catch (Exception e) { // not an integer } try { if (port > -1) { jmxBundleDeployer = new JMXBundleDeployer(port); } else { jmxBundleDeployer = new JMXBundleDeployer(); } } catch (Exception e) { // ignore if we can't create bundle deployer (no remote osgi.core // jmx avail) } if (jmxBundleDeployer != null) { for (String path : this.getRunpath()) { File file = new File(path); try { Jar jar = new Jar(file); try { if (bsn.equals(jar.getBsn())) { long bundleId = jmxBundleDeployer.deploy(bsn, file); trace("agent installed with bundleId=%s", bundleId); break; } } finally { jar.close(); } } catch (Exception e) { // } } } }
private Manifest _calculateManifest(URL url, Manifest manifest) { Analyzer analyzer = new Analyzer(); Jar jar = null; try { URLConnection urlConnection = url.openConnection(); String fileName = url.getFile(); if (urlConnection instanceof JarURLConnection) { JarURLConnection jarURLConnection = (JarURLConnection) urlConnection; URL jarFileURL = jarURLConnection.getJarFileURL(); fileName = jarFileURL.getFile(); } File file = new File(fileName); if (!file.exists() || !file.canRead()) { return manifest; } fileName = file.getName(); analyzer.setJar(new Jar(fileName, file)); jar = analyzer.getJar(); String bundleSymbolicName = fileName; Matcher matcher = _bundleSymbolicNamePattern.matcher(bundleSymbolicName); if (matcher.matches()) { bundleSymbolicName = matcher.group(1); } analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName); String exportPackage = _calculateExportPackage(jar); analyzer.setProperty(Analyzer.EXPORT_PACKAGE, exportPackage); analyzer.mergeManifest(manifest); String bundleVersion = analyzer.getProperty(Analyzer.BUNDLE_VERSION); if (bundleVersion != null) { bundleVersion = Builder.cleanupVersion(bundleVersion); analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion); } return analyzer.calcManifest(); } catch (Exception e) { _log.error(e, e); return manifest; } finally { if (jar != null) { jar.close(); } analyzer.close(); } }
protected void doBaselineJar(Jar jar, File output, aQute.bnd.build.Project bndProject) throws Exception { if (_reportLevelIsOff) { return; } ProjectBuilder projectBuilder = new ProjectBuilder(bndProject); projectBuilder.setClasspath(_classpathFiles.toArray(new File[_classpathFiles.size()])); projectBuilder.setPedantic(isPedantic()); projectBuilder.setProperties(_file); projectBuilder.setSourcepath(new File[] {_sourcePath}); Jar baselineJar = projectBuilder.getBaselineJar(); try { if (baselineJar == null) { bndProject.deploy(output); return; } Baseline baseline = new Baseline(this, _diffPluginImpl); Set<Info> infos = baseline.baseline(jar, baselineJar, null); if (infos.isEmpty()) { return; } BundleInfo bundleInfo = baseline.getBundleInfo(); Info[] infosArray = infos.toArray(new Info[infos.size()]); Arrays.sort( infosArray, new Comparator<Info>() { @Override public int compare(Info info1, Info info2) { return info1.packageName.compareTo(info2.packageName); } }); for (Info info : infosArray) { String warnings = "-"; Version newerVersion = info.newerVersion; Version suggestedVersion = info.suggestedVersion; if (suggestedVersion != null) { if (newerVersion.compareTo(suggestedVersion) > 0) { warnings = "EXCESSIVE VERSION INCREASE"; } else if (newerVersion.compareTo(suggestedVersion) < 0) { warnings = "VERSION INCREASE REQUIRED"; } } Diff packageDiff = info.packageDiff; Delta delta = packageDiff.getDelta(); if (delta == Delta.REMOVED) { warnings = "PACKAGE REMOVED"; } else if (delta == Delta.UNCHANGED) { boolean newVersionSuggested = false; if ((suggestedVersion.getMajor() != newerVersion.getMajor()) || (suggestedVersion.getMicro() != newerVersion.getMicro()) || (suggestedVersion.getMinor() != newerVersion.getMinor())) { warnings = "VERSION INCREASE SUGGESTED"; newVersionSuggested = true; } if (!newVersionSuggested && !info.mismatch) { continue; } } if (((_reportLevelIsStandard || _reportOnlyDirtyPackages) && warnings.equals("-")) || (_reportOnlyDirtyPackages && (delta == Delta.REMOVED))) { continue; } doInfo(bundleInfo, info, warnings); if (_reportLevelIsDiff && (delta != Delta.REMOVED)) { doPackageDiff(packageDiff); } } } finally { if (baselineJar != null) { baselineJar.close(); } if (_printWriter != null) { _printWriter.close(); } projectBuilder.close(); } }
@Override protected void doExecute() throws Exception { aQute.bnd.build.Project bndProject = getBndProject(); Builder builder = new Builder(bndProject); builder.setClasspath(_classpathFiles.toArray(new File[_classpathFiles.size()])); builder.setPedantic(isPedantic()); builder.setProperties(_file); builder.setSourcepath(new File[] {_sourcePath}); Jar[] jars = builder.builds(); // Report both task failures and bnd build failures boolean taskFailed = report(); boolean bndFailed = report(builder); // Fail this build if failure is not ok and either the task failed or // the bnd build failed if (taskFailed || bndFailed) { throw new BuildException( "bnd failed", new org.apache.tools.ant.Location(_file.getAbsolutePath())); } for (Jar jar : jars) { String bsn = jar.getName(); File outputFile = _outputPath; if (_outputPath.isDirectory()) { String path = builder.getProperty("-output"); if (path != null) { outputFile = getFile(_outputPath, path); } else { outputFile = getFile(_outputPath, bsn + ".jar"); } } if (!outputFile.exists() || (outputFile.lastModified() <= jar.lastModified())) { jar.write(outputFile); Map<String, Resource> resources = jar.getResources(); log(jar.getName() + " (" + outputFile.getName() + ") " + resources.size()); doBaselineJar(jar, outputFile, bndProject); } else { Map<String, Resource> resources = jar.getResources(); log( jar.getName() + " (" + outputFile.getName() + ") " + resources.size() + " (not modified)"); } report(); jar.close(); } builder.close(); }
@SuppressWarnings("cast") private void executeBackwardCompatible() throws BuildException { try { if (files == null) throw new BuildException("No files set"); if (eclipse) { File project = getProject().getBaseDir(); EclipseClasspath cp = new EclipseClasspath(this, project.getParentFile(), project); classpath.addAll(cp.getClasspath()); classpath.addAll(cp.getBootclasspath()); sourcepath.addAll(cp.getSourcepath()); // classpath.add(cp.getOutput()); if (report()) throw new BuildException("Errors during Eclipse Path inspection"); } if (output == null) output = getProject().getBaseDir(); for (Iterator<File> f = files.iterator(); f.hasNext(); ) { File file = f.next(); Builder builder = new Builder(); builder.setPedantic(isPedantic()); if (file.exists()) { // Do nice property calculations // merging includes etc. builder.setProperties(file); } // get them and merge them with the project // properties, if the inherit flag is specified if (inherit) { Properties projectProperties = new UTF8Properties(); @SuppressWarnings("unchecked") Hashtable<Object, Object> antProps = getProject().getProperties(); projectProperties.putAll(antProps); projectProperties.putAll(builder.getProperties()); builder.setProperties(projectProperties); } builder.setClasspath(toFiles(classpath, "classpath")); builder.setSourcepath(toFiles(sourcepath, "sourcepath")); Jar jars[] = builder.builds(); // Report both task failures and bnd build failures. boolean taskFailed = report(); boolean bndFailed = report(builder); // Fail this build if failure is not ok and either the task // failed or the bnd build failed. if (!failok && (taskFailed || bndFailed)) { throw new BuildException( "bnd failed", new org.apache.tools.ant.Location(file.getAbsolutePath())); } for (int i = 0; i < jars.length; i++) { Jar jar = jars[i]; String bsn = jar.getName(); File base = file.getParentFile(); File output = this.output; String path = builder.getProperty("-output"); if (output == null) { if (path == null) output = getFile(base, bsn + ".jar"); else { output = getFile(base, path); } } else if (output.isDirectory()) { if (path == null) output = getFile(this.output, bsn + ".jar"); else output = getFile(this.output, path); } else if (output.isFile()) { if (files.size() > 1) messages.GotFileNeedDir_(output.getAbsoluteFile()); } String msg = ""; if (!output.exists() || output.lastModified() <= jar.lastModified()) { jar.write(output); } else { msg = "(not modified)"; } trace("%s (%s) %s %s", jar.getName(), output.getName(), jar.getResources().size(), msg); report(); jar.close(); } builder.close(); } } catch (Exception e) { // if (exceptions) e.printStackTrace(); if (!failok) throw new BuildException("Failed to build jar file: ", e); } }