Package com.flaptor.hounder.searcher

Source Code of com.flaptor.hounder.searcher.MultipleRpcSearcher

/*
Copyright 2008 Flaptor (flaptor.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.flaptor.hounder.searcher;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.flaptor.hounder.searcher.filter.AFilter;
import com.flaptor.hounder.searcher.group.AGroup;
import com.flaptor.hounder.searcher.query.AQuery;
import com.flaptor.hounder.searcher.sort.ASort;
import com.flaptor.util.Config;
import com.flaptor.util.Execute;
import com.flaptor.util.FileUtil;
import com.flaptor.util.PortUtil;
import com.flaptor.util.remote.RmiServer;
import com.flaptor.util.remote.WebServer;
import com.flaptor.util.remote.XmlrpcServer;
import com.flaptor.util.web.RedirectHandler;

/**
* Searcher exposed by several RPC interfaces
* @author Flaptor Development Team
*/
public class MultipleRpcSearcher implements ISearcher {
  public static final String XMLRPC_CONTEXT = null;
    private static final Logger logger = Logger.getLogger(Execute.whoAmI());
    private final ISearcher baseSearcher;
    private RmiSearcherWrapper rmiSearcherWrapper;
    private RmiServer rmiServer = null;
    private XmlrpcServer xmlRpcServer = null;
    private WebServer httpServer = null;


    @Override
    public void requestStop() {
        if (null != rmiServer) {
            rmiServer.requestStop();
        }
        if (null != xmlRpcServer) {
            xmlRpcServer.requestStop();
        }
    }

    @Override
    public boolean isStopped() {
        boolean running = true;
        if (null != rmiServer) {
            running = running && !rmiServer.isStopped();
        }
        if (null != xmlRpcServer) {
            running = running && !xmlRpcServer.isStopped();
        }
        return !running;
    }

    public MultipleRpcSearcher(ISearcher baseSearcher, boolean rmi, boolean xmlrpc, boolean openSearch, boolean web, boolean xml) {
        this.baseSearcher = baseSearcher;
        if (rmi) {
            int port = PortUtil.getPort("searcher.rmi");
            logger.info("MultipleRpcSearcher constructor: starting rmi searcher on port " + port);
            rmiSearcherWrapper = new RmiSearcherWrapper(baseSearcher);
            rmiServer = new RmiServer(port);
            rmiServer.addHandler(RmiServer.DEFAULT_SERVICE_NAME, rmiSearcherWrapper);
            rmiServer.start();
        }
        if (xmlrpc) {
            int port = PortUtil.getPort("searcher.xmlrpc");
            logger.info("MultipleRpcSearcher constructor: starting xmlRpc searcher on port " + port);
            xmlRpcServer = new XmlrpcServer(port);
            xmlRpcServer.addHandler(XMLRPC_CONTEXT, new VectorResults(baseSearcher));
            xmlRpcServer.start();
        }
        if (openSearch || web || xml) {
            Config config = Config.getConfig("searcher.properties");
            int httpServerPort = PortUtil.getPort("searcher.http");
            httpServer = new WebServer(httpServerPort);

            if (openSearch) {
                String context = config.getString("opensearch.context");
                logger.info("MultipleRpcSearcher constructor: starting OpenSearch searcher on port " + httpServerPort + " context "+context);
                httpServer.addHandler(context, new OpenSearchHandler(baseSearcher));
            }
            if (xml) {
                String context = config.getString("xmlsearch.context");
                logger.info("MultipleRpcSearcher constructor: starting xml searcher on port " + httpServerPort  + " context "+context);
                httpServer.addHandler(context, new XmlSearchHandler(baseSearcher));
            }
            if (web) {
                String context = config.getString("websearch.context");
                logger.info("MultipleRpcSearcher constructor: starting web searcher on port " + httpServerPort  + " context "+context);
                WebSearchUtil.setSearcher(baseSearcher);
                String webappPath = this.getClass().getClassLoader().getResource("web-searcher").getPath();
                httpServer.addWebAppHandler(context, webappPath);
            }
            boolean redirect = config.getBoolean("websearch.redirect");
            if (redirect) {
                String from = config.getString("websearch.redirect.from");
                String to = config.getString("websearch.redirect.to");
                httpServer.addHandler("/", new RedirectHandler(from,to));
            }
            try {httpServer.start();} catch (Exception e) {throw new RuntimeException(e);}
        }
    }


    public GroupedSearchResults search(AQuery query, int firstResult, int count, AGroup group, int groupSize, AFilter filter, ASort sortthrows SearcherException{
        return baseSearcher.search(query, firstResult, count, group, groupSize, filter, sort);
    }

    public static void main(String[] args) {

        String log4jConfigPath = FileUtil.getFilePathFromClasspath("log4j.properties");
        if (null != log4jConfigPath) {
            PropertyConfigurator.configureAndWatch(log4jConfigPath);
        } else {
            logger.warn("log4j.properties not found in classpath! Reload disabled.");
        }

        Config conf = Config.getConfig("searcher.properties");
        ISearcher baseSearcher = new CompositeSearcher();
        new MultipleRpcSearcher(baseSearcher,conf.getBoolean("rmiInterface"), conf.getBoolean("xmlRpcInterface"), conf.getBoolean("openSearchInterface"), conf.getBoolean("webInterface"), conf.getBoolean("xmlInterface"));
    }
}
TOP

Related Classes of com.flaptor.hounder.searcher.MultipleRpcSearcher

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.