use of org.apache.felix.ipojo.manipulator.reporter.SystemReporter in project felix by apache.
the class IPOJOURLHandler method openConnection.
/**
* Opens a connection using the ipojo url handler.
* This methods parses the URL and manipulate the given bundle.
*
* @param url the url.
* @return the URL connection on the manipulated bundle
* @throws java.io.IOException occurs when the bundle cannot be either downloaded, or manipulated or
* installed correctly.
* @see org.osgi.service.url.AbstractURLStreamHandlerService#openConnection(java.net.URL)
*/
public URLConnection openConnection(URL url) throws IOException {
logger.log(LOG_DEBUG, format("Processing URL %s", url));
String full = removeScheme(url);
// Now full is like : URL!URL or URL
String[] urls = full.split("!");
URL bundleURL = null;
URL metadataURL = null;
if (urls.length == 1) {
// URL form
bundleURL = new URL(urls[0]);
} else if (urls.length == 2) {
// URL!URL form
bundleURL = new URL(urls[0]);
metadataURL = new URL(urls[1]);
} else {
throw new MalformedURLException("The iPOJO url is not formatted correctly, ipojo:bundle_url[!metadata_url] expected");
}
logger.log(LOG_DEBUG, format("Extracted URL %s", url));
// Dump the referenced bundle on disk
File original = File.createTempFile("original-", ".jar", m_temp);
dump(bundleURL.openStream(), original);
JarFile jf = new JarFile(original);
File metadata = null;
if (metadataURL != null) {
metadata = File.createTempFile("ipojo-", ".xml", m_temp);
dump(metadataURL, metadata);
} else {
// Check that the metadata are in the jar file
metadata = findMetadata(jf);
}
Reporter reporter = new SystemReporter();
File out = File.createTempFile("ipojo-", ".jar", m_temp);
ResourceStore store = new BundleAwareJarFileResourceStore(jf, out, m_context);
CompositeMetadataProvider composite = new CompositeMetadataProvider(reporter);
if (metadata != null) {
FileMetadataProvider provider = new FileMetadataProvider(metadata, reporter);
composite.addMetadataProvider(provider);
}
ClassLoader classloader = new BridgeClassLoader(original, m_context);
// Pojoization
Pojoization pojoizator = new Pojoization(createModuleProvider());
try {
pojoizator.pojoization(store, composite, createVisitor(store, reporter), classloader);
} catch (Exception e) {
if (!pojoizator.getErrors().isEmpty()) {
throw new IOException("Errors occurred during the manipulation : " + pojoizator.getErrors(), e);
}
e.printStackTrace();
throw new IOException("Cannot manipulate the Url: " + url, e);
}
if (!pojoizator.getErrors().isEmpty()) {
throw new IOException("Errors occurred during the manipulation : " + pojoizator.getErrors());
}
if (!pojoizator.getWarnings().isEmpty()) {
logger.log(LOG_WARNING, format("Warnings occurred during the manipulation %s", pojoizator.getWarnings()));
}
logger.log(LOG_DEBUG, format("Manipulation done %s", out.exists()));
// Cleanup
if (metadata != null) {
metadata.delete();
}
original.delete();
out.deleteOnExit();
// Returns the URL Connection
return out.toURI().toURL().openConnection();
}
Aggregations