001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsSearchIndexSource.java,v $
003: * Date : $Date: 2008-02-27 12:05:39 $
004: * Version: $Revision: 1.16 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.search;
033:
034: import org.opencms.main.CmsIllegalArgumentException;
035: import org.opencms.main.CmsLog;
036: import org.opencms.main.OpenCms;
037: import org.opencms.util.CmsStringUtil;
038:
039: import java.util.ArrayList;
040: import java.util.HashMap;
041: import java.util.Iterator;
042: import java.util.List;
043: import java.util.Map;
044:
045: import org.apache.commons.logging.Log;
046:
047: /**
048: * A search index source is a description of a list of Cms resources
049: * to be indexed.<p>
050: *
051: * @author Thomas Weckert
052: *
053: * @version $Revision: 1.16 $
054: *
055: * @since 6.0.0
056: */
057: public class CmsSearchIndexSource implements Comparable {
058:
059: /** The log object for this class. */
060: private static final Log LOG = CmsLog
061: .getLog(CmsSearchIndexSource.class);
062:
063: /** A list of Cms resource types to be indexed. */
064: private List m_documentTypes;
065:
066: /** The indexer. */
067: private I_CmsIndexer m_indexer;
068:
069: /** The class name of the indexer. */
070: private String m_indexerClassName;
071:
072: /** The logical key/name of this index. */
073: private String m_name;
074:
075: /** A map of optional key/value parameters. */
076: private Map m_params;
077:
078: /** A list of Cms resources to be indexed. */
079: private List m_resourcesNames;
080:
081: /**
082: * Creates a new CmsSearchIndexSource.<p>
083: */
084: public CmsSearchIndexSource() {
085:
086: m_params = new HashMap();
087: m_resourcesNames = new ArrayList();
088: m_documentTypes = new ArrayList();
089: }
090:
091: /**
092: * Adds a parameter.<p>
093: *
094: * @param key the key/name of the parameter
095: * @param value the value of the parameter
096: */
097: public void addConfigurationParameter(String key, String value) {
098:
099: m_params.put(key, value);
100: }
101:
102: /**
103: * Adds the name of a document type.<p>
104: *
105: * @param key the name of a document type to add
106: */
107: public void addDocumentType(String key) {
108:
109: m_documentTypes.add(key);
110: }
111:
112: /**
113: * Adds the path of a Cms resource.<p>
114: *
115: * @param resourceName the path of a Cms resource
116: */
117: public void addResourceName(String resourceName) {
118:
119: m_resourcesNames.add(resourceName);
120: }
121:
122: /**
123: * Returns <code>0</code> if the given object is an index source with the same name. <p>
124: *
125: * Note that the name of an index source has to be unique within OpenCms.<p>
126: *
127: * @param obj another index source
128: *
129: * @return <code>0</code> if the given object is an index source with the same name
130: *
131: * @see java.lang.Comparable#compareTo(java.lang.Object)
132: */
133: public int compareTo(Object obj) {
134:
135: if (obj == this ) {
136: return 0;
137: }
138: if (obj instanceof CmsSearchIndexSource) {
139: return getName().compareTo(
140: ((CmsSearchIndexSource) obj).getName());
141: }
142: return -1;
143: }
144:
145: /**
146: * Two index sources are consided equal if their names as returned by {@link #getName()} is equal.<p>
147: *
148: * Note that the name of an index source has to be unique within OpenCms.<p>
149: *
150: * @see java.lang.Object#equals(java.lang.Object)
151: */
152: public boolean equals(Object obj) {
153:
154: return compareTo(obj) == 0;
155: }
156:
157: /**
158: * Returns the list of names (Strings) of the document types to be indexed.<p>
159: *
160: * @return the list of names (Strings) of the document types to be indexed
161: */
162: public List getDocumentTypes() {
163:
164: return m_documentTypes;
165: }
166:
167: /**
168: * Returns the indexer.<p>
169: *
170: * @return the indexer
171: */
172: public I_CmsIndexer getIndexer() {
173:
174: return m_indexer;
175: }
176:
177: /**
178: * Returns the class name of the indexer.<p>
179: *
180: * @return the class name of the indexer
181: */
182: public String getIndexerClassName() {
183:
184: return m_indexerClassName;
185: }
186:
187: /**
188: * Returns the logical key/name of this search index source.<p>
189: *
190: * @return the logical key/name of this search index source
191: */
192: public String getName() {
193:
194: return m_name;
195: }
196:
197: /**
198: * Returns the value for a specified parameter key.<p>
199: *
200: * @param key the parameter key/name
201: * @return the value for the specified parameter key
202: */
203: public String getParam(String key) {
204:
205: return (String) m_params.get(key);
206: }
207:
208: /**
209: * Returns the map of optional key/value parameters.<p>
210: *
211: * @return the map of optional key/value parameters
212: */
213: public Map getParams() {
214:
215: return m_params;
216: }
217:
218: /**
219: * Returns the list of VFS resources to be indexed.<p>
220: *
221: * @return the list of VFS resources to be indexed
222: */
223: public List getResourcesNames() {
224:
225: return m_resourcesNames;
226: }
227:
228: /**
229: * Overriden to be consistents with overridden method
230: * <code>{@link #equals(Object)}</code>.
231: *
232: * @see java.lang.Object#hashCode()
233: */
234: public int hashCode() {
235:
236: return m_name.hashCode();
237: }
238:
239: /**
240: * Returns <code>true</code> in case the given resource root path is contained in the list of
241: * configured resource names of this index source.<p>
242: *
243: * @param rootPath the resource root path to check
244: *
245: * @return <code>true</code> in case the given resource root path is contained in the list of
246: * configured resource names of this index source
247: *
248: * @see #getResourcesNames()
249: */
250: public boolean isContaining(String rootPath) {
251:
252: if ((rootPath != null) && (m_resourcesNames != null)) {
253: Iterator i = m_resourcesNames.iterator();
254: while (i.hasNext()) {
255: String path = (String) i.next();
256: if (rootPath.startsWith(path)) {
257: return true;
258: }
259: }
260: }
261: return false;
262: }
263:
264: /**
265: * Returns <code>true</code> in case the given resource root path is contained in the list of
266: * configured resource names, and the given document type name is contained in the
267: * list if configured document type names of this index source.<p>
268: *
269: * @param rootPath the resource root path to check
270: * @param documentType the document type factory name to check
271: *
272: * @return <code>true</code> in case the given resource root path is contained in the list of
273: * configured resource names, and the given document type name is contained in the
274: * list if configured document type names of this index source
275: *
276: * @see #isContaining(String)
277: * @see #getDocumentTypes()
278: */
279: public boolean isIndexing(String rootPath, String documentType) {
280:
281: return m_documentTypes.contains(documentType)
282: && isContaining(rootPath);
283: }
284:
285: /**
286: * Removes the name of a document type from the list of configured types of this index source.<p>
287: *
288: * @param key the name of the document type to remove
289: *
290: * @return true if the given document type name was contained before thus could be removed successfully, false otherwise
291: */
292: public boolean removeDocumentType(String key) {
293:
294: return m_documentTypes.remove(key);
295: }
296:
297: /**
298: * Sets the list of document type names (Strings) to be indexed.<p>
299: *
300: * @param documentTypes the list of document type names (Strings) to be indexed
301: */
302: public void setDocumentTypes(List documentTypes) {
303:
304: m_documentTypes = documentTypes;
305: }
306:
307: /**
308: * Sets the class name of the indexer.<p>
309: *
310: * An Exception is thrown to allow GUI-display of wrong input.<p>
311: *
312: * @param indexerClassName the class name of the indexer
313: *
314: * @throws CmsIllegalArgumentException if the given String is not a fully qualified classname (within this Java VM)
315: */
316: public void setIndexerClassName(String indexerClassName)
317: throws CmsIllegalArgumentException {
318:
319: try {
320: m_indexer = (I_CmsIndexer) Class.forName(indexerClassName)
321: .newInstance();
322: m_indexerClassName = indexerClassName;
323: } catch (Exception exc) {
324: if (LOG.isWarnEnabled()) {
325: LOG.warn(Messages.get().getBundle().key(
326: Messages.LOG_INDEXER_CREATION_FAILED_1,
327: m_indexerClassName), exc);
328: }
329: throw new CmsIllegalArgumentException(
330: Messages
331: .get()
332: .container(
333: Messages.ERR_INDEXSOURCE_INDEXER_CLASS_NAME_2,
334: indexerClassName,
335: I_CmsIndexer.class.getName()));
336: }
337: }
338:
339: /**
340: * Sets the logical key/name of this search index source.<p>
341: *
342: * @param name the logical key/name of this search index source
343: *
344: * @throws CmsIllegalArgumentException if argument name is null, an empty or whitespace-only Strings
345: * or already used for another indexsource's name.
346: */
347: public void setName(String name) throws CmsIllegalArgumentException {
348:
349: if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
350: throw new CmsIllegalArgumentException(
351: Messages
352: .get()
353: .container(
354: Messages.ERR_INDEXSOURCE_CREATE_MISSING_NAME_0));
355: }
356: // already used? Don't test this at xml-configuration time (no manager)
357: if (OpenCms.getRunLevel() > OpenCms.RUNLEVEL_2_INITIALIZING) {
358: CmsSearchManager mngr = OpenCms.getSearchManager();
359: // don't test this if the indexsource is not new (widget invokes setName even if it was not changed)
360: if (mngr.getIndexSource(name) != this ) {
361: if (mngr.getSearchIndexSources().keySet()
362: .contains(name)) {
363: throw new CmsIllegalArgumentException(
364: Messages
365: .get()
366: .container(
367: Messages.ERR_INDEXSOURCE_CREATE_INVALID_NAME_1,
368: name));
369: }
370: }
371: }
372: m_name = name;
373: }
374:
375: /**
376: * Sets the map of optional key/value parameters.<p>
377: *
378: * @param params the map of optional key/value parameters
379: */
380: public void setParams(Map params) {
381:
382: m_params = params;
383: }
384:
385: /**
386: * Sets the list of Cms resources to be indexed.<p>
387: *
388: * @param resources the list of Cms resources (Strings) to be indexed
389: */
390: public void setResourcesNames(List resources) {
391:
392: m_resourcesNames = resources;
393: }
394: }
|