001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.lucene.store.jdbc;
018:
019: import java.util.Properties;
020:
021: import org.apache.lucene.store.jdbc.handler.MarkDeleteFileEntryHandler;
022: import org.apache.lucene.store.jdbc.index.FetchOnBufferReadJdbcIndexInput;
023: import org.apache.lucene.store.jdbc.index.RAMAndFileJdbcIndexOutput;
024:
025: /**
026: * A file entry level settings. An abstract view of any type of setting that cab be
027: * used by the actual file entry handler that uses it.
028: * <p/>
029: * Holds the {@link #FILE_ENTRY_HANDLER_TYPE} that defines the type of the
030: * {@link org.apache.lucene.store.jdbc.handler.FileEntryHandler} that will be created
031: * and initialized with the settings.
032: * <p/>
033: * Default values for a new instanciated instnce are: {@link MarkDeleteFileEntryHandler} for
034: * the {@link #FILE_ENTRY_HANDLER_TYPE} setting, {@link FetchOnBufferReadJdbcIndexInput} for the
035: * {@link #INDEX_INPUT_TYPE_SETTING} setting, and {@link RAMAndFileJdbcIndexOutput} for the
036: * {@link #INDEX_OUTPUT_TYPE_SETTING} setting.
037: *
038: * @author kimchy
039: */
040: public class JdbcFileEntrySettings {
041:
042: /**
043: * The class name of the {@link org.apache.lucene.store.IndexInput}. Only applies
044: * to {@link org.apache.lucene.store.jdbc.handler.FileEntryHandler}s that use it.
045: */
046: public static final String INDEX_INPUT_TYPE_SETTING = "indexInput.type";
047:
048: /**
049: * The class name of the {@link org.apache.lucene.store.IndexOutput}. Only applies
050: * to {@link org.apache.lucene.store.jdbc.handler.FileEntryHandler}s that use it.
051: */
052: public static final String INDEX_OUTPUT_TYPE_SETTING = "indexOutput.type";
053:
054: /**
055: * The class name of the {@link org.apache.lucene.store.jdbc.handler.FileEntryHandler}.
056: */
057: public static final String FILE_ENTRY_HANDLER_TYPE = "type";
058:
059: private Properties settings = new Properties();
060:
061: /**
062: * Creates a new file entry settings, and intialize it to default values.
063: */
064: public JdbcFileEntrySettings() {
065: setClassSetting(JdbcFileEntrySettings.FILE_ENTRY_HANDLER_TYPE,
066: MarkDeleteFileEntryHandler.class);
067: setClassSetting(JdbcFileEntrySettings.INDEX_INPUT_TYPE_SETTING,
068: FetchOnBufferReadJdbcIndexInput.class);
069: setClassSetting(
070: JdbcFileEntrySettings.INDEX_OUTPUT_TYPE_SETTING,
071: RAMAndFileJdbcIndexOutput.class);
072: }
073:
074: /**
075: * Returns the inner java properties.
076: */
077: public Properties getProperties() {
078: return settings;
079: }
080:
081: /**
082: * Returns the value match for the given setting. <code>null</code> if no
083: * setting is found.
084: *
085: * @param setting The setting name
086: * @return The value of the setting, or <code>null</code> if none is found
087: */
088: public String getSetting(String setting) {
089: return settings.getProperty(setting);
090: }
091:
092: /**
093: * Returns the value that matches the given setting. If none is found,
094: * the default value is used.
095: *
096: * @param setting The setting name
097: * @param defaultValue The default value to be used if no setting is found
098: * @return The value of the setting, or defaultValue if none is found.
099: */
100: public String getSetting(String setting, String defaultValue) {
101: return settings.getProperty(setting, defaultValue);
102: }
103:
104: /**
105: * Returns the float value that matches the given setting. If none if found,
106: * the default value is used.
107: *
108: * @param setting The setting name
109: * @param defaultValue The default value to be used if no setting is found
110: * @return The value of the setting, or defaultValue if none is found.
111: */
112: public float getSettingAsFloat(String setting, float defaultValue) {
113: String sValue = getSetting(setting);
114: if (sValue == null) {
115: return defaultValue;
116: }
117: return Float.parseFloat(sValue);
118: }
119:
120: /**
121: * Returns the int value that matches the given setting. If none if found,
122: * the default value is used.
123: *
124: * @param setting The setting name
125: * @param defaultValue The default value to be used if no setting is found
126: * @return The value of the setting, or defaultValue if none is found.
127: */
128: public int getSettingAsInt(String setting, int defaultValue) {
129: String sValue = getSetting(setting);
130: if (sValue == null) {
131: return defaultValue;
132: }
133: return Integer.parseInt(sValue);
134: }
135:
136: /**
137: * Returns the long value that matches the given setting. If none if found,
138: * the default value is used.
139: *
140: * @param setting The setting name
141: * @param defaultValue The default value to be used if no setting is found
142: * @return The value of the setting, or defaultValue if none is found.
143: */
144: public long getSettingAsLong(String setting, long defaultValue) {
145: String sValue = getSetting(setting);
146: if (sValue == null) {
147: return defaultValue;
148: }
149: return Long.parseLong(sValue);
150: }
151:
152: /**
153: * Returns the class value that matches the given setting. If none if found,
154: * the default value is used.
155: *
156: * @param setting The setting name
157: * @param defaultValue The default value to be used if no setting is found
158: * @return The value of the setting, or defaultValue if none is found.
159: */
160: public Class getSettingAsClass(String setting, Class defaultValue)
161: throws ClassNotFoundException {
162: return getSettingAsClass(setting, defaultValue, Thread
163: .currentThread().getContextClassLoader());
164: }
165:
166: /**
167: * Returns the class value that matches the given setting. If none if found,
168: * the default value is used.
169: *
170: * @param setting The setting name
171: * @param defaultValue The default value to be used if no setting is found
172: * @param classLoader The class loader to be used to load the class
173: * @return The value of the setting, or defaultValue if none is found.
174: * @throws ClassNotFoundException
175: */
176: public Class getSettingAsClass(String setting, Class defaultValue,
177: ClassLoader classLoader) throws ClassNotFoundException {
178: String sValue = getSetting(setting);
179: if (sValue == null) {
180: return defaultValue;
181: }
182: return Class.forName(sValue, true, classLoader);
183: }
184:
185: /**
186: * Returns the boolean value that matches the given setting. If none if found,
187: * the default value is used.
188: *
189: * @param setting The setting name
190: * @param defaultValue The default value to be used if no setting is found
191: * @return The value of the setting, or defaultValue if none is found.
192: */
193: public boolean getSettingAsBoolean(String setting,
194: boolean defaultValue) {
195: String sValue = getSetting(setting);
196: if (sValue == null) {
197: return defaultValue;
198: }
199: return Boolean.valueOf(sValue).booleanValue();
200: }
201:
202: public JdbcFileEntrySettings setSetting(String setting, String value) {
203: this .settings.setProperty(setting, value);
204: return this ;
205: }
206:
207: public JdbcFileEntrySettings setBooleanSetting(String setting,
208: boolean value) {
209: setSetting(setting, String.valueOf(value));
210: return this ;
211: }
212:
213: public JdbcFileEntrySettings setFloatSetting(String setting,
214: float value) {
215: setSetting(setting, String.valueOf(value));
216: return this ;
217: }
218:
219: public JdbcFileEntrySettings setIntSetting(String setting, int value) {
220: setSetting(setting, String.valueOf(value));
221: return this ;
222: }
223:
224: public JdbcFileEntrySettings setLongSetting(String setting,
225: long value) {
226: setSetting(setting, String.valueOf(value));
227: return this ;
228: }
229:
230: public JdbcFileEntrySettings setClassSetting(String setting,
231: Class clazz) {
232: setSetting(setting, clazz.getName());
233: return this;
234: }
235: }
|