Search in sources :

Example 1 with Operation

use of org.apache.sling.repoinit.parser.operations.Operation in project sling by apache.

the class ParserTest method checkResult.

@Test
public void checkResult() throws ParseException, IOException {
    final String expected = IOUtils.toString(tc.expected, DEFAULT_ENCODING).trim();
    try {
        final StringWriter sw = new StringWriter();
        final OperationVisitor v = new OperationToStringVisitor(new PrintWriter(sw));
        final List<Operation> result = new RepoInitParserImpl(tc.input).parse();
        for (Operation o : result) {
            o.accept(v);
        }
        sw.flush();
        String actual = sw.toString().trim();
        // normalize line endings to ensure tests run on windows as well
        actual = actual.replaceAll("\r\n", "\n");
        assertEquals(expected, actual);
    } finally {
        tc.close();
    }
}
Also used : RepoInitParserImpl(org.apache.sling.repoinit.parser.impl.RepoInitParserImpl) StringWriter(java.io.StringWriter) OperationVisitor(org.apache.sling.repoinit.parser.operations.OperationVisitor) Operation(org.apache.sling.repoinit.parser.operations.Operation) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with Operation

use of org.apache.sling.repoinit.parser.operations.Operation in project sling by apache.

the class RepositoryInitializer method processRepository.

@Override
public void processRepository(SlingRepository repo) throws Exception {
    if (config.references() != null && config.references().length > 0) {
        // loginAdministrative is ok here, definitely an admin operation
        final Session s = repo.loginAdministrative(null);
        try {
            final RepoinitTextProvider p = new RepoinitTextProvider();
            for (String reference : config.references()) {
                final String repoinitText = p.getRepoinitText(reference);
                final List<Operation> ops = parser.parse(new StringReader(repoinitText));
                log.info("Executing {} repoinit operations", ops.size());
                processor.apply(s, ops);
                s.save();
            }
        } finally {
            s.logout();
        }
    }
}
Also used : StringReader(java.io.StringReader) Operation(org.apache.sling.repoinit.parser.operations.Operation) Session(javax.jcr.Session)

Example 3 with Operation

use of org.apache.sling.repoinit.parser.operations.Operation in project sling by apache.

the class SystemUsersInitializer method processRepository.

@Override
public void processRepository(SlingRepository repo) throws Exception {
    final Session s = repo.loginAdministrative(null);
    final InputStream is = getClass().getResourceAsStream(REPOINIT_FILE);
    try {
        if (is == null) {
            throw new IOException("Class Resource not found:" + REPOINIT_FILE);
        }
        final Reader r = new InputStreamReader(is, "UTF-8");
        final List<Operation> ops = parser.parse(r);
        log.info("Executing {} repoinit Operations", ops.size());
        processor.apply(s, ops);
        s.save();
    } finally {
        s.logout();
        is.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Operation(org.apache.sling.repoinit.parser.operations.Operation) Session(javax.jcr.Session)

Example 4 with Operation

use of org.apache.sling.repoinit.parser.operations.Operation 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();
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) InstallableResource(org.apache.sling.installer.api.InstallableResource) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) RepositoryException(javax.jcr.RepositoryException) Operation(org.apache.sling.repoinit.parser.operations.Operation) IOException(java.io.IOException) OsgiInstaller(org.apache.sling.installer.api.OsgiInstaller) StringReader(java.io.StringReader) RepoInitParsingException(org.apache.sling.repoinit.parser.RepoInitParsingException) File(java.io.File) Session(javax.jcr.Session)

Aggregations

Operation (org.apache.sling.repoinit.parser.operations.Operation)4 Session (javax.jcr.Session)3 IOException (java.io.IOException)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 RepositoryException (javax.jcr.RepositoryException)1 InstallableResource (org.apache.sling.installer.api.InstallableResource)1 OsgiInstaller (org.apache.sling.installer.api.OsgiInstaller)1 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)1 ModelArchiveReader (org.apache.sling.provisioning.model.io.ModelArchiveReader)1 ModelReader (org.apache.sling.provisioning.model.io.ModelReader)1 RepoInitParsingException (org.apache.sling.repoinit.parser.RepoInitParsingException)1 RepoInitParserImpl (org.apache.sling.repoinit.parser.impl.RepoInitParserImpl)1 OperationVisitor (org.apache.sling.repoinit.parser.operations.OperationVisitor)1 Test (org.junit.Test)1