Skip to content

Commit

Permalink
Merge pull request #6012 from sancar/fix/jcacheExceptionsProtocol
Browse files Browse the repository at this point in the history
Load jcache exceptions only when jcache is avaliable
  • Loading branch information
mustafa sancar koyunlu committed Aug 20, 2015
2 parents a7452e4 + 6a23b06 commit 0d9f846
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 29 deletions.
Expand Up @@ -31,6 +31,7 @@
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.ClassLoaderUtil;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.spi.exception.TargetDisconnectedException;
Expand Down Expand Up @@ -66,7 +67,7 @@ abstract class ClientInvocationServiceSupport implements ClientInvocationService
private final ConcurrentMap<Integer, ClientListenerInvocation> eventHandlerMap
= new ConcurrentHashMap<Integer, ClientListenerInvocation>();
private final AtomicInteger callIdIncrementer = new AtomicInteger();
private final ClientExceptionFactory clientExceptionFactory = new ClientExceptionFactory();
private final ClientExceptionFactory clientExceptionFactory;
private volatile boolean isShutdown;


Expand All @@ -77,11 +78,18 @@ public ClientInvocationServiceSupport(HazelcastClientInstanceImpl client) {
connectionManager.addConnectionListener(this);
connectionManager.addConnectionHeartbeatListener(this);
this.partitionService = client.getClientPartitionService();
this.clientExceptionFactory = initClientExceptionFactory();
responseThread = new ResponseThread(client.getThreadGroup(), client.getName() + ".response-",
client.getClientConfig().getClassLoader());
responseThread.start();
}

private ClientExceptionFactory initClientExceptionFactory() {
ClassLoader classLoader = client.getClientConfig().getClassLoader();
boolean jcacheAvailable = ClassLoaderUtil.isClassAvailable(classLoader, "javax.cache.Caching");
return new ClientExceptionFactory(jcacheAvailable);
}

@Override
public boolean isRedoOperation() {
return client.getClientConfig().getNetworkConfig().isRedoOperation();
Expand Down
Expand Up @@ -41,6 +41,7 @@
import com.hazelcast.instance.Node;
import com.hazelcast.logging.ILogger;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.ClassLoaderUtil;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.nio.Packet;
Expand Down Expand Up @@ -116,7 +117,7 @@ public class ClientEngineImpl implements ClientEngine, CoreService, PostJoinAwar
private final ConnectionListener connectionListener = new ConnectionListenerImpl();

private final MessageTaskFactory messageTaskFactory;
private final ClientExceptionFactory clientExceptionFactory = new ClientExceptionFactory();
private final ClientExceptionFactory clientExceptionFactory;

public ClientEngineImpl(Node node) {
this.logger = node.getLogger(ClientEngine.class);
Expand All @@ -126,12 +127,19 @@ public ClientEngineImpl(Node node) {
this.endpointManager = new ClientEndpointManagerImpl(this, nodeEngine);
this.executor = newExecutor();
this.messageTaskFactory = node.getNodeExtension().createMessageTaskFactory(node);
this.clientExceptionFactory = initClientExceptionFactory();

ClientHeartbeatMonitor heartBeatMonitor = new ClientHeartbeatMonitor(
endpointManager, this, nodeEngine.getExecutionService(), node.groupProperties);
heartBeatMonitor.start();
}

private ClientExceptionFactory initClientExceptionFactory() {
ClassLoader classLoader = nodeEngine.getConfigClassLoader();
boolean jcacheAvailable = ClassLoaderUtil.isClassAvailable(classLoader, "javax.cache.Caching");
return new ClientExceptionFactory(jcacheAvailable);
}

private Executor newExecutor() {
final ExecutionService executionService = nodeEngine.getExecutionService();
int coreSize = Runtime.getRuntime().availableProcessors();
Expand Down
Expand Up @@ -79,10 +79,38 @@
*/
public class ClientExceptionFactory {

private Map<Class, Integer> classToInt = new HashMap<Class, Integer>();
private Map<Integer, ExceptionFactory> intToFactory = new HashMap<Integer, ExceptionFactory>();
private final Map<Class, Integer> classToInt = new HashMap<Class, Integer>();
private final Map<Integer, ExceptionFactory> intToFactory = new HashMap<Integer, ExceptionFactory>();

public ClientExceptionFactory(boolean jcacheAvailable) {
if (jcacheAvailable) {
register(ClientProtocolErrorCodes.CACHE, CacheException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new CacheException(message, cause);
}
});
register(ClientProtocolErrorCodes.CACHE_LOADER, CacheLoaderException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new CacheLoaderException(message, cause);
}
});
register(ClientProtocolErrorCodes.CACHE_WRITER, CacheWriterException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new CacheWriterException(message, cause);
}
});

register(ClientProtocolErrorCodes.ENTRY_PROCESSOR, EntryProcessorException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new EntryProcessorException(message, cause);
}
});
}

public ClientExceptionFactory() {
register(ClientProtocolErrorCodes.ARRAY_INDEX_OUT_OF_BOUNDS, ArrayIndexOutOfBoundsException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
Expand All @@ -101,30 +129,12 @@ public Throwable createException(String message, Throwable cause) {
return new AuthenticationException(message);
}
});
register(ClientProtocolErrorCodes.CACHE, CacheException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new CacheException(message, cause);
}
});
register(ClientProtocolErrorCodes.CACHE_LOADER, CacheLoaderException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new CacheLoaderException(message, cause);
}
});
register(ClientProtocolErrorCodes.CACHE_NOT_EXISTS, CacheNotExistsException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new CacheNotExistsException(message);
}
});
register(ClientProtocolErrorCodes.CACHE_WRITER, CacheWriterException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new CacheWriterException(message, cause);
}
});
register(ClientProtocolErrorCodes.CALLER_NOT_MEMBER, CallerNotMemberException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
Expand Down Expand Up @@ -185,12 +195,6 @@ public Throwable createException(String message, Throwable cause) {
return new EOFException(message);
}
});
register(ClientProtocolErrorCodes.ENTRY_PROCESSOR, EntryProcessorException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
return new EntryProcessorException(message, cause);
}
});
register(ClientProtocolErrorCodes.EXECUTION, ExecutionException.class, new ExceptionFactory() {
@Override
public Throwable createException(String message, Throwable cause) {
Expand Down
12 changes: 12 additions & 0 deletions hazelcast/src/main/java/com/hazelcast/nio/ClassLoaderUtil.java
Expand Up @@ -17,6 +17,7 @@
package com.hazelcast.nio;

import com.hazelcast.util.ConcurrentReferenceHashMap;
import com.hazelcast.util.EmptyStatement;

import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -116,6 +117,17 @@ public static Class<?> loadClass(final ClassLoader classLoader, final String cla
return Class.forName(className);
}

public static boolean isClassAvailable(final ClassLoader classLoader, final String className) {
try {
Class<?> clazz = loadClass(classLoader, className);
return clazz != null;
} catch (ClassNotFoundException e) {
EmptyStatement.ignore(e);
}
return false;

}

private static Class<?> tryLoadClass(String className, ClassLoader classLoader)
throws ClassNotFoundException {

Expand Down

0 comments on commit 0d9f846

Please sign in to comment.