Skip to content

Commit

Permalink
clean up the TxAwareSession delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Nov 14, 2013
1 parent 3c9fc57 commit 4a26e39
Showing 1 changed file with 250 additions and 31 deletions.
281 changes: 250 additions & 31 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/TxAwareSession.java
Expand Up @@ -16,19 +16,46 @@

package org.fcrepo.kernel;

import static java.lang.reflect.Proxy.newProxyInstance;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.AccessControlException;

import javax.jcr.AccessDeniedException;
import javax.jcr.Credentials;
import javax.jcr.InvalidItemStateException;
import javax.jcr.InvalidSerializedDataException;
import javax.jcr.Item;
import javax.jcr.ItemExistsException;
import javax.jcr.ItemNotFoundException;
import javax.jcr.LoginException;
import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.ReferentialIntegrityException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.ValueFactory;
import javax.jcr.Workspace;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.retention.RetentionManager;
import javax.jcr.security.AccessControlManager;
import javax.jcr.version.VersionException;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

/**
* A dynamic proxy that wraps JCR sessions. It is aware of fcrepo transactions,
* and turns mutating methods (e.g. logout, session) into no-ops. Those no-op'ed
* methods should be called from the Transaction level instead.
*/
public class TxAwareSession implements InvocationHandler {
public class TxAwareSession implements Session {

private final String txId;

Expand All @@ -43,31 +70,223 @@ public TxAwareSession(final Session session, final String txID) {
this.txId = txID;
}

/**
* Wrap a JCR session with this dynamic proxy
*
* @param session a JCR session
* @param txId the transaction identifier
* @return a wrapped JCR session
*/
public static Session newInstance(final Session session, final String txId) {
return (Session) newProxyInstance(session.getClass().getClassLoader(),
new Class[] {TxSession.class},
new TxAwareSession(session, txId));
}

@Override
public Object invoke(final Object proxy, final Method method,
final Object[] args) throws ReflectiveOperationException {
if (method.getName().equals("logout") ||
method.getName().equals("save")) {
return null;
} else if (method.getName().equals("getTxId")) {
return txId;
} else if (method.getName().equals("impersonate")) {
return newInstance((Session) method.invoke(session, args), txId);
} else {
return method.invoke(session, args);
}
public Repository getRepository() {
return session.getRepository();
}

public String getUserID() {
return session.getUserID();
}

public String[] getAttributeNames() {
return session.getAttributeNames();
}

public Object getAttribute(String name) {
return session.getAttribute(name);
}

public Workspace getWorkspace() {
return session.getWorkspace();
}

public Node getRootNode() throws RepositoryException {
return session.getRootNode();
}

public Session impersonate(Credentials credentials) throws LoginException,
RepositoryException {
return new TxAwareSession(session.impersonate(credentials), txId);
}

@Deprecated
public Node getNodeByUUID(String uuid) throws ItemNotFoundException,
RepositoryException {
return session.getNodeByUUID(uuid);
}

public Node getNodeByIdentifier(String id) throws ItemNotFoundException,
RepositoryException {
return session.getNodeByIdentifier(id);
}

public Item getItem(String absPath) throws PathNotFoundException,
RepositoryException {
return session.getItem(absPath);
}

public Node getNode(String absPath) throws PathNotFoundException,
RepositoryException {
return session.getNode(absPath);
}

public Property getProperty(String absPath) throws PathNotFoundException,
RepositoryException {
return session.getProperty(absPath);
}

public boolean itemExists(String absPath) throws RepositoryException {
return session.itemExists(absPath);
}

public boolean nodeExists(String absPath) throws RepositoryException {
return session.nodeExists(absPath);
}

public boolean propertyExists(String absPath) throws RepositoryException {
return session.propertyExists(absPath);
}

public void move(String srcAbsPath, String destAbsPath)
throws ItemExistsException, PathNotFoundException,
VersionException, ConstraintViolationException, LockException,
RepositoryException {
session.move(srcAbsPath, destAbsPath);
}

public void removeItem(String absPath) throws VersionException,
LockException, ConstraintViolationException, AccessDeniedException,
RepositoryException {
session.removeItem(absPath);
}

public void save() throws AccessDeniedException, ItemExistsException,
ReferentialIntegrityException, ConstraintViolationException,
InvalidItemStateException, VersionException, LockException,
NoSuchNodeTypeException, RepositoryException {
// no-op
}

public void refresh(boolean keepChanges) throws RepositoryException {
session.refresh(keepChanges);
}

public boolean hasPendingChanges() throws RepositoryException {
return session.hasPendingChanges();
}

public ValueFactory getValueFactory()
throws UnsupportedRepositoryOperationException, RepositoryException {
return session.getValueFactory();
}

public boolean hasPermission(String absPath, String actions)
throws RepositoryException {
return session.hasPermission(absPath, actions);
}

public void checkPermission(String absPath, String actions)
throws AccessControlException, RepositoryException {
session.checkPermission(absPath, actions);
}

public boolean hasCapability(String methodName, Object target,
Object[] arguments) throws RepositoryException {
return session.hasCapability(methodName, target, arguments);
}

public ContentHandler getImportContentHandler(String parentAbsPath,
int uuidBehavior) throws PathNotFoundException,
ConstraintViolationException, VersionException, LockException,
RepositoryException {
return session.getImportContentHandler(parentAbsPath, uuidBehavior);
}

public void
importXML(String parentAbsPath, InputStream in, int uuidBehavior)
throws IOException, PathNotFoundException,
ItemExistsException, ConstraintViolationException,
VersionException, InvalidSerializedDataException,
LockException, RepositoryException {
session.importXML(parentAbsPath, in, uuidBehavior);
}

public void exportSystemView(String absPath, ContentHandler contentHandler,
boolean skipBinary, boolean noRecurse)
throws PathNotFoundException, SAXException, RepositoryException {
session.exportSystemView(absPath, contentHandler, skipBinary, noRecurse);
}

public void exportSystemView(String absPath, OutputStream out,
boolean skipBinary, boolean noRecurse) throws IOException,
PathNotFoundException, RepositoryException {
session.exportSystemView(absPath, out, skipBinary, noRecurse);
}

public void
exportDocumentView(String absPath, ContentHandler contentHandler,
boolean skipBinary, boolean noRecurse)
throws PathNotFoundException, SAXException,
RepositoryException {
session.exportDocumentView(absPath, contentHandler, skipBinary,
noRecurse);
}

public void exportDocumentView(String absPath, OutputStream out,
boolean skipBinary, boolean noRecurse) throws IOException,
PathNotFoundException, RepositoryException {
session.exportDocumentView(absPath, out, skipBinary, noRecurse);
}

public void setNamespacePrefix(String prefix, String uri)
throws NamespaceException, RepositoryException {
session.setNamespacePrefix(prefix, uri);
}

public String[] getNamespacePrefixes() throws RepositoryException {
return session.getNamespacePrefixes();
}

public String getNamespaceURI(String prefix) throws NamespaceException,
RepositoryException {
return session.getNamespaceURI(prefix);
}

public String getNamespacePrefix(String uri) throws NamespaceException,
RepositoryException {
return session.getNamespacePrefix(uri);
}

public void logout() {
// no-op
throw new UnsupportedOperationException();
}

public boolean isLive() {
return session.isLive();
}

@Deprecated
public void addLockToken(String lt) {
session.addLockToken(lt);
}

@Deprecated
public String[] getLockTokens() {
return session.getLockTokens();
}

@Deprecated
public void removeLockToken(String lt) {
session.removeLockToken(lt);
}

public AccessControlManager getAccessControlManager()
throws UnsupportedRepositoryOperationException, RepositoryException {
return session.getAccessControlManager();
}

public RetentionManager getRetentionManager()
throws UnsupportedRepositoryOperationException, RepositoryException {
return session.getRetentionManager();
}

public String getTxId() {
return txId;
}

public String toString() {
return "TxAwareSession(" + super.toString() + ")";
}

}

0 comments on commit 4a26e39

Please sign in to comment.