Search in sources :

Example 21 with ModelledResource

use of org.apache.aries.application.modelling.ModelledResource in project aries by apache.

the class ModellerTest method testParsingByInputStreamProvider.

@Test
public void testParsingByInputStreamProvider() throws Exception {
    final URL pathToTestBundle = getClass().getClassLoader().getResource("test.bundle.jar");
    ModelledResource resource = sut.getModelledResource("file:///test.bundle.uri", new ModelledResourceManager.InputStreamProvider() {

        public InputStream open() throws IOException {
            return pathToTestBundle.openStream();
        }
    });
    checkTestBundleResource(resource);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ModelledResourceManager(org.apache.aries.application.modelling.ModelledResourceManager) URL(java.net.URL) ModelledResource(org.apache.aries.application.modelling.ModelledResource) ParserProxyTest(org.apache.aries.application.modelling.impl.ParserProxyTest) Test(org.junit.Test)

Example 22 with ModelledResource

use of org.apache.aries.application.modelling.ModelledResource in project aries by apache.

the class OBRResolverTest method createAndResolveSelfContainedApp.

private Collection<ModelledResource> createAndResolveSelfContainedApp(String extraImport) throws Exception {
    FileOutputStream fout = new FileOutputStream(new File("a.bundle.jar"));
    ArchiveFixture.newJar().manifest().attribute(BUNDLE_SYMBOLICNAME, "a.bundle").attribute(BUNDLE_VERSION, "1.0.0").attribute(BUNDLE_MANIFESTVERSION, "2").attribute(IMPORT_PACKAGE, "a.pack.age").end().writeOut(fout);
    fout.close();
    fout = new FileOutputStream(new File("b.bundle.jar"));
    ArchiveFixture.newJar().manifest().attribute(BUNDLE_SYMBOLICNAME, "b.bundle").attribute(BUNDLE_VERSION, "1.0.0").attribute(BUNDLE_MANIFESTVERSION, "2").attribute(IMPORT_PACKAGE, extraImport).attribute(EXPORT_PACKAGE, "a.pack.age").end().writeOut(fout);
    fout.close();
    ModelledResourceManager mrm = context().getService(ModelledResourceManager.class);
    ModelledResource aBundle = mrm.getModelledResource(FileSystem.getFSRoot(new File("a.bundle.jar")));
    ModelledResource bBundle = mrm.getModelledResource(FileSystem.getFSRoot(new File("b.bundle.jar")));
    AriesApplicationResolver resolver = context().getService(AriesApplicationResolver.class);
    return resolver.resolveInIsolation("test.app", "1.0.0", Arrays.asList(aBundle, bBundle), Arrays.<Content>asList(ContentFactory.parseContent("a.bundle", "1.0.0"), ContentFactory.parseContent("b.bundle", "1.0.0")));
}
Also used : FileOutputStream(java.io.FileOutputStream) AriesApplicationResolver(org.apache.aries.application.management.spi.resolve.AriesApplicationResolver) File(java.io.File) ModelledResourceManager(org.apache.aries.application.modelling.ModelledResourceManager) ModelledResource(org.apache.aries.application.modelling.ModelledResource)

Example 23 with ModelledResource

use of org.apache.aries.application.modelling.ModelledResource in project aries by apache.

the class ModelledResourceManagerImpl method model.

private ModelledResource model(String uri, BundleManifest bm, ParsedServiceElements pse) throws ModellerException {
    Attributes attributes = bm.getRawAttributes();
    ModelledResource mbi = null;
    try {
        mbi = _modellingManager.getModelledResource(uri, attributes, pse.getReferences(), pse.getServices());
    } catch (InvalidAttributeException iae) {
        ModellerException me = new ModellerException(iae);
        _logger.debug(LOG_EXIT, "getModelledResource", me);
        throw me;
    }
    _logger.debug(LOG_EXIT, "getModelledResource", mbi);
    return mbi;
}
Also used : InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) Attributes(java.util.jar.Attributes) ModellerException(org.apache.aries.application.modelling.ModellerException) ModelledResource(org.apache.aries.application.modelling.ModelledResource)

Example 24 with ModelledResource

use of org.apache.aries.application.modelling.ModelledResource in project aries by apache.

the class RepositoryGeneratorImpl method generateRepository.

