KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > search > CmsSearchIndexSource


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsSearchIndexSource.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.13 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.search;
33
34 import org.opencms.main.CmsIllegalArgumentException;
35 import org.opencms.main.CmsLog;
36 import org.opencms.main.OpenCms;
37 import org.opencms.util.CmsStringUtil;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import org.apache.commons.logging.Log;
45
46 /**
47  * A search index source is a description of a list of Cms resources
48  * to be indexed.<p>
49  *
50  * @author Thomas Weckert
51  *
52  * @version $Revision: 1.13 $
53  *
54  * @since 6.0.0
55  */

56 public class CmsSearchIndexSource implements Comparable JavaDoc {
57
58     /** The log object for this class. */
59     private static final Log LOG = CmsLog.getLog(CmsSearchIndexSource.class);
60
61     /** A list of Cms resource types to be indexed. */
62     private List JavaDoc m_documentTypes;
63
64     /** The indexer. */
65     private I_CmsIndexer m_indexer;
66
67     /** The class name of the indexer. */
68     private String JavaDoc m_indexerClassName;
69
70     /** The logical key/name of this index. */
71     private String JavaDoc m_name;
72
73     /** A map of optional key/value parameters. */
74     private Map JavaDoc m_params;
75
76     /** A list of Cms resources to be indexed. */
77     private List JavaDoc m_resourcesNames;
78
79     /**
80      * Creates a new CmsSearchIndexSource.<p>
81      */

82     public CmsSearchIndexSource() {
83
84         m_params = new HashMap JavaDoc();
85         m_resourcesNames = new ArrayList JavaDoc();
86         m_documentTypes = new ArrayList JavaDoc();
87     }
88
89     /**
90      * Adds a parameter.<p>
91      *
92      * @param key the key/name of the parameter
93      * @param value the value of the parameter
94      */

95     public void addConfigurationParameter(String JavaDoc key, String JavaDoc value) {
96
97         m_params.put(key, value);
98     }
99
100     /**
101      * Adds the key/name of a document type.<p>
102      *
103      * @param key the key/name of a document type
104      */

105     public void addDocumentType(String JavaDoc key) {
106
107         m_documentTypes.add(key);
108     }
109
110     /**
111      * Adds the path of a Cms resource.<p>
112      *
113      * @param resourceName the path of a Cms resource
114      */

115     public void addResourceName(String JavaDoc resourceName) {
116
117         m_resourcesNames.add(resourceName);
118     }
119
120     /**
121      * Compares the internal name Strings of this instance and the argument casted
122      * to this type. <p>
123      *
124      * Note that this method only should return 0 for the statement
125      * <code>a.compareTo(a)</code>
126      * as the name of a indexsource has
127      * to be unique within OpenCms.<p>
128      *
129      * @param o another indexsource.
130      *
131      * @return the comparison result (as specified in {@link String#compareTo(java.lang.String)} for the
132      * name member of both indexsource instances involved.
133      *
134      * @throws ClassCastException if the given argument is not assignable from this class.
135      *
136      * @see java.lang.Comparable#compareTo(java.lang.Object)
137      */

138     public int compareTo(Object JavaDoc o) throws ClassCastException JavaDoc {
139
140         CmsSearchIndexSource other = (CmsSearchIndexSource)o;
141         String JavaDoc otherName = other.getName();
142         String JavaDoc myName = getName();
143         return myName.compareTo(otherName);
144     }
145
146     /**
147      * Implemented to be consistent with overridden method
148      * <code>{@link #compareTo(Object)}</code>.<p>
149      *
150      * Note that this method only should return true for the statement
151      * <code>a.compareTo(a)</code>
152      * as the name of a indexsource has
153      * to be unique within OpenCms.<p>
154      *
155      * @param obj another indexsource.
156      *
157      * @return true if <code>{@link #compareTo(Object)}</code> with this argument returns 0, false else.
158      *
159      * @see java.lang.Object#equals(java.lang.Object)
160      */

161     public boolean equals(Object JavaDoc obj) {
162
163         boolean ret = false;
164         try {
165             int cp = compareTo(obj);
166             ret = cp == 0;
167         } catch (Exception JavaDoc e) {
168             // remain false
169
}
170         return ret;
171     }
172
173     /**
174      * Returns the list of Cms resource types to be indexed.<p>
175      *
176      * @return the list of Cms resource types to be indexed
177      */

178     public List JavaDoc getDocumentTypes() {
179
180         return m_documentTypes;
181     }
182
183     /**
184      * Returns the indexer.<p>
185      *
186      * @return the indexer
187      */

188     public I_CmsIndexer getIndexer() {
189
190         return m_indexer;
191     }
192
193     /**
194      * Returns the class name of the indexer.<p>
195      *
196      * @return the class name of the indexer
197      */

198     public String JavaDoc getIndexerClassName() {
199
200         return m_indexerClassName;
201     }
202
203     /**
204      * Returns the logical key/name of this search index source.<p>
205      *
206      * @return the logical key/name of this search index source
207      */

208     public String JavaDoc getName() {
209
210         return m_name;
211     }
212
213     /**
214      * Returns the value for a specified parameter key.<p>
215      *
216      * @param key the parameter key/name
217      * @return the value for the specified parameter key
218      */

219     public String JavaDoc getParam(String JavaDoc key) {
220
221         return (String JavaDoc)m_params.get(key);
222     }
223
224     /**
225      * Returns the map of optional key/value parameters.<p>
226      *
227      * @return the map of optional key/value parameters
228      */

229     public Map JavaDoc getParams() {
230
231         return m_params;
232     }
233
234     /**
235      * Returns the list of Cms resources to be indexed.<p>
236      *
237      * @return the list of Cms resources to be indexed
238      */

239     public List JavaDoc getResourcesNames() {
240
241         return m_resourcesNames;
242     }
243
244     /**
245      * Overriden to be consistents with overridden method
246      * <code>{@link #equals(Object)}</code>.
247      *
248      * @see java.lang.Object#hashCode()
249      */

250     public int hashCode() {
251
252         return m_name.hashCode();
253     }
254
255     /**
256      * Removes the key/name of a document type.<p>
257      *
258      * @param key the key/name of a document type
259      *
260      * @return true if the given key was contained before thus could be removed successfully, false else.
261      */

262     public boolean removeDocumentType(String JavaDoc key) {
263
264         return m_documentTypes.remove(key);
265     }
266
267     /**
268      * Sets the list of Cms resource types to be indexed.<p>
269      *
270      * @param documentTypes the list of Cms resource types to be indexed
271      */

272     public void setDocumentTypes(List JavaDoc documentTypes) {
273
274         m_documentTypes = documentTypes;
275     }
276
277     /**
278      * Sets the class name of the indexer.<p>
279      *
280      * An Exception is thrown to allow GUI-display of wrong input.<p>
281      *
282      * @param indexerClassName the class name of the indexer
283      *
284      * @throws CmsIllegalArgumentException if the given String is not a fully qualified classname (within this Java VM)
285      */

286     public void setIndexerClassName(String JavaDoc indexerClassName) throws CmsIllegalArgumentException {
287
288         try {
289             m_indexer = (I_CmsIndexer)Class.forName(indexerClassName).newInstance();
290             m_indexerClassName = indexerClassName;
291         } catch (Exception JavaDoc exc) {
292             if (LOG.isWarnEnabled()) {
293                 LOG.warn(
294                     Messages.get().getBundle().key(Messages.LOG_INDEXER_CREATION_FAILED_1, m_indexerClassName),
295                     exc);
296             }
297             throw new CmsIllegalArgumentException(Messages.get().container(
298                 Messages.ERR_INDEXSOURCE_INDEXER_CLASS_NAME_2,
299                 indexerClassName,
300                 I_CmsIndexer.class.getName()));
301         }
302     }
303
304     /**
305      * Sets the logical key/name of this search index source.<p>
306      *
307      * @param name the logical key/name of this search index source
308      *
309      * @throws CmsIllegalArgumentException if argument name is null, an empty or whitespace-only Strings
310      * or already used for another indexsource's name.
311      */

312     public void setName(String JavaDoc name) throws CmsIllegalArgumentException {
313
314         if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
315             throw new CmsIllegalArgumentException(Messages.get().container(
316                 Messages.ERR_INDEXSOURCE_CREATE_MISSING_NAME_0));
317         }
318         // already used? Don't test this at xml-configuration time (no manager)
319
if (OpenCms.getRunLevel() > OpenCms.RUNLEVEL_2_INITIALIZING) {
320             CmsSearchManager mngr = OpenCms.getSearchManager();
321             // don't test this if the indexsource is not new (widget invokes setName even if it was not changed)
322
if (mngr.getIndexSource(name) != this) {
323                 if (mngr.getSearchIndexSources().keySet().contains(name)) {
324                     throw new CmsIllegalArgumentException(Messages.get().container(
325                         Messages.ERR_INDEXSOURCE_CREATE_INVALID_NAME_1,
326                         name));
327                 }
328             }
329         }
330         m_name = name;
331     }
332
333     /**
334      * Sets the map of optional key/value parameters.<p>
335      *
336      * @param params the map of optional key/value parameters
337      */

338     public void setParams(Map JavaDoc params) {
339
340         m_params = params;
341     }
342
343     /**
344      * Sets the list of Cms resources to be indexed.<p>
345      *
346      * @param resources the list of Cms resources (Strings) to be indexed
347      */

348     public void setResourcesNames(List JavaDoc resources) {
349
350         m_resourcesNames = resources;
351     }
352 }
Free Books   Free Magazines  
Popular Tags