1 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 ; 34 import java.lang.reflect.Method ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.Set ; 38 39 43 44 public class EnvironmentProperty extends Descriptor implements InitializationParameter, ContextParameter, WebDescriptor, EnvironmentEntry, InjectionCapable { 45 private String value; 46 private String type; 47 private Object valueObject; 48 private boolean setValueCalled = false; 49 50 private Set <InjectionTarget> injectionTargets; 52 53 private static Class [] 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 .class, 63 java.lang.Boolean .class, 64 java.lang.Integer .class, 65 java.lang.Double .class, 66 java.lang.Byte .class, 67 java.lang.Short .class, 68 java.lang.Long .class, 69 java.lang.Float .class, 70 java.lang.Character .class 71 }; 72 private static LocalStringManagerImpl localStrings = 73 new LocalStringManagerImpl(EnvironmentProperty.class); 74 75 protected String mappedName; 76 77 80 81 public EnvironmentProperty(EnvironmentProperty other) { 82 super(other); 83 value = other.value; 84 type = other.type; 85 valueObject = other.valueObject; 86 } 87 88 91 92 public EnvironmentProperty() { 93 } 94 95 98 99 public EnvironmentProperty(String name, String value, String description) { 100 this(name, value, description, null); 101 } 102 103 108 109 public EnvironmentProperty(String name, String value, String description, String type) { 110 super(name, description); 111 this.value = value; 112 checkType(type); 113 this.type = type; 114 } 115 116 119 120 public String getValue() { 121 if (this.value == null) { 122 this.value = ""; 123 } 124 return value; 125 } 126 127 130 public String getResolvedValue() { 131 return RelativePathResolver.resolvePath(getValue()); 132 } 133 134 139 public Object getResolvedValueObject() { 140 if (this.valueObject == null) { 141 this.valueObject = ""; 142 } 143 return getObjectFromString(this.getResolvedValue(), this.getValueType()); 144 } 145 146 150 151 private void checkType(String type) { 152 if (type != null) { 153 Class typeClass = null; 154 try { 156 typeClass = Class.forName(type); 157 } catch (Throwable t) { 158 if (this.isBoundsChecking()) { 159 throw new IllegalArgumentException (localStrings.getLocalString( 160 "enterprise.deployment..exceptiontypenotallowedpropertytype", 161 "{0} is not an allowed property value type", new Object [] {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 (localStrings.getLocalString( 175 "enterprise.deployment.exceptiontypenotallowedprprtytype", 176 "{0} is not an allowed property value type", new Object [] {type})); 177 } 178 } 179 } 180 181 186 public Object getValueObject() { 187 if (this.valueObject == null) { 188 this.valueObject = ""; 189 } 190 return getObjectFromString(this.getValue(), this.getValueType()); 191 } 192 193 196 197 public Class getValueType() { 198 if (this.type == null) { 199 return String .class; 200 } else { 201 try { 202 return Class.forName(this.type); 203 } catch (Throwable t) { 204 return null; 205 } 206 } 207 } 208 209 213 214 public void setType(String type) { 215 checkType(type); 216 this.type = type; 217 } 218 219 222 223 public String getType() { 224 if (type == null && this.isBoundsChecking()) { 225 return String .class.getName(); 226 } else { 227 return type; 228 } 229 } 230 231 public void setMappedName(String mName) { 232 mappedName = mName; 233 } 234 235 public String getMappedName() { 236 return (mappedName != null)? mappedName : ""; 237 } 238 239 240 243 244 public void setValue(String value) { 245 this.value = value; 246 this.setValueCalled = true; 247 super.changed(); 248 } 249 250 public boolean hasAValue() { 251 return setValueCalled; 252 } 253 254 257 258 public boolean equals(Object other) { 259 if (other instanceof EnvironmentProperty && 260 this.getName().equals( ((EnvironmentProperty) other).getName() )) { 261 return true; 262 } 263 return false; 264 } 265 266 269 public int hashCode() { 270 return this.getName().hashCode(); 271 } 272 273 276 public void print(StringBuffer 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 getObjectFromString(String string, Class type) { 283 if (type == null && !this.isBoundsChecking()) { 284 Object obj = getValueObjectUsingAllowedTypes(string); 285 if (obj != null) return obj; 286 } 287 if (string == null || ("".equals(string) && !type.equals(String .class))) { 288 return null; 289 } 290 try { 291 if (String .class.equals(type)) { 292 return string; 293 } else if (Boolean .class.equals(type)) { 294 return new Boolean (string); 295 } else if (Integer .class.equals(type)) { 296 return new Integer (string); 297 } else if (Double .class.equals(type)) { 298 return new Double (string); 299 } else if (Float .class.equals(type)) { 300 return new Float (string); 301 } else if (Short .class.equals(type)) { 302 return new Short (string); 303 } else if (Byte .class.equals(type)) { 304 return new Byte (string); 305 } else if (Long .class.equals(type)) { 306 return new Long (string); 307 } else if (Character .class.equals(type)) { 308 if (string.length() != 1) { 309 throw new IllegalArgumentException (); 310 } else { 311 return new Character (string.charAt(0)); 312 } 313 } 314 } catch (Throwable t) { 315 throw new IllegalArgumentException (localStrings.getLocalString( 316 "enterprise.deployment.exceptioncouldnotcreateinstancetype", 317 "Could not create instance of {0} from {1}\n reason: {2}" + t, new Object [] {type, string, t})); 318 } 319 throw new IllegalArgumentException (localStrings.getLocalString( 320 "enterprise.deployment.exceptionillegaltypeenvproperty", 321 "Illegal type for environment properties: {0}", new Object [] {type})); 322 } 323 324 325 private Object getValueObjectUsingAllowedTypes(String string) 326 throws IllegalArgumentException { 327 if (this.type.equals(int.class.getName())) { 328 return new Integer (string); 329 } else if (this.type.equals(long.class.getName())) { 330 return new Long (string); 331 } else if (this.type.equals(short.class.getName())) { 332 return new Short (string); 333 } else if (this.type.equals(boolean.class.getName())) { 334 return new Boolean (string); 335 } else if (this.type.equals(float.class.getName())) { 336 return new Float (string); 337 } else if (this.type.equals(double.class.getName())) { 338 return new Double (string); 339 } else if (this.type.equals(byte.class.getName())) { 340 return new Byte (string); 341 } else if (this.type.equals(char.class.getName())) { 342 if (string.length() != 1) { 343 throw new IllegalArgumentException (); 344 } else { 345 return new Character (string.charAt(0)); 346 } 347 } 348 return null; 349 } 350 351 public void addInjectionTarget(InjectionTarget target) { 355 if (injectionTargets==null) { 356 injectionTargets = new HashSet <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 <InjectionTarget> getInjectionTargets() { 371 return injectionTargets; 372 } 373 374 public boolean isInjectable() { 375 return (injectionTargets!=null && injectionTargets.size()>0); 376 } 378 379 public String getComponentEnvName() { 380 return getName(); 381 } 382 383 public String getInjectResourceType() { 384 return type; 385 } 386 387 public void setInjectResourceType(String resourceType) { 388 type = resourceType; 389 } 390 391 392 public StringBuffer printInjectableResourceInfo 393 (StringBuffer 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 418 419 } 420 | Popular Tags |