001: /*---------------------------------------------------------------------------*\
002: $Id: OutputHandlerFactory.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.lang.reflect.Constructor;
049: import java.lang.reflect.InvocationTargetException;
050:
051: import org.clapper.util.logging.Logger;
052:
053: /**
054: * This class provides a factory for retrieving a specific
055: * <tt>OutputHandler</tt> implementation.
056: *
057: * @see OutputHandler
058: *
059: * @version <tt>$Revision: 7041 $</tt>
060: */
061: public class OutputHandlerFactory {
062: /*----------------------------------------------------------------------*\
063: Private Data Items
064: \*----------------------------------------------------------------------*/
065:
066: /**
067: * For log messages
068: */
069: private static final Logger log = new Logger(
070: OutputHandlerFactory.class);
071:
072: /*----------------------------------------------------------------------*\
073: Constructor
074: \*----------------------------------------------------------------------*/
075:
076: private OutputHandlerFactory() {
077: // Can't be instantiated directly.
078: }
079:
080: /*----------------------------------------------------------------------*\
081: Public Methods
082: \*----------------------------------------------------------------------*/
083:
084: /**
085: * Get an instance of the named <tt>OutputHandler</tt> class. This
086: * method loads the specified class, verifies that it conforms to the
087: * {@link OutputHandler} interface, instantiates it (via its default
088: * constructor), and returns the resulting <tt>OutputHandler</tt> object.
089: *
090: * @param cls the class for the output handler
091: *
092: * @return an <tt>OutputHandler</tt> object
093: *
094: * @throws CurnException Error instantiating class. The exception will
095: * contain the real underlying exception.
096: */
097: public static OutputHandler getOutputHandler(final Class<?> cls)
098: throws CurnException {
099: try {
100: log.debug("Instantiating output handler: " + cls.getName());
101:
102: Constructor constructor = cls.getConstructor();
103: return (OutputHandler) constructor.newInstance();
104: }
105:
106: catch (NoSuchMethodException ex) {
107: throw new CurnException(ex);
108: }
109:
110: catch (InvocationTargetException ex) {
111: throw new CurnException(ex);
112: }
113:
114: catch (InstantiationException ex) {
115: throw new CurnException(ex);
116: }
117:
118: catch (IllegalAccessException ex) {
119: throw new CurnException(ex);
120: }
121: }
122:
123: /**
124: * Get an instance of the named <tt>OutputHandler</tt> class. This
125: * method loads the specified class, verifies that it conforms to the
126: * {@link OutputHandler} interface, instantiates it (via its default
127: * constructor), and returns the resulting <tt>OutputHandler</tt> object.
128: *
129: * @param className the class name
130: *
131: * @return an <tt>OutputHandler</tt> object
132: *
133: * @throws CurnException Error instantiating class. The exception will
134: * contain the real underlying exception.
135: */
136: public static OutputHandler getOutputHandler(final String className)
137: throws CurnException {
138: try {
139: return getOutputHandler(Class.forName(className));
140: }
141:
142: catch (ClassNotFoundException ex) {
143: throw new CurnException(ex);
144: }
145: }
146: }
|