use of org.apache.sling.repoinit.parser.RepoInitParsingException in project sling by apache.
the class InstallModelTask method execute.
@SuppressWarnings("deprecation")
@Override
public void execute(final InstallationContext ctx) {
try {
final TaskResource resource = this.getResource();
ctx.log("Installing {}", resource.getEntityId());
final String modelTxt = (String) resource.getAttribute(ModelTransformer.ATTR_MODEL);
final Integer featureIndex = (Integer) resource.getAttribute(ModelTransformer.ATTR_FEATURE_INDEX);
final String name = (String) resource.getAttribute(ModelTransformer.ATTR_FEATURE_NAME);
if (modelTxt == null || featureIndex == null || name == null) {
ctx.log("Unable to install model resource {} : no model found", resource);
this.getResourceGroup().setFinishState(ResourceState.IGNORED);
} else {
final String path = (String) resource.getAttribute(ModelTransformer.ATTR_BASE_PATH);
final File baseDir = (path == null ? null : new File(path));
boolean success = false;
try {
final Result result = this.transform(name, modelTxt, featureIndex, resource, baseDir);
if (result == null) {
ctx.log("Unable to install model resource {} : unable to create resources", resource);
this.getResourceGroup().setFinishState(ResourceState.IGNORED);
} else {
// repo init first
if (result.repoinit != null) {
List<Operation> ops = null;
try (final Reader r = new StringReader(result.repoinit)) {
ops = this.repoInitParser.parse(r);
} catch (final IOException | RepoInitParsingException e) {
logger.error("Unable to parse repoinit block.", e);
ctx.log("Unable to install model resource {} : unable parse repoinit block.", resource);
this.getResourceGroup().setFinishState(ResourceState.IGNORED);
return;
}
// login admin is required for repo init
Session session = null;
try {
session = this.repository.loginAdministrative(null);
this.repoInitProcessor.apply(session, ops);
session.save();
} catch (final RepositoryException re) {
logger.error("Unable to process repoinit block.", re);
ctx.log("Unable to install model resource {} : unable to process repoinit block.", resource);
this.getResourceGroup().setFinishState(ResourceState.IGNORED);
return;
} finally {
if (session != null) {
session.logout();
}
}
}
if (!result.resources.isEmpty()) {
final OsgiInstaller installer = this.getService(OsgiInstaller.class);
if (installer != null) {
installer.registerResources("model-" + name, result.resources.toArray(new InstallableResource[result.resources.size()]));
} else {
ctx.log("Unable to install model resource {} : unable to get OSGi installer", resource);
this.getResourceGroup().setFinishState(ResourceState.IGNORED);
return;
}
}
this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
success = true;
}
} finally {
if (!success && baseDir != null) {
this.deleteDirectory(baseDir);
}
}
if (success) {
ctx.log("Installed {}", resource.getEntityId());
}
}
} finally {
this.cleanup();
}
}
Aggregations