Java 5 code style

This commit is contained in:
Juergen Hoeller
2008-11-25 01:29:54 +00:00
parent 1f9e63af49
commit 29657105da
71 changed files with 726 additions and 877 deletions

View File

@@ -26,7 +26,6 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
@@ -87,7 +86,8 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
private volatile boolean active = true;
private final Map cachedSessions = new HashMap();
private final Map<Integer, LinkedList<Session>> cachedSessions =
new HashMap<Integer, LinkedList<Session>>();
/**
@@ -177,11 +177,9 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
public void resetConnection() {
this.active = false;
synchronized (this.cachedSessions) {
for (Iterator it = this.cachedSessions.values().iterator(); it.hasNext();) {
LinkedList sessionList = (LinkedList) it.next();
for (LinkedList<Session> sessionList : this.cachedSessions.values()) {
synchronized (sessionList) {
for (Iterator it2 = sessionList.iterator(); it2.hasNext();) {
Session session = (Session) it2.next();
for (Session session : sessionList) {
try {
session.close();
}
@@ -203,18 +201,18 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
* Checks for a cached Session for the given mode.
*/
protected Session getSession(Connection con, Integer mode) throws JMSException {
LinkedList sessionList = null;
LinkedList<Session> sessionList = null;
synchronized (this.cachedSessions) {
sessionList = (LinkedList) this.cachedSessions.get(mode);
sessionList = this.cachedSessions.get(mode);
if (sessionList == null) {
sessionList = new LinkedList();
sessionList = new LinkedList<Session>();
this.cachedSessions.put(mode, sessionList);
}
}
Session session = null;
synchronized (sessionList) {
if (!sessionList.isEmpty()) {
session = (Session) sessionList.removeFirst();
session = sessionList.removeFirst();
}
}
if (session != null) {
@@ -241,8 +239,8 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
* @param sessionList the List of cached Sessions that the given Session belongs to
* @return the wrapped Session
*/
protected Session getCachedSessionProxy(Session target, LinkedList sessionList) {
List classes = new ArrayList(3);
protected Session getCachedSessionProxy(Session target, LinkedList<Session> sessionList) {
List<Class> classes = new ArrayList<Class>(3);
classes.add(SessionProxy.class);
if (target instanceof QueueSession) {
classes.add(QueueSession.class);
@@ -252,7 +250,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
}
return (Session) Proxy.newProxyInstance(
SessionProxy.class.getClassLoader(),
(Class[]) classes.toArray(new Class[classes.size()]),
classes.toArray(new Class[classes.size()]),
new CachedSessionInvocationHandler(target, sessionList));
}
@@ -264,15 +262,17 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
private final Session target;
private final LinkedList sessionList;
private final LinkedList<Session> sessionList;
private final Map cachedProducers = new HashMap();
private final Map<Destination, MessageProducer> cachedProducers =
new HashMap<Destination, MessageProducer>();
private final Map cachedConsumers = new HashMap();
private final Map<ConsumerCacheKey, MessageConsumer> cachedConsumers =
new HashMap<ConsumerCacheKey, MessageConsumer>();
private boolean transactionOpen = false;
public CachedSessionInvocationHandler(Session target, LinkedList sessionList) {
public CachedSessionInvocationHandler(Session target, LinkedList<Session> sessionList) {
this.target = target;
this.sessionList = sessionList;
}
@@ -285,7 +285,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
}
else if (methodName.equals("hashCode")) {
// Use hashCode of Session proxy.
return new Integer(System.identityHashCode(proxy));
return System.identityHashCode(proxy);
}
else if (methodName.equals("toString")) {
return "Cached JMS Session: " + this.target;
@@ -295,7 +295,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
if (active) {
synchronized (this.sessionList) {
if (this.sessionList.size() < getSessionCacheSize()) {
logicalClose(proxy);
logicalClose((Session) proxy);
// Remain open in the session list.
return null;
}
@@ -321,11 +321,11 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
else if ((methodName.equals("createConsumer") || methodName.equals("createReceiver") ||
methodName.equals("createSubscriber")) && isCacheConsumers()) {
return getCachedConsumer((Destination) args[0], (args.length > 1 ? (String) args[1] : null),
(args.length > 2 && ((Boolean) args[2]).booleanValue()), null);
(args.length > 2 && (Boolean) args[2]), null);
}
else if (methodName.equals("createDurableSubscriber") && isCacheConsumers()) {
return getCachedConsumer((Destination) args[0], (args.length > 2 ? (String) args[2] : null),
(args.length > 3 && ((Boolean) args[3]).booleanValue()), (String) args[1]);
(args.length > 3 && (Boolean) args[3]), (String) args[1]);
}
}
try {
@@ -337,7 +337,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
}
private MessageProducer getCachedProducer(Destination dest) throws JMSException {
MessageProducer producer = (MessageProducer) this.cachedProducers.get(dest);
MessageProducer producer = this.cachedProducers.get(dest);
if (producer != null) {
if (logger.isTraceEnabled()) {
logger.trace("Found cached JMS MessageProducer for destination [" + dest + "]: " + producer);
@@ -356,8 +356,8 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
private MessageConsumer getCachedConsumer(
Destination dest, String selector, boolean noLocal, String subscription) throws JMSException {
Object cacheKey = new ConsumerCacheKey(dest, selector, noLocal, subscription);
MessageConsumer consumer = (MessageConsumer) this.cachedConsumers.get(cacheKey);
ConsumerCacheKey cacheKey = new ConsumerCacheKey(dest, selector, noLocal, subscription);
MessageConsumer consumer = this.cachedConsumers.get(cacheKey);
if (consumer != null) {
if (logger.isTraceEnabled()) {
logger.trace("Found cached JMS MessageConsumer for destination [" + dest + "]: " + consumer);
@@ -380,18 +380,17 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
return new CachedMessageConsumer(consumer);
}
private void logicalClose(Object proxy) throws JMSException {
private void logicalClose(Session proxy) throws JMSException {
// Preserve rollback-on-close semantics.
if (this.transactionOpen && this.target.getTransacted()) {
this.transactionOpen = false;
this.target.rollback();
}
// Physically close durable subscribers at time of Session close call.
for (Iterator it = this.cachedConsumers.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
ConsumerCacheKey key = (ConsumerCacheKey) entry.getKey();
if (key.subscription != null) {
((MessageConsumer) entry.getValue()).close();
for (Iterator<Map.Entry<ConsumerCacheKey, MessageConsumer>> it = this.cachedConsumers.entrySet().iterator(); it.hasNext();) {
Map.Entry<ConsumerCacheKey, MessageConsumer> entry = it.next();
if (entry.getKey().subscription != null) {
entry.getValue().close();
it.remove();
}
}
@@ -411,11 +410,11 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
// Explicitly close all MessageProducers and MessageConsumers that
// this Session happens to cache...
try {
for (Iterator it = this.cachedProducers.values().iterator(); it.hasNext();) {
((MessageProducer) it.next()).close();
for (MessageProducer producer : this.cachedProducers.values()) {
producer.close();
}
for (Iterator it = this.cachedConsumers.values().iterator(); it.hasNext();) {
((MessageConsumer) it.next()).close();
for (MessageConsumer consumer : this.cachedConsumers.values()) {
consumer.close();
}
}
finally {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,7 @@
package org.springframework.jms.connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
@@ -35,7 +33,7 @@ import org.springframework.util.Assert;
public class ChainedExceptionListener implements ExceptionListener {
/** List of ExceptionListeners */
private final List delegates = new ArrayList(2);
private final List<ExceptionListener> delegates = new ArrayList<ExceptionListener>(2);
/**
@@ -50,13 +48,12 @@ public class ChainedExceptionListener implements ExceptionListener {
* Return all registered ExceptionListener delegates (as array).
*/
public final ExceptionListener[] getDelegates() {
return (ExceptionListener[]) this.delegates.toArray(new ExceptionListener[this.delegates.size()]);
return this.delegates.toArray(new ExceptionListener[this.delegates.size()]);
}
public void onException(JMSException ex) {
for (Iterator it = this.delegates.iterator(); it.hasNext();) {
ExceptionListener listener = (ExceptionListener) it.next();
for (ExceptionListener listener : this.delegates) {
listener.onException(ex);
}
}

View File

@@ -17,11 +17,9 @@
package org.springframework.jms.connection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
@@ -55,11 +53,12 @@ public class JmsResourceHolder extends ResourceHolderSupport {
private boolean frozen = false;
private final List connections = new LinkedList();
private final List<Connection> connections = new LinkedList<Connection>();
private final List sessions = new LinkedList();
private final List<Session> sessions = new LinkedList<Session>();
private final Map sessionsPerConnection = new HashMap();
private final Map<Connection, List<Session>> sessionsPerConnection =
new HashMap<Connection, List<Session>>();
/**
@@ -136,9 +135,9 @@ public class JmsResourceHolder extends ResourceHolderSupport {
if (!this.sessions.contains(session)) {
this.sessions.add(session);
if (connection != null) {
List sessions = (List) this.sessionsPerConnection.get(connection);
List<Session> sessions = this.sessionsPerConnection.get(connection);
if (sessions == null) {
sessions = new LinkedList();
sessions = new LinkedList<Session>();
this.sessionsPerConnection.put(connection, sessions);
}
sessions.add(session);
@@ -152,34 +151,34 @@ public class JmsResourceHolder extends ResourceHolderSupport {
public Connection getConnection() {
return (!this.connections.isEmpty() ? (Connection) this.connections.get(0) : null);
return (!this.connections.isEmpty() ? this.connections.get(0) : null);
}
public Connection getConnection(Class connectionType) {
return (Connection) CollectionUtils.findValueOfType(this.connections, connectionType);
public Connection getConnection(Class<? extends Connection> connectionType) {
return CollectionUtils.findValueOfType(this.connections, connectionType);
}
public Session getSession() {
return (!this.sessions.isEmpty() ? (Session) this.sessions.get(0) : null);
return (!this.sessions.isEmpty() ? this.sessions.get(0) : null);
}
public Session getSession(Class sessionType) {
public Session getSession(Class<? extends Session> sessionType) {
return getSession(sessionType, null);
}
public Session getSession(Class sessionType, Connection connection) {
List sessions = this.sessions;
public Session getSession(Class<? extends Session> sessionType, Connection connection) {
List<Session> sessions = this.sessions;
if (connection != null) {
sessions = (List) this.sessionsPerConnection.get(connection);
sessions = this.sessionsPerConnection.get(connection);
}
return (Session) CollectionUtils.findValueOfType(sessions, sessionType);
return CollectionUtils.findValueOfType(sessions, sessionType);
}
public void commitAll() throws JMSException {
for (Iterator it = this.sessions.iterator(); it.hasNext();) {
for (Session session : this.sessions) {
try {
((Session) it.next()).commit();
session.commit();
}
catch (TransactionInProgressException ex) {
// Ignore -> can only happen in case of a JTA transaction.
@@ -191,16 +190,15 @@ public class JmsResourceHolder extends ResourceHolderSupport {
}
public void closeAll() {
for (Iterator it = this.sessions.iterator(); it.hasNext();) {
for (Session session : this.sessions) {
try {
((Session) it.next()).close();
session.close();
}
catch (Throwable ex) {
logger.debug("Could not close synchronized JMS Session after transaction", ex);
}
}
for (Iterator it = this.connections.iterator(); it.hasNext();) {
Connection con = (Connection) it.next();
for (Connection con : this.connections) {
ConnectionFactoryUtils.releaseConnection(con, this.connectionFactory, true);
}
this.connections.clear();

View File

@@ -179,7 +179,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
private int idleTaskExecutionLimit = 1;
private final Set scheduledInvokers = new HashSet();
private final Set<AsyncMessageListenerInvoker> scheduledInvokers = new HashSet<AsyncMessageListenerInvoker>();
private int activeInvokerCount = 0;
@@ -658,8 +658,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
*/
private int getIdleInvokerCount() {
int count = 0;
for (Iterator it = this.scheduledInvokers.iterator(); it.hasNext();) {
AsyncMessageListenerInvoker invoker = (AsyncMessageListenerInvoker) it.next();
for (AsyncMessageListenerInvoker invoker : this.scheduledInvokers) {
if (invoker.isIdle()) {
count++;
}

View File

@@ -72,9 +72,9 @@ public class SimpleMessageListenerContainer extends AbstractMessageListenerConta
private TaskExecutor taskExecutor;
private Set sessions;
private Set<Session> sessions;
private Set consumers;
private Set<MessageConsumer> consumers;
private final Object consumersMonitor = new Object();
@@ -221,8 +221,8 @@ public class SimpleMessageListenerContainer extends AbstractMessageListenerConta
// Register Sessions and MessageConsumers.
synchronized (this.consumersMonitor) {
if (this.consumers == null) {
this.sessions = new HashSet(this.concurrentConsumers);
this.consumers = new HashSet(this.concurrentConsumers);
this.sessions = new HashSet<Session>(this.concurrentConsumers);
this.consumers = new HashSet<MessageConsumer>(this.concurrentConsumers);
Connection con = getSharedConnection();
for (int i = 0; i < this.concurrentConsumers; i++) {
Session session = createSession(con);
@@ -301,13 +301,11 @@ public class SimpleMessageListenerContainer extends AbstractMessageListenerConta
*/
protected void doShutdown() throws JMSException {
logger.debug("Closing JMS MessageConsumers");
for (Iterator it = this.consumers.iterator(); it.hasNext();) {
MessageConsumer consumer = (MessageConsumer) it.next();
for (MessageConsumer consumer : this.consumers) {
JmsUtils.closeMessageConsumer(consumer);
}
logger.debug("Closing JMS Sessions");
for (Iterator it = this.sessions.iterator(); it.hasNext();) {
Session session = (Session) it.next();
for (Session session : this.sessions) {
JmsUtils.closeSession(session);
}
}

View File

@@ -19,9 +19,7 @@ package org.springframework.jms.support.converter;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
@@ -150,10 +148,9 @@ public class SimpleMessageConverter implements MessageConverter {
* @throws JMSException if thrown by JMS methods
* @see javax.jms.Session#createMapMessage
*/
protected MapMessage createMessageForMap(Map map, Session session) throws JMSException {
protected MapMessage createMessageForMap(Map<?, ?> map, Session session) throws JMSException {
MapMessage message = session.createMapMessage();
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
for (Map.Entry entry : map.entrySet()) {
if (!(entry.getKey() instanceof String)) {
throw new MessageConversionException("Cannot convert non-String key of type [" +
ObjectUtils.nullSafeClassName(entry.getKey()) + "] to JMS MapMessage entry");
@@ -205,7 +202,7 @@ public class SimpleMessageConverter implements MessageConverter {
* @throws JMSException if thrown by JMS methods
*/
protected Map extractMapFromMessage(MapMessage message) throws JMSException {
Map map = new HashMap();
Map<String, Object> map = new HashMap<String, Object>();
Enumeration en = message.getMapNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();