View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.server.actions.impl;
36  
37  import java.io.InputStream;
38  
39  import org.apache.commons.collections.ExtendedProperties;
40  import org.apache.log4j.Logger;
41  import org.apache.velocity.exception.ResourceNotFoundException;
42  import org.apache.velocity.runtime.resource.Resource;
43  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
44  
45  /**
46   * Resource loader that loads velocity resources from the classpath.
47   * 
48   * @author owaeld
49   * 
50   */
51  public class ClasspathResourceLoader extends ResourceLoader
52  {
53  
54      private static final Logger LOG = Logger.getLogger( ClasspathResourceLoader.class );
55  
56      /**
57       * This is abstract in the base class, so we need it.
58       * 
59       * {@inheritDoc}
60       */
61      public void init( ExtendedProperties configuration )
62      {
63          LOG.debug( "ClasspathResourceLoader : initialization starting." );
64          LOG.debug( "ClasspathResourceLoader : initialization complete." );
65      }
66  
67      /**
68       * Get an InputStream so that the Runtime can build a template with it.
69       * 
70       * @param name
71       *            name of template to get
72       * @return InputStream containing the template
73       */
74      public synchronized InputStream getResourceStream( String name )
75      {
76          InputStream result = null;
77  
78          if ( name == null || name.length() == 0 )
79          {
80              throw new ResourceNotFoundException( "No template name provided" );
81          }
82  
83          try
84          {
85              result = ClasspathResourceLoader.class.getResourceAsStream( name );
86          }
87          catch ( Exception fnfe )
88          {
89              /*
90               * log and convert to a general Velocity ResourceNotFoundException
91               */
92  
93              throw new ResourceNotFoundException( fnfe.getMessage() );
94          }
95  
96          return result;
97      }
98  
99      /**
100      * Defaults to return false.
101      * 
102      * {@inheritDoc}
103      */
104     public boolean isSourceModified( Resource resource )
105     {
106         return false;
107     }
108 
109     /**
110      * Defaults to return 0
111      * 
112      * {@inheritDoc}
113      */
114     public long getLastModified( Resource resource )
115     {
116         return 0;
117     }
118 
119 }