java.util.LinkedHashMapjava.util.concurrent.ConcurrentHashMap



Project netty/netty in file ...ain.java.io.netty.bootstrap.AbstractBootstrap.java (2013-01-21)
@@ -28,8 +28,8 @@ import io.netty.util.AttributeKey;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
-import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * {@link AbstractBootstrap} is a helper class that makes it easy to bootstrap a {@link Channel}. It support
@@ -40,8 +40,8 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
     private EventLoopGroup group;
     private ChannelFactory factory;
     private SocketAddress localAddress;
-    private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<ChannelOption<?>, Object>();
-    private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap<AttributeKey<?>, Object>();
+    private final Map<ChannelOption<?>, Object> options = new ConcurrentHashMap<ChannelOption<?>, Object>();
+    private final Map<AttributeKey<?>, Object> attrs = new ConcurrentHashMap<AttributeKey<?>, Object>();
     private ChannelHandler handler;
 
     /**
Project orientechnologies/orientdb in file ...e.impl.local.paginated.OLocalPaginatedStorage.java (2014-05-21)
@@ -23,7 +23,6 @@ import com.orientechnologies.common.io.OFileUtils;
 import com.orientechnologies.common.io.OIOUtils;
 import com.orientechnologies.common.log.OLogManager;
 import com.orientechnologies.common.parser.OSystemVariableResolver;
-import com.orientechnologies.common.util.OArrays;
 import com.orientechnologies.orient.core.Orient;
 import com.orientechnologies.orient.core.command.OCommandOutputListener;
 import com.orientechnologies.orient.core.config.OGlobalConfiguration;
@@ -81,11 +80,18 @@ import java.util.Arrays;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.*;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author Andrey Lomakin
@@ -1739,7 +1745,7 @@ public class OLocalPaginatedStorage extends OStorageLocalAbstract {
   }
 
   private void restoreFromBegging() throws IOException {
-    OLogManager.instance().info(this, "Date restore procedure is started.");
+    OLogManager.instance().info(this, "Data restore procedure is started.");
     OLogSequenceNumber lsn = writeAheadLog.begin();
 
     restoreFrom(lsn);
Project SpringSource/spring-framework in file ...tion.AnnotationMethodHandlerExceptionResolver.java (2010-10-31)
@@ -27,10 +27,11 @@ import java.security.Principal;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -82,6 +83,12 @@ import org.springframework.web.servlet.support.RequestContextUtils;
  */
 public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
 
+	// dummy method placeholder
+	private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis", null);
+	
+	private final Map<Class<?>, Map<Class<? extends Throwable>, Method>> exceptionHandlerCache = 
+			new ConcurrentHashMap<Class<?>, Map<Class<? extends Throwable>, Method>>(); 
+
 	private WebArgumentResolver[] customArgumentResolvers;
 
 	private HttpMessageConverter<?>[] messageConverters =
@@ -147,10 +154,24 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
 	 * @return the best matching method; or <code>null</code> if none is found
 	 */
 	private Method findBestExceptionHandlerMethod(Object handler, final Exception thrownException) {
-		final Class<?> handlerType = handler.getClass();
+		final Class<?> handlerType = ClassUtils.getUserClass(handler);
 		final Class<? extends Throwable> thrownExceptionType = thrownException.getClass();
-		final Map<Class<? extends Throwable>, Method> resolverMethods =
-				new LinkedHashMap<Class<? extends Throwable>, Method>();
+		Method handlerMethod = null;
+		
+		Map<Class<? extends Throwable>, Method> handlers = exceptionHandlerCache.get(handlerType);
+
+		if (handlers != null) {
+			handlerMethod = handlers.get(thrownExceptionType);
+			if (handlerMethod != null) {
+				return (handlerMethod == NO_METHOD_FOUND ? null : handlerMethod);
+			}
+		}
+		else {
+			handlers = new ConcurrentHashMap<Class<? extends Throwable>, Method>();
+			exceptionHandlerCache.put(handlerType, handlers);
+		}
+		
+		final Map<Class<? extends Throwable>, Method> resolverMethods = handlers;
 
 		ReflectionUtils.doWithMethods(handlerType, new ReflectionUtils.MethodCallback() {
 			public void doWith(Method method) {
@@ -174,7 +195,9 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
 			}
 		});
 
-		return getBestMatchingMethod(resolverMethods, thrownException);
+		handlerMethod = getBestMatchingMethod(resolverMethods, thrownException);
+		handlers.put(thrownExceptionType, (handlerMethod == null ? NO_METHOD_FOUND : handlerMethod));
+		return handlerMethod;
 	}
 
 	/**
Project SpringSource/spring-framework in file ...mework.jms.config.JmsListenerEndpointRegistry.java (2015-03-04)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 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.
@@ -18,8 +18,8 @@ package org.springframework.jms.config;
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.commons.logging.Log;
@@ -57,7 +57,7 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
 	protected final Log logger = LogFactory.getLog(getClass());
 
 	private final Map<String, MessageListenerContainer> listenerContainers =
-			new LinkedHashMap<String, MessageListenerContainer>();
+			new ConcurrentHashMap<String, MessageListenerContainer>();
 
 	private int phase = Integer.MAX_VALUE;
 
@@ -86,21 +86,43 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
 	 * Create a message listener container for the given {@link JmsListenerEndpoint}.
 	 * <p>This create the necessary infrastructure to honor that endpoint
 	 * with regards to its configuration.
+	 * <p>The {@code startImmediately} flag determines if the container should be
+	 * started immediately.
 	 * @param endpoint the endpoint to add
+	 * @param factory the listener factory to use
+	 * @param startImmediately start the container immediately if necessary
 	 * @see #getListenerContainers()
 	 * @see #getListenerContainer(String)
 	 */
