Search in sources :

Example 46 with Version

use of org.osgi.framework.Version in project sling by apache.

the class DeploymentPackageInstaller method transform.

/**
     * @see org.apache.sling.installer.api.tasks.ResourceTransformer#transform(org.apache.sling.installer.api.tasks.RegisteredResource)
     */
public TransformationResult[] transform(final RegisteredResource resource) {
    if (resource.getType().equals(InstallableResource.TYPE_FILE)) {
        try {
            final Manifest m = getManifest(resource.getInputStream());
            if (m != null) {
                final String sn = m.getMainAttributes().getValue(DEPLOYMENTPACKAGE_SYMBOLICMAME);
                if (sn != null) {
                    final String v = m.getMainAttributes().getValue(DEPLOYMENTPACKAGE_VERSION);
                    if (v != null) {
                        final Map<String, Object> attr = new HashMap<String, Object>();
                        attr.put(DEPLOYMENTPACKAGE_SYMBOLICMAME, sn);
                        final TransformationResult tr = new TransformationResult();
                        tr.setId(sn);
                        tr.setVersion(new Version(v));
                        tr.setResourceType(TYPE_DP);
                        tr.setAttributes(attr);
                        return new TransformationResult[] { tr };
                    }
                }
            }
        } catch (final IOException ignore) {
        // ignore
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) Version(org.osgi.framework.Version) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Example 47 with Version

use of org.osgi.framework.Version in project sling by apache.

the class ModelTransformer method transform.

@Override
public TransformationResult[] transform(final RegisteredResource resource) {
    Model model = null;
    File baseDir = null;
    if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".model")) {
        try (final Reader reader = new InputStreamReader(resource.getInputStream(), "UTF-8")) {
            model = ModelReader.read(reader, resource.getURL());
        } catch (final IOException ioe) {
            logger.info("Unable to read model from " + resource.getURL(), ioe);
        }
    }
    if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".mar")) {
        baseDir = this.bundleContext.getDataFile("");
        try (final InputStream is = resource.getInputStream()) {
            model = ModelArchiveReader.read(is, new ModelArchiveReader.ArtifactConsumer() {

                @Override
                public void consume(final Artifact artifact, final InputStream is) throws IOException {
                // nothing to do, install task does extraction
                }
            });
        } catch (final IOException ioe) {
            logger.info("Unable to read model from " + resource.getURL(), ioe);
        }
    }
    if (model != null) {
        Map<Traceable, String> errors = ModelUtility.validate(model);
        if (errors == null) {
            try {
                final Model effectiveModel = ModelUtility.getEffectiveModel(model);
                errors = ModelUtility.validateIncludingVersion(effectiveModel);
                if (errors == null) {
                    String modelTxt = null;
                    try (final StringWriter sw = new StringWriter()) {
                        ModelWriter.write(sw, effectiveModel);
                        modelTxt = sw.toString();
                    } catch (final IOException ioe) {
                        logger.info("Unable to read model from " + resource.getURL(), ioe);
                    }
                    if (modelTxt != null) {
                        final TransformationResult[] result = new TransformationResult[effectiveModel.getFeatures().size()];
                        int index = 0;
                        for (final Feature f : effectiveModel.getFeatures()) {
                            final TransformationResult tr = new TransformationResult();
                            tr.setResourceType(TYPE_PROV_MODEL);
                            tr.setId(f.getName());
                            tr.setVersion(new Version(f.getVersion()));
                            final Map<String, Object> attributes = new HashMap<>();
                            attributes.put(ATTR_MODEL, modelTxt);
                            attributes.put(ATTR_FEATURE_INDEX, index);
                            attributes.put(ATTR_FEATURE_NAME, f.getName() + "-" + f.getVersion());
                            if (baseDir != null) {
                                final File dir = new File(baseDir, f.getName() + "-" + f.getVersion());
                                attributes.put(ATTR_BASE_PATH, dir.getAbsolutePath());
                            }
                            tr.setAttributes(attributes);
                            result[index] = tr;
                            index++;
                        }
                        return result;
                    }
                }
            } catch (final IllegalArgumentException iae) {
                errors = Collections.singletonMap((Traceable) model, iae.getMessage());
            }
        }
        if (errors != null) {
            logger.warn("Errors during parsing model at {} : {}", resource.getURL(), errors.values());
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.sling.provisioning.model.Artifact) StringWriter(java.io.StringWriter) Version(org.osgi.framework.Version) Model(org.apache.sling.provisioning.model.Model) Traceable(org.apache.sling.provisioning.model.Traceable) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) File(java.io.File)

Example 48 with Version

use of org.osgi.framework.Version in project sling by apache.

the class PackageTransformer method checkForPackage.

/**
     * Check if the resource is a content package
     * @param resource The resource
     * @return {@code null} if not a content package, a result otherwise
     */
