Example usage for org.apache.commons.lang ClassUtils getClass

List of usage examples for org.apache.commons.lang ClassUtils getClass

Introduction

In this page you can find the example usage for org.apache.commons.lang ClassUtils getClass.

Prototype

public static Class<?> getClass(String className) throws ClassNotFoundException 

Source Link

Document

Returns the (initialized) class represented by className using the current thread's context class loader.

Usage

From source file:org.jamwiki.servlets.ServletUtil.java

/**
 * Validate that vital system properties, such as database connection
 * settings, have been specified properly.
 * //from w w w  .j av a 2 s.c  om
 * @param props
 *          The property object to validate against.
 * @return A list of WikiMessage objects containing any errors encountered, or
 *         an empty list if no errors are encountered.
 */
protected static List<WikiMessage> validateSystemSettings(Properties props) {
    List<WikiMessage> errors = new ArrayList<WikiMessage>();
    // test directory permissions & existence
    // WikiMessage baseDirError = WikiUtil.validateDirectory(props
    // .getProperty(Environment.PROP_BASE_FILE_DIR));
    // if (baseDirError != null) {
    // errors.add(baseDirError);
    // }
    // WikiMessage fullDirError = WikiUtil.validateDirectory(props
    // .getProperty(Environment.PROP_FILE_DIR_FULL_PATH));
    // if (fullDirError != null) {
    // errors.add(fullDirError);
    // }
    // String classesDir = null;
    // try {
    // classesDir = Utilities.getClassLoaderRoot().getPath();
    // WikiMessage classesDirError = WikiUtil.validateDirectory(classesDir);
    // if (classesDirError != null) {
    // errors.add(classesDirError);
    // }
    // } catch (FileNotFoundException e) {
    // errors.add(new WikiMessage("error.directorywrite", classesDir, e
    // .getMessage()));
    // }

    // test database
    // String driver = props.getProperty(Environment.PROP_DB_DRIVER);
    // String url = props.getProperty(Environment.PROP_DB_URL);
    // String userName = props.getProperty(Environment.PROP_DB_USERNAME);
    // String password = Encryption.getEncryptedProperty(
    // Environment.PROP_DB_PASSWORD, props);
    // try {
    // DatabaseConnection.testDatabase(driver, url, userName, password, false);
    // } catch (ClassNotFoundException e) {
    // logger.severe("Invalid database settings", e);
    // errors.add(new WikiMessage("error.databaseconnection", e.getMessage()));
    // } catch (SQLException e) {
    // logger.severe("Invalid database settings", e);
    // errors.add(new WikiMessage("error.databaseconnection", e.getMessage()));
    // }

    // verify valid parser class
    String parserClass = props.getProperty(Environment.PROP_PARSER_CLASS);
    String abstractParserClass = "org.jamwiki.parser.AbstractParser";
    boolean validParser = (parserClass != null && !parserClass.equals(abstractParserClass));
    if (validParser) {
        try {
            Class parent = ClassUtils.getClass(parserClass);
            Class child = ClassUtils.getClass(abstractParserClass);
            if (!child.isAssignableFrom(parent)) {
                validParser = false;
            }
        } catch (ClassNotFoundException e) {
            validParser = false;
        }
    }
    if (!validParser) {
        errors.add(new WikiMessage("error.parserclass", parserClass));
    }
    return errors;
}

From source file:org.jamwiki.utils.Utilities.java

/**
 * Given a String representation of a class name (for example, org.jamwiki.db.AnsiDataHandler)
 * return an instance of the class.  The constructor for the class being instantiated must
 * not take any arguments./* www.j  av a  2 s.  c  o m*/
 *
 * @param className The name of the class being instantiated.
 * @return A Java Object representing an instance of the specified class.
 */
public static Object instantiateClass(String className) {
    if (StringUtils.isBlank(className)) {
        throw new IllegalArgumentException("Cannot call instantiateClass with an empty class name");
    }
    logger.fine("Instantiating class: " + className);
    try {
        Class clazz = ClassUtils.getClass(className);
        Class[] parameterTypes = new Class[0];
        Constructor constructor = clazz.getConstructor(parameterTypes);
        Object[] initArgs = new Object[0];
        return constructor.newInstance(initArgs);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Invalid class name specified: " + className, e);
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException("Specified class does not have a valid constructor: " + className, e);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Specified class does not have a valid constructor: " + className, e);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException("Specified class does not have a valid constructor: " + className, e);
    } catch (InstantiationException e) {
        throw new IllegalStateException("Specified class could not be instantiated: " + className, e);
    }
}

