Search in sources :

Example 1 with DataModelHelper

use of org.apache.felix.bundlerepository.DataModelHelper in project aries by apache.

the class OBRAriesResolver method getConfiguredObrResolver.

private Resolver getConfiguredObrResolver(String appName, String appVersion, Collection<ModelledResource> byValueBundles, boolean noExtraRepositories) throws ResolverException {
    log.debug(LOG_ENTRY, "getConfiguredObrResolver", new Object[] { appName, appVersion, byValueBundles });
    DataModelHelper helper = repositoryAdmin.getHelper();
    Repository appRepo;
    try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        RepositoryGeneratorImpl.generateRepository(repositoryAdmin, appName + "_" + appVersion, byValueBundles, bytesOut);
        appRepo = helper.readRepository(new InputStreamReader(new ByteArrayInputStream(bytesOut.toByteArray())));
    } catch (Exception e) {
        throw new ResolverException(e);
    }
    List<Repository> resolveRepos = new ArrayList<Repository>();
    // add system repository
    resolveRepos.add(repositoryAdmin.getSystemRepository());
    // add application repository
    resolveRepos.add(appRepo);
    if (!!!noExtraRepositories) {
        // add local repository if configured
        if (!(excludeLocalRuntime())) {
            resolveRepos.add(getLocalRepository(repositoryAdmin));
        }
        // Need to refresh the repositories added to repository admin 
        // add user-defined repositories
        Repository[] repos = repositoryAdmin.listRepositories();
        for (Repository r : repos) {
            resolveRepos.add(r);
        }
    }
    Resolver obrResolver = repositoryAdmin.resolver(resolveRepos.toArray(new Repository[resolveRepos.size()]));
    addPlatformRepositories(obrResolver, appName, platformRepository);
    log.debug(LOG_EXIT, "getConfiguredObrResolver", obrResolver);
    return obrResolver;
}
Also used : PlatformRepository(org.apache.aries.application.management.spi.repository.PlatformRepository) Repository(org.apache.felix.bundlerepository.Repository) ResolverException(org.apache.aries.application.management.ResolverException) InputStreamReader(java.io.InputStreamReader) Resolver(org.apache.felix.bundlerepository.Resolver) AriesApplicationResolver(org.apache.aries.application.management.spi.resolve.AriesApplicationResolver) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) DataModelHelper(org.apache.felix.bundlerepository.DataModelHelper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResolverException(org.apache.aries.application.management.ResolverException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException)

Example 2 with DataModelHelper

use of org.apache.felix.bundlerepository.DataModelHelper in project aries by apache.

the class OBRCapability method getProperties.

public Property[] getProperties() {
    logger.debug(LOG_ENTRY, "getProperties");
    DataModelHelper helper = repositoryAdmin.getHelper();
    List<Property> properties = new ArrayList<Property>();
    // we then get OBR to parse. This is really convoluted and nasty.
    for (final Map.Entry<String, Object> entry : _props.entrySet()) {
        String propXML = helper.writeProperty(new Property() {

            @Override
            public String getValue() {
                Object value = entry.getValue();
                if (value instanceof String[]) {
                    String newValue = Arrays.toString((String[]) value);
                    value = newValue.substring(1, newValue.length() - 1);
                } else if (value instanceof Collection) {
                    //We can't rely on Collections having a sensible toString() as it isn't
                    //part of the API (although all base Java ones do). We can use an array
                    //to get consistency
                    String newValue = Arrays.toString(((Collection<?>) value).toArray());
                    value = newValue.substring(1, newValue.length() - 1);
                }
                return String.valueOf(value);
            }

            @Override
            public String getType() {
                String name = entry.getKey();
                String type = null;
                if (Constants.VERSION_ATTRIBUTE.equals(name) || (Constants.BUNDLE_VERSION_ATTRIBUTE.equals(name))) {
                    type = "version";
                } else if (Constants.OBJECTCLASS.equals(name) || (Constants.MANDATORY_DIRECTIVE + ":").equals(name) || entry.getValue() instanceof String[] || entry.getValue() instanceof Collection)
                    type = "set";
                return type;
            }

            @Override
            public String getName() {
                return entry.getKey();
            }

            @Override
            public Object getConvertedValue() {
                return null;
            }
        });
        try {
            properties.add(helper.readProperty(propXML));
        } catch (Exception e) {
        // Do nothing and hope it OBR doesn't generate XML it can't parse.
        }
    }
    logger.debug(LOG_EXIT, "getProperties", properties);
    return properties.toArray(new Property[properties.size()]);
}
Also used : ArrayList(java.util.ArrayList) DataModelHelper(org.apache.felix.bundlerepository.DataModelHelper) Collection(java.util.Collection) Property(org.apache.felix.bundlerepository.Property) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DataModelHelper

use of org.apache.felix.bundlerepository.DataModelHelper in project aries by apache.

the class OBRAriesResolver method addPlatformRepositories.

/* A 'platform repository' describes capabilities of the target runtime environment
   * These should be added to the resolver without being listed as coming from a particular 
   * repository or bundle.  
   */
private void addPlatformRepositories(Resolver obrResolver, String appName, PlatformRepository platformRepository) {
    log.debug(LOG_ENTRY, "addPlatformRepositories", new Object[] { obrResolver, appName });
    DataModelHelper helper = repositoryAdmin.getHelper();
    if (platformRepository != null) {
        Collection<URI> uris = platformRepository.getPlatformRepositoryURLs();
        if ((uris != null) && (!uris.isEmpty())) {
            for (URI uri : uris) {
                InputStream is = null;
                try {
                    is = uri.toURL().openStream();
                    Reader repoReader = new InputStreamReader(is);
                    Repository aPlatformRepo = helper.readRepository(repoReader);
                    Resource[] resources = aPlatformRepo.getResources();
                    for (Resource r : resources) {
                        Capability[] caps = r.getCapabilities();
                        for (Capability c : caps) {
                            obrResolver.addGlobalCapability(c);
                        }
                    }
                } catch (Exception e) {
                    // not a big problem
                    log.error(MessageUtil.getMessage("RESOLVER_UNABLE_TO_READ_REPOSITORY_EXCEPTION", new Object[] { appName, uri }));
                } finally {
                    IOUtils.close(is);
                }
            }
        }
    }
    log.debug(LOG_EXIT, "addPlatformRepositories");
}
Also used : PlatformRepository(org.apache.aries.application.management.spi.repository.PlatformRepository) Repository(org.apache.felix.bundlerepository.Repository) InputStreamReader(java.io.InputStreamReader) Capability(org.apache.felix.bundlerepository.Capability) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) DataModelHelper(org.apache.felix.bundlerepository.DataModelHelper) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) URI(java.net.URI) ResolverException(org.apache.aries.application.management.ResolverException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException)

