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, boolean initialize) throws ClassNotFoundException 

Source Link

Document

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

Usage

From source file:com.marvelution.hudson.plugins.apiv2.dozer.utils.HudsonDozerClassLoader.java

/**
 * {@inheritDoc}/*from w w w. j a va  2  s  .c  o m*/
 */
@Override
public Class<?> loadClass(String className) {
    LOGGER.log(Level.FINE, "Trying to load Class: " + className);
    Class<?> result = null;
    try {
        result = ClassUtils.getClass(HudsonPluginUtils.getPluginClassloader(), className);
    } catch (ClassNotFoundException e) {
        MappingUtils.throwMappingException(e);
    }
    return result;
}

From source file:com.haulmont.cuba.core.sys.remoting.LocalServiceInvokerImpl.java

@Override
public LocalServiceInvocationResult invoke(LocalServiceInvocation invocation) {
    if (invocation == null) {
        throw new IllegalArgumentException("Invocation is null");
    }/*  w w w  .  java 2  s  .c  o  m*/

    LocalServiceInvocationResult result = new LocalServiceInvocationResult();
    ClassLoader clientClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        ClassLoader classLoader = target.getClass().getClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);

        String[] parameterTypeNames = invocation.getParameterTypeNames();
        Class[] parameterTypes = new Class[parameterTypeNames.length];
        for (int i = 0; i < parameterTypeNames.length; i++) {
            Class<?> paramClass = ClassUtils.getClass(classLoader, parameterTypeNames[i]);
            parameterTypes[i] = paramClass;
        }

        byte[][] argumentsData = invocation.getArgumentsData();
        Object[] notSerializableArguments = invocation.getNotSerializableArguments();
        Object[] arguments;
        if (argumentsData == null) {
            arguments = null;
        } else {
            arguments = new Object[argumentsData.length];
            for (int i = 0; i < argumentsData.length; i++) {
                if (argumentsData[i] == null) {
                    if (notSerializableArguments[i] == null) {
                        arguments[i] = null;
                    } else {
                        arguments[i] = notSerializableArguments[i];
                    }
                } else {
                    arguments[i] = SerializationSupport.deserialize(argumentsData[i]);
                }
            }
        }

        SecurityContext targetSecurityContext = null;
        if (invocation.getSessionId() != null) {
            targetSecurityContext = new SecurityContext(invocation.getSessionId());
        }
        AppContext.setSecurityContext(targetSecurityContext);

        if (invocation.getLocale() != null) {
            Locale locale = Locale.forLanguageTag(invocation.getLocale());
            UserInvocationContext.setRequestScopeInfo(invocation.getSessionId(), locale,
                    invocation.getTimeZone(), invocation.getAddress(), invocation.getClientInfo());
        }

        Method method = target.getClass().getMethod(invocation.getMethodName(), parameterTypes);
        Object data = method.invoke(target, arguments);

        if (invocation.canResultBypassSerialization()) {
            result.setNotSerializableData(data);
        } else {
            result.setData(SerializationSupport.serialize(data));
        }
        return result;
    } catch (Throwable t) {
        if (t instanceof InvocationTargetException)
            t = ((InvocationTargetException) t).getTargetException();
        result.setException(SerializationSupport.serialize(t));
        return result;
    } finally {
        Thread.currentThread().setContextClassLoader(clientClassLoader);
        AppContext.setSecurityContext(null);
        UserInvocationContext.clearRequestScopeInfo();
    }
}

From source file:com.haulmont.cuba.core.sys.serialization.StandardSerialization.java

@Override
public Object deserialize(InputStream is) {
    try {/*from  w  w w . ja v  a 2s . c  o m*/
        ObjectInputStream ois;
        boolean isObjectStream = is instanceof ObjectInputStream;
        if (isObjectStream) {
            ois = (ObjectInputStream) is;
        } else {
            ois = new ObjectInputStream(is) {
                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc)
                        throws IOException, ClassNotFoundException {
                    return ClassUtils.getClass(StandardSerialization.class.getClassLoader(), desc.getName());
                }
            };
        }
        return ois.readObject();
    } catch (IOException ex) {
        throw new IllegalArgumentException("Failed to deserialize object", ex);
    } catch (ClassNotFoundException ex) {
        throw new IllegalStateException("Failed to deserialize object type", ex);
    }
}

