KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > EnvironmentProperty


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  package com.sun.enterprise.deployment;
24
25 import com.sun.enterprise.deployment.web.InitializationParameter;
26 import com.sun.enterprise.deployment.web.ContextParameter;
27 import com.sun.enterprise.deployment.web.EnvironmentEntry;
28 import com.sun.enterprise.deployment.web.WebDescriptor;
29 import com.sun.enterprise.util.LocalStringManagerImpl;
30 import com.sun.enterprise.util.RelativePathResolver;
31 import com.sun.enterprise.util.TypeUtil;
32
33 import java.lang.reflect.Field JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Set JavaDoc;
38
39     /**
40     ** The EnvironmentProperty class hold the data about a single environment entry for J2EE components.
41     ** @author Danny Coward
42     */

43  
44 public class EnvironmentProperty extends Descriptor implements InitializationParameter, ContextParameter, WebDescriptor, EnvironmentEntry, InjectionCapable {
45     private String JavaDoc value;
46     private String JavaDoc type;
47     private Object JavaDoc valueObject;
48     private boolean setValueCalled = false;
49
50     // list of injection targes
51
private Set JavaDoc<InjectionTarget> injectionTargets;
52
53     private static Class JavaDoc[] allowedTypes = {
54                                         int.class,
55                                         boolean.class,
56                                         double.class,
57                                         float.class,
58                                         long.class,
59                                         short.class,
60                                         byte.class,
61                                         char.class,
62                     java.lang.String JavaDoc.class,
63                     java.lang.Boolean JavaDoc.class,
64                     java.lang.Integer JavaDoc.class,
65                     java.lang.Double JavaDoc.class,
66                     java.lang.Byte JavaDoc.class,
67                     java.lang.Short JavaDoc.class,
68                     java.lang.Long JavaDoc.class,
69                     java.lang.Float JavaDoc.class,
70                                         java.lang.Character JavaDoc.class
71                         };
72    private static LocalStringManagerImpl localStrings =
73         new LocalStringManagerImpl(EnvironmentProperty.class);
74
75     protected String JavaDoc mappedName;
76                             
77     /**
78     ** copy constructor.
79     */

80
81     public EnvironmentProperty(EnvironmentProperty other) {
82     super(other);
83     value = other.value;
84     type = other.type;
85     valueObject = other.valueObject;
86     }
87                     
88     /**
89     ** Construct an environment property if type String and empty string value and no description.
90     */

91
92     public EnvironmentProperty() {
93     }
94     
95      /**
96     ** Construct an environment property of given name value and description.
97     */

98     
99     public EnvironmentProperty(String JavaDoc name, String JavaDoc value, String JavaDoc description) {
100     this(name, value, description, null);
101     }
102     
103     /**
104     ** Construct an environment property of given name value and description and type.
105     ** Throws an IllegalArgumentException if bounds checking is true and the value cannot be
106     ** reconciled with the given type.
107     */

108     
109     public EnvironmentProperty(String JavaDoc name, String JavaDoc value, String JavaDoc description, String JavaDoc type) {
110     super(name, description);
111     this.value = value;
112     checkType(type);
113     this.type = type;
114     }
115     
116     /**
117     ** Returns the String value of this environment property
118     */

119     
120     public String JavaDoc getValue() {
121     if (this.value == null) {
122         this.value = "";
123     }
124     return value;
125     }
126     
127     /**
128      * Returns a resolved value of this environment property
129      */

130     public String JavaDoc getResolvedValue() {
131         return RelativePathResolver.resolvePath(getValue());
132     }
133     
134     /**
135      ** Returns the typed value object of this environment property. Throws an IllegalArgumentException if bounds checking is
136      ** true and the value cannot be
137      ** reconciled with the given type.
138      */

139      public Object JavaDoc getResolvedValueObject() {
140     if (this.valueObject == null) {
141         this.valueObject = "";
142     }
143     return getObjectFromString(this.getResolvedValue(), this.getValueType());
144      }
145     
146     /**
147     ** checks the given class type. throws an IllegalArgumentException if bounds checking
148     ** if the type is not allowed.
149     */

150     
151     private void checkType(String JavaDoc type) {
152     if (type != null) {
153         Class JavaDoc typeClass = null;
154         // is it loadable ?
155
try {
156         typeClass = Class.forName(type);
157         } catch (Throwable JavaDoc t) {
158         if (this.isBoundsChecking()) {
159             throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
160                                            "enterprise.deployment..exceptiontypenotallowedpropertytype",
161                                            "{0} is not an allowed property value type", new Object JavaDoc[] {type}));
162         } else {
163             return;
164         }
165         }
166         boolean allowedType = false;
167         for (int i = 0; i < allowedTypes.length; i++) {
168         if (allowedTypes[i].equals(typeClass)) {
169             allowedType = true;
170             break;
171         }
172         }
173         if (this.isBoundsChecking() && !allowedType) {
174         throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
175                                            "enterprise.deployment.exceptiontypenotallowedprprtytype",
176                                            "{0} is not an allowed property value type", new Object JavaDoc[] {type}));
177         }
178     }
179     }
180     
181     /**
182     ** Returns the typed value object of this environment property. Throws an IllegalArgumentException if bounds checking is
183     ** true and the value cannot be
184     ** reconciled with the given type.
185     */

