use of aQute.bnd.osgi.Resource in project bnd by bndtools.
the class bnd method addAll.
private void addAll(Jar output, Jar sub, String prefix, List<String> bundleClassPath) {
if (prefix.length() > 0 && !prefix.endsWith("/"))
prefix += "/";
for (Map.Entry<String, Resource> e : sub.getResources().entrySet()) {
String path = e.getKey();
if (bundleClassPath != null && bundleClassPath.contains(path))
continue;
logger.debug("Add {}", path);
if (path.equals("META-INF/MANIFEST.MF"))
continue;
Resource r = e.getValue();
if (path.startsWith(prefix)) {
logger.debug("Add {}", path);
path = path.substring(prefix.length());
output.putResource(path, r);
} else
logger.debug("Ignore {} because it does not start with prefix {}", path, prefix);
}
}
use of aQute.bnd.osgi.Resource in project bnd by bndtools.
the class bnd method _view.
/**
* View files from JARs We parse the commandline and print each file on it.
*
* @throws Exception
*/
@Description("View a resource from a JAR file.")
public void _view(viewOptions options) throws Exception {
Charset charset = UTF_8;
if (options.charset() != null)
charset = Charset.forName(options.charset());
if (options._arguments().isEmpty()) {
error("Need a jarfile as source");
return;
}
List<String> args = options._arguments();
File file = getFile(args.remove(0));
if (!file.isFile()) {
error("File does not exist %s", file);
return;
}
try (Jar jar = new Jar(file)) {
if (args.isEmpty())
args.add("*");
Instructions instructions = new Instructions(args);
Collection<String> selected = instructions.select(jar.getResources().keySet(), true);
for (String selection : selected) {
Resource r = jar.getResource(selection);
if (selection.endsWith(".MF")) {
Manifest m = new Manifest(r.openInputStream());
printManifest(m);
} else if (selection.endsWith(".class")) {
ClassDumper clsd = new ClassDumper(selection, r.openInputStream());
clsd.dump(out);
} else {
InputStreamReader isr = new InputStreamReader(r.openInputStream(), charset);
IO.copy(isr, out);
}
}
}
}
use of aQute.bnd.osgi.Resource in project bnd by bndtools.
the class bnd method _export.
public void _export(ExportOptions opts) throws Exception {
Project project = getProject(opts.project());
if (project == null) {
error("No project");
return;
}
// temporary
project.getWorkspace().addBasicPlugin(new SubsystemExporter());
List<Exporter> exporters = project.getPlugins(Exporter.class);
Exporter exporter = null;
for (Exporter e : exporters) {
String[] types = e.getTypes();
for (String type : types) {
if (type.equals(opts.exporter()))
;
}
}
for (String bndrun : opts._arguments()) {
File f = getFile(bndrun);
if (!f.isFile()) {
error("No such file: %s", f);
continue;
}
Run run = new Run(project.getWorkspace(), getBase(), f);
run.getSettings(this);
Parameters exports = new Parameters();
List<String> types = opts.exporter();
if (types != null) {
for (String type : types) {
exports.putAll(new Parameters(type, this));
}
} else {
String exportTypes = run.getProperty(Constants.EXPORTTYPE);
exports.putAll(new Parameters(exportTypes, this));
}
for (Entry<String, Attrs> e : exports.entrySet()) {
logger.debug("exporting {} to {} with {}", run, e.getKey(), e.getValue());
Map.Entry<String, Resource> result = run.export(e.getKey(), e.getValue());
getInfo(run);
if (result != null && isOk()) {
String name = result.getKey();
File output = new File(project.getTarget(), opts.output() == null ? name : opts.output());
if (output.isDirectory())
output = new File(output, name);
IO.mkdirs(output.getParentFile());
logger.debug("Got a result for {}, store in {}", e.getKey(), output);
IO.copy(result.getValue().openInputStream(), output);
}
}
}
}
use of aQute.bnd.osgi.Resource in project bnd by bndtools.
the class bnd method _buildx.
@Description("Build project, is deprecated but here for backward compatibility")
public void _buildx(buildxOptions options) throws Exception {
// Create a build order
List<Builder> builders = new ArrayList<Builder>();
List<String> order = new ArrayList<String>();
List<String> active = new ArrayList<String>();
for (String s : options._arguments()) {
prebuild(active, order, builders, s);
}
for (Builder b : builders) {
if (options.classpath() != null) {
for (String f : options.classpath()) {
b.addClasspath(getFile(f));
}
}
if (options.sourcepath() != null) {
for (String f : options.sourcepath()) {
b.addSourcepath(getFile(f));
}
}
if (options.sources())
b.setSources(true);
if (options.eclipse()) {
EclipseClasspath ep = new EclipseClasspath(this, getBase().getParentFile(), getBase());
b.addClasspath(ep.getClasspath());
b.addClasspath(ep.getBootclasspath());
b.addSourcepath(ep.getSourcepath());
}
Jar jar = b.build();
File outputFile = b.getOutputFile(options.output());
if (options.pom()) {
Resource r = new PomFromManifest(jar.getManifest());
jar.putResource("pom.xml", r);
String path = outputFile.getName().replaceAll("\\.jar$", ".pom");
if (path.equals(outputFile.getName()))
path = outputFile.getName() + ".pom";
File pom = new File(outputFile.getParentFile(), path);
try (OutputStream os = IO.outputStream(pom)) {
r.write(os);
}
}
getInfo(b, b.getPropertiesFile().getName());
if (isOk()) {
b.save(outputFile, options.force());
}
b.close();
}
}
use of aQute.bnd.osgi.Resource in project bnd by bndtools.
the class BuilderTest method testRemoveClassFromPackage.
/**
* https://github.com/bndtools/bnd/issues/359 Starts with a bundle that has
* one package 'a' containing classes A and B. First removes B from 'a', and
* checks that the last modified date of the resulting bundle changed. Then
* removes A from 'a', and checks again that the last modified data changed.
*/
public static void testRemoveClassFromPackage() throws Exception {
try {
Builder b = new Builder();
try {
IO.getFile("bin/a1/a").mkdirs();
IO.copy(IO.getFile("bin/a/A.class"), IO.getFile("bin/a1/a/A.class"));
IO.copy(IO.getFile("bin/a/B.class"), IO.getFile("bin/a1/a/B.class"));
Jar classpath = new Jar(IO.getFile("bin/a1"));
b.addClasspath(classpath);
b.setPrivatePackage("a");
Jar result = b.build();
Resource ra = result.getResource("a/A.class");
Resource rb = result.getResource("a/B.class");
long lm1 = result.lastModified();
assertTrue("Last modified date of bundle > 0", lm1 > 0);
// windows has a very low resolution sometimes
Thread.sleep(isWindows() ? 1000 : 100);
IO.getFile("bin/a1/a/B.class").delete();
classpath.remove("a/B.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file B");
result = b.build();
long lm2 = result.lastModified();
assertTrue("Last modified date of bundle has increased after deleting class from package", lm2 > lm1);
// windows has a very low resolution sometimes
Thread.sleep(isWindows() ? 1000 : 100);
IO.getFile("bin/a1/a/A.class").delete();
classpath.remove("a/A.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file A");
// windows has a very low resolution sometimes
Thread.sleep(isWindows() ? 1000 : 100);
result = b.build();
long lm3 = result.lastModified();
assertTrue("Last modified date of bundle has increased after deleting last class from package", lm3 > lm2);
} finally {
b.close();
}
} finally {
try {
IO.getFile("bin/a1/a/A.class").delete();
} catch (Exception e) {
}
try {
IO.getFile("bin/a1/a/B.class").delete();
} catch (Exception e) {
}
try {
IO.getFile("bin/a1/a").delete();
} catch (Exception e) {
}
try {
IO.getFile("bin/a1").delete();
} catch (Exception e) {
}
}
}
Aggregations