Example 4 with DataModelHelper

use of org.apache.felix.bundlerepository.DataModelHelper in project aries by apache.

the class Aries1522Test method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    BundleContext context = context();
    ServiceReference<RepositoryAdmin> ref = context.getServiceReference(RepositoryAdmin.class);
    assertNotNull("The RepositoryAdmin service does not exist", ref);
    try {
        repositoryAdmin = (RepositoryAdmin) context.getService(ref);
        DataModelHelper helper = repositoryAdmin.getHelper();
        url = createRepositoryXml(helper);
        Repository repository = repositoryAdmin.addRepository(url);
        Resource resource = repository.getResources()[0];
        System.out.println(resource.getURI());
    } finally {
        context.ungetService(ref);
    }
}
Also used : Repository(org.apache.felix.bundlerepository.Repository) RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) Resource(org.apache.felix.bundlerepository.Resource) DataModelHelper(org.apache.felix.bundlerepository.DataModelHelper) BundleContext(org.osgi.framework.BundleContext)

Example 5 with DataModelHelper

use of org.apache.felix.bundlerepository.DataModelHelper in project aries by apache.

the class Aries1523Test method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    BundleContext context = context();
    ServiceReference<RepositoryAdmin> ref = context.getServiceReference(RepositoryAdmin.class);
    assertNotNull("The RepositoryAdmin service does not exist", ref);
    try {
        repositoryAdmin = (RepositoryAdmin) context.getService(ref);
        DataModelHelper helper = repositoryAdmin.getHelper();
        url = createRepositoryXml(helper);
        Repository repository = repositoryAdmin.addRepository(url);
        Resource resource = repository.getResources()[0];
        System.out.println(resource.getURI());
    } finally {
        context.ungetService(ref);
    }
}
Also used : Repository(org.apache.felix.bundlerepository.Repository) RepositoryAdmin(org.apache.felix.bundlerepository.RepositoryAdmin) Resource(org.apache.felix.bundlerepository.Resource) DataModelHelper(org.apache.felix.bundlerepository.DataModelHelper) BundleContext(org.osgi.framework.BundleContext)

Aggregations

DataModelHelper (org.apache.felix.bundlerepository.DataModelHelper)6 Repository (org.apache.felix.bundlerepository.Repository)5 Resource (org.apache.felix.bundlerepository.Resource)4 RepositoryAdmin (org.apache.felix.bundlerepository.RepositoryAdmin)3 BundleContext (org.osgi.framework.BundleContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)2 ResolverException (org.apache.aries.application.management.ResolverException)2 PlatformRepository (org.apache.aries.application.management.spi.repository.PlatformRepository)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 URI (java.net.URI)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AriesApplicationResolver (org.apache.aries.application.management.spi.resolve.AriesApplicationResolver)1