View Javadoc

1   package org.astrogrid.common.j2ee.environment;
2   
3   import javax.naming.Context;
4   import javax.naming.InitialContext;
5   
6   /***
7    * A Java bean representing an environment entry in a web-application.
8    * <p>
9    * The bean has the following properties: name, type, description,
10   * defaultValue, operationValue and replacementValue.
11   * <p>
12   * The first four properties map to the elements env-entry-name, env-entry-type,
13   * description, and env-entry-value that define the environment entry in web.xml.
14   * These properties must all be set buy the bean's client; the bean itself
15   * does not parse web.xml.
16   * <p>
17   * The operationValue property of the bean represents the current value of the
18   * environment entry in the context of the running web-application. There is
19   * no setter for this property and the value is read from JNDI. If the bean
20   * should be used outside a web application then the value of this property
21   * depends on the JNDI environment. if JNDI cannot deliver a value, then the
22   * property value is null.
23   * <p>
24   * The replacementValue property of the bean holds the value for the
25   * environment entry set by some client of the bean that is editing the
26   * environment. Until this property is set by the client, the bean makes
27   * the property equal to the operationalValue property. Once the property is
28   * explictly set it keeps its value until changed by the client, even if the
29   * operationalValue property changes.
30   *
31   * @author Guy Rixon
32   */
33  public class EnvEntry {
34  
35    /*** Creates a new instance of EnvEntry */
36    public EnvEntry() {
37    }
38  
39    /***
40     * The name of the entry. This maps to the env-entry-name element.
41     */
42    private String name;
43  
44    /***
45     * Gets the name of the entry.
46     */
47    public String getName() {
48      return this.name;
49    }
50  
51    /***
52     * Sets the name of the entry.
53     */
54    public void setName(String name) {
55      this.name = name;
56    }
57  
58    /***
59     * The Java class-name of the entry. This maps to the env-entry-type element.
60     */
61    private String type;
62  
63    /***
64     * Gets the Java class-name of the entry.
65     */
66    public String getType() {
67      return this.type;
68    }
69  
70    /***
71     * Sets the Java class-name of the entry.
72     */
73    public void setType(String type) {
74      this.type = type;
75    }
76  
77    /***
78     * The value of the entry set in web.xml. This maps to the env-entry-value element.
79     * The types are constrained by the J2EE specification to be the classes
80     * java.lang.Boolean, java.lang.Integer, java.lang.Float and java.lang.String.
81     * That contraint is not enforced here, but the value is assumed to be of a
82     * suitable type.
83     */
84    private Object defaultValue;
85  
86    /***
87     * Gets the current value of the entry.
88     */
89    public Object getDefaultValue() {
90      return this.defaultValue;
91    }
92  
93    /***
94     * Sets the value of the entry.
95     */
96    public void setDefaultValue(Object value) {
97      this.defaultValue = value;
98    }
99  
100   /***
101    * The value of the entry currently used by the application.
102    * The types are constrained by the J2EE specification to be the classes
103    * java.lang.Boolean, java.lang.Integer, java.lang.Float and java.lang.String.
104    * That contraint is not enforced here, but the value is assumed to be of a
105    * suitable type.
106    */
107   private Object operationalValue;
108 
109   /***
110    * Gets the current value of the entry.
111    */
112   public Object getOperationalValue() {
113     // Read the value from JNDI.
114     try {
115       Context initCtx = new InitialContext();
116       Context envCtx = (Context)initCtx.lookup("java:comp/env");
117       return envCtx.lookup(this.name);
118     }
119     catch (Exception e) {
120       return null;
121     }
122   }
123 
124   /***
125    * Sets the value of the entry.
126    */
127   public void setOperationalValue(Object value) {
128     this.operationalValue = value;
129   }
130 
131  /***
132    * The value of the entry to be used in the next reconfiguration.
133    * The types are constrained by the J2EE specification to be the classes
134    * java.lang.Boolean, java.lang.Integer, java.lang.Float and java.lang.String.
135    * That contraint is not enforced here, but the value is assumed to be of a
136    * suitable type.
137    */
138   private Object replacementValue;
139 
140   /***
141    * Gets the current value of the entry.
142    */
143   public Object getReplacementValue() {
144     if (this.replacementValue != null) {
145       return this.replacementValue;
146     }
147     else {
148       return this.getOperationalValue();
149     }
150   }
151 
152   /***
153    * Sets the value of the entry.
154    */
155   public void setReplacementValue(Object value) {
156     this.replacementValue = value;
157   }
158 
159   /***
160    * A textual description of the use of the entry. This should contain
161    * plain text rather than XML and should be fairly short:
162    * a couple of sentences at most. This maps to the
163    * description element.
164    */
165   private String description;
166 
167   /***
168    * Gets the description of the entry's purpose.
169    */
170   public String getDescription() {
171     return this.description;
172   }
173 
174   /***
175    * Sets the description of the entry's purpose.
176    * All white-space characters are replaced with
177    * spaces. This eliminates line breaks which would
178    * otherwise cause problems in the formatting of the
179    * text.
180    */
181   public void setDescription(String description) {
182     this.description = description.replaceAll("//s", " ");
183   }
184 
185 }