001: /*
002: * $Id: ServletConfigPropertyReader.java,v 1.19 2007/08/13 08:40:08 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.servlet;
008:
009: import java.util.Enumeration;
010: import java.util.Iterator;
011:
012: import javax.servlet.ServletConfig;
013:
014: import org.xins.common.MandatoryArgumentChecker;
015: import org.xins.common.collections.EnumerationIterator;
016: import org.xins.common.collections.PropertyReader;
017: import org.xins.common.collections.PropertyReaderUtils;
018:
019: /**
020: * Implementation of a <code>PropertyReader</code> that returns the
021: * initialization properties from a <code>ServletConfig</code> object.
022: *
023: * @version $Revision: 1.19 $ $Date: 2007/08/13 08:40:08 $
024: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
025: *
026: * @since XINS 1.0.0
027: */
028: public final class ServletConfigPropertyReader implements
029: PropertyReader {
030:
031: /**
032: * The servlet configuration object.
033: */
034: private final ServletConfig _servletConfig;
035:
036: /**
037: * The number of properties. This field is lazily initialized by
038: * {@link #size()}.
039: */
040: private int _size;
041:
042: /**
043: * Constructs a new <code>ServletConfigPropertyReader</code>.
044: *
045: * @param servletConfig
046: * the {@link ServletConfig} object, cannot be <code>null</code>.
047: *
048: * @throws IllegalArgumentException
049: * if <code>servletConfig == null</code>.
050: */
051: public ServletConfigPropertyReader(ServletConfig servletConfig)
052: throws IllegalArgumentException {
053:
054: // Check preconditions
055: MandatoryArgumentChecker.check("servletConfig", servletConfig);
056:
057: _servletConfig = servletConfig;
058: }
059:
060: /**
061: * Retrieves the value of the property with the specified name.
062: *
063: * @param name
064: * the name of the property, cannot be <code>null</code>.
065: *
066: * @return
067: * the value of the property, possibly <code>null</code>.
068: *
069: * @throws IllegalArgumentException
070: * if <code>name == null</code>.
071: */
072: public String get(String name) throws IllegalArgumentException {
073: MandatoryArgumentChecker.check("name", name);
074: return _servletConfig.getInitParameter(name);
075: }
076:
077: /**
078: * Returns an <code>Iterator</code> that returns all property names.
079: *
080: * @return
081: * an {@link Iterator} for all property names, never <code>null</code>.
082: */
083: public Iterator getNames() {
084: return new EnumerationIterator(_servletConfig
085: .getInitParameterNames());
086: }
087:
088: /**
089: * Determines the number of properties.
090: *
091: * @return
092: * the size, always >= 0.
093: */
094: public int size() {
095: if (_size < 0) {
096: int size = 0;
097: Enumeration e = _servletConfig.getInitParameterNames();
098: while (e.hasMoreElements()) {
099: e.nextElement();
100: size++;
101: }
102: _size = size;
103: }
104:
105: return _size;
106: }
107:
108: /**
109: * Compares this object with the specified argument for equality.
110: *
111: * @param obj
112: * the object to compare with, can be <code>null</code>.
113: *
114: * @return
115: * <code>true</code> if the objects <code>a</code> and <code>b</code>
116: * are considered to be equal, <code>false</code> if they are considered
117: * different.
118: */
119: public boolean equals(Object obj) {
120: return PropertyReaderUtils.equals(this , obj);
121: }
122:
123: /**
124: * Returns a hash code value for this object.
125: *
126: * @return
127: * a hash code value for this object.
128: */
129: public int hashCode() {
130: // XXX: This is compute-intensive.
131: // Possible optimization is to store the hash code in a field.
132: return PropertyReaderUtils.hashCode(this);
133: }
134: }
|