/** * Loads a class using the class loader. * 1. The class loader of the current class is being used. * 2. The thread context class loader is being used. * If both approaches fail, returns null. * * @param className The name of the class to load. * @return The class or null if no class loader could load the class. * @throws ClassNotFoundException if and only if no definition for the class with the specified name could be found. */ public static Class<?> loadClass(String className) throws ClassNotFoundException { return ClassLoadingUtils.loadClass(ClassLoadingUtils.class,className); }
/** * Loads a class using the class loader. * 1. The class loader of the current class is being used. * 2. The thread context class loader is being used. * If both approaches fail, returns null. * * @param className The name of the class to load. * @return The class or null if no class loader could load the class. * @throws ClassNotFoundException if and only if no definition for the class with the specified name could be found. */ public static Class<?> loadClass(String className) throws ClassNotFoundException { return ClassLoadingUtils.loadClass(ClassLoadingUtils.class,className); }
/** * Loads a class using the class loader. * 1. The class loader of the context class is being used. * 2. The thread context class loader is being used. * If both approaches fail, returns null. * * @param contextClass The name of a context class to use. * @param className The name of the class to load. * @return The class or null if no class loader could load the class. * @throws ClassNotFoundException Aif and only if no definition for the class with the specified name could be found. */ public static Class<?> loadClass(Class<?> contextClass, String className) throws ClassNotFoundException { Class<?> clazz = null; if (contextClass.getClassLoader() != null) { clazz = loadClass(className, contextClass.getClassLoader()); } if (clazz == null && Thread.currentThread().getContextClassLoader() != null) { clazz = loadClass(className, Thread.currentThread().getContextClassLoader()); } if (clazz == null) { throw new ClassNotFoundException("Failed to load class" + className); } return clazz; }
/** * Loads a class using the class loader. * 1. The class loader of the context class is being used. * 2. The thread context class loader is being used. * If both approaches fail, returns null. * * @param contextClass The name of a context class to use. * @param className The name of the class to load. * @return The class or null if no class loader could load the class. * @throws ClassNotFoundException Aif and only if no definition for the class with the specified name could be found. */ public static Class<?> loadClass(Class<?> contextClass, String className) throws ClassNotFoundException { Class<?> clazz = null; if (contextClass.getClassLoader() != null) { clazz = loadClass(className, contextClass.getClassLoader()); } if (clazz == null && Thread.currentThread().getContextClassLoader() != null) { clazz = loadClass(className, Thread.currentThread().getContextClassLoader()); } if (clazz == null) { throw new ClassNotFoundException("Failed to load class" + className); } return clazz; }
/** * Deserializes the object in the given data input using * available Hadoop serializations. * * @param conf Hadoop conf. * @param in data input stream where serialized content is read. * @param <T> object class type. * @param obj data object. * @param objClass object class type as String. * @return deserialized object. * @throws IOException occurred while deserializing the byte content. * @throws ClassNotFoundException class definition cannot be found for given class name. */ @SuppressWarnings("unchecked") public static<T> T deserialize(Configuration conf, DataInput in , T obj , String objClass) throws IOException, ClassNotFoundException { Class<T> c = (Class<T>) ClassLoadingUtils.loadClass(objClass); return deserialize(conf, in, obj, c); }
/** * Deserializes the object in the given data input using * available Hadoop serializations. * * @param conf Hadoop conf. * @param in data input stream where serialized content is read. * @param <T> object class type. * @param obj data object. * @param objClass object class type as String. * @return deserialized object. * @throws IOException occurred while deserializing the byte content. * @throws ClassNotFoundException class definition cannot be found for given class name. */ @SuppressWarnings("unchecked") public static<T> T deserialize(Configuration conf, DataInput in , T obj , String objClass) throws IOException, ClassNotFoundException { Class<T> c = (Class<T>) ClassLoadingUtils.loadClass(objClass); return deserialize(conf, in, obj, c); }
/** * Loads the object stored by {@link #storeToConf(Object, Configuration, String)} * method from the configuration under the given dataKey. * * @param conf the configuration to read from. * @param dataKey the key to get the data from. * @param <T> the given object class type. * @return the store object. * @throws IOException when failed retrieving the data given key from Hadoop conf. */ @SuppressWarnings("unchecked") public static<T> T loadFromConf(Configuration conf, String dataKey) throws IOException { String classKey = dataKey + "._class"; String className = conf.get(classKey); try { T obj = (T) DefaultStringifier.load(conf, dataKey, ClassLoadingUtils.loadClass(className)); return obj; } catch (Exception ex) { throw new IOException(ex); } }
/** * Loads the object stored by {@link #storeToConf(Object, Configuration, String)} * method from the configuration under the given dataKey. * * @param conf the configuration to read from. * @param dataKey the key to get the data from. * @param <T> the given object class type. * @return the store object. * @throws IOException when failed retrieving the data given key from Hadoop conf. */ @SuppressWarnings("unchecked") public static<T> T loadFromConf(Configuration conf, String dataKey) throws IOException { String classKey = dataKey + "._class"; String className = conf.get(classKey); try { T obj = (T) DefaultStringifier.load(conf, dataKey, ClassLoadingUtils.loadClass(className)); return obj; } catch (Exception ex) { throw new IOException(ex); } }
@SuppressWarnings("unchecked") public void readFields(DataInput in) { try { Class<K> keyClass = (Class<K>) ClassLoadingUtils.loadClass(Text.readString(in)); Class<T> persistentClass = (Class<T>)ClassLoadingUtils.loadClass(Text.readString(in)); Properties props = WritableUtils.readProperties(in); initialize(keyClass, persistentClass, props); } catch (ClassNotFoundException | IOException ex) { LOG.error(ex.getMessage(), ex); } }
/** * Constructs a new instance of the class using the no-arg constructor. * * @param classStr the class name of the object. * @return a new instance of the object. * @throws SecurityException if the caller's class loader is not the same as the * class loader of above class. * @throws IllegalArgumentException this will not be thrown since <code>field.get(obj)</code> passing obj is null. * since the field is a static class level variable inside the class. * @throws IllegalAccessException if the field is inaccessible due to java language access control. * @throws ClassNotFoundException class definition cannot be found for the class type. * @throws InstantiationException could not be instantiated from the given constructor. * @throws NoSuchMethodException default construct cannot by found for given class type. * @throws InvocationTargetException if the underlying constructor throws an exception. */ public static Object newInstance(String classStr) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, IllegalArgumentException, NoSuchMethodException, InvocationTargetException { if(classStr == null) { throw new IllegalArgumentException("class cannot be null"); } Class<?> clazz = ClassLoadingUtils.loadClass(classStr); return newInstance(clazz); }
/** * Constructs a new instance of the class using the no-arg constructor. * * @param classStr the class name of the object. * @return a new instance of the object. * @throws SecurityException if the caller's class loader is not the same as the * class loader of above class. * @throws IllegalArgumentException this will not be thrown since <code>field.get(obj)</code> passing obj is null. * since the field is a static class level variable inside the class. * @throws IllegalAccessException if the field is inaccessible due to java language access control. * @throws ClassNotFoundException class definition cannot be found for the class type. * @throws InstantiationException could not be instantiated from the given constructor. * @throws NoSuchMethodException default construct cannot by found for given class type. * @throws InvocationTargetException if the underlying constructor throws an exception. */ public static Object newInstance(String classStr) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, IllegalArgumentException, NoSuchMethodException, InvocationTargetException { if(classStr == null) { throw new IllegalArgumentException("class cannot be null"); } Class<?> clazz = ClassLoadingUtils.loadClass(classStr); return newInstance(clazz); }
@SuppressWarnings("unchecked") public void readFields(DataInput in) throws IOException { try { Class<K> keyClass = (Class<K>) ClassLoadingUtils.loadClass(Text.readString(in)); Class<T> persistentClass = (Class<T>)ClassLoadingUtils.loadClass(Text.readString(in)); Properties props = WritableUtils.readProperties(in); initialize(keyClass, persistentClass, props); } catch (ClassNotFoundException e) { LOG.error("ClassNotFoundException", e); throw new IOException(e); } }
/** * Instantiate a new {@link DataStore}. Uses default properties. Uses 'null' schema. * * @param dataStoreClass The datastore implementation class <i>as string</i>. * @param keyClass The key class <i>as string</i>. * @param persistentClass The value class <i>as string</i>. * @param auth an authentication {@link Object} to be used for communication. * @return A new store instance. * @throws GoraException */ @SuppressWarnings({ "unchecked" }) public static <K, T extends Persistent> DataStore<K, T> getDataStore( String dataStoreClass, String keyClass, String persistentClass, Object auth) throws GoraException { try { Class<? extends DataStore<K,T>> c = (Class<? extends DataStore<K, T>>) Class.forName(dataStoreClass); Class<K> k = (Class<K>) ClassLoadingUtils.loadClass(keyClass); Class<T> p = (Class<T>) ClassLoadingUtils.loadClass(persistentClass); return createDataStore(c, k, p, auth, createProps(), null); } catch(GoraException ex) { throw ex; } catch (Exception ex) { throw new GoraException(ex); } }
/** * Instantiate a new {@link DataStore}. Uses default properties. Uses 'null' schema. * * @param dataStoreClass The datastore implementation class <i>as string</i>. * @param keyClass The key class <i>as string</i>. * @param persistentClass The value class <i>as string</i>. * @param conf {@link Configuration} to be used be the store. * @return A new store instance. * @throws GoraException */ @SuppressWarnings({ "unchecked" }) public static <K, T extends Persistent> DataStore<K, T> getDataStore( String dataStoreClass, String keyClass, String persistentClass, Configuration conf) throws GoraException { try { Class<? extends DataStore<K,T>> c = (Class<? extends DataStore<K, T>>) Class.forName(dataStoreClass); Class<K> k = (Class<K>) ClassLoadingUtils.loadClass(keyClass); Class<T> p = (Class<T>) ClassLoadingUtils.loadClass(persistentClass); return createDataStore(c, k, p, conf, createProps(), null); } catch(GoraException ex) { throw ex; } catch (Exception ex) { throw new GoraException(ex); } }
/** * Instantiate a new {@link DataStore}. Uses default properties. Uses 'null' schema. * * @param dataStoreClass The datastore implementation class <i>as string</i>. * @param keyClass The key class <i>as string</i>. * @param persistentClass The value class <i>as string</i>. * @param auth an authentication {@link Object} to be used for communication. * @return A new store instance. * @throws GoraException */ @SuppressWarnings({ "unchecked" }) public static <K, T extends Persistent> DataStore<K, T> getDataStore( String dataStoreClass, String keyClass, String persistentClass, Object auth) throws GoraException { try { Class<? extends DataStore<K,T>> c = (Class<? extends DataStore<K, T>>) Class.forName(dataStoreClass); Class<K> k = (Class<K>) ClassLoadingUtils.loadClass(keyClass); Class<T> p = (Class<T>) ClassLoadingUtils.loadClass(persistentClass); return createDataStore(c, k, p, auth, createProps(), null); } catch(GoraException ex) { throw ex; } catch (Exception ex) { throw new GoraException(ex); } }
/** * Instantiate a new {@link DataStore}. Uses default properties. Uses 'null' schema. * * @param dataStoreClass The datastore implementation class <i>as string</i>. * @param keyClass The key class <i>as string</i>. * @param persistentClass The value class <i>as string</i>. * @param conf {@link Configuration} to be used be the store. * @return A new store instance. * @throws GoraException */ @SuppressWarnings({ "unchecked" }) public static <K, T extends Persistent> DataStore<K, T> getDataStore( String dataStoreClass, String keyClass, String persistentClass, Configuration conf) throws GoraException { try { Class<? extends DataStore<K,T>> c = (Class<? extends DataStore<K, T>>) Class.forName(dataStoreClass); Class<K> k = (Class<K>) ClassLoadingUtils.loadClass(keyClass); Class<T> p = (Class<T>) ClassLoadingUtils.loadClass(persistentClass); return createDataStore(c, k, p, conf, createProps(), null); } catch(GoraException ex) { throw ex; } catch (Exception ex) { throw new GoraException(ex); } }
/** * Deserializes the object in the given data input using * available Hadoop serializations. * * @param conf Hadoop conf. * @param in data input stream where serialized content is read. * @param <T> object class type. * @param obj data object. * @throws IOException occurred while deserializing the byte content. * @throws ClassNotFoundException class definition cannot be found for given class name. * @return deserialized object. */ @SuppressWarnings("unchecked") public static<T> T deserialize(Configuration conf, DataInput in , T obj) throws IOException, ClassNotFoundException { String clazz = Text.readString(in); Class<T> c = (Class<T>)ClassLoadingUtils.loadClass(clazz); return deserialize(conf, in, obj, c); }
/** * Deserializes the object in the given data input using * available Hadoop serializations. * * @param conf Hadoop conf. * @param in data input stream where serialized content is read. * @param <T> object class type. * @param obj data object. * @throws IOException occurred while deserializing the byte content. * @throws ClassNotFoundException class definition cannot be found for given class name. * @return deserialized object. */ @SuppressWarnings("unchecked") public static<T> T deserialize(Configuration conf, DataInput in , T obj) throws IOException, ClassNotFoundException { String clazz = Text.readString(in); Class<T> c = (Class<T>)ClassLoadingUtils.loadClass(clazz); return deserialize(conf, in, obj, c); }
private Object fromCouchDBRecord(final Schema fieldSchema, final String docf, final Object value) { final Object innerValue = ((Map) value).get(docf); if (innerValue == null) { return null; } Class<?> clazz = null; try { clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName()); } catch (ClassNotFoundException e) { LOG.debug(e.getMessage()); } final PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent(); for (Field recField : fieldSchema.getFields()) { Schema innerSchema = recField.schema(); record.put(recField.pos(), fromDBObject(innerSchema, recField, recField.name(), innerValue)); } return record; }
private Object fromCouchDBRecord(final Schema fieldSchema, final String docf, final Object value) throws GoraException { final Object innerValue = ((Map) value).get(docf); if (innerValue == null) { return null; } Class<?> clazz = null; try { clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName()); } catch (ClassNotFoundException e) { throw new GoraException(e) ; } final PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent(); for (Field recField : fieldSchema.getFields()) { Schema innerSchema = recField.schema(); record.put(recField.pos(), fromDBObject(innerSchema, recField, recField.name(), innerValue)); } return record; }