private TransformationResult[] checkForPackage(final RegisteredResource resource) {
    // first check if this is a zip archive
    try (final ZipInputStream zin = new ZipInputStream(new BufferedInputStream(resource.getInputStream()))) {
        if (zin.getNextEntry() == null) {
            return null;
        }
    } catch (final IOException ioe) {
        logger.debug("Unable to read resource.", ioe);
        return null;
    }
    Session session = null;
    JcrPackage pck = null;
    try {
        // create an admin session
        session = repository.loginAdministrative(null);
        final JcrPackageManager pckMgr = pkgSvc.getPackageManager(session);
        pck = pckMgr.upload(resource.getInputStream(), true, true);
        if (pck.isValid()) {
            final PackageId pid = pck.getDefinition().getId();
            final Map<String, Object> attrs = new HashMap<String, Object>();
            attrs.put(ATTR_PCK_ID, pid.toString());
            final TransformationResult tr = new TransformationResult();
            tr.setId(pid.getGroup() + ':' + pid.getName());
            tr.setResourceType(RESOURCE_TYPE);
            tr.setAttributes(attrs);
            // version
            final String version = pid.getVersionString();
            if (version.length() > 0) {
                tr.setVersion(new Version(cleanupVersion(version)));
            }
            return new TransformationResult[] { tr };
        }
    } catch (final Exception ioe) {
        logger.debug("Unable to check content package " + resource.getURL(), ioe);
    } finally {
        if (pck != null) {
            pck.close();
        }
        if (session != null) {
            session.logout();
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) JcrPackageManager(org.apache.jackrabbit.vault.packaging.JcrPackageManager) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) Version(org.osgi.framework.Version) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) Session(javax.jcr.Session)

Example 49 with Version

use of org.osgi.framework.Version in project sling by apache.

the class SubsystemBaseTransformer method transform.

public TransformationResult[] transform(RegisteredResource resource) {
    // TODO start level of the subsystem
    if (resource.getType().equals(InstallableResource.TYPE_FILE)) {
        if (resource.getURL().endsWith("." + TYPE_SUBSYSTEM_BASE)) {
            logger.info("Found subsystem-base resource {}", resource);
            try {
                SubsystemData ssd = createSubsystemFile(resource);
                TransformationResult tr = new TransformationResult();
                Attributes mfAttributes = ssd.manifest.getMainAttributes();
                tr.setId(mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME));
                tr.setVersion(new Version(mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_VERSION)));
                tr.setResourceType("esa");
                tr.setInputStream(new DeleteOnCloseFileInputStream(ssd.file));
                Map<String, Object> attr = new HashMap<String, Object>();
                attr.put(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME, mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME));
                attr.put(SubsystemConstants.SUBSYSTEM_VERSION, mfAttributes.getValue(SubsystemConstants.SUBSYSTEM_VERSION));
                tr.setAttributes(attr);
                return new TransformationResult[] { tr };
            } catch (IOException ioe) {
                logger.error("Problem processing subsystem-base file " + resource, ioe);
            }
        }
    }
    return null;
}
Also used : Version(org.osgi.framework.Version) HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Example 50 with Version

use of org.osgi.framework.Version in project sling by apache.

the class BundleInstallUpgradeExceptionRetryTest method testUpdateSuccedsWithLessThanMaxRetrys.

@Test
public void testUpdateSuccedsWithLessThanMaxRetrys() throws Exception {
    // install version 1.1 and fail activation with exception all attempts
    final Object listener = this.startObservingBundleEvents();
    final AtomicReference<ServiceRegistration<AtomicInteger>> ref = new AtomicReference<ServiceRegistration<AtomicInteger>>(null);
    final AtomicInteger counter = new AtomicInteger(3);
    bundleContext.addBundleListener(new SynchronousBundleListener() {

        @Override
        public void bundleChanged(org.osgi.framework.BundleEvent event) {
            if (event.getType() == org.osgi.framework.BundleEvent.STOPPED && event.getBundle().getSymbolicName().equals(symbolicName) && event.getBundle().getVersion().equals(new Version("1.0.0"))) {
                if (ref.get() == null) {
                    try {
                        event.getBundle().start();
                        ref.set(bundleContext.registerService(AtomicInteger.class, counter, null));
                    } catch (BundleException e) {
                    }
                } else {
                    ref.getAndSet(null).unregister();
                    if (counter.get() == 0) {
                        bundleContext.removeBundleListener(this);
                    }
                }
            }
        }
    });
    installer.updateResources(URL_SCHEME, getInstallableResource(createTestBundle(new Version("2.0.0"))), null);
    try {
        long time = 0;
        while (counter.get() >= 0 && time < 1000) {
            sleep(100);
            time += 100;
        }
        assertBundle("After installing", symbolicName, "2.0.0", Bundle.ACTIVE);
    } finally {
        if (ref.get() != null) {
            ref.get().unregister();
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(org.osgi.framework.Version) BundleException(org.osgi.framework.BundleException) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

Version (org.osgi.framework.Version)165 Test (org.junit.Test)30 Bundle (org.osgi.framework.Bundle)26 ArrayList (java.util.ArrayList)22 Capability (org.osgi.resource.Capability)19 File (java.io.File)17 HashMap (java.util.HashMap)16 Resource (org.osgi.resource.Resource)14 IOException (java.io.IOException)13 InputStream (java.io.InputStream)11 Manifest (java.util.jar.Manifest)11 Map (java.util.Map)9 HashSet (java.util.HashSet)8 Content (org.apache.aries.application.Content)8 AriesApplication (org.apache.aries.application.management.AriesApplication)8 VersionRange (org.apache.felix.utils.version.VersionRange)8 BundleRevision (org.osgi.framework.wiring.BundleRevision)8 List (java.util.List)7 BundleWiring (org.osgi.framework.wiring.BundleWiring)7 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)6