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.FederatedIndexer;
026: import org.jboss.portal.search.IndexingException;
027: import org.jboss.portal.search.federation.IndexFederation;
028: import org.jboss.portal.search.federation.Info;
029:
030: import java.util.ArrayList;
031: import java.util.Collections;
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Map;
036:
037: /**
038: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
039: * @version $Revision: 8784 $
040: */
041: public class IndexFederationService extends AbstractJBossService
042: implements IndexFederation {
043:
044: private Map registry;
045:
046: public IndexFederationService() {
047: registry = new HashMap();
048: }
049:
050: public synchronized void register(FederatedIndexer federatedIndexer) {
051: if (federatedIndexer == null) {
052: throw new IllegalArgumentException("No null indexer");
053: }
054: String id = federatedIndexer.getId();
055: if (registry.containsKey(id)) {
056: throw new IllegalArgumentException(
057: "Attempting dual registration of " + id);
058: }
059: Map copy = new HashMap(registry);
060: copy.put(id, federatedIndexer);
061: registry = copy;
062: }
063:
064: public synchronized void unregister(String id) {
065: if (id == null) {
066: throw new IllegalArgumentException("No null id accepted");
067: }
068: if (!registry.containsKey(id)) {
069: throw new IllegalArgumentException(
070: "Attempting to unregister unknown federated search service "
071: + id);
072: }
073: Map copy = new HashMap(registry);
074: copy.remove(id);
075: registry = copy;
076: }
077:
078: public void reindexAll() {
079: if (registry != null) {
080: Iterator it = registry.values().iterator();
081: while (it.hasNext()) {
082: FederatedIndexer federated = (FederatedIndexer) it
083: .next();
084: int status = federated.getInfo().getStatus();
085: if (status != Info.Status.INDEXING) {
086: Thread thread = new ReindexFederatedIndexThread(
087: federated);
088: thread.run();
089: }
090: }
091: }
092: }
093:
094: private class ReindexFederatedIndexThread extends Thread {
095: private FederatedIndexer federated;
096:
097: public ReindexFederatedIndexThread(FederatedIndexer federated) {
098: this .federated = federated;
099: }
100:
101: public void run() {
102: federated.getInfo().setStatus(Info.Status.INDEXING);
103: try {
104: federated.destroyIndex();
105: federated.createIndex();
106: federated.getInfo().setStatus(Info.Status.AVAILABLE);
107: } catch (IndexingException e) {
108: federated.getInfo().setStatus(Info.Status.CORRUPTED);
109: e.printStackTrace();
110: }
111: }
112: }
113:
114: public List getFederatedIndexerList() {
115: return Collections.unmodifiableList(new ArrayList(registry
116: .values()));
117: }
118:
119: public FederatedIndexer getFederatedIndexer(String id) {
120: return (FederatedIndexer) registry.get(id);
121: }
122: /*
123: public List search(Query query)
124: {
125: List results = new ArrayList();
126: Iterator it = registry.values().iterator();
127: while (it.hasNext())
128: {
129: FederatedIndexer federated = (FederatedIndexer)it.next();
130: if (federated.getInfo().getStatus() == Info.Status.AVAILABLE)
131: {
132: results.add(federated.search(query));
133: }
134: }
135: return results;
136: }
137: */
138: }
|