From source file:org.mandarax.dsl.util.DefaultResolver.java

private Class tryToLoadPrimitive(String name) throws ResolverException {
    try {/*  w ww . j a va2  s .c om*/
        return ClassUtils.getClass(name);
    } catch (Exception x) {
        return null;
    }
}

From source file:org.netbeans.orm.converter.compiler.VariableDefSnippet.java

public Class getWrapper(String premitive) throws ClassNotFoundException {
    return ClassUtils.primitiveToWrapper(ClassUtils.getClass(premitive));
}

From source file:org.xmlactions.action.config.ExecContext.java

public BaseAction getActionClass(String actionMapName, String actionKey)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    BaseAction action = null;/*from   w  w w  .  j av a2  s.c o  m*/
    String className = getAction(actionMapName, actionKey);
    if (StringUtils.isNotEmpty(className)) {
        Class<?> clas = ClassUtils.getClass(className);
        Object bean = clas.newInstance();
        if (!(bean instanceof BaseAction)) {
            throw new InstantiationException("class [" + bean.getClass().getName() + " must extend ["
                    + BaseAction.class.getName() + "]");
        }
        action = (BaseAction) bean;
    } else {
        log.info("no action class found for [" + actionMapName + "][" + actionKey + "]");
        // throw new IllegalArgumentException("no action class found for [" + actionMapName + "][" + actionKey + "]");
    }
    return action;
}

From source file:org.xmlactions.action.config.ExecContext.java

/**
  * Returns a class that matches the xml action name. This differs from
  * the getActionClass in that it does not require the class to extend BaseAction.
  * @param actionMapName/* w  w  w . ja v a2s .c o m*/
  * @param actionKey
  * @return matching class
  * @throws ClassNotFoundException
  * @throws InstantiationException
  * @throws IllegalAccessException
  */
public Object getClassAsObject(String actionMapName, String actionKey)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    Object bean = null;
    String className = getAction(actionMapName, actionKey);
    if (StringUtils.isNotEmpty(className)) {
        Class<?> clas = ClassUtils.getClass(className);
        bean = clas.newInstance();
    } else {
        throw new IllegalArgumentException(
                "no action class found for [" + actionMapName + "][" + actionKey + "]");
    }
    return bean;
}

From source file:org.xmlactions.common.reflection.BeanConstruct.java

public Object getBean(String beanName) {

    Class<?> clas;/*from   w  w  w.  j  ava 2  s . c o  m*/
    Object bean;
    try {
        clas = ClassUtils.getClass(beanName);
        bean = clas.newInstance();
    } catch (Exception e) {
        throw new IllegalArgumentException("No bean created for [" + beanName + "]", e);
    }
    return bean;
}

From source file:org.xmlactions.mapping.xml_to_bean.Bean.java

public Class<?> getAsClass() throws ClassNotFoundException {
    Class<?> _clas = ClassUtils.getClass(clas);
    return _clas;
}

From source file:org.xmlactions.pager.actions.CodeAction.java

public String execute(IExecContext execContext) throws Exception {

    String methodName = getMethod();
    String clas = getClas();/* w w w .  j  ava2  s .c  om*/
    String className = (String) execContext.getAction(null, clas);
    if (className == null) {
        className = clas;
    }
    Validate.notEmpty(methodName);
    Validate.notEmpty(className, "no property value set for [" + clas + "]");
    Class<?> _clas = ClassUtils.getClass(className);
    Object bean = _clas.newInstance();

    Object[] objs = new Object[getParams().size()];
    for (int i = 0; i < getParams().size(); i++) {
        objs[i] = getParams().get(i).getResolvedValue(execContext);
    }
    try {
        Object obj = MethodUtils.invokeMethod(bean, methodName, objs);
        return (obj != null ? obj.toString() : null);
    } catch (Exception ex) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < getParams().size(); i++) {
            sb.append("\nParam[" + i + "] '" + getParams().get(i).getValue() + "' = " + objs[i]);
        }
        String params = sb.toString();
        if (StringUtils.isEmpty(params)) {
            params = "no params";
        }
        throw new Exception("Unable to invoke [" + clas + "." + methodName + "]" + params, ex);
    }
}