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.excalibur.source.Source;
023: import org.apache.excalibur.source.SourceResolver;
024: import org.apache.lenya.cms.repository.Node;
025: import org.apache.lenya.cms.usecase.DocumentUsecase;
026: import org.apache.lenya.cms.usecase.UsecaseException;
027: import org.xml.sax.InputSource;
028:
029: /**
030: * Usecase to maintain lucene index.
031: */
032: public class IndexDocument extends DocumentUsecase {
033:
034: /**
035: * The URI to copy the document source from.
036: */
037: public static final String SOURCE_URI = "sourceUri";
038:
039: public static final String INDEX_ACTION = "indexAction";
040: public static final String INDEX = "index";
041: public static final String DELETE = "delete";
042: public static final String INDEX_AREA = "indexArea";
043:
044: /**
045: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
046: */
047: protected void doExecute() throws Exception {
048: super .doExecute();
049: SourceResolver resolver = null;
050: Source source = null;
051:
052: String action = super .getParameterAsString(INDEX_ACTION);
053: String area = super .getParameterAsString(INDEX_AREA);
054:
055: try {
056: String[] formats = getSourceDocument().getResourceType()
057: .getFormats();
058: if (!Arrays.asList(formats).contains("luceneIndex")) {
059: getLogger()
060: .warn(
061: "Document ["
062: + getSourceDocument()
063: + "] is not being indexed because resource type ["
064: + getSourceDocument()
065: .getResourceType()
066: .getName()
067: + "] does not support indexing!");
068: return;
069: }
070: resolver = (SourceResolver) this .manager
071: .lookup(SourceResolver.ROLE);
072: if (action.equals(INDEX)) {
073: // index
074: source = resolver
075: .resolveURI("cocoon://modules/lucene/index-"
076: + area + ".xml");
077: InputSource xmlInputSource = org.apache.cocoon.components.source.SourceUtil
078: .getInputSource(source);
079: } else if (action.equals(DELETE)) {
080: // delete
081: source = resolver
082: .resolveURI("cocoon://modules/lucene/delete-"
083: + area + ".xml");
084: InputSource xmlInputSource = org.apache.cocoon.components.source.SourceUtil
085: .getInputSource(source);
086: }
087: } finally {
088: if (resolver != null) {
089: if (source != null) {
090: resolver.release(source);
091: }
092: this .manager.release(resolver);
093: }
094: }
095: }
096:
097: /**
098: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
099: */
100: protected Node[] getNodesToLock() throws UsecaseException {
101: if (getLogger().isDebugEnabled()) {
102: getLogger().debug(
103: "IndexDocument::getObjectsToLock() called on source document ["
104: + getSourceDocument() + "]");
105: }
106:
107: Node[] objects = { getSourceDocument().getRepositoryNode() };
108: return objects;
109: }
110:
111: }
|