001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.search.federation.impl;
023:
024: import org.jboss.portal.jems.as.system.AbstractJBossService;
025: import org.jboss.portal.search.FederatedQuery;
026: import org.jboss.portal.search.FederatedSearcher;
027: import org.jboss.portal.search.Query;
028: import org.jboss.portal.search.QueryConversionException;
029: import org.jboss.portal.search.SearchingException;
030: import org.jboss.portal.search.federation.SearchFederation;
031: import org.jboss.portal.search.result.ResultSet;
032:
033: import java.util.ArrayList;
034: import java.util.Collections;
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.List;
038: import java.util.Map;
039:
040: /**
041: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
042: * @version $Revision: 8784 $
043: */
044: public class SearchFederationService extends AbstractJBossService
045: implements SearchFederation {
046:
047: private Map registry;
048:
049: public SearchFederationService() {
050: registry = new HashMap();
051: }
052:
053: public synchronized void register(
054: FederatedSearcher federatedSearcher) {
055: if (federatedSearcher == null) {
056: throw new IllegalArgumentException("No null invoker");
057: }
058: String id = federatedSearcher.getId();
059: if (registry.containsKey(id)) {
060: throw new IllegalArgumentException(
061: "Attempting dual registration of " + id);
062: }
063: Map copy = new HashMap(registry);
064: copy.put(id, federatedSearcher);
065: registry = copy;
066: }
067:
068: public synchronized void unregister(String id) {
069: if (id == null) {
070: throw new IllegalArgumentException("No null id accepted");
071: }
072: if (!registry.containsKey(id)) {
073: throw new IllegalArgumentException(
074: "Attempting to unregister unknown federated search service "
075: + id);
076: }
077: Map copy = new HashMap(registry);
078: copy.remove(id);
079: registry = copy;
080: }
081:
082: public List getFederatedSearcherList() {
083: return Collections.unmodifiableList(new ArrayList(registry
084: .values()));
085: }
086:
087: public FederatedSearcher getFederatedSearcher(String id) {
088: return (FederatedSearcher) registry.get(id);
089: }
090:
091: public List search(FederatedQuery query) throws SearchingException {
092: List results = new ArrayList();
093: Iterator it = registry.values().iterator();
094: while (it.hasNext()) {
095: FederatedSearcher searcher = (FederatedSearcher) it.next();
096: try {
097: results.add(search(query, searcher));
098: } catch (QueryConversionException e) {
099: e.printStackTrace();
100: }
101: }
102: return results;
103: }
104:
105: public List search(FederatedQuery query, List searchers)
106: throws SearchingException {
107: List results = new ArrayList();
108: Iterator it = searchers.iterator();
109: while (it.hasNext()) {
110: String federatedId = (String) it.next();
111: FederatedSearcher searcher = getFederatedSearcher(federatedId);
112: if (searcher == null) {
113: throw new SearchingException("Cannot find searcher: "
114: + federatedId);
115: }
116: try {
117: results.add(search(query, searcher));
118: } catch (QueryConversionException e) {
119: e.printStackTrace();
120: }
121: }
122: return results;
123: }
124:
125: private ResultSet search(FederatedQuery query,
126: FederatedSearcher searcher)
127: throws QueryConversionException, SearchingException {
128: Query convertedQuery = null;
129: convertedQuery = searcher.getQueryConverter().convert(query);
130: return searcher.search(convertedQuery);
131: }
132:
133: }
|