186     public Object JavaDoc getValueObject() {
187     if (this.valueObject == null) {
188         this.valueObject = "";
189     }
190     return getObjectFromString(this.getValue(), this.getValueType());
191     }
192     
193     /**
194     ** Returns value type of this environment property.
195     */

196     
197     public Class JavaDoc getValueType() {
198     if (this.type == null) {
199         return String JavaDoc.class;
200     } else {
201         try {
202         return Class.forName(this.type);
203         } catch (Throwable JavaDoc t) {
204         return null;
205         }
206     }
207     }
208     
209      /**
210     ** Returns value type of this environment property. Throws Illegal argument exception if this is not an
211     ** allowed type and bounds checking.
212     */

213     
214     public void setType(String JavaDoc type) {
215     checkType(type);
216     this.type = type;
217     }
218     
219      /**
220     ** Returns value type of this environment property as a classname.
221     */

222     
223     public String JavaDoc getType() {
224     if (type == null && this.isBoundsChecking()) {
225             return String JavaDoc.class.getName();
226     } else {
227             return type;
228         }
229     }
230
231     public void setMappedName(String JavaDoc mName) {
232         mappedName = mName;
233     }
234
235     public String JavaDoc getMappedName() {
236         return (mappedName != null)? mappedName : "";
237     }
238
239     
240      /**
241     ** Sets the value of the environment property to the given string.
242     */

243     
244     public void setValue(String JavaDoc value) {
245     this.value = value;
246         this.setValueCalled = true;
247     super.changed();
248     }
249
250     public boolean hasAValue() {
251         return setValueCalled;
252     }
253     
254      /**
255     ** Returns true if the argument is an environment property of the same name, false else.
256     */

257     
258     public boolean equals(Object JavaDoc other) {
259     if (other instanceof EnvironmentProperty &&
260         this.getName().equals( ((EnvironmentProperty) other).getName() )) {
261         return true;
262     }
263     return false;
264     }
265     
266     /**
267     ** The hashCode of an environment property is the same as that of the name String.
268     */

269     public int hashCode() {
270     return this.getName().hashCode();
271     }
272     
273     /**
274     ** Returns a String representation of this environment property.
275     */