-	public void registerListenerContainer(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory) {
+	public void registerListenerContainer(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory,
+			boolean startImmediately) {
+
 		Assert.notNull(endpoint, "Endpoint must not be null");
 		Assert.notNull(factory, "Factory must not be null");
 
 		String id = endpoint.getId();
 		Assert.notNull(id, "Endpoint id must not be null");
-		Assert.state(!this.listenerContainers.containsKey(id),
-				"Another endpoint is already registered with id '" + id + "'");
+		synchronized (this.listenerContainers) {
+			Assert.state(!this.listenerContainers.containsKey(id),
+					"Another endpoint is already registered with id '" + id + "'");
+			MessageListenerContainer container = createListenerContainer(endpoint, factory);
+			this.listenerContainers.put(id, container);
+			if (startImmediately) {
+				startIfNecessary(container);
+			}
+		}
+	}
 
-		MessageListenerContainer container = createListenerContainer(endpoint, factory);
-		this.listenerContainers.put(id, container);
+	/**
+	 * Create a message listener container for the given {@link JmsListenerEndpoint}.
+	 * <p>This create the necessary infrastructure to honor that endpoint
+	 * with regards to its configuration.
+	 * @param endpoint the endpoint to add
+	 * @param factory the listener factory to use
+	 * @see #registerListenerContainer(JmsListenerEndpoint, JmsListenerContainerFactory, boolean)
+	 */
+	public void registerListenerContainer(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory) {
+		registerListenerContainer(endpoint, factory, false);
 	}
 
 	/**
@@ -163,9 +185,7 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
 	@Override
 	public void start() {
 		for (MessageListenerContainer listenerContainer : getListenerContainers()) {
-			if (listenerContainer.isAutoStartup()) {
-				listenerContainer.start();
-			}
+			startIfNecessary(listenerContainer);
 		}
 	}
 
@@ -195,6 +215,17 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
 		return false;
 	}
 
+	/**
+	 * Start the specified {@link MessageListenerContainer} if it should be started
+	 * on startup.
+	 * @see MessageListenerContainer#isAutoStartup()
+	 */
+	private static void startIfNecessary(MessageListenerContainer listenerContainer) {
+		if (listenerContainer.isAutoStartup()) {
+			listenerContainer.start();
+		}
+	}
+
 
 	private static class AggregatingCallback implements Runnable {
 
Project spring-projects/spring-framework in file ...tion.AnnotationMethodHandlerExceptionResolver.java (2010-10-31)
@@ -27,10 +27,11 @@ import java.security.Principal;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -82,6 +83,12 @@ import org.springframework.web.servlet.support.RequestContextUtils;
  */
 public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
 
+	// dummy method placeholder
+	private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis", null);
+	
+	private final Map<Class<?>, Map<Class<? extends Throwable>, Method>> exceptionHandlerCache = 
+			new ConcurrentHashMap<Class<?>, Map<Class<? extends Throwable>, Method>>(); 
+
 	private WebArgumentResolver[] customArgumentResolvers;
 
 	private HttpMessageConverter<?>[] messageConverters =
@@ -147,10 +154,24 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
 	 * @return the best matching method; or <code>null</code> if none is found
 	 */
 	private Method findBestExceptionHandlerMethod(Object handler, final Exception thrownException) {
-		final Class<?> handlerType = handler.getClass();
+		final Class<?> handlerType = ClassUtils.getUserClass(handler);
 		final Class<? extends Throwable> thrownExceptionType = thrownException.getClass();
-		final Map<Class<? extends Throwable>, Method> resolverMethods =
-				new LinkedHashMap<Class<? extends Throwable>, Method>();
+		Method handlerMethod = null;
+		
+		Map<Class<? extends Throwable>, Method> handlers = exceptionHandlerCache.get(handlerType);
+
+		if (handlers != null) {
+			handlerMethod = handlers.get(thrownExceptionType);
+			if (handlerMethod != null) {
+				return (handlerMethod == NO_METHOD_FOUND ? null : handlerMethod);
+			}
+		}
+		else {
+			handlers = new ConcurrentHashMap<Class<? extends Throwable>, Method>();
+			exceptionHandlerCache.put(handlerType, handlers);
+		}
+		
+		final Map<Class<? extends Throwable>, Method> resolverMethods = handlers;
 
 		ReflectionUtils.doWithMethods(handlerType, new ReflectionUtils.MethodCallback() {
 			public void doWith(Method method) {
@@ -174,7 +195,9 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
 			}
 		});
 
-		return getBestMatchingMethod(resolverMethods, thrownException);
+		handlerMethod = getBestMatchingMethod(resolverMethods, thrownException);
+		handlers.put(thrownExceptionType, (handlerMethod == null ? NO_METHOD_FOUND : handlerMethod));
+		return handlerMethod;
 	}
 
 	/**