001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.lucene;
019:
020: import java.util.Arrays;
021:
022: import org.apache.avalon.framework.activity.Startable;
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.Serviceable;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028: import org.apache.cocoon.components.search.IndexException;
029: import org.apache.lenya.cms.cocoon.source.SourceUtil;
030: import org.apache.lenya.cms.observation.DocumentEvent;
031: import org.apache.lenya.cms.observation.ObservationRegistry;
032: import org.apache.lenya.cms.observation.RepositoryEvent;
033: import org.apache.lenya.cms.publication.Area;
034: import org.apache.lenya.cms.publication.DocumentFactory;
035: import org.apache.lenya.cms.publication.DocumentUtil;
036: import org.apache.lenya.cms.publication.Publication;
037: import org.apache.lenya.cms.publication.ResourceType;
038: import org.apache.lenya.cms.repository.Session;
039:
040: /**
041: * Index updater implementation.
042: */
043: public class IndexUpdaterImpl extends AbstractLogEnabled implements
044: IndexUpdater, Startable, Serviceable, ThreadSafe {
045:
046: public void eventFired(RepositoryEvent repoEvent) {
047:
048: if (!(repoEvent instanceof DocumentEvent)) {
049: return;
050: }
051: DocumentEvent event = (DocumentEvent) repoEvent;
052:
053: try {
054: if (event.getDescriptor().equals(DocumentEvent.CHANGED)) {
055: index(event.getSession(), event.getResourceType(),
056: event.getPublicationId(), event.getArea(),
057: event.getUuid(), event.getLanguage());
058: } else if (event.getDescriptor().equals(
059: DocumentEvent.REMOVED)) {
060: delete(event.getSession(), event.getResourceType(),
061: event.getPublicationId(), event.getArea(),
062: event.getUuid(), event.getLanguage());
063: }
064:
065: } catch (IndexException e) {
066: throw new RuntimeException(e);
067: }
068: }
069:
070: protected void updateIndex(String operation,
071: ResourceType resourceType, String pubId, String area,
072: String uuid, String language) throws IndexException {
073:
074: String uri = null;
075: try {
076: String[] formats = resourceType.getFormats();
077: if (Arrays.asList(formats).contains("luceneIndex")) {
078: String docString = pubId + "/" + area + "/" + uuid
079: + "/" + language;
080: // + event.getDocumentUrl();
081: uri = "cocoon://modules/lucene/" + operation
082: + "-document/" + docString;
083: SourceUtil.readDOM(uri, this .manager);
084: } else {
085: getLogger()
086: .info(
087: "Document ["
088: + pubId
089: + ":"
090: + area
091: + ":"
092: + uuid
093: + ":"
094: + language
095: + "] is not being indexed because resource type ["
096: + resourceType.getName()
097: + "] does not support indexing!");
098: }
099: } catch (Exception e) {
100: getLogger().error(
101: "Invoking indexing failed for URL [" + uri + "]: ",
102: e);
103: throw new IndexException(e);
104: }
105: }
106:
107: public void start() throws Exception {
108: ObservationRegistry registry = null;
109: try {
110: registry = (ObservationRegistry) this .manager
111: .lookup(ObservationRegistry.ROLE);
112: registry.registerListener(this );
113: } finally {
114: if (registry != null) {
115: this .manager.release(registry);
116: }
117: }
118: }
119:
120: public void stop() throws Exception {
121: }
122:
123: private ServiceManager manager;
124:
125: public void service(ServiceManager manager) throws ServiceException {
126: this .manager = manager;
127: }
128:
129: public void delete(Session session, ResourceType resourceType,
130: String pubId, String area, String uuid, String language)
131: throws IndexException {
132: updateIndex("delete", resourceType, pubId, area, uuid, language);
133: }
134:
135: public void index(Session session, ResourceType resourceType,
136: String pubId, String area, String uuid, String language)
137: throws IndexException {
138: DocumentFactory factory = DocumentUtil.createDocumentFactory(
139: this .manager, session);
140: try {
141: Publication pub = factory.getPublication(pubId);
142: Area areaObj = pub.getArea(area);
143: if (areaObj.contains(uuid, language)) {
144: updateIndex("index", resourceType, pubId, area, uuid,
145: language);
146: } else {
147: getLogger()
148: .debug(
149: "Ignoring document ["
150: + pubId
151: + ":"
152: + area
153: + ":"
154: + uuid
155: + ":"
156: + language
157: + "] because it doesn't exist (anymore).");
158: }
159: } catch (Exception e) {
160: throw new IndexException(e);
161: }
162: }
163:
164: }
|