From source file:com.opoopress.maven.plugins.plugin.AbstractDeployMojo.java

private static void configureScpWagonIfRequired(Wagon wagon, Log log) {
    log.debug("configureScpWagonIfRequired: " + wagon.getClass().getName());

    if (System.console() == null) {
        log.debug("No System.console(), skip configure Wagon");
        return;//  w ww.  ja v a2s.com
    }

    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    if (parent == null) {
        parent = AbstractDeployMojo.class.getClassLoader();
    }
    List<ClassLoader> loaders = new ArrayList<ClassLoader>();
    loaders.add(wagon.getClass().getClassLoader());
    ChainingClassLoader loader = new ChainingClassLoader(parent, loaders);

    Class<?> scpWagonClass;
    try {
        scpWagonClass = ClassUtils.getClass(loader, "org.apache.maven.wagon.providers.ssh.jsch.ScpWagon");
    } catch (ClassNotFoundException e) {
        log.debug(
                "Class 'org.apache.maven.wagon.providers.ssh.jsch.ScpWagon' not found, skip configure Wagon.");
        return;
    }

    //is ScpWagon
    if (scpWagonClass.isInstance(wagon)) {
        try {
            Class<?> userInfoClass = ClassUtils.getClass(loader,
                    "com.opoopress.maven.plugins.plugin.ssh.SystemConsoleInteractiveUserInfo");
            Object userInfo = userInfoClass.newInstance();
            MethodUtils.invokeMethod(wagon, "setInteractiveUserInfo", userInfo);
            log.debug("ScpWagon using SystemConsoleInteractiveUserInfo(Java 6+).");
        } catch (ClassNotFoundException e) {
            log.debug(
                    "Class 'com.opoopress.maven.plugins.plugin.ssh.SystemConsoleInteractiveUserInfo' not found, skip configure Wagon.");
        } catch (InstantiationException e) {
            log.debug("Instantiate class exception", e);
        } catch (IllegalAccessException e) {
            log.debug(e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            log.debug(e.getMessage(), e);
        } catch (InvocationTargetException e) {
            log.debug(e.getMessage(), e);
        }
    } else {
        log.debug("Not a ScpWagon.");
    }
}

From source file:net.stuxcrystal.simpledev.configuration.parser.BaseConstructor.java

/**
 * Registers a tree-generator if it does not throw an exception while preparing it.
 * @param loader    The generator.//from www .j a  v a  2 s  .c  o m
 * @param classname The name of the class.
 */
private void registerTreeGeneratorClass(ConfigurationHandler loader, String classname) {
    try {
        Class<?> cls = ClassUtils.getClass(this.getClass().getClassLoader(), classname);
        loader.addTreeGenerator((NodeTreeGenerator) cls.newInstance());
    } catch (Throwable e) {
        // Do a catchall, that's easier.
        loader.getLoggingInterface().debug("Not installing: " + loader);
        loader.getLoggingInterface().debugException(e);
    }
}

From source file:org.apache.cayenne.tools.dbimport.config.DefaultTypeMapperBuilder.java

private DefaultJdbc2JavaTypeMapper createMapper(String className) {
    if (!isBlank(className)) {
        try {/*ww  w .  jav a 2 s  .  co  m*/
            return (DefaultJdbc2JavaTypeMapper) ClassUtils
                    .getClass(Thread.currentThread().getContextClassLoader(), className).newInstance();
        } catch (ClassNotFoundException e) {
            logger.error("Can't load class '" + className + "': ", e);
        } catch (InstantiationException e) {
            logger.error("Can't instantiate '" + className + "' make sure it has default constructor.", e);
        } catch (IllegalAccessException e) {
            logger.error("Can't instantiate '" + className + "' make sure it has default constructor.", e);
        }
    }

    return new DefaultJdbc2JavaTypeMapper();
}

From source file:org.itest.impl.ITestValueConverterImpl.java

@Override
public <T> T convert(Class<T> clazz, String value) {
    Object res;/*  ww  w .ja  v a  2 s  .co m*/
    if (null == value) {
        res = null;
    } else if (String.class == clazz || Object.class == clazz) {
        res = StringEscapeUtils.unescapeJava(value);
    } else if (Integer.class == clazz || int.class == clazz) {
        res = Integer.valueOf(value);
    } else if (Long.class == clazz || long.class == clazz) {
        res = Long.valueOf(value);
    } else if (Double.class == clazz || double.class == clazz) {
        res = Double.valueOf(value);
    } else if (Float.class == clazz || float.class == clazz) {
        res = Float.valueOf(value);
    } else if (Boolean.class == clazz || boolean.class == clazz) {
        res = Boolean.valueOf(value);
    } else if (Character.class == clazz || char.class == clazz) {
        value = StringEscapeUtils.unescapeJava(value);
        if (value.length() > 1) {
            throw new ITestInitializationException(
                    "Character expected, found (" + value + ") " + value.length() + " characters.", null);
        }
        res = value.charAt(0);
    } else if (Date.class == clazz || Time.class == clazz || Timestamp.class == clazz
            || java.sql.Date.class == clazz) {
        Long milis = Long.valueOf(value);
        try {
            res = clazz.getConstructor(long.class).newInstance(milis);
        } catch (Exception e) {
            throw new ITestException("", e);
        }
    } else if (clazz.isEnum()) {
        res = Enum.valueOf((Class<Enum>) clazz, value);
    } else if (Class.class == clazz) {
        try {
            res = ClassUtils.getClass(getClass().getClassLoader(), value);
        } catch (ClassNotFoundException e) {
            throw new ITestInitializationException(value, e);
        }
    } else {
        throw new ITestInitializationException(clazz.getName() + "(" + value + ")", null);
    }
    return (T) res;
}

From source file:org.kuali.rice.core.api.util.ClassLoaderUtils.java

public static Class<?> getClass(String className) {
    if (StringUtils.isEmpty(className)) {
        return null;
    }//  w  w  w.  ja  v a2  s. c  o  m
    try {
        return ClassUtils.getClass(getDefaultClassLoader(), className);
    } catch (ClassNotFoundException e) {
        throw new RiceRuntimeException(e);
    }
}

From source file:org.kuali.rice.core.api.util.ClassLoaderUtils.java

public static <T> Class<? extends T> getClass(String className, Class<T> type) throws ClassNotFoundException {
    Class<?> theClass = ClassUtils.getClass(getDefaultClassLoader(), className);
    return theClass.asSubclass(type);
}

From source file:org.kuali.rice.kns.datadictionary.control.ControlDefinitionBase.java

/**
 * Directly validate simple fields./*from   w w w.  j a va 2 s.  c o m*/
 *
 * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
 */
public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
    if (!isCheckbox() && !isHidden() && !isRadio() && !isSelect() && !isMultiselect() && !isText()
            && !isTextarea() && !isCurrency() && !isKualiUser() && !isLookupHidden() && !isLookupReadonly()
            && !isWorkflowWorkgroup() && !isFile() && !isButton() && !isLink()) {
        throw new CompletionException("error validating " + rootBusinessObjectClass.getName()
                + " control: unknown control type in control definition (" + "" + ")");
    }
    if (valuesFinderClass != null) {
        try {
            Class valuesFinderClassObject = ClassUtils.getClass(ClassLoaderUtils.getDefaultClassLoader(),
                    getValuesFinderClass());
            if (!KeyValuesFinder.class.isAssignableFrom(valuesFinderClassObject)) {
                throw new ClassValidationException(
                        "valuesFinderClass is not a valid instance of " + KeyValuesFinder.class.getName()
                                + " instead was: " + valuesFinderClassObject.getName());
            }
        } catch (ClassNotFoundException e) {
            throw new ClassValidationException(
                    "valuesFinderClass could not be found: " + getValuesFinderClass(), e);
        }
    }
    if (businessObjectClass != null) {
        try {
            Class businessObjectClassObject = ClassUtils.getClass(ClassLoaderUtils.getDefaultClassLoader(),
                    getBusinessObjectClass());
            if (!BusinessObject.class.isAssignableFrom(businessObjectClassObject)) {
                throw new ClassValidationException(
                        "dataObjectClass is not a valid instance of " + BusinessObject.class.getName()
                                + " instead was: " + businessObjectClassObject.getName());
            }
        } catch (ClassNotFoundException e) {
            throw new ClassValidationException(
                    "dataObjectClass could not be found: " + getBusinessObjectClass(), e);
        }
    }
}