private static <T> T createInstance(String className, ClassLoader clsLoader, Class<T> baseClass) { if (StringUtils.isEmpty(className)) { return null; } else { return Reflections.createInstance(className, baseClass, clsLoader); } } }
public static Object createInstance(String userClassName, java.io.File jar) { try { return createInstance(userClassName, loadJar(jar)); } catch (Exception ex) { return null; } }
throw new ClassNotFoundException(className); } else if (isPrimitive(className)) { return (Class)PRIMITIVE_NAME_TYPE_MAP.get(className); } else if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') { Class componentType = loadClass(className.substring(arrayDimension), classLoader); return Array.newInstance(componentType, new int[arrayDimension]).getClass();
/** * Check if a class is in a jar * @param jar location of the jar * @param fqcn fully qualified class name to search for in jar * @return true if class can be loaded from jar and false if otherwise */ public static boolean classExistsInJar(java.io.File jar, String fqcn) { try { java.net.URLClassLoader loader = (URLClassLoader) loadJar(jar); Class.forName(fqcn, false, loader); loader.close(); return true; } catch (ClassNotFoundException | IOException e) { return false; } }
@SuppressWarnings("unchecked") @VisibleForTesting Schema<T> initializeSchema() throws ClassNotFoundException { if (StringUtils.isEmpty(this.pulsarSinkConfig.getTypeClassName())) { return (Schema<T>) Schema.BYTES; } Class<?> typeArg = Reflections.loadClass(this.pulsarSinkConfig.getTypeClassName(), Thread.currentThread().getContextClassLoader()); if (Void.class.equals(typeArg)) { // return type is 'void', so there's no schema to check return null; } if (!StringUtils.isEmpty(pulsarSinkConfig.getSchemaType())) { return (Schema<T>) topicSchema.getSchema(pulsarSinkConfig.getTopic(), typeArg, pulsarSinkConfig.getSchemaType(), false); } else { return (Schema<T>) topicSchema.getSchema(pulsarSinkConfig.getTopic(), typeArg, pulsarSinkConfig.getSerdeClassName(), false); } } }
/** * check if a class implements an interface * @param fqcn fully qualified class name to search for in jar * @param xface interface to check if implement * @return true if class from jar implements interface xface and false if otherwise */ public static boolean classInJarImplementsIface(java.io.File jar, String fqcn, Class xface) { boolean ret = false; try { java.net.URLClassLoader loader = (URLClassLoader) loadJar(jar); if (xface.isAssignableFrom(Class.forName(fqcn, false, loader))){ ret = true; } loader.close(); } catch (ClassNotFoundException | IOException e) { throw new RuntimeException(e); } return ret; }
@SuppressWarnings("unchecked") @VisibleForTesting Map<String, ConsumerConfig<T>> setupConsumerConfigs() throws ClassNotFoundException { Map<String, ConsumerConfig<T>> configs = new TreeMap<>(); Class<?> typeArg = Reflections.loadClass(this.pulsarSourceConfig.getTypeClassName(), Thread.currentThread().getContextClassLoader()); checkArgument(!Void.class.equals(typeArg), "Input type of Pulsar Function cannot be Void"); // Check new config with schema types or classnames pulsarSourceConfig.getTopicSchema().forEach((topic, conf) -> { Schema<T> schema; if (conf.getSerdeClassName() != null && !conf.getSerdeClassName().isEmpty()) { schema = (Schema<T>) topicSchema.getSchema(topic, typeArg, conf.getSerdeClassName(), true); } else { schema = (Schema<T>) topicSchema.getSchema(topic, typeArg, conf.getSchemaType(), true); } configs.put(topic, ConsumerConfig.<T> builder().schema(schema).isRegexPattern(conf.isRegexPattern()).build()); }); return configs; }
public static Class<?> getSinkType(String className, ClassLoader classLoader) { Object userClass = Reflections.createInstance(className, classLoader); Class<?> typeArg; Sink sink = (Sink) userClass; if (sink == null) { throw new IllegalArgumentException(String.format("The Pulsar sink class %s could not be instantiated", className)); } typeArg = TypeResolver.resolveRawArgument(Sink.class, sink.getClass()); return typeArg; }
File file = new File(jarFilePath); try { classLoader = Reflections.loadJar(file); } catch (MalformedURLException e) { throw new ParameterException(
public static Class<?> getSourceType(String className, ClassLoader classloader) { Object userClass = Reflections.createInstance(className, classloader); Class<?> typeArg; Source source = (Source) userClass; if (source == null) { throw new IllegalArgumentException(String.format("The Pulsar source class %s could not be instantiated", className)); } typeArg = TypeResolver.resolveRawArgument(Source.class, source.getClass()); return typeArg; }
private static void validateSerDeType(String serdeClassName, Class<?> typeArg, ClassLoader clsLoader) { SerDe<?> serDe = (SerDe<?>) Reflections.createInstance(serdeClassName, clsLoader); if (serDe == null) { throw new IllegalArgumentException(String.format("The SerDe class %s does not exist", serdeClassName)); } Class<?>[] serDeTypes = TypeResolver.resolveRawArguments(SerDe.class, serDe.getClass()); // type inheritance information seems to be lost in generic type // load the actual type class for verification Class<?> fnInputClass; Class<?> serdeInputClass; try { fnInputClass = Class.forName(typeArg.getName(), true, clsLoader); serdeInputClass = Class.forName(serDeTypes[0].getName(), true, clsLoader); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Failed to load type class", e); } if (!fnInputClass.isAssignableFrom(serdeInputClass)) { throw new IllegalArgumentException( "Serializer type mismatch " + typeArg + " vs " + serDeTypes[0]); } }
private java.util.function.Function<Collection<I>, O> intializeUserFunction(WindowConfig windowConfig) { String actualWindowFunctionClassName = windowConfig.getActualWindowFunctionClassName(); ClassLoader clsLoader = Thread.currentThread().getContextClassLoader(); Object userClassObject = Reflections.createInstance( actualWindowFunctionClassName, clsLoader); if (userClassObject instanceof java.util.function.Function) { Class<?>[] typeArgs = TypeResolver.resolveRawArguments( java.util.function.Function.class, userClassObject.getClass()); if (typeArgs[0].equals(Collection.class)) { return (java.util.function.Function) userClassObject; } else { throw new IllegalArgumentException("Window function must take a collection as input"); } } else { throw new IllegalArgumentException("Window function does not implement the correct interface"); } }
private static void validateCustomSchemaType(String schemaClassName, Class<?> typeArg, ClassLoader clsLoader, boolean input) { Schema<?> schema = (Schema<?>) Reflections.createInstance(schemaClassName, clsLoader); if (schema == null) { throw new IllegalArgumentException(String.format("The Schema class %s does not exist",
SerDe serDe = (SerDe) Reflections.createInstance(inputSerializer, clsLoader); if (serDe == null) { throw new IllegalArgumentException(String.format("The SerDe class %s does not exist",
object = Reflections.createInstance( sinkSpec.getClassName(), Thread.currentThread().getContextClassLoader());
/** * NOTE: this method should be called in the instance thread, in order to make class loading work. */ JavaInstance setupJavaInstance(ContextImpl contextImpl) throws Exception { // initialize the thread context ThreadContext.put("function", FunctionDetailsUtils.getFullyQualifiedName(instanceConfig.getFunctionDetails())); ThreadContext.put("functionname", instanceConfig.getFunctionDetails().getName()); ThreadContext.put("instance", instanceConfig.getInstanceName()); log.info("Starting Java Instance {} : \n Details = {}", instanceConfig.getFunctionDetails().getName(), instanceConfig.getFunctionDetails()); // start the function thread loadJars(); ClassLoader clsLoader = Thread.currentThread().getContextClassLoader(); Object object = Reflections.createInstance( instanceConfig.getFunctionDetails().getClassName(), clsLoader); if (!(object instanceof Function) && !(object instanceof java.util.function.Function)) { throw new RuntimeException("User class must either be Function or java.util.Function"); } // start the state table setupStateTable(); // start the output producer setupOutput(contextImpl); // start the input consumer setupInput(contextImpl); // start any log topic handler setupLogHandler(); return new JavaInstance(contextImpl, object); }
FunctionDetailsUtils.getFullyQualifiedName(this.instanceConfig.getFunctionDetails())); } else { object = Reflections.createInstance( sourceSpec.getClassName(), Thread.currentThread().getContextClassLoader());