01: /*
02: * Title: EnvEntryDecoratorMapper
03: * Description:
04: *
05: * This software is published under the terms of the OpenSymphony Software
06: * License version 1.1, of which a copy has been included with this
07: * distribution in the LICENSE.txt file.
08: */
09:
10: package com.opensymphony.module.sitemesh.mapper;
11:
12: import com.opensymphony.module.sitemesh.Decorator;
13:
14: import javax.naming.Context;
15: import javax.naming.InitialContext;
16: import javax.naming.NamingException;
17: import javax.servlet.http.HttpServletRequest;
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><env-entry></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.3 $
40: *
41: * @see com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper
42: */
43: public 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 request,
51: String name) {
52: String resourceValue = getStringResource(name);
53: if (resourceValue == null) {
54: return super .getNamedDecorator(request, name);
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 getStringResource(String name) {
65: String value = null;
66: Context ctx = null;
67: try {
68: ctx = new InitialContext();
69: Object o = ctx.lookup("java:comp/env/" + name);
70: if (o != null) {
71: value = o.toString();
72: }
73: } catch (NamingException ne) {
74: } finally {
75: try {
76: if (ctx != null)
77: ctx.close();
78: } catch (NamingException ne) {
79: }
80: }
81: return value;
82: }
83: }
|