1 16 17 package org.apache.naming.config; 18 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import java.util.TreeSet ; 27 28 import javax.naming.CompositeName ; 29 import javax.naming.InvalidNameException ; 30 import javax.naming.StringRefAddr ; 31 32 import org.apache.commons.lang.builder.ToStringBuilder; 33 import org.apache.commons.lang.builder.ToStringStyle; 34 import org.apache.naming.ResourceRef; 35 36 42 public final class Config 43 { 44 47 public static final class Naming 48 { 49 50 private final Collection contextList = new LinkedList (); 51 52 57 public void addContext(Context context) 58 { 59 contextList.add(context); 60 } 61 62 67 public Collection getContextList() 68 { 69 return Collections.unmodifiableCollection(contextList); 70 } 71 72 78 public Set generateSortedSubcontextNameSet() throws InvalidNameException 79 { 80 Set sortedSubcontextNameSet = new TreeSet (); 81 for (Iterator i = contextList.iterator(); i.hasNext();) 82 { 83 Context context = (Context) i.next(); 84 context.addSubContextNames(sortedSubcontextNameSet); 85 } 86 return Collections.unmodifiableSet(sortedSubcontextNameSet); 87 } 88 89 94 public String toString() 95 { 96 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) 97 .append("contextList", contextList) 98 .toString(); 99 } 100 } 101 102 107 public static final class Context 108 { 109 private String name; 110 private final Collection environmentList = new LinkedList (); 111 private final Collection resourceList = new LinkedList (); 112 113 118 public void addEnvironment(Environment environment) 119 { 120 environmentList.add(environment); 121 } 122 123 131 public void addSubContextNames(Set sortedSubcontextNameSet) throws InvalidNameException 132 { 133 if ((name != null) && !environmentList.iterator().hasNext() && !resourceList.iterator().hasNext()) 134 { 135 sortedSubcontextNameSet.add(name); 136 } 137 for (Iterator i = environmentList.iterator(); i.hasNext();) 138 { 139 Environment e = (Environment) i.next(); 140 CompositeName name = new CompositeName (e.getName()); 141 addSubContextNames(name, sortedSubcontextNameSet); 142 } 143 for (Iterator i = resourceList.iterator(); i.hasNext();) 144 { 145 Resource r = (Resource) i.next(); 146 CompositeName name = new CompositeName (r.getName()); 147 addSubContextNames(name, sortedSubcontextNameSet); 148 } 149 } 150 151 private void addSubContextNames(CompositeName name, Set sortedSubcontextNameSet) { 152 for (int j = 1; j <= name.size() - 1; j++) { 153 sortedSubcontextNameSet.add(name.getPrefix(j).toString()); 154 } 155 } 156 157 162 public void addResource(Resource resource) 163 { 164 resourceList.add(resource); 165 } 166 167 172 public Collection getEnvironmentList() 173 { 174 return Collections.unmodifiableCollection(environmentList); 175 } 176 177 182 public String getName() 183 { 184 return name; 185 } 186 187 192 public void setName(String name) 193 { 194 this.name = name; 195 } 196 197 202 public Collection getResourceList() 203 { 204 return Collections.unmodifiableCollection(resourceList); 205 } 206 207 213 public String toString() 214 { 215 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) 216 .append("name", name) 217 .append("environmentList", environmentList) 218 .append("resourceList", resourceList) 219 .toString(); 220 } 221 } 222 223 229 public static final class Environment 230 { 231 private String name; 232 private String value; 233 private String type; 234 235 240 public String getName() 241 { 242 return name; 243 } 244 245 250 public void setName(String name) 251 { 252 this.name = name; 253 } 254 255 260 public String getType() 261 { 262 return type; 263 } 264 265 270 public void setType(String type) 271 { 272 this.type = type; 273 } 274 275 280 public String getValue() 281 { 282 return value; 283 } 284 285 290 public void setValue(String value) 291 { 292 this.value = value; 293 } 294 295 301 public String toString() 302 { 303 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) 304 .append("name", name) 305 .append("type", type) 306 .append("value", value) 307 .toString(); 308 } 309 310 321 public Object createValue() 322 { 324 if (type.equals(String .class.getName())) 325 { 326 return value; 327 } 328 else if (type.equals(Boolean .class.getName())) 329 { 330 return Boolean.valueOf(value); 331 } 332 else if (type.equals(Integer .class.getName())) 333 { 334 return Integer.valueOf(value); 335 } 336 else if (type.equals(Short .class.getName())) 337 { 338 return Short.valueOf(value); 339 } 340 else if (type.equals(Character .class.getName())) 341 { 342 return new Character (value.charAt(0)); 343 } 344 else if (type.equals(Double .class.getName())) 345 { 346 return Double.valueOf(value); 347 } 348 else if (type.equals(Float .class.getName())) 349 { 350 return Float.valueOf(value); 351 } 352 else if (type.equals(Byte .class.getName())) 353 { 354 return Byte.valueOf(value); 355 } 356 else if (type.equals(Long .class.getName())) 357 { 358 return Long.valueOf(value); 359 } 360 return null; 361 } 362 } 363 364 370 public static final class Resource 371 { 372 private String name; 373 private String type; 374 private final Map parameters = new HashMap (); 375 376 382 public void addParameter(String name, String value) 383 { 384 parameters.put(name, value); 385 } 386 387 392 public String getName() 393 { 394 return name; 395 } 396 397 402 public void setName(String name) 403 { 404 this.name = name; 405 } 406 407 413 public Map getParameters() 414 { 415 return parameters; 416 } 417 418 423 public String getType() 424 { 425 return type; 426 } 427 428 433 public void setType(String type) 434 { 435 this.type = type; 436 } 437 438 444 public Object createValue() 445 { 447 ResourceRef ref = new ResourceRef(type, null, null, null); 448 for (Iterator i = parameters.keySet().iterator(); i.hasNext();) 449 { 450 String name = (String ) i.next(); 451 String value = (String ) parameters.get(name); 452 ref.add(new StringRefAddr (name, value)); 453 } 454 return ref; 455 } 456 457 462 public String toString() 463 { 464 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) 465 .append("name", name) 466 .append("type", type) 467 .append("parameters", parameters) 468 .toString(); 469 } 470 } 471 } 472 473 | Popular Tags |