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;
}
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;
}
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;
}
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;
}
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();
}
}
}
Aggregations