KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > mapper > EnvEntryDecoratorMapper


1 /*
2  * Title: EnvEntryDecoratorMapper
3  * Description:
4  *
5  * This software is published under the terms of the OpenSymphony Software
6  * License version 1.1, of which a copy has been included with this
7  * distribution in the LICENSE.txt file.
8  */

9
10 package com.opensymphony.module.sitemesh.mapper;
11
12 import com.opensymphony.module.sitemesh.Decorator;
13
14 import javax.naming.Context JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import javax.naming.NamingException JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18
19 /**
20  * The EnvEntryDecoratorMapper allows the reference to a web-app environment entry for the
21  * decorator name, and falls back to ConfigDecoratorMapper's behavior if no matching
22  * environment entry is found.
23  *
24  * <p>In some cases, it's desirable to allow a deployer, as opposed to a developer,
25  * to specify a decorator. In a .WAR file, this can be very difficult, since
26  * decorator mappings are specified in <code>decorators.xml</code> (more or less).
27  * This mapper corrects that by allowing two types of mapping. If the decorator
28  * name is found in an <code>&lt;env-entry&gt;</code>, the entry's value is used
29  * as the decorator reference.</p>
30  *
31  * <p>Known Issues:</p>
32  * <ol>
33  * <li>It still uses the decorator path (from <code>decorators.xml</code>). This
34  * needs to be corrected for full functionality. If anyone has a suggestion
35  * on how...</li>
36  * </ol>
37  *
38  * @author <a HREF="mailto:joeo@enigmastation.com">Joseph B. Ottinger</a>
39  * @version $Revision: 1.2 $
40  *
41  * @see com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper
42  */

43 public final class EnvEntryDecoratorMapper extends ConfigDecoratorMapper {
44     /**
45      * Retrieves the {@link com.opensymphony.module.sitemesh.Decorator}
46      * specified by the decorator name. If it's not in the environment
47      * entries of the web application, assume it's a named decorator
48      * from <code>decorators.xml</code>.
49      */

50     public Decorator getNamedDecorator(HttpServletRequest JavaDoc request, String JavaDoc name) {
51         String JavaDoc resourceValue = getStringResource(name);
52         if (resourceValue == null) {
53             return super.getNamedDecorator(request, name);
54         }
55         else {
56             return new DefaultDecorator(name, resourceValue, null);
57         }
58     }
59
60     /**
61      * This pulls a value out of the web-app environment.
62      * If the value isn't there, returns null.
63      */

64     public static String JavaDoc getStringResource(String JavaDoc name) {
65         String JavaDoc value = null;
66         Context JavaDoc ctx = null;
67         try {
68             ctx = new InitialContext JavaDoc();
69             Object JavaDoc o = ctx.lookup("java:comp/env/" + name);
70             if (o != null) {
71                 value = o.toString();
72             }
73         }
74         catch (NamingException JavaDoc ne) {
75         }
76         finally {
77             try {
78                 if (ctx != null) ctx.close();
79             }
80             catch (NamingException JavaDoc ne) {
81             }
82         }
83         return value;
84     }
85 }
Popular Tags