Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More code cleanup for Sonar
  • Loading branch information
ajs6f committed Jul 5, 2013
1 parent 6dd0551 commit af94620
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 414 deletions.
25 changes: 15 additions & 10 deletions fcrepo-kernel/src/main/java/org/fcrepo/TxAwareSession.java
Expand Up @@ -13,22 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo;

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

import java.lang.reflect.InvocationHandler;
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.
* 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 {

private final String txId;

private Session session;

/**
Expand All @@ -42,25 +44,28 @@ public TxAwareSession(final Session session, final String 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));
new Class[] {TxSession.class},
new TxAwareSession(session, txId));
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("logout") || method.getName().equals("save")) {
public Object invoke(final Object proxy, final Method method,
final Object[] args) throws ReflectiveOperationException,
IllegalArgumentException {
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 TxAwareSession.newInstance((Session)method.invoke(session, args), txId);
return newInstance((Session) method.invoke(session, args), txId);
} else {
return method.invoke(session, args);
}
Expand Down
Expand Up @@ -13,37 +13,45 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.identifiers;

import static com.codahale.metrics.MetricRegistry.name;
import static com.google.common.base.Joiner.on;
import static com.google.common.base.Splitter.fixedLength;
import static java.util.UUID.randomUUID;
import static org.fcrepo.metrics.RegistryService.getMetrics;

import com.codahale.metrics.Timer;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

/**
* PID minter that creates hierarchical IDs for a UUID
*/
public class UUIDPathMinter extends BasePidMinter {

static final Timer timer = getMetrics().timer(name(UUIDPathMinter.class, "mint"));
static final Timer timer = getMetrics().timer(
name(UUIDPathMinter.class, "mint"));

private static final int DEFAULT_LENGTH = 2;

private static final int DEFAULT_COUNT = 4;

private final int length;

private final int count;

/**
* Configure the path minter using some reasonable defaults for
* the length (2) and count (4) of the branch nodes
* Configure the path minter using some reasonable defaults for the length
* and count of the branch nodes
*/
public UUIDPathMinter() {
this(2,4);
this(DEFAULT_LENGTH, DEFAULT_COUNT);
}

/**
* Configure the path minter for the length of the keys and depth of
* the branch node prefix
*
* Configure the path minter for the length of the keys and depth of the
* branch node prefix
*
* @param length how long the branch node identifiers should be
* @param count how many branch nodes should be inserted
*/
Expand All @@ -54,6 +62,7 @@ public UUIDPathMinter(final int length, final int count) {

/**
* Mint a unique identifier as a UUID
*
* @return
*/
@Override
Expand All @@ -63,9 +72,10 @@ public String mintPid() {

try {
final String s = randomUUID().toString();
final Iterable<String> split = Splitter.fixedLength(length).split(s.substring(0, length * count));
final Iterable<String> split =
fixedLength(length).split(s.substring(0, length * count));

return Joiner.on("/").join(split) + "/" + s;
return on("/").join(split) + "/" + s;
} finally {
context.stop();
}
Expand Down
Expand Up @@ -54,7 +54,7 @@ public class TransactionService {
* be either persisted or written to a distributed map or sth, not just this
* plain hashmap that follows
*/
private static Map<String, Transaction> TRANSACTIONS =
private static Map<String, Transaction> transactions =
new ConcurrentHashMap<String, Transaction>();

public static final long REAP_INTERVAL = 1000;
Expand All @@ -65,9 +65,9 @@ public class TransactionService {
*/
@Scheduled(fixedRate = REAP_INTERVAL)
public void removeAndRollbackExpired() {
synchronized (TRANSACTIONS) {
synchronized (transactions) {
final Iterator<Entry<String, Transaction>> txs =
TRANSACTIONS.entrySet().iterator();
transactions.entrySet().iterator();
while (txs.hasNext()) {
final Transaction tx = txs.next().getValue();
if (tx.getExpires().getTime() <= currentTimeMillis()) {
Expand All @@ -92,7 +92,7 @@ public void removeAndRollbackExpired() {
*/
public Transaction beginTransaction(final Session sess) {
final Transaction tx = new Transaction(sess);
TRANSACTIONS.put(tx.getId(), tx);
transactions.put(tx.getId(), tx);
return tx;
}

Expand All @@ -105,7 +105,7 @@ public Transaction beginTransaction(final Session sess) {
public Transaction getTransaction(final String txid)
throws TransactionMissingException {

final Transaction tx = TRANSACTIONS.get(txid);
final Transaction tx = transactions.get(txid);

if (tx == null) {
throw new TransactionMissingException(
Expand All @@ -122,7 +122,7 @@ public Transaction getTransaction(final String txid)
* @return the {@link Transaction}
*/
public boolean exists(final String txid) {
return TRANSACTIONS.containsKey(txid);
return transactions.containsKey(txid);
}

/**
Expand All @@ -132,7 +132,7 @@ public boolean exists(final String txid) {
* @throws RepositoryException
*/
public Transaction commit(final String txid) throws RepositoryException {
final Transaction tx = TRANSACTIONS.remove(txid);
final Transaction tx = transactions.remove(txid);
if (tx == null) {
throw new RepositoryException("Transaction with id " + txid +
" is not available");
Expand All @@ -149,7 +149,7 @@ public Transaction commit(final String txid) throws RepositoryException {
* @throws RepositoryException if the {@link Transaction} could not be found
*/
public Transaction rollback(final String txid) throws RepositoryException {
final Transaction tx = TRANSACTIONS.remove(txid);
final Transaction tx = transactions.remove(txid);
if (tx == null) {
throw new RepositoryException("Transaction with id " + txid +
" is not available");
Expand Down
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.services.functions;

import java.io.Serializable;
Expand All @@ -32,6 +33,7 @@

/**
* Apply a Function on a BinaryKey in a LOCAL CacheStore
*
* @param <K> Cache key class
* @param <V> Cache value class
* @param <T> Output of the transform
Expand All @@ -40,20 +42,22 @@ public class CacheLocalTransform<K, V, T> implements
DistributedCallable<K, V, T>, Serializable {

/**
* Because this class will be communicated between cache nodes,
* it must be serializable
* Because this class will be communicated between cache nodes, it must be
* serializable
*/
private static final long serialVersionUID = -7014104738830230123L;

private static GetCacheStore TRANSFORM = new GetCacheStore();
private static GetCacheStore transform = new GetCacheStore();

private BinaryKey key;

private Function<LowLevelCacheEntry, T> entryTransform;

private CacheStore store;

private String cacheName = "";

/**
*
* @param key the BinaryKey to transform
* @param entryTransform a Function from LowLevelCacheEntries to T
*/
Expand All @@ -65,17 +69,18 @@ public CacheLocalTransform(final BinaryKey key,
}

@Override
public T call() throws Exception {
LowLevelCacheEntry entry =
(store instanceof ChainingCacheStore) ?
new ChainingCacheStoreEntry((ChainingCacheStore)store, cacheName, key) :
new CacheStoreEntry(store, cacheName, key);
public T call() {
final LowLevelCacheEntry entry =
(store instanceof ChainingCacheStore)
? new ChainingCacheStoreEntry(
(ChainingCacheStore) store, cacheName, key)
: new CacheStoreEntry(store, cacheName, key);
return this.entryTransform.apply(entry);
}

@Override
public void setEnvironment(Cache<K, V> cache, Set<K> keys) {
this.store = TRANSFORM.apply(cache);
public void setEnvironment(final Cache<K, V> cache, final Set<K> keys) {
this.store = transform.apply(cache);
this.cacheName = ((CacheImpl<K, V>) cache).getName();
}
}
18 changes: 10 additions & 8 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/EventType.java
Expand Up @@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.utils;

/**
* A convenient abstraction over JCR's integer-typed events.
*
*
* @author ajs6f
* @date Feb 7, 2013
*/
Expand All @@ -27,24 +28,25 @@ public enum EventType {

/**
* Get the Fedora event type for a JCR type
*
* @param i
* @return
*/
public static EventType getEventType(final Integer i) {
switch (i) {
case 0x1:
case javax.jcr.observation.Event.NODE_ADDED:
return NODE_ADDED;
case 0x2:
case javax.jcr.observation.Event.NODE_REMOVED:
return NODE_REMOVED;
case 0x4:
case javax.jcr.observation.Event.PROPERTY_ADDED:
return PROPERTY_ADDED;
case 0x8:
case javax.jcr.observation.Event.PROPERTY_REMOVED:
return PROPERTY_REMOVED;
case 0x10:
case javax.jcr.observation.Event.PROPERTY_CHANGED:
return PROPERTY_CHANGED;
case 0x20:
case javax.jcr.observation.Event.NODE_MOVED:
return NODE_MOVED;
case 0x40:
case javax.jcr.observation.Event.PERSIST:
return PERSIST;
// no default
default:
Expand Down

0 comments on commit af94620

Please sign in to comment.