001: /*---------------------------------------------------------------------------*\
002: $Id: ConfiguredOutputHandler.java 7041 2007-09-09 01:04:47Z bmc $
003: ---------------------------------------------------------------------------
004: This software is released under a BSD-style license:
005:
006: Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions are
010: met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. The end-user documentation included with the redistribution, if any,
016: must include the following acknowlegement:
017:
018: "This product includes software developed by Brian M. Clapper
019: (bmc@clapper.org, http://www.clapper.org/bmc/). That software is
020: copyright (c) 2004-2007 Brian M. Clapper."
021:
022: Alternately, this acknowlegement may appear in the software itself,
023: if wherever such third-party acknowlegements normally appear.
024:
025: 3. Neither the names "clapper.org", "curn", nor any of the names of the
026: project contributors may be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact bmc@clapper.org.
029:
030: 4. Products derived from this software may not be called "curn", nor may
031: "clapper.org" appear in their names without prior written permission
032: of Brian M. Clapper.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
036: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
037: NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
038: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
039: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
040: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
041: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: \*---------------------------------------------------------------------------*/
045:
046: package org.clapper.curn;
047:
048: import java.util.HashMap;
049: import java.util.Map;
050:
051: import org.clapper.util.logging.Logger;
052:
053: /**
054: * Contains parsed configuration information for an output handler,
055: * including the class name and output handler name (which is really the
056: * section name).
057: *
058: * @see CurnConfig
059: * @see Curn
060: *
061: * @version <tt>$Revision: 7041 $</tt>
062: */
063: public class ConfiguredOutputHandler implements Comparable {
064: /*----------------------------------------------------------------------*\
065: Private Data Items
066: \*----------------------------------------------------------------------*/
067:
068: private final String sectionName;
069: private final String className;
070: private final String name;
071: private OutputHandler handler = null;
072: private boolean disabled = false;
073: private final Map<String, String> extraVariables = new HashMap<String, String>();
074:
075: /**
076: * For log messages
077: */
078: private static final Logger log = new Logger(
079: ConfiguredOutputHandler.class);
080:
081: /*----------------------------------------------------------------------*\
082: Constructor
083: \*----------------------------------------------------------------------*/
084:
085: /**
086: * Construct a new <tt>ConfiguredOutputHandler</tt> object.
087: *
088: * @param name a unique name for the handler
089: * @param sectionName the name of the configuration file section
090: * where the output handler was defined, or null
091: * if the handler has no corresponding section
092: * @param className the output handler's class name
093: */
094: public ConfiguredOutputHandler(String name, String sectionName,
095: String className) {
096: log.debug("section="
097: + ((sectionName == null) ? "null" : sectionName));
098: this .sectionName = sectionName;
099: this .className = className;
100: this .name = name;
101: }
102:
103: /*----------------------------------------------------------------------*\
104: Public Methods
105: \*----------------------------------------------------------------------*/
106:
107: /**
108: * Get the class name associated with this output handler
109: *
110: * @return the class name
111: */
112: public String getClassName() {
113: return className;
114: }
115:
116: /**
117: * Get the configuration file section name where this output handler
118: * was defined
119: *
120: * @return the section name, or null for none
121: */
122: public String getSectionName() {
123: return sectionName;
124: }
125:
126: /**
127: * Get the unique name for this handler
128: *
129: * @return the unique name. Should never be null.
130: */
131: public String getName() {
132: return name;
133: }
134:
135: /**
136: * Mark this handler disabled, usually in response to a plug-in
137: * action.
138: *
139: * @see #isDisabled
140: */
141: public void disable() {
142: disabled = true;
143: }
144:
145: /**
146: * Determine whether this handler is disabled.
147: *
148: * @return <tt>true</tt> if disabled, <tt>false</tt> if enabled
149: *
150: * @see #disable
151: */
152: public boolean isDisabled() {
153: return disabled;
154: }
155:
156: /**
157: * Get the actual <tt>OutputHandler</tt> object, instantiating it if
158: * it hasn't already been instantiated.
159: *
160: * @return the <tt>OutputHandler</tt> object
161: *
162: * @throws CurnException on error
163: */
164: public synchronized OutputHandler getOutputHandler()
165: throws CurnException {
166: if (this .handler == null) {
167: this .handler = OutputHandlerFactory
168: .getOutputHandler(className);
169: this .handler.setName(name);
170: }
171:
172: return this .handler;
173: }
174:
175: /**
176: * Get the extra variables defined with this output handler.
177: *
178: * @return a <tt>Map</tt> of variables. The map is keyed by variable
179: * name, and each entry is a variable value. The <tt>Map</tt>
180: * will be empty, but never null, if there are no extra variables
181: * for this output handler.
182: */
183: public Map getExtraVariables() {
184: return extraVariables;
185: }
186:
187: /**
188: * Compares this object with the specified object for order. Returns a
189: * negative integer, zero, or a positive integer as this object is less
190: * than, equal to, or greater than the specified object.
191: *
192: * @param o the object to compare
193: *
194: * @return a negative integer, zero, or a positive integer as this object
195: * is less than, equal to, or greater than the specified object.
196: *
197: * @throws ClassCastException if the specified object's type prevents
198: * it from being compared to this Object.
199: */
200: public int compareTo(final Object o) throws ClassCastException {
201: int cmp = 0;
202:
203: if (!(o instanceof ConfiguredOutputHandler))
204: cmp = this .hashCode() - o.hashCode();
205:
206: else {
207: ConfiguredOutputHandler other = (ConfiguredOutputHandler) o;
208:
209: cmp = this .sectionName.compareTo(other.sectionName);
210: }
211:
212: return cmp;
213: }
214:
215: /*----------------------------------------------------------------------*\
216: Package-visible Methods
217: \*----------------------------------------------------------------------*/
218:
219: /**
220: * Add an extra variable to the list of extra variables.
221: *
222: * @param name the variable name
223: * @param value the value
224: */
225: void addExtraVariable(final String name, final String value) {
226: extraVariables.put(name, value);
227: }
228: }
|