KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > gen > ClassGeneratorResourceLoader


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.gen;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import org.apache.velocity.exception.ResourceNotFoundException;
29 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
30
31 /**
32  * Velocity template resource loader customized for Cayenne use. Supports loading
33  * templates from the thread ClassLoader and from relative and absolute paths.
34  *
35  * @since 1.2
36  * @author Andrus Adamchik
37  */

38 public class ClassGeneratorResourceLoader extends FileResourceLoader {
39
40     /**
41      * Returns resource as InputStream. First calls super implementation. If resource
42      * wasn't found, it attempts to load it from current directory or as an absolute path.
43      */

44     public synchronized InputStream JavaDoc getResourceStream(String JavaDoc name)
45             throws ResourceNotFoundException {
46
47         InputStream JavaDoc stream = loadFromRelativePath(name);
48         if (stream != null) {
49             return stream;
50         }
51
52         stream = loadFromAbsPath(name);
53         if (stream != null) {
54             return stream;
55         }
56
57         stream = loadFromThreadClassLoader(name);
58         if (stream != null) {
59             return stream;
60         }
61
62         stream = loadFromThisClassLoader(name);
63         if (stream != null) {
64             return stream;
65         }
66
67         throw new ResourceNotFoundException("Couldn't find resource '"
68                 + name
69                 + "'. Searched filesystem path and classpath");
70     }
71
72     protected InputStream JavaDoc loadFromRelativePath(String JavaDoc name) {
73         try {
74             return super.getResourceStream(name);
75         }
76         catch (ResourceNotFoundException rnfex) {
77             return null;
78         }
79     }
80
81     protected InputStream JavaDoc loadFromAbsPath(String JavaDoc name) {
82         try {
83             File JavaDoc file = new File JavaDoc(name);
84             return (file.canRead()) ? new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file
85                     .getAbsolutePath())) : null;
86
87         }
88         catch (FileNotFoundException JavaDoc fnfe) {
89             return null;
90         }
91     }
92
93     protected InputStream JavaDoc loadFromThreadClassLoader(String JavaDoc name) {
94         return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
95     }
96
97     protected InputStream JavaDoc loadFromThisClassLoader(String JavaDoc name) {
98         return getClass().getClassLoader().getResourceAsStream(name);
99     }
100 }
101
Popular Tags