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:
019: package org.apache.cocoon.components.modules.input;
020:
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.Serviceable;
030: import org.apache.cocoon.components.search.Index;
031: import org.apache.cocoon.components.search.components.IndexManager;
032: import org.apache.lucene.store.FSDirectory;
033:
034: /**
035: * This module returns the directory path of a search index given by its id,
036: * which normally is "pubid-area".
037: */
038: public class IndexPathModule extends AbstractInputModule implements
039: Serviceable {
040:
041: protected ServiceManager serviceManager;
042:
043: public void service(ServiceManager manager) throws ServiceException {
044: this .serviceManager = manager;
045: }
046:
047: /**
048: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
049: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
050: */
051: public Object getAttribute(String name, Configuration modeConf,
052: Map objectModel) throws ConfigurationException {
053:
054: String value = null;
055:
056: IndexManager indexManager = null;
057: try {
058: indexManager = (IndexManager) this .serviceManager
059: .lookup(IndexManager.ROLE);
060:
061: Index index = indexManager.getIndex(name);
062:
063: if (index == null) {
064: throw new ConfigurationException(
065: "no search index with id [" + name + "] found.");
066: }
067:
068: value = ((FSDirectory) index.getDirectory()).getFile()
069: .getCanonicalPath();
070:
071: if (getLogger().isDebugEnabled()) {
072: getLogger().debug(
073: "resolved search index with id [" + name
074: + "] to directory " + value);
075: }
076: } catch (Exception e) {
077: throw new ConfigurationException("Resolving attribute ["
078: + name + "] failed: ", e);
079: } finally {
080: if (indexManager != null) {
081: this .serviceManager.release(indexManager);
082: }
083: }
084: return value;
085: }
086:
087: /**
088: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
089: * java.util.Map)
090: */
091: public Iterator getAttributeNames(Configuration modeConf,
092: Map objectModel) throws ConfigurationException {
093: return Collections.EMPTY_SET.iterator();
094: }
095:
096: /**
097: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
098: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
099: */
100: public Object[] getAttributeValues(String name,
101: Configuration modeConf, Map objectModel)
102: throws ConfigurationException {
103: Object[] objects = { getAttribute(name, modeConf, objectModel) };
104:
105: return objects;
106: }
107:
108: }
|