001: /*---------------------------------------------------------------------------*\
002: $Id: DataPersisterFactory.java 6548 2006-11-03 03:40:07Z bmc $
003: \*---------------------------------------------------------------------------*/
004:
005: package org.clapper.curn;
006:
007: import org.clapper.util.classutil.ClassUtil;
008: import org.clapper.util.classutil.ClassUtilException;
009: import org.clapper.util.config.ConfigurationException;
010:
011: /**
012: * Class that's used to create a {@link DataPersister}. This class is
013: * implemented as a plug-in, so that <i>curn</i>'s plug-in discovery
014: * mechanism will find it and automatically configure it. The factory
015: * methods, though, are static; the plug-in logic merely sets up the data
016: * structures that are used by the static methods.
017: *
018: * @see PlugIn
019: * @see DataPersister
020: *
021: * @version <tt>$Revision: 6548 $</tt>
022: */
023: public final class DataPersisterFactory implements
024: MainConfigItemPlugIn, PostConfigPlugIn {
025: /*----------------------------------------------------------------------*\
026: Private Constants
027: \*----------------------------------------------------------------------*/
028:
029: private static final String CFG_VAR_PERSISTER_CLASS = "DataPersisterClass";
030: private static final String DEF_DATA_PERSISTER_CLASS_NAME = "org.clapper.curn.XMLDataPersister";
031:
032: /*----------------------------------------------------------------------*\
033: Private Instance Data
034: \*----------------------------------------------------------------------*/
035:
036: /**
037: * The data persister class name.
038: */
039: private static String dataPersisterClassName = DEF_DATA_PERSISTER_CLASS_NAME;
040:
041: /**
042: * The data persister object to use.
043: */
044: private static DataPersister dataPersisterInstance = null;
045:
046: /*----------------------------------------------------------------------*\
047: Constructor
048: \*----------------------------------------------------------------------*/
049:
050: /**
051: * Creates a new instance of DataPersisterFactory
052: */
053: public DataPersisterFactory() {
054: }
055:
056: /*----------------------------------------------------------------------*\
057: Public Methods
058: \*----------------------------------------------------------------------*/
059:
060: /**
061: * Get the configured data persister object.
062: *
063: * @return the configured data persister.
064: */
065: public static DataPersister getInstance() {
066: return dataPersisterInstance;
067: }
068:
069: /**
070: * Initialize the plug-in. This method is called before any of the
071: * plug-in methods are called.
072: *
073: * @throws CurnException on error
074: */
075: public void initPlugIn() throws CurnException {
076: }
077:
078: /**
079: * Get a displayable name for the plug-in.
080: *
081: * @return the name
082: */
083: public String getPlugInName() {
084: return null; // "invisible" plug-in
085: }
086:
087: /**
088: * Get the sort key for this plug-in.
089: *
090: * @return the sort key string.
091: */
092: public String getPlugInSortKey() {
093: return ClassUtil.getShortClassName(getClass().getName());
094: }
095:
096: /**
097: * Called immediately after <i>curn</i> has read and processed a
098: * configuration item in the main [curn] configuration section. All
099: * configuration items are passed, one by one, to each loaded plug-in.
100: * If a plug-in class is not interested in a particular configuration
101: * item, this method should simply return without doing anything. Note
102: * that some configuration items may simply be variable assignment;
103: * there's no real way to distinguish a variable assignment from a
104: * blessed configuration item.
105: *
106: * @param sectionName the name of the configuration section where
107: * the item was found
108: * @param paramName the name of the parameter
109: * @param config the {@link CurnConfig} object
110: *
111: * @throws CurnException on error
112: *
113: * @see CurnConfig
114: */
115: public void runMainConfigItemPlugIn(String sectionName,
116: String paramName, CurnConfig config) throws CurnException {
117: try {
118: if (paramName.equals(CFG_VAR_PERSISTER_CLASS)) {
119: dataPersisterClassName = config.getConfigurationValue(
120: sectionName, paramName);
121: }
122: }
123:
124: catch (ConfigurationException ex) {
125: throw new CurnException(ex);
126: }
127: }
128:
129: /**
130: * Called after the entire configuration has been read and parsed, but
131: * before any feeds are processed. Intercepting this event is useful
132: * for plug-ins that want to adjust the configuration. For instance,
133: * the <i>curn</i> command-line wrapper intercepts this plug-in event
134: * so it can adjust the configuration to account for command line
135: * options.
136: *
137: * @param config the parsed {@link CurnConfig} object
138: *
139: * @throws CurnException on error
140: *
141: * @see CurnConfig
142: */
143: public void runPostConfigPlugIn(CurnConfig config)
144: throws CurnException {
145: synchronized (DEF_DATA_PERSISTER_CLASS_NAME) {
146: if (dataPersisterInstance == null) {
147: try {
148: // Try to load and instantiate the data persister class.
149:
150: dataPersisterInstance = (DataPersister) ClassUtil
151: .instantiateClass(dataPersisterClassName);
152: dataPersisterInstance.init(config);
153: }
154:
155: catch (ClassUtilException ex) {
156: throw new CurnException("Can't instantiate data "
157: + "persister class "
158: + dataPersisterClassName, ex);
159: }
160: }
161: }
162: }
163:
164: /*----------------------------------------------------------------------*\
165: Protected Methods
166: \*----------------------------------------------------------------------*/
167:
168: /*----------------------------------------------------------------------*\
169: Private Methods
170: \*----------------------------------------------------------------------*/
171: }
|