use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class OsgiInstallerImpl method internalResourceAddedOrUpdated.
/**
* Handle external addition or update of a resource
* @see org.apache.sling.installer.api.ResourceChangeListener#resourceAddedOrUpdated(java.lang.String, java.lang.String, java.io.InputStream, java.util.Dictionary, Map)
*/
private void internalResourceAddedOrUpdated(final String resourceType, final String entityId, final ResourceData data, final Dictionary<String, Object> dict, final Map<String, Object> attributes) {
final String key = resourceType + ':' + entityId;
final boolean persistChange = (attributes != null ? PropertiesUtil.toBoolean(attributes.get(ResourceChangeListener.RESOURCE_PERSIST), true) : true);
try {
boolean compactAndSave = false;
boolean done = false;
synchronized (this.resourcesLock) {
final EntityResourceList erl = this.persistentList.getEntityResourceList(key);
logger.debug("Added or updated {} : {}", key, erl);
// we first check for update
if (erl != null && erl.getFirstResource() != null) {
// check digest for dictionaries
final TaskResource tr = erl.getFirstResource();
if (dict != null) {
final String digest = FileDataStore.computeDigest(dict);
if (tr.getDigest().equals(digest)) {
if (tr.getState() == ResourceState.INSTALLED) {
logger.debug("Resource did not change {}", key);
} else if (tr.getState() == ResourceState.INSTALL || tr.getState() == ResourceState.IGNORED) {
erl.setForceFinishState(ResourceState.INSTALLED, null);
compactAndSave = true;
}
done = true;
}
}
final UpdateHandler handler;
if (!done && persistChange) {
handler = this.findHandler(tr.getScheme());
if (handler == null) {
logger.debug("No handler found to handle update of resource with scheme {}", tr.getScheme());
}
} else {
handler = null;
}
if (!done && handler == null) {
compactAndSave = this.handleExternalUpdateWithoutWriteBack(erl);
done = true;
}
if (!done) {
final InputStream localIS = data.getInputStream();
try {
final UpdateResult result = (localIS == null ? handler.handleUpdate(resourceType, entityId, tr.getURL(), data.getDictionary(), attributes) : handler.handleUpdate(resourceType, entityId, tr.getURL(), localIS, attributes));
if (result != null) {
if (!result.getURL().equals(tr.getURL()) && !result.getResourceIsMoved()) {
// resource has been added!
final InternalResource internalResource = new InternalResource(result.getScheme(), result.getResourceId(), null, data.getDictionary(), (data.getDictionary() != null ? InstallableResource.TYPE_PROPERTIES : InstallableResource.TYPE_FILE), data.getDigest(result.getURL(), result.getDigest()), result.getPriority(), data.getDataFile(), null);
final RegisteredResource rr = this.persistentList.addOrUpdate(internalResource);
final TransformationResult transRes = new TransformationResult();
// use the old entity id
final int pos = erl.getResourceId().indexOf(':');
transRes.setId(erl.getResourceId().substring(pos + 1));
transRes.setResourceType(resourceType);
if (attributes != null) {
transRes.setAttributes(attributes);
}
this.persistentList.transform(rr, new TransformationResult[] { transRes });
final EntityResourceList newGroup = this.persistentList.getEntityResourceList(key);
newGroup.setFinishState(ResourceState.INSTALLED, null);
newGroup.compact();
} else {
// resource has been updated or moved
((RegisteredResourceImpl) tr).update(data.getDataFile(), data.getDictionary(), data.getDigest(result.getURL(), result.getDigest()), result.getPriority(), result.getURL());
erl.setForceFinishState(ResourceState.INSTALLED, null);
}
compactAndSave = true;
} else {
// handler does not persist
compactAndSave = this.handleExternalUpdateWithoutWriteBack(erl);
}
} finally {
if (localIS != null) {
// always close the input stream!
try {
localIS.close();
} catch (final IOException ignore) {
// ignore
}
}
}
done = true;
}
}
if (!done) {
// create
final List<UpdateHandler> handlerList = this.updateHandlerTracker.getSortedServices();
for (final UpdateHandler handler : handlerList) {
final InputStream localIS = data.getInputStream();
try {
final UpdateResult result = (localIS == null ? handler.handleUpdate(resourceType, entityId, null, data.getDictionary(), attributes) : handler.handleUpdate(resourceType, entityId, null, localIS, attributes));
if (result != null) {
final InternalResource internalResource = new InternalResource(result.getScheme(), result.getResourceId(), null, data.getDictionary(), (data.getDictionary() != null ? InstallableResource.TYPE_PROPERTIES : InstallableResource.TYPE_FILE), data.getDigest(result.getURL(), result.getDigest()), result.getPriority(), data.getDataFile(), null);
final RegisteredResource rr = this.persistentList.addOrUpdate(internalResource);
final TransformationResult transRes = new TransformationResult();
transRes.setId(entityId);
transRes.setResourceType(resourceType);
if (attributes != null) {
transRes.setAttributes(attributes);
}
this.persistentList.transform(rr, new TransformationResult[] { transRes });
final EntityResourceList newGroup = this.persistentList.getEntityResourceList(key);
newGroup.setFinishState(ResourceState.INSTALLED);
newGroup.compact();
compactAndSave = true;
done = true;
break;
}
} finally {
if (localIS != null) {
// always close the input stream!
try {
localIS.close();
} catch (final IOException ignore) {
// ignore
}
}
}
}
if (!done) {
logger.debug("No handler found to handle creation of resource {}", key);
}
}
if (compactAndSave) {
if (erl != null) {
erl.compact();
}
this.persistentList.save();
}
}
} catch (final IOException ioe) {
logger.error("Unable to handle resource add or update of " + key, ioe);
}
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class TaskOrderingTest method getRegisteredResource.
private static EntityResourceList getRegisteredResource(String url) throws IOException {
new FileDataStore(new MockBundleContext());
final InternalResource internal = InternalResource.create("test", new InstallableResource(url, null, new Hashtable<String, Object>(), null, null, null));
RegisteredResourceImpl rr = RegisteredResourceImpl.create(internal);
TransformationResult[] tr = new DefaultTransformer().transform(rr);
if (tr == null) {
final TransformationResult result = new TransformationResult();
result.setId(url);
result.setResourceType(InstallableResource.TYPE_CONFIG);
tr = new TransformationResult[] { result };
}
rr = (RegisteredResourceImpl) rr.clone(tr[0]);
final EntityResourceList erl = new EntityResourceList("test", new MockInstallationListener());
erl.addOrUpdate(rr);
return erl;
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class RegisteredResourceTest method create.
private TaskResource create(final InstallableResource is) throws IOException {
new FileDataStore(new MockBundleContext());
final InternalResource internal = InternalResource.create("test", is);
final RegisteredResourceImpl rr = RegisteredResourceImpl.create(internal);
final TransformationResult[] tr = new DefaultTransformer().transform(rr);
if (tr != null) {
return rr.clone(tr[0]);
} else if (is.getId().startsWith("configuration:")) {
final TransformationResult result = new TransformationResult();
result.setId(is.getId().substring(14));
result.setResourceType(InstallableResource.TYPE_CONFIG);
result.setAttributes(Collections.singletonMap(Constants.SERVICE_PID, (Object) result.getId()));
return rr.clone(result);
}
return rr;
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project stanbol by apache.
the class SolrIndexInstaller method checkIndex.
/**
* Checks if the installed resource is an Solr Index Archive
*
* @param registeredResource
* the registered resource parsed by the Apache Sling installer framework
* @return the transformed resource or <code>null</code> if the parsed resource is not an Solr Index
* Archive.
*/
private TransformationResult[] checkIndex(RegisteredResource registeredResource) {
// the URL is <schema>:<filePath>
// where the schema is the provider that registered the resource
Map<String, Object> properties = new HashMap<String, Object>();
String filePath = registeredResource.getURL().substring(registeredResource.getURL().lastIndexOf(':') + 1);
// get the name of the index
String indexName = FilenameUtils.getBaseName(filePath);
// only the String until the first '.' -> multiple endings (e.g. slrindex.zip) expected
indexName = indexName.indexOf('.') > 0 ? indexName.substring(0, indexName.indexOf('.')) : indexName;
properties.put(INDEX_NAME, indexName);
// now convert to lover case to ease the tests for file endings
filePath = filePath.toLowerCase();
if (!filePath.contains('.' + SOLR_INDEX_ARCHIVE_EXTENSION)) {
// -> can not transform
return null;
}
String extension = FilenameUtils.getExtension(filePath);
String archiveFormat = SUPPORTED_SOLR_ARCHIVE_FORMAT.get(extension);
if (archiveFormat == null) {
log.error("Unable to process Solr Index Archive from RDFTerm " + registeredResource.getURL() + "because of unsupported archive format \"" + extension + "\" (supported are " + SUPPORTED_SOLR_ARCHIVE_FORMAT.keySet() + ")");
return null;
} else {
properties.put(PROPERTY_ARCHIVE_FORMAT, archiveFormat);
}
TransformationResult tr = new TransformationResult();
// try {
// tr.setInputStream(registeredResource.getInputStream());
// } catch (IOException e) {
// log.error(String.format("Unable to transform RegisteredResource %s with type %s and scheme %s",
// registeredResource.getURL(), registeredResource.getType(), registeredResource.getScheme()),e);
// return null;
// }
tr.setId(indexName + '.' + SOLR_INDEX_ARCHIVE_EXTENSION + '.' + archiveFormat);
tr.setAttributes(properties);
tr.setResourceType(SOLR_INDEX_ARCHIVE_RESOURCE_TYPE);
return new TransformationResult[] { tr };
}
use of org.apache.sling.installer.api.tasks.TransformationResult in project sling by apache.
the class SubsystemBaseTransformerTest method testTransformNoRunMode.
@Test
public void testTransformNoRunMode() throws Exception {
SlingSettingsService slingSettings = Mockito.mock(SlingSettingsService.class);
SubsystemBaseTransformer sbt = new SubsystemBaseTransformer(slingSettings);
URL testArchive = getClass().getResource("/test1.subsystem-base");
RegisteredResource resource = new TestRegisteredResource(testArchive);
TransformationResult[] tra = sbt.transform(resource);
assertEquals(1, tra.length);
TransformationResult tr = tra[0];
assertEquals("esa", tr.getResourceType());
assertEquals("test1", tr.getId());
DeleteOnCloseFileInputStream dcis = (DeleteOnCloseFileInputStream) tr.getInputStream();
assertTrue("The file backing the stream should exist", dcis.file.exists());
Set<String> foundBundles = new HashSet<>();
try (ZipInputStream zis = new ZipInputStream(dcis);
JarFile jf = new JarFile(testArchive.getFile())) {
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
foundBundles.add(ze.getName());
switch(ze.getName()) {
case "OSGI-INF/SUBSYSTEM.MF":
Manifest mf = new Manifest(zis);
Attributes attrs = mf.getMainAttributes();
assertEquals("test1", attrs.getValue("Subsystem-SymbolicName"));
assertEquals("osgi.subsystem.composite", attrs.getValue("Subsystem-Type"));
assertEquals("(c) 2015 yeah!", attrs.getValue("Subsystem-Copyright"));
assertEquals("Extra subsystem headers can go here including very long ones " + "that would span multiple lines in a manifest", attrs.getValue("Subsystem-Description"));
assertEquals("org.apache.sling.commons.osgi;version=2.3.0;type=osgi.bundle;start-order:=0," + "org.apache.sling.commons.json;version=2.0.12;type=osgi.bundle;start-order:=10," + "org.apache.sling.commons.mime;version=2.1.8;type=osgi.bundle;start-order:=10", attrs.getValue("Subsystem-Content"));
break;
case "org.apache.sling.commons.osgi-2.3.0.jar":
ZipEntry oze = jf.getEntry("Potential_Bundles/0/org.apache.sling.commons.osgi-2.3.0.jar");
assertArtifactsEqual(oze.getName(), jf.getInputStream(oze), zis);
break;
case "org.apache.sling.commons.json-2.0.12.jar":
ZipEntry jze = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.json-2.0.12.jar");
assertArtifactsEqual(jze.getName(), jf.getInputStream(jze), zis);
break;
case "org.apache.sling.commons.mime-2.1.8.jar":
ZipEntry mze = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.mime-2.1.8.jar");
assertArtifactsEqual(mze.getName(), jf.getInputStream(mze), zis);
break;
}
}
}
assertEquals(new HashSet<>(Arrays.asList("OSGI-INF/SUBSYSTEM.MF", "org.apache.sling.commons.osgi-2.3.0.jar", "org.apache.sling.commons.json-2.0.12.jar", "org.apache.sling.commons.mime-2.1.8.jar")), foundBundles);
assertFalse("After closing the stream the temp file should have been deleted.", dcis.file.exists());
}
Aggregations