jvanzyl     02/01/14 20:59:27

  Added:       src/java/org/apache/stratum/resources AbstractResource.java
                        ClasspathResource.java FileResource.java
                        Resource.java ResourceManager.java
  Log:
  - working portion of the resources package.
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/resources/AbstractResource.java
  
  Index: AbstractResource.java
  ===================================================================
  package org.apache.stratum.resources;
  
  /**
   * This class represent a general text resource that
   * may have been retrieved from any number of possible
   * sources.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   * @version $Id: AbstractResource.java,v 1.1 2002/01/15 04:59:27 jvanzyl Exp $
   */
  public abstract class AbstractResource
      implements Resource
  {
      protected static final long MILLIS_PER_SECOND =  1000;
  
      /**
       * How often the file modification time is checked (in milliseconds).
       */
      protected long checkInterval = 0;
  
      /**
       * The file modification time (in milliseconds) for the cached template.
       */
      protected long lastModified = 0;
  
      /**
       * The next time the file modification time will be checked (in 
       * milliseconds).
       */
      protected long nextCheck = 0;
  
      /**
       *  Name of the resource
       */
      protected String name;
      
      /**
       * Encoding of this resource.
       */
      protected String encoding;
  
      /** 
       *  Default constructor 
       */
      public AbstractResource()
      {
      }
  
      /**
       * Set the name of this resource, for example
       * test.vm.
       */
      public void setName(String name)
      {
          this.name = name;
      }        
  
      /**
       * Get the name of this template.
       */
      public String getName()
      {
          return name;
      }        
  
      /**
       *  set the encoding of this resource
       *  for example, "ISO-8859-1"
       */
      public void setEncoding( String encoding )
      {
          this.encoding = encoding;
      }
  
      /**
       *  get the encoding of this resource
       *  for example, "ISO-8859-1"
       */
      public String getEncoding()
      {
          return encoding;
      }
  
      /**
       * Set the last modified time for this
       * template.
       */
      public void setLastModified(long lastModified)
      {
          this.lastModified = lastModified;
      }        
  
      /**
       * Return the lastModifed time of this
       * template.
       */
      public long getLastModified()
      {
          return lastModified;
      }        
      
      /**
       * Set the modification check interval.
       * @param interval The interval (in minutes).
       */
      public void setCheckInterval(long checkInterval)
      {
          this.checkInterval = checkInterval;
      }
      
      /**
       * Is it time to check to see if the resource
       * source has been updated?
       */
      public boolean requiresChecking()
      {
           // Short circuit this if checkInterval == 0
           // as this means "don't check"
          if (checkInterval <= 0 )
          {
             return false;
          }
  
          // see if we need to check now
          return ( System.currentTimeMillis() >= nextCheck );
      }
  
      /**
       * 'Touch' this template and thereby resetting
       * the nextCheck field.
       */
      public void touch()
      {
          nextCheck = System.currentTimeMillis() + 
              ( MILLIS_PER_SECOND *  checkInterval);
      }
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/resources/ClasspathResource.java
  
  Index: ClasspathResource.java
  ===================================================================
  package org.apache.stratum.resources;
  
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
   
  import java.io.InputStream;
  import org.apache.stratum.exception.NestableException;
  
  /**
   *  ClasspathResourceLoader is a simple loader that will load
   *  templates from the classpath.
   *  <br>
   *  <br>
   *  Will load templates from  from multiple instances of 
   *  and arbitrary combinations of :
   *  <ul>
   *  <li> jar files
   *  <li> zip files
   *  <li> template directories (any directory containing templates)
   *  </ul>
   *  This is a configuration-free loader, in that there are no
   *  parameters to be specified in the configuration properties,
   *  other than specifying this as the loader to use.  For example
   *  the following is all that the loader needs to be functional :
   *  <br>
   *  <br>
   *  resource.loader.1.class = 
   *    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
   *  <br>
   *  <br>
   *  To use, put your template directories, jars
   *  and zip files into the classpath or other mechanisms that make
   *  resources accessable to the classloader.
   *  <br>
   *  <br>
   *  This makes deployment trivial for web applications running in
   *  any Servlet 2.2 compliant servlet runner, such as Tomcat 3.2 
   *  and others.
   *  <br>
   *  <br>
   *  For a Servlet Spec v2.2 servlet runner, 
   *  just drop the jars of template files into the WEB-INF/lib
   *  directory of your webapp, and you won't have to worry about setting
   *  template paths or altering them with the root of the webapp
   *  before initializing.
   *  <br>
   *  <br>
   *  I have also tried it with a WAR deployment, and that seemed to
   *  work just fine.
   *  
   * @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   * @version $Id: ClasspathResource.java,v 1.1 2002/01/15 04:59:27 jvanzyl Exp $
   */
  public class ClasspathResource
      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 NestableException if template not found
       *         in  classpath.
       */
      public synchronized InputStream getResourceStream( String name )
          throws NestableException
      {
          InputStream result = null;
          
          if (name == null || name.length() == 0)
          {
              throw new NestableException ("No template name provided");
          }
          
          try 
          {
              ClassLoader classLoader = this.getClass().getClassLoader();
              result= classLoader.getResourceAsStream( name );
          }
          catch( Exception fnfe )
          {
              // Log and convert to a general Velocity NestableException
              throw new NestableException(fnfe.getMessage());
          }
          
          return result;
      }
      
      /**
       * Defaults to return false.
       */
      public boolean isSourceModified(Resource resource)
      {
          return false;
      }
  
      /**
       * Defaults to return 0
       */
      public long getLastModified(Resource resource)
      {
          return 0;
      }
  }
  
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/resources/FileResource.java
  
  Index: FileResource.java
  ===================================================================
  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.stratum.exception.NestableException;
  
  /**
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: FileResource.java,v 1.1 2002/01/15 04:59:27 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())
          {
              if (file.lastModified() != getLastModified())
              {
                  return true;
              }                
              else
              {
                  return false;
              }                
          }
          
          // 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;
          }            
      }
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/resources/Resource.java
  
  Index: Resource.java
  ===================================================================
  package org.apache.stratum.resources;
  
  // The general problem of resource inclusion
  
  public interface Resource
  {
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/resources/ResourceManager.java
  
  Index: ResourceManager.java
  ===================================================================
  package org.apache.stratum.resources;
  
  /* Generated by Together */
  
  public interface ResourceManager
  {
      Resource getResource(String resourceName, String encoding);
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to