Java Code Examples for org.apache.velocity.util.ClassUtils

The following examples show how to use org.apache.velocity.util.ClassUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: velocity-engine   Source File: ResourceLoaderFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the loader specified in the configuration file.
 * @param rs
 * @param loaderClassName
 * @return TemplateLoader
 */
public static ResourceLoader getLoader(RuntimeServices rs, String loaderClassName)
{
    ResourceLoader loader = null;

    try
    {
        loader = (ResourceLoader) ClassUtils.getNewInstance( loaderClassName );

        rs.getLog().debug("ResourceLoader instantiated: {}", loader.getClass().getName());

        return loader;
    }
    // The ugly three strike again: ClassNotFoundException,IllegalAccessException,InstantiationException
    catch(Exception e)
    {
        String msg = "Problem instantiating the template loader: "+loaderClassName+"." + System.lineSeparator() +
            "Look at your properties file and make sure the" + System.lineSeparator() +
            "name of the template loader is correct.";
        rs.getLog().error(msg, e);
        throw new VelocityException(msg, e);
    }
}
 
Example 2
@Override
protected Object evalValue() throws JspException {
       try {
           return ClassUtils.getNewInstance(className);
       } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
           throw new JspException(e);
       }
}
 
Example 3
Source Project: oxTrust   Source File: ClasspathPathResourceLoader.java    License: MIT License 5 votes vote down vote up
/**
 * Get a Reader so that the Runtime can build a template with it.
 *
 * @param name     name of template to get
 * @param encoding asked encoding
 * @return InputStream containing the template
 * @throws ResourceNotFoundException if template not found in classpath.
 * @since 2.0
 */
public Reader getResourceReader(String templateName, String encoding) throws ResourceNotFoundException {
	Reader result = null;

	if (StringUtils.isEmpty(templateName)) {
		throw new ResourceNotFoundException("No template name provided");
	}

	for (String path : paths) {
		InputStream rawStream = null;
		try {
			rawStream = ClassUtils.getResourceAsStream(getClass(), path + "/" + templateName);
			if (rawStream != null) {
				result = buildReader(rawStream, encoding);
				if (result != null) {
					break;
				}
			}
		} catch (Exception fnfe) {
			if (rawStream != null) {
				try {
					rawStream.close();
				} catch (IOException ioe) {
				}
			}
			throw new ResourceNotFoundException("ClasspathResourceLoader problem with template: " + templateName, fnfe);
		}
	}

	if (result == null) {
		String msg = "ClasspathResourceLoader Error: cannot find resource " + templateName;

		throw new ResourceNotFoundException(msg);
	}

	return result;
}
 
Example 4
Source Project: velocity-engine   Source File: FieldMethodizer.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Add the Name of the class to methodize
 * @param s
 * @throws Exception
 */
public void addObject ( String s )
    throws Exception
{
    inspect(ClassUtils.getClass(s));
}
 
Example 5
Source Project: velocity-engine   Source File: ClasspathResourceLoader.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Get a Reader so that the Runtime can build a
 * template with it.
 *
 * @param name name of template to get
 * @param encoding asked encoding
 * @return InputStream containing the template
 * @throws ResourceNotFoundException if template not found
 *         in  classpath.
 * @since 2.0
 */
public Reader getResourceReader( String name, String encoding )
        throws ResourceNotFoundException
{
    Reader result = null;

    if (StringUtils.isEmpty(name))
    {
        throw new ResourceNotFoundException ("No template name provided");
    }

    /**
     * look for resource in thread classloader first (e.g. WEB-INF\lib in
     * a servlet container) then fall back to the system classloader.
     */

    InputStream rawStream = null;
    try
    {
        rawStream = ClassUtils.getResourceAsStream( getClass(), name );
        if (rawStream != null)
        {
            result = buildReader(rawStream, encoding);
        }
    }
    catch( Exception fnfe )
    {
        if (rawStream != null)
        {
            try
            {
                rawStream.close();
            }
            catch (IOException ioe) {}
        }
        throw new ResourceNotFoundException("ClasspathResourceLoader problem with template: " + name, fnfe, rsvc.getLogContext().getStackTrace() );
    }

    if (result == null)
    {
        String msg = "ClasspathResourceLoader Error: cannot find resource " + name;

        throw new ResourceNotFoundException( msg, null, rsvc.getLogContext().getStackTrace() );
    }

    return result;
}