public static boolean isClassAvailable(final ClassLoader classLoader, final String className) { try { Class<?> clazz = loadClass(classLoader, className); return clazz != null; } catch (ClassNotFoundException e) { return false; } }
private CachingProvider createClientProvider() { try { return ClassLoaderUtil.newInstance(getClass().getClassLoader(), CLIENT_CACHING_PROVIDER_CLASS); } catch (Exception e) { LOGGER.finest("Could not load client CachingProvider! Fallback to server one... " + e.toString()); } return null; }
public static Class<?>[] getAllInterfaces(Class<?> clazz) { Collection<Class<?>> interfaces = new HashSet<Class<?>>(); addOwnInterfaces(clazz, interfaces); addInterfacesOfSuperclasses(clazz, interfaces); return interfaces.toArray(new Class<?>[0]); }
private static void addInterfacesOfSuperclasses(Class<?> clazz, Collection<Class<?>> interfaces) { Class<?> superClass = clazz.getSuperclass(); while (superClass != null) { addOwnInterfaces(superClass, interfaces); superClass = superClass.getSuperclass(); } }
private static boolean isJCacheAvailableInternal(ClassLoader classLoader, ILogger logger) { if (!ClassLoaderUtil.isClassAvailable(classLoader, JCACHE_CACHING_CLASSNAME)) { // no cache-api jar in the classpath return false; } for (String className : JCACHE_ADDITIONAL_REQUIRED_CLASSES) { if (!ClassLoaderUtil.isClassAvailable(classLoader, className)) { if (logger != null) { logger.warning("An outdated version of JCache API was located in the classpath, please use newer versions of " + "JCache API rather than 1.0.0-PFD or 0.x versions."); } return false; } } return true; } }
/** * Adds the JDK build-in JavaScript engine into the given list of scripting engine factories. * * @param factoryCandidates List of scripting engine factories */ private void addJavaScriptEngine(List<String> factoryCandidates) { // Add default script engine manager factoryCandidates.add(OSGiScriptEngineFactory.class.getName()); // Rhino is available in java < 8, Nashorn is available in java >= 8 if (ClassLoaderUtil.isClassDefined(RHINO_SCRIPT_ENGINE_FACTORY)) { factoryCandidates.add(RHINO_SCRIPT_ENGINE_FACTORY); } else if (ClassLoaderUtil.isClassDefined(NASHORN_SCRIPT_ENGINE_FACTORY)) { factoryCandidates.add(NASHORN_SCRIPT_ENGINE_FACTORY); } else { logger.warning("No built-in JavaScript ScriptEngineFactory found."); } }
private WanReplicationEndpoint[] createPublishers(WanReplicationConfig wanReplicationConfig, List<WanPublisherConfig> publisherConfigs) { WanReplicationEndpoint[] targetEndpoints = new WanReplicationEndpoint[publisherConfigs.size()]; int count = 0; for (WanPublisherConfig publisherConfig : publisherConfigs) { final WanReplicationEndpoint target = getOrCreate((WanReplicationEndpoint) publisherConfig.getImplementation(), node.getConfigClassLoader(), publisherConfig.getClassName()); if (target == null) { throw new InvalidConfigurationException("Either \'implementation\' or \'className\' " + "attribute need to be set in WanPublisherConfig"); } target.init(node, wanReplicationConfig, publisherConfig); targetEndpoints[count++] = target; } return targetEndpoints; }
private void onNonAssignableClass(String className, Class candidate) { if (expectedType.isInterface()) { if (ClassLoaderUtil.implementsInterfaceWithSameName(candidate, expectedType)) { // this can happen in application containers - different Hazelcast JARs are loaded // by different classloaders. LOGGER.fine("There appears to be a classloading conflict. " + "Class " + className + " loaded by " + candidate.getClassLoader() + " implements " + expectedType.getName() + " from its own class loader, but it does not implement " + expectedType.getName() + " loaded by " + expectedType.getClassLoader()); } else { //the class does not implement interface with the expected name. LOGGER.fine("There appears to be a classloading conflict. " + "Class " + className + " loaded by " + candidate.getClassLoader() + " does not " + "implement an interface with name " + expectedType.getName() + " in both class loaders." + "the interface currently loaded by " + expectedType.getClassLoader()); } } }
private void registerSerializerHooks(InternalSerializationService ss) { SerializerHookLoader serializerHookLoader = new SerializerHookLoader(config, classLoader); Map<Class, Object> serializers = serializerHookLoader.getSerializers(); for (Map.Entry<Class, Object> entry : serializers.entrySet()) { Class serializationType = entry.getKey(); Object value = entry.getValue(); Serializer serializer; if (value instanceof SerializerHook) { serializer = ((SerializerHook) value).createSerializer(); } else { serializer = (Serializer) value; } if (value instanceof HazelcastInstanceAware) { ((HazelcastInstanceAware) value).setHazelcastInstance(hazelcastInstance); } if (ClassLoaderUtil.isInternalType(value.getClass())) { ((AbstractSerializationService) ss).safeRegister(serializationType, serializer); } else { ((AbstractSerializationService) ss).register(serializationType, serializer); } } }
/** * Check whether given class implements an interface with the same name. * It returns true even when the implemented interface is loaded by a different * classloader and hence the class is not assignable into it. * * An interface is considered as implemented when either: * <ul> * <li>The class directly implements the interface</li> * <li>The class implements an interface which extends the original interface</li> * <li>One of superclasses directly implements the interface</li> * <li>One of superclasses implements an interface which extends the original interface</li> * </ul> * * This is useful for logging purposes. * * @param clazz class to check whether implements the interface * @param iface interface to be implemented * @return <code>true</code> when the class implements the inteface with the same name */ public static boolean implementsInterfaceWithSameName(Class<?> clazz, Class<?> iface) { Class<?>[] interfaces = getAllInterfaces(clazz); for (Class implementedInterface : interfaces) { if (implementedInterface.getName().equals(iface.getName())) { return true; } } return false; }
private static void addOwnInterfaces(Class<?> clazz, Collection<Class<?>> allInterfaces) { Class<?>[] interfaces = clazz.getInterfaces(); Collections.addAll(allInterfaces, interfaces); for (Class cl : interfaces) { addOwnInterfaces(cl, allInterfaces); } }
private static boolean isJCacheAvailableInternal(ClassLoader classLoader, ILogger logger) { if (!ClassLoaderUtil.isClassAvailable(classLoader, JCACHE_CACHING_CLASSNAME)) { // no cache-api jar in the classpath return false; } for (String className : JCACHE_ADDITIONAL_REQUIRED_CLASSES) { if (!ClassLoaderUtil.isClassAvailable(classLoader, className)) { if (logger != null) { logger.warning("An outdated version of JCache API was located in the classpath, please use newer versions of " + "JCache API rather than 1.0.0-PFD or 0.x versions."); } return false; } } return true; } }
/** * Adds the JDK build-in JavaScript engine into the given list of scripting engine factories. * * @param factoryCandidates List of scripting engine factories */ private void addJavaScriptEngine(List<String> factoryCandidates) { // Add default script engine manager factoryCandidates.add(OSGiScriptEngineFactory.class.getName()); // Rhino is available in java < 8, Nashorn is available in java >= 8 if (ClassLoaderUtil.isClassDefined(RHINO_SCRIPT_ENGINE_FACTORY)) { factoryCandidates.add(RHINO_SCRIPT_ENGINE_FACTORY); } else if (ClassLoaderUtil.isClassDefined(NASHORN_SCRIPT_ENGINE_FACTORY)) { factoryCandidates.add(NASHORN_SCRIPT_ENGINE_FACTORY); } else { logger.warning("No built-in JavaScript ScriptEngineFactory found."); } }
private WanReplicationEndpoint[] createPublishers(WanReplicationConfig wanReplicationConfig, List<WanPublisherConfig> publisherConfigs) { WanReplicationEndpoint[] targetEndpoints = new WanReplicationEndpoint[publisherConfigs.size()]; int count = 0; for (WanPublisherConfig publisherConfig : publisherConfigs) { final WanReplicationEndpoint target = getOrCreate((WanReplicationEndpoint) publisherConfig.getImplementation(), node.getConfigClassLoader(), publisherConfig.getClassName()); if (target == null) { throw new InvalidConfigurationException("Either \'implementation\' or \'className\' " + "attribute need to be set in WanPublisherConfig"); } target.init(node, wanReplicationConfig, publisherConfig); targetEndpoints[count++] = target; } return targetEndpoints; }
private void onNonAssignableClass(String className, Class candidate) { if (expectedType.isInterface()) { if (ClassLoaderUtil.implementsInterfaceWithSameName(candidate, expectedType)) { // this can happen in application containers - different Hazelcast JARs are loaded // by different classloaders. LOGGER.fine("There appears to be a classloading conflict. " + "Class " + className + " loaded by " + candidate.getClassLoader() + " implements " + expectedType.getName() + " from its own class loader, but it does not implement " + expectedType.getName() + " loaded by " + expectedType.getClassLoader()); } else { //the class does not implement interface with the expected name. LOGGER.fine("There appears to be a classloading conflict. " + "Class " + className + " loaded by " + candidate.getClassLoader() + " does not " + "implement an interface with name " + expectedType.getName() + " in both class loaders." + "the interface currently loaded by " + expectedType.getClassLoader()); } } }
private void registerSerializerHooks(InternalSerializationService ss) { SerializerHookLoader serializerHookLoader = new SerializerHookLoader(config, classLoader); Map<Class, Object> serializers = serializerHookLoader.getSerializers(); for (Map.Entry<Class, Object> entry : serializers.entrySet()) { Class serializationType = entry.getKey(); Object value = entry.getValue(); Serializer serializer; if (value instanceof SerializerHook) { serializer = ((SerializerHook) value).createSerializer(); } else { serializer = (Serializer) value; } if (value instanceof HazelcastInstanceAware) { ((HazelcastInstanceAware) value).setHazelcastInstance(hazelcastInstance); } if (ClassLoaderUtil.isInternalType(value.getClass())) { ((AbstractSerializationService) ss).safeRegister(serializationType, serializer); } else { ((AbstractSerializationService) ss).register(serializationType, serializer); } } }
/** * Check whether given class implements an interface with the same name. * It returns true even when the implemented interface is loaded by a different * classloader and hence the class is not assignable into it. * * An interface is considered as implemented when either: * <ul> * <li>The class directly implements the interface</li> * <li>The class implements an interface which extends the original interface</li> * <li>One of superclasses directly implements the interface</li> * <li>One of superclasses implements an interface which extends the original interface</li> * </ul> * * This is useful for logging purposes. * * @param clazz class to check whether implements the interface * @param iface interface to be implemented * @return <code>true</code> when the class implements the inteface with the same name */ public static boolean implementsInterfaceWithSameName(Class<?> clazz, Class<?> iface) { Class<?>[] interfaces = getAllInterfaces(clazz); for (Class implementedInterface : interfaces) { if (implementedInterface.getName().equals(iface.getName())) { return true; } } return false; }
private void readObject(ObjectInputStream in) throws Exception { index = in.readInt(); split = ClassLoaderUtil.newInstance(Thread.currentThread().getContextClassLoader(), in.readUTF()); split.readFields(in); }
public static boolean isClassAvailable(final ClassLoader classLoader, final String className) { try { Class<?> clazz = loadClass(classLoader, className); return clazz != null; } catch (ClassNotFoundException e) { return false; } }
public static Class<?>[] getAllInterfaces(Class<?> clazz) { Collection<Class<?>> interfaces = new HashSet<Class<?>>(); addOwnInterfaces(clazz, interfaces); addInterfacesOfSuperclasses(clazz, interfaces); return interfaces.toArray(new Class<?>[0]); }