276     public void print(StringBuffer JavaDoc toStringBuffer) {
277     toStringBuffer.append("Env-Prop: ").append(super.getName()).append("@");
278         printInjectableResourceInfo(toStringBuffer);
279         toStringBuffer.append("@").append(this.getType()).append("@").append(this.getValue()).append("@").append("@").append(super.getDescription());
280     }
281     
282     private Object JavaDoc getObjectFromString(String JavaDoc string, Class JavaDoc type) {
283         if (type == null && !this.isBoundsChecking()) {
284             Object JavaDoc obj = getValueObjectUsingAllowedTypes(string);
285             if (obj != null) return obj;
286         }
287     if (string == null || ("".equals(string) && !type.equals(String JavaDoc.class))) {
288         return null;
289     }
290     try {
291             if (String JavaDoc.class.equals(type)) {
292         return string;
293             } else if (Boolean JavaDoc.class.equals(type)) {
294         return new Boolean JavaDoc(string);
295         } else if (Integer JavaDoc.class.equals(type)) {
296         return new Integer JavaDoc(string);
297         } else if (Double JavaDoc.class.equals(type)) {
298         return new Double JavaDoc(string);
299         } else if (Float JavaDoc.class.equals(type)) {
300         return new Float JavaDoc(string);
301         } else if (Short JavaDoc.class.equals(type)) {
302         return new Short JavaDoc(string);
303         } else if (Byte JavaDoc.class.equals(type)) {
304         return new Byte JavaDoc(string);
305         } else if (Long JavaDoc.class.equals(type)) {
306         return new Long JavaDoc(string);
307         } else if (Character JavaDoc.class.equals(type)) {
308                 if (string.length() != 1) {
309                     throw new IllegalArgumentException JavaDoc();
310                 } else {
311                     return new Character JavaDoc(string.charAt(0));
312                 }
313             }
314     } catch (Throwable JavaDoc t) {
315         throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
316                                        "enterprise.deployment.exceptioncouldnotcreateinstancetype",
317                                        "Could not create instance of {0} from {1}\n reason: {2}" + t, new Object JavaDoc[] {type, string, t}));
318     }
319     throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
320                                        "enterprise.deployment.exceptionillegaltypeenvproperty",
321                                        "Illegal type for environment properties: {0}", new Object JavaDoc[] {type}));
322     }
323     
324
325     private Object JavaDoc getValueObjectUsingAllowedTypes(String JavaDoc string)
326                                   throws IllegalArgumentException JavaDoc {
327         if (this.type.equals(int.class.getName())) {
328             return new Integer JavaDoc(string);
329         } else if (this.type.equals(long.class.getName())) {
330             return new Long JavaDoc(string);
331         } else if (this.type.equals(short.class.getName())) {
332             return new Short JavaDoc(string);
333         } else if (this.type.equals(boolean.class.getName())) {
334             return new Boolean JavaDoc(string);
335         } else if (this.type.equals(float.class.getName())) {
336             return new Float JavaDoc(string);
337         } else if (this.type.equals(double.class.getName())) {
338             return new Double JavaDoc(string);
339         } else if (this.type.equals(byte.class.getName())) {
340             return new Byte JavaDoc(string);
341         } else if (this.type.equals(char.class.getName())) {
342             if (string.length() != 1) {
343                 throw new IllegalArgumentException JavaDoc();
344             } else {
345                 return new Character JavaDoc(string.charAt(0));
346             }
347         }
348         return null;
349     }
350
351     //
352
// InjectableResource implementation
353
//
354
public void addInjectionTarget(InjectionTarget target) {
355         if (injectionTargets==null) {
356             injectionTargets = new HashSet JavaDoc<InjectionTarget>();
357         }
358         boolean found = false;
359         for (InjectionTarget injTarget : injectionTargets) {
360             if (injTarget.equals(target)) {
361                 found = true;
362                 break;
363             }
364         }
365         if (!found) {
366             injectionTargets.add(target);
367         }
368     }
369     
370     public Set JavaDoc<InjectionTarget> getInjectionTargets() {
371         return injectionTargets;
372     }
373
374     public boolean isInjectable() {
375         return (injectionTargets!=null && injectionTargets.size()>0);
376         //return (getInjectTargetName() != null);
377
}
378
379     public String JavaDoc getComponentEnvName() {
380         return getName();
381     }
382
383     public String JavaDoc getInjectResourceType() {
384         return type;
385     }
386
387     public void setInjectResourceType(String JavaDoc resourceType) {
388         type = resourceType;
389     }
390     
391
392     public StringBuffer JavaDoc printInjectableResourceInfo
393         (StringBuffer JavaDoc toStringBuffer) {
394         
395         if( isInjectable() ) {
396             for (InjectionTarget target : getInjectionTargets()) {
397                 if( target.isFieldInjectable() ) {
398                     toStringBuffer.append("Field-Injectable Resource. Class name = ").
399                             append(target.getClassName()).append(" Field name=").
400                             append(target.getFieldName());
401                 } else {
402                     toStringBuffer.append("Method-Injectable Resource. Class name =").
403                             append(target.getClassName()).append(" Method =").
404                             append(target.getMethodName());
405                 }
406             }
407         } else {
408             toStringBuffer.append("Non-Injectable Resource");
409         }
410
411         return toStringBuffer;
412     }
413     
414     //
415
// End InjectableResource implementation
416
//
417

418     
419 }
420
Popular Tags