/*
 * Copyright 2015 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.kernel.modeshape;

import org.fcrepo.kernel.api.TxSession;

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

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.jcr.Session;

/**
 * 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.
 *
 * @author awoods
 */
public class TxAwareSession implements InvocationHandler {

    private final String txId;

    private final Session session;

    /**
     * @param session a JCR session
     * @param txID the transaction identifier
     */
    public TxAwareSession(final Session session, final String txID) {
        this.session = session;
        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 Throwable {
        final String name = method.getName();
        if (name.equals("logout") || name.equals("save")) {
            return null;
        } else if (name.equals("getTxId")) {
            return txId;
        } else {
            final Object invocationResult;
            try {
                invocationResult = method.invoke(session, args);
            } catch (final InvocationTargetException e) {
                throw e.getCause();
            }
            if (name.equals("impersonate")) {
                return newInstance((Session) invocationResult, txId);
            }
            return invocationResult;
        }
    }
}

@@ -641,142 +641,62 @@
 ort 
-org.fcrepo.kernel.api.TxSession;%0A%0Aimport static java.lang.reflect.Proxy.newProxyInstance;%0A%0Aimport java.lang.reflect.InvocationHandler;
+static com.google.common.reflect.Reflection.newProxy;%0A
 %0Aimp
@@ -805,16 +805,118 @@
 ssion;%0A%0A
+import org.fcrepo.kernel.api.TxSession;%0A%0Aimport com.google.common.reflect.AbstractInvocationHandler;%0A%0A
 /**%0A * A
@@ -1193,19 +1193,24 @@
 ion 
-implement
+extend
 s 
+Abstract
 Invo
@@ -1820,134 +1820,33 @@
 urn 
-(Session) newProxyInstance(session.getClass().getClassLoader(),%0A                new Class%5B%5D %7BTxSession.class%7D,%0A               
+newProxy(TxSession.class,
  new
@@ -1908,27 +1908,40 @@
    p
-ublic Object invoke
+rotected Object handleInvocation
 (fin
@@ -1977,28 +1977,16 @@
  method,
-%0A           
  final O

Use Guava utilities for dynamic proxies