KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > core > urltemplates > URLTemplateDescriptor


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

18 package org.apache.beehive.netui.core.urltemplates;
19
20 import org.apache.beehive.netui.util.logging.Logger;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Maintains optional deployment information about templates and the
28  * URL template config file.
29  */

30 public class URLTemplateDescriptor
31 {
32     // Logger
33
private static final Logger _log = new Logger( URLTemplateDescriptor.class );
34
35     // Singleton instance
36
private static URLTemplateDescriptor instance = new URLTemplateDescriptor();
37
38     // Constants for URL template types
39
public static final String JavaDoc DEFAULT_TEMPLATE = "default";
40     public static final String JavaDoc SECURE_DEFAULT_TEMPLATE = "secure-default";
41     public static final String JavaDoc ACTION_TEMPLATE = "action";
42     public static final String JavaDoc SECURE_ACTION_TEMPLATE = "secure-action";
43     public static final String JavaDoc RESOURCE_TEMPLATE = "resource";
44     public static final String JavaDoc SECURE_RESOURCE_TEMPLATE = "secure-resource";
45     public static final String JavaDoc RENDER_TEMPLATE = "render";
46     public static final String JavaDoc SECURE_RENDER_TEMPLATE = "secure-render";
47
48     // Tokens
49
public static final String JavaDoc SCHEME_TOKEN = "{url:scheme}";
50     public static final String JavaDoc DOMAIN_TOKEN = "{url:domain}";
51     public static final String JavaDoc PORT_TOKEN = "{url:port}";
52     public static final String JavaDoc PATH_TOKEN = "{url:path}";
53     public static final String JavaDoc QUERY_STRING_TOKEN = "{url:queryString}";
54     public static final String JavaDoc FRAGMENT_TOKEN = "{url:fragment}";
55
56     private static final List JavaDoc KNOWN_TEMPLATE_TOKENS =
57             Arrays.asList( new String JavaDoc[]{ SCHEME_TOKEN, DOMAIN_TOKEN, PORT_TOKEN, FRAGMENT_TOKEN } );
58
59     private static final List JavaDoc REQUIRED_TEMPLATE_TOKENS =
60             Arrays.asList( new String JavaDoc[]{ PATH_TOKEN, QUERY_STRING_TOKEN } );
61
62     // URL templates
63
private URLTemplates _urlTemplates = new URLTemplates();
64
65     private boolean _loaded = false;
66
67     /**
68      * Constructs an instance.
69      */

70     protected URLTemplateDescriptor()
71     {
72     }
73
74     /**
75      * Returns URL template given the name of the template.
76      *
77      * @param name name of the template
78      * @return template
79      */

80     public URLTemplate getURLTemplate( String JavaDoc name )
81     {
82         return _urlTemplates.getTemplate( name );
83     }
84
85     /**
86      * Returns URL template name of the given type (by key).
87      *
88      * @param refGroupName name of a group of templates from the config file.
89      * @param key type of the template
90      * @return template name
91      */

92     public String JavaDoc getURLTemplateRef( String JavaDoc refGroupName, String JavaDoc key )
93     {
94         String JavaDoc ref = _urlTemplates.getTemplateNameByRef( refGroupName, key );
95         if ( ref == null )
96         {
97             // If the template is a secure template, look for the secure default
98
// before resolving to the default
99
if ( key.equals( SECURE_RENDER_TEMPLATE ) ||
100                     key.equals( SECURE_ACTION_TEMPLATE ) ||
101                     key.equals( SECURE_RESOURCE_TEMPLATE ) )
102             {
103                 ref = _urlTemplates.getTemplateNameByRef( refGroupName, SECURE_DEFAULT_TEMPLATE );
104             }
105         }
106
107         return ref;
108     }
109
110     /**
111      * Returns an instance of <code>URLTemplateDescriptor</code>.
112      *
113      * @return portal app descriptor
114      */

115     public static URLTemplateDescriptor getInstance()
116     {
117         return instance;
118     }
119
120     public synchronized void load( ServletContext JavaDoc servletContext )
121     {
122         if ( _loaded )
123         {
124             return;
125         }
126
127         URLTemplatesFactory urlTemplatesFactory = new URLTemplatesFactory();
128         urlTemplatesFactory.setKnownTokens( KNOWN_TEMPLATE_TOKENS );
129         urlTemplatesFactory.setRequiredTokens( REQUIRED_TEMPLATE_TOKENS );
130         _urlTemplates = urlTemplatesFactory.getTemplates( servletContext );
131
132         _loaded = true;
133     }
134 }
135
Popular Tags