public void generateRepository(String[] source, OutputStream fout) throws IOException {
    logger.debug(LOG_ENTRY, "generateRepository", new Object[] { source, fout });
    List<URI> jarFiles = new ArrayList<URI>();
    InputStream in = null;
    OutputStream out = null;
    File wstemp = null;
    Set<ModelledResource> mrs = new HashSet<ModelledResource>();
    if (source != null) {
        try {
            for (String urlString : source) {
                // for each entry, we need to find out whether it is in local file system. If yes, we would like to
                // scan the bundles recursively under that directory
                URI entry;
                try {
                    File f = new File(urlString);
                    if (f.exists()) {
                        entry = f.toURI();
                    } else {
                        entry = new URI(urlString);
                    }
                    if ("file".equals(entry.toURL().getProtocol())) {
                        jarFiles.addAll(FileUtils.getBundlesRecursive(entry));
                    } else {
                        jarFiles.add(entry);
                    }
                } catch (URISyntaxException use) {
                    throw new IOException(urlString + " is not a valide uri.");
                }
            }
            for (URI jarFileURI : jarFiles) {
                String uriString = jarFileURI.toString();
                File f = null;
                if ("file".equals(jarFileURI.toURL().getProtocol())) {
                    f = new File(jarFileURI);
                } else {
                    int lastIndexOfSlash = uriString.lastIndexOf("/");
                    String fileName = uriString.substring(lastIndexOfSlash + 1);
                    //we need to download this jar/war to wstemp and work on it
                    URLConnection jarConn = jarFileURI.toURL().openConnection();
                    in = jarConn.getInputStream();
                    if (wstemp == null) {
                        wstemp = new File(tempDir.getTemporaryDirectory(), "generateRepositoryXML_" + System.currentTimeMillis());
                        boolean created = wstemp.mkdirs();
                        if (created) {
                            logger.debug("The temp directory was created successfully.");
                        } else {
                            logger.debug("The temp directory was NOT created.");
                        }
                    }
                    //Let's open the stream to download the bundles from remote
                    f = new File(wstemp, fileName);
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                }
                IDirectory jarDir = FileSystem.getFSRoot(f);
                mrs.add(modelledResourceManager.getModelledResource(uriString, jarDir));
            }
            generateRepository("Resource Repository", mrs, fout);
        } catch (Exception e) {
            logger.debug(LOG_EXIT, "generateRepository");
            throw new IOException(e);
        } finally {
            IOUtils.close(in);
            IOUtils.close(out);
            if (wstemp != null) {
                IOUtils.deleteRecursive(wstemp);
            }
        }
    } else {
        logger.debug("The URL list is empty");
    }
    logger.debug(LOG_EXIT, "generateRepository");
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IDirectory(org.apache.aries.util.filesystem.IDirectory) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URLConnection(java.net.URLConnection) TransformerException(javax.xml.transform.TransformerException) URISyntaxException(java.net.URISyntaxException) ResolverException(org.apache.aries.application.management.ResolverException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ModelledResource(org.apache.aries.application.modelling.ModelledResource) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashSet(java.util.HashSet)

Example 25 with ModelledResource

use of org.apache.aries.application.modelling.ModelledResource in project aries by apache.

the class RepositoryGeneratorImpl method generateRepository.

public static void generateRepository(RepositoryAdmin repositoryAdmin, String repositoryName, Collection<? extends ModelledResource> byValueBundles, OutputStream os) throws ResolverException, IOException {
    logger.debug(LOG_ENTRY, "generateRepository", new Object[] { repositoryAdmin, repositoryName, byValueBundles, os });
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException pce) {
        throw new ResolverException(pce);
    }
    Element root = doc.createElement("repository");
    root.setAttribute("name", repositoryName);
    doc.appendChild(root);
    for (ModelledResource mr : byValueBundles) {
        BundleResource bundleResource = new BundleResource(mr, repositoryAdmin);
        if (bundleResourceTransformers.size() > 0) {
            for (BundleResourceTransformer brt : bundleResourceTransformers) {
                bundleResource = brt.transform(bundleResource);
            }
        }
        writeResource(bundleResource, mr.getLocation(), doc, root);
    }
    try {
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.transform(new DOMSource(doc), new StreamResult(os));
    } catch (TransformerException te) {
        logger.debug(LOG_EXIT, "generateRepository", te);
        throw new ResolverException(te);
    }
    logger.debug(LOG_EXIT, "generateRepository");
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) ResolverException(org.apache.aries.application.management.ResolverException) Transformer(javax.xml.transform.Transformer) BundleResourceTransformer(org.apache.aries.application.resolver.obr.ext.BundleResourceTransformer) StreamResult(javax.xml.transform.stream.StreamResult) BundleResourceTransformer(org.apache.aries.application.resolver.obr.ext.BundleResourceTransformer) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) BundleResource(org.apache.aries.application.resolver.obr.ext.BundleResource) TransformerException(javax.xml.transform.TransformerException) ModelledResource(org.apache.aries.application.modelling.ModelledResource)

Aggregations

ModelledResource (org.apache.aries.application.modelling.ModelledResource)41 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ZipOutputStream (java.util.zip.ZipOutputStream)11 ResolverException (org.apache.aries.application.management.ResolverException)11 AbstractIntegrationTest (org.apache.aries.itest.AbstractIntegrationTest)11 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)10 File (java.io.File)8 Content (org.apache.aries.application.Content)8 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)7 FileOutputStream (java.io.FileOutputStream)6 HashSet (java.util.HashSet)6 MethodCall (org.apache.aries.unittest.mocks.MethodCall)6 Manifest (java.util.jar.Manifest)5 AriesApplication (org.apache.aries.application.management.AriesApplication)5 ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)5 HashMap (java.util.HashMap)4 Attributes (java.util.jar.Attributes)4 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)4