Package org.apache.stratum.resources

Source Code of org.apache.stratum.resources.FileResource

package org.apache.stratum.resources;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;

import org.apache.commons.lang.exception.NestableException;

/**
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @version $Id: FileResource.java,v 1.3 2002/03/06 23:16:37 jvanzyl Exp $
*/
public class FileResource
    extends AbstractResource
{
    /**
     * Get an InputStream so that the Runtime can build a
     * template with it.
     *
     * @param name name of template to get
     * @return InputStream containing the template
     * @throws ResourceNotFoundException if template not found
     *         in the file template path.
     */
    public synchronized InputStream getResourceStream(String resourceName)
        throws NestableException
    {
        // Make sure we have a valid templateName.
        if (resourceName == null || resourceName.length() == 0)
        {
            // If we don't get a properly formed resourceName
            // then there's not much we can do. So
            // we'll forget about trying to search
            // any more paths for the template.
            throw new NestableException(
                "Need to specify a file name or file path!");
        }

        try
        {
            File file = new File(resourceName);  
       
            if (file.canRead())
            {
                return new BufferedInputStream(
                    new FileInputStream(file.getAbsolutePath()));
            }
            else
            {
                throw new NestableException(
                    "File resource " + resourceName + " is not readable!");
            }
        }
        catch( FileNotFoundException fnfe )
        {
            throw new NestableException(
                "File resource " + resourceName + " cannot be found!");
        }
    }
   
    /**
     * How to keep track of all the modified times
     * across the paths.
     */
    public boolean isModified()
    {
        File file = new File(getName());          
       
        if (file.canRead())
        {
            return (file.lastModified() != getLastModified());
        }
       
        // If the file is now unreadable, or it has
        // just plain disappeared then we'll just say
        // that it's modified :-) When the loader attempts
        // to load the stream it will fail and the error
        // will be reported then.
        return true;
    }

    public long getLastModified()
    {
        File file = new File(getName());

        if (file.canRead())
        {
            return file.lastModified();
        }           
        else
        {
            return 0;
        }           
    }
}
TOP

Related Classes of org.apache.stratum.resources.FileResource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.