KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > appclient > jws > TemplateCache


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.appclient.jws;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  *Manages the caching and retrieval of templates used for generating JNLP and
35  *HTML documents to be served to Java Web Start.
36  *
37  * @author tjquinn
38  */

39 public class TemplateCache {
40     
41     /** path relative to this class where the templates reside */
42     private static final String JavaDoc TEMPLATES_PATH_PREFIX = "templates/";
43
44     /** caches templates */
45     private Map JavaDoc<String JavaDoc,String JavaDoc> templateCache;
46     
47     
48     /** Creates a new instance of TemplateCache */
49     public TemplateCache() {
50         templateCache = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
51     }
52     
53     /**
54      *Returns a template retrieved using the path specified.
55      *@param name of the template, relative to the current package
56      *@return the retrieved template as a String
57      *@throws IOException reporting any error retrieving the template as a resource
58      */

59     public String JavaDoc getTemplate(String JavaDoc templateName) throws IOException JavaDoc {
60         
61         /*
62          *Look for the named template in the cache and, if it is not there,
63          *retrieve it and store it in the cache.
64          */

65         String JavaDoc template = templateCache.get(templateName);
66         if (template == null) {
67             template = Util.loadResource(this.getClass(), TEMPLATES_PATH_PREFIX + templateName);
68             templateCache.put(templateName, template);
69         }
70         
71         /*
72          *Regardless of how the template was found, return it.
73          */

74         return template;
75     }
76 }
Popular Tags