Created
September 25, 2012 13:28
-
-
Save trekawek/3781847 to your computer and use it in GitHub Desktop.
Oakathon - oak plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.apache.jackrabbit.oak.jcr; | |
import java.util.concurrent.Executors; | |
import javax.jcr.Repository; | |
import javax.jcr.RepositoryException; | |
import javax.jcr.Session; | |
import javax.jcr.SimpleCredentials; | |
import org.apache.jackrabbit.oak.api.CommitFailedException; | |
import org.apache.jackrabbit.oak.api.ContentRepository; | |
import org.apache.jackrabbit.oak.api.PropertyState; | |
import org.apache.jackrabbit.oak.core.ContentRepositoryImpl; | |
import org.apache.jackrabbit.oak.spi.commit.CommitHook; | |
import org.apache.jackrabbit.oak.spi.commit.CompositeHook; | |
import org.apache.jackrabbit.oak.spi.commit.DefaultValidator; | |
import org.apache.jackrabbit.oak.spi.commit.ValidatingHook; | |
import org.apache.jackrabbit.oak.spi.state.NodeBuilder; | |
import org.apache.jackrabbit.oak.spi.state.NodeState; | |
import org.apache.jackrabbit.oak.spi.state.NodeStore; | |
import org.junit.Test; | |
public class OakathonTest { | |
private class TestValidator extends DefaultValidator { | |
@Override | |
public void propertyAdded(PropertyState after) throws CommitFailedException { | |
if (after.getName().equals("test")) { | |
throw new CommitFailedException("Test content not allowed"); | |
} | |
System.out.println(after.getName() + " added"); | |
} | |
} | |
private class FixContentHook implements CommitHook { | |
@Override | |
public NodeState processCommit(NodeStore store, NodeState before, NodeState after) | |
throws CommitFailedException { | |
if (after.getProperty("test") != null) { | |
NodeBuilder builder = store.getBuilder(after); | |
builder.setProperty("fixed", after.getProperty("test").getValue()); | |
builder.removeProperty("test"); | |
return builder.getNodeState(); | |
} | |
return after; | |
} | |
} | |
@Test | |
public void getPlugin() throws RepositoryException { | |
CommitHook hook = new CompositeHook(new FixContentHook(), new ValidatingHook(new TestValidator())); | |
ContentRepository cr = new ContentRepositoryImpl(hook); | |
Repository repository = new RepositoryImpl(cr, Executors.newScheduledThreadPool(0)); | |
System.out.println(repository.getDescriptor(Repository.REP_NAME_DESC)); | |
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray())); | |
try { | |
session.getRootNode().setProperty("test", "foo"); | |
session.save(); | |
System.out.println(session.getProperty("/fixed").getString()); | |
} finally { | |
session.logout(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment