use of aQute.bnd.osgi.URLResource in project bnd by bndtools.
the class RemoteProjectLauncherPlugin method executable.
/**
* Created a JAR that is a bundle and that contains its dependencies
*/
@Override
public Jar executable() throws Exception {
Collection<String> bsns = getProject().getBsns();
if (bsns.size() != 1)
throw new IllegalArgumentException("Can only handle a single bsn for a run configuration " + bsns);
String bsn = bsns.iterator().next();
Jar jar = new Jar(bsn);
String path = "aQute/remote/embedded/activator/EmbeddedActivator.class";
URLResource resource = new URLResource(getClass().getClassLoader().getResource(path));
jar.putResource("aQute/remote/embedded/activator/EmbeddedActivator.class", resource);
Collection<Container> rb = getProject().getRunbundles();
rb = Container.flatten(rb);
Attrs attrs = new Attrs();
for (Container c : rb) {
if (c.getError() != null) {
getProject().error("invalid runbundle %s", c);
} else {
File f = c.getFile();
String tbsn = c.getBundleSymbolicName();
String version = c.getVersion();
if (version == null || !Version.isVersion(version))
getProject().warning("The version of embedded bundle %s does not have a proper version", c);
jar.putResource("jar/" + c.getBundleSymbolicName() + ".jar", new FileResource(f));
attrs.put(tbsn, version);
}
}
Analyzer a = new Analyzer(getProject());
a.setJar(jar);
a.setBundleActivator(EmbeddedActivator.class.getName());
a.setProperty("Bnd-Embedded", attrs.toString().replace(';', ','));
Manifest manifest = a.calcManifest();
jar.setManifest(manifest);
getProject().getInfo(a);
return jar;
}
use of aQute.bnd.osgi.URLResource in project bnd by bndtools.
the class ProjectLauncherImpl method executable.
/**
* Create a standalone executable. All entries on the runpath are rolled out
* into the JAR and the runbundles are copied to a directory in the jar. The
* launcher will see that it starts in embedded mode and will automatically
* detect that it should load the bundles from inside. This is drive by the
* launcher.embedded flag.
*
* @throws Exception
*/
@Override
public Jar executable() throws Exception {
// TODO use constants in the future
Parameters packageHeader = OSGiHeader.parseHeader(getProject().getProperty("-package"));
boolean useShas = packageHeader.containsKey("jpm");
logger.debug("useShas {} {}", useShas, packageHeader);
Jar jar = new Jar(getProject().getName());
Builder b = new Builder();
getProject().addClose(b);
if (!getProject().getIncludeResource().isEmpty()) {
b.setIncludeResource(getProject().getIncludeResource().toString());
b.setProperty(Constants.RESOURCEONLY, "true");
b.build();
if (b.isOk()) {
jar.addAll(b.getJar());
}
getProject().getInfo(b);
}
List<String> runpath = getRunpath();
Set<String> runpathShas = new LinkedHashSet<String>();
Set<String> runbundleShas = new LinkedHashSet<String>();
List<String> classpath = new ArrayList<String>();
for (String path : runpath) {
logger.debug("embedding runpath {}", path);
File file = new File(path);
if (file.isFile()) {
if (useShas) {
String sha = SHA1.digest(file).asHex();
runpathShas.add(sha + ";name=\"" + file.getName() + "\"");
} else {
String newPath = nonCollidingPath(file, jar);
jar.putResource(newPath, new FileResource(file));
classpath.add(newPath);
}
}
}
// Copy the bundles to the JAR
List<String> runbundles = (List<String>) getRunBundles();
List<String> actualPaths = new ArrayList<String>();
for (String path : runbundles) {
logger.debug("embedding run bundles {}", path);
File file = new File(path);
if (!file.isFile())
getProject().error("Invalid entry in -runbundles %s", file);
else {
if (useShas) {
String sha = SHA1.digest(file).asHex();
runbundleShas.add(sha + ";name=\"" + file.getName() + "\"");
actualPaths.add("${JPMREPO}/" + sha);
} else {
String newPath = nonCollidingPath(file, jar);
jar.putResource(newPath, new FileResource(file));
actualPaths.add(newPath);
}
}
}
LauncherConstants lc = getConstants(actualPaths, true);
lc.embedded = !useShas;
// cannot use local info
lc.storageDir = null;
final Properties p = lc.getProperties(new UTF8Properties());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
p.store(bout, "");
jar.putResource(LauncherConstants.DEFAULT_LAUNCHER_PROPERTIES, new EmbeddedResource(bout.toByteArray(), 0L));
Manifest m = new Manifest();
Attributes main = m.getMainAttributes();
for (Entry<Object, Object> e : getProject().getFlattenedProperties().entrySet()) {
String key = (String) e.getKey();
if (key.length() > 0 && Character.isUpperCase(key.charAt(0)))
main.putValue(key, (String) e.getValue());
}
Instructions instructions = new Instructions(getProject().getProperty(Constants.REMOVEHEADERS));
Collection<Object> result = instructions.select(main.keySet(), false);
main.keySet().removeAll(result);
if (useShas) {
logger.debug("Use JPM launcher");
m.getMainAttributes().putValue("Main-Class", JPM_LAUNCHER_FQN);
m.getMainAttributes().putValue("JPM-Classpath", Processor.join(runpathShas));
m.getMainAttributes().putValue("JPM-Runbundles", Processor.join(runbundleShas));
URLResource jpmLauncher = new URLResource(this.getClass().getResource("/" + JPM_LAUNCHER));
jar.putResource(JPM_LAUNCHER, jpmLauncher);
doStart(jar, JPM_LAUNCHER_FQN);
} else {
logger.debug("Use Embedded launcher");
m.getMainAttributes().putValue("Main-Class", EMBEDDED_LAUNCHER_FQN);
m.getMainAttributes().putValue(EmbeddedLauncher.EMBEDDED_RUNPATH, Processor.join(classpath));
URLResource embeddedLauncher = new URLResource(this.getClass().getResource("/" + EMBEDDED_LAUNCHER));
jar.putResource(EMBEDDED_LAUNCHER, embeddedLauncher);
doStart(jar, EMBEDDED_LAUNCHER_FQN);
}
if (getProject().getProperty(Constants.DIGESTS) != null)
jar.setDigestAlgorithms(getProject().getProperty(Constants.DIGESTS).trim().split("\\s*,\\s*"));
else
jar.setDigestAlgorithms(new String[] { "SHA-1", "MD-5" });
jar.setManifest(m);
cleanup();
return jar;
}
use of aQute.bnd.osgi.URLResource in project bnd by bndtools.
the class MakeCopy method make.
public Resource make(Builder builder, String destination, Map<String, String> argumentsOnMake) throws Exception {
String type = argumentsOnMake.get("type");
if (!type.equals("copy"))
return null;
String from = argumentsOnMake.get("from");
if (from == null) {
String content = argumentsOnMake.get("content");
if (content == null)
throw new IllegalArgumentException("No 'from' or 'content' field in copy " + argumentsOnMake);
return new EmbeddedResource(content.getBytes(UTF_8), 0);
}
File f = builder.getFile(from);
if (f.isFile())
return new FileResource(f);
try {
URL url = new URL(from);
return new URLResource(url);
} catch (MalformedURLException mfue) {
// We ignore this
}
throw new IllegalArgumentException("Copy source does not exist " + from + " for destination " + destination);
}
use of aQute.bnd.osgi.URLResource in project felix by apache.
the class BndJarResourceStoreTestCase method testAnalysisWithExcludedEmbedComponents.
public void testAnalysisWithExcludedEmbedComponents() throws Exception {
PojoizationPlugin plugin = new PojoizationPlugin();
Map<String, String> props = new HashMap<String, String>();
Resource resource = new URLResource(getClass().getResource("/EMBED-MANIFEST.MF"));
doReturn(dot).when(analyzer).getJar();
doReturn(resource).when(embed).getResource(eq("META-INF/MANIFEST.MF"));
analyzer.setClasspath(new Jar[] { embed });
plugin.setReporter(reporter);
plugin.setProperties(props);
plugin.analyzeJar(analyzer);
assertNull(analyzer.getProperty("IPOJO-Components"));
}
use of aQute.bnd.osgi.URLResource in project felix by apache.
the class BndJarResourceStoreTestCase method testAnalysisWithLocallyDefinedComponentAndEmbedResource.
public void testAnalysisWithLocallyDefinedComponentAndEmbedResource() throws Exception {
PojoizationPlugin plugin = new PojoizationPlugin();
Map<String, String> props = new HashMap<String, String>();
props.put("include-embed-bundles", "true");
String path = EmptyComponent.class.getName().replace('.', '/').concat(".class");
Resource resource2 = new URLResource(getClass().getResource("/metadata-test-component.xml"));
doReturn(dot).when(analyzer).getJar();
doReturn(resource2).when(dot).getResource(eq("META-INF/metadata.xml"));
Collection<Clazz> classes = new ArrayList<Clazz>();
Resource typeResource = new URLResource(getClass().getResource("EmptyComponent.class"));
Clazz clazz = new Clazz(analyzer, path, typeResource);
clazz.parseClassFile();
classes.add(clazz);
doReturn(classes).when(analyzer).getClasses(Matchers.<String[]>anyVararg());
Resource resource = new URLResource(getClass().getResource("/EMBED-MANIFEST-EMPTY.MF"));
doReturn(resource).when(embed).getResource(eq("META-INF/MANIFEST.MF"));
doReturn(typeResource).when(embed).getResource(path);
doReturn("aaa").when(embed).getBsn();
analyzer.setClasspath(new Jar[] { embed });
plugin.setReporter(reporter);
plugin.setProperties(props);
plugin.analyzeJar(analyzer);
assertContains("component { $classname=\"org.apache.felix.ipojo.bnd.EmptyComponent\" manipulation { $classname=\"org.apache.felix.ipojo.bnd.EmptyComponent\" method { $name=\"$init\" }}}", analyzer.getProperty("IPOJO-Components"));
verify(dot).putResource(eq(path), any(Resource.class));
}
Aggregations