KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > SQLTemplateResourceManager


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.access.jdbc;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.commons.collections.ExtendedProperties;
27 import org.apache.commons.collections.map.LRUMap;
28 import org.apache.velocity.Template;
29 import org.apache.velocity.exception.ParseErrorException;
30 import org.apache.velocity.exception.ResourceNotFoundException;
31 import org.apache.velocity.runtime.RuntimeServices;
32 import org.apache.velocity.runtime.resource.Resource;
33 import org.apache.velocity.runtime.resource.ResourceManager;
34 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
35
36 /**
37  * An implementation of the Velocity ResourceManager and ResourceLoader that
38  * creates templates from in-memory Strings.
39  *
40  * @author Andrus Adamchik
41  * @since 1.1
42  */

43 // class must be public since it is instantiated by Velocity via reflection.
44
public class SQLTemplateResourceManager
45     extends ResourceLoader
46     implements ResourceManager {
47
48     protected Map JavaDoc templateCache;
49
50     public void initialize(RuntimeServices rs) throws Exception JavaDoc {
51         super.rsvc = rs;
52         this.templateCache = new LRUMap(100);
53     }
54
55     public void clearCache() {
56         templateCache.clear();
57     }
58
59     /**
60      * Returns a Velocity Resource which is a Template for the given SQL.
61      */

62     public Resource getResource(String JavaDoc resourceName, int resourceType, String JavaDoc encoding)
63         throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc {
64
65         synchronized (templateCache) {
66             Template resource = (Template) templateCache.get(resourceName);
67
68             if (resource == null) {
69                 resource = new Template();
70                 resource.setRuntimeServices(rsvc);
71                 resource.setResourceLoader(this);
72                 resource.setName(resourceName);
73                 resource.setEncoding(encoding);
74                 resource.process();
75
76                 templateCache.put(resourceName, resource);
77             }
78
79             return resource;
80         }
81     }
82
83     public String JavaDoc getLoaderNameForResource(String JavaDoc resourceName) {
84         return getClass().getName();
85     }
86
87     public long getLastModified(Resource resource) {
88         return -1;
89     }
90
91     public InputStream JavaDoc getResourceStream(String JavaDoc source)
92         throws ResourceNotFoundException {
93         return new ByteArrayInputStream JavaDoc(source.getBytes());
94     }
95
96     public void init(ExtendedProperties configuration) {
97
98     }
99
100     public boolean isSourceModified(Resource resource) {
101         return false;
102     }
103 }
104
Popular Tags