Code Search for Developers
 
 
  

RobotsController.java from Kneobase at Krugle


Show RobotsController.java syntax highlighted

/*
 * Created on 16/06/2004
 *
 */
package com.kneobase.web.robots;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.kneobase.KneobaseException;
import com.kneobase.driver.content.I_Content;
import com.kneobase.driver.content.I_TreeContent;
import com.kneobase.logging.LogIndexSessionDecorator;
import com.kneobase.server.I_IndexConstants;
import com.kneobase.server.I_IndexSession;
import com.kneobase.server.index.IndexManager;

/**
 * @author gustavo
 *
 */
public class RobotsController extends AbstractCommandController {

    private final String STATUS_IDLE_KEY = "driver.status.idle";
	private final String STATUS_STOPPED_KEY = "driver.status.stopped";
	private final String STATUS_STOPING_KEY = "driver.status.stopping";
	private final String STATUS_UPDATING_INDEX_KEY ="driver.status.updating_index";
	private final String STATUS_INDEXING_KEY = "driver.status.indexing";
	private final String STATUS_FATAL_EXCEPTION_KEY = "driver.status.fatal_exception";
	private final String FATAL_EXCEPTION_KEY = "driver.log.fatal_exception";
	private final String STATUS_FINISHED_OK_KEY ="driver.status.finished_ok";
	private final String ERRORS_KEY = "driver.log.errors";
	private final String DOCS_READ_KEY = "driver.log.docs_read";
	private final String DOCS_SKIPPED_KEY = "driver.log.docs_skipped";
	private final String DOCS_INDEXED_KEY = "driver.log.docs_indexed";
    private final String CURRENT_CONTENT = "driver.log.current_content";

	private IndexManager indexManager; 
    private String contextBasePath = "";
    
    protected ModelAndView handle(
        HttpServletRequest request,
        HttpServletResponse response,
        Object command,
        BindException errors) throws IOException, KneobaseException{

		contextBasePath = request.getSession().getServletContext().getRealPath("");
        RobotsCommand robotsCommand = (RobotsCommand) command;
        
        if(!robotsCommand.getDeleteAll().equals("")){
            indexManager.deleteIndex();
            //TODO catchar, informar y loguear.
        }

        if(!robotsCommand.getRefresh().equals("")){
            indexManager.refreshSources();
			//TODO catchar, informar y loguear.
        }

        Collection runRobots = null;
        HashMap sessions = null;

        if (robotsCommand.getStop().length() > 0) {
            Collection stopRobots = getRunRobots(robotsCommand);

			//TODO catchar, informar y loguear.
            sessions = stopRobots(stopRobots, request.getSession());

            if (sessions != null)
                robotsCommand.setIndexing(true);
        } else if (
            robotsCommand.getExecute().length() > 0
                || robotsCommand.getUpdate().length() > 0) {
                    
            boolean rebuild = robotsCommand.getExecute().length() > 0;
            // obtiene los robots a ejecutar.
            runRobots = getRunRobots(robotsCommand);

            //run Robots
            sessions = runRobots(runRobots, rebuild);

            request.getSession().setAttribute("sessions", sessions);
            robotsCommand.setIndexing(true);
        } else if(robotsCommand.getClose().length() > 0){
        	String source = robotsCommand.getSource();

			//TODO catchar, informar y loguear.
            indexManager.closeSource(source);
        } else if(robotsCommand.getOpen().length() > 0){
			String source = robotsCommand.getSource();
			//TODO catchar, informar y loguear.
			indexManager.openSource(source);
		} else {
            sessions = (HashMap) request.getSession().getAttribute("sessions");
            robotsCommand.setIndexing(sessionRunning(sessions));
        }

        //construye el estado y configuracion de cada robot
        Collection robots = getRobotsBeans(indexManager.getSources(), sessions);
        robotsCommand.setRobotBeans(robots);

        Map model = errors.getModel();

        return new ModelAndView("robots", model);
    }

    private Collection getRunRobots(RobotsCommand robotsCommand) {
        Collection robots = new Vector();

        if (robotsCommand.getSource().length() == 0) {
            // todos los robots
            robots = indexManager.getSources(true);
        } else {
            // solo un robot
            String robotName = robotsCommand.getSource();
            robots.add(robotName);
        }

        return robots;
    }

    private HashMap runRobots(Collection robots, boolean rebuild) {
        HashMap indexSessions = new HashMap();
        Iterator itRobots = robots.iterator();
        while (itRobots.hasNext()) {
            String robotName = (String) itRobots.next();

            LogIndexSessionDecorator sessionDecorator;
            sessionDecorator = runRobot(contextBasePath, robotName, rebuild);

            indexSessions.put(robotName, sessionDecorator);
        }
        return indexSessions;
    }

    private HashMap stopRobots(Collection robots, HttpSession session)
        throws IOException {
        HashMap indexSessions = (HashMap) session.getAttribute("sessions");
        if (indexSessions == null)
            return null;

        Iterator itRobots = robots.iterator();
        while (itRobots.hasNext()) {
            String robotName = (String) itRobots.next();

            LogIndexSessionDecorator sessionDecorator;

            sessionDecorator =
                (LogIndexSessionDecorator) indexSessions.get(robotName);

            if (sessionDecorator != null)
                sessionDecorator.stopIndexation();
        }
        return indexSessions;
    }

    private Collection getRobotsBeans(
        Collection robotConfigurationFiles,
        HashMap sessions) {

        Collection robotsBean = new Vector();
        Iterator itRobots = indexManager.getSources().iterator();

        while (itRobots.hasNext()) {
            String sourceName = (String) itRobots.next();

            RobotIndexBean robotBean = new RobotIndexBean();

            robotBean.setRobotName(sourceName);
            // TODO cambiar por algo mas adecuado
            String fullPath =
                indexManager
                    .getRobotConfiguration(sourceName)
                    .getConfigFileName();
            String relativePath =
                fullPath.substring(contextBasePath.length());
            robotBean.setConfigFile(relativePath);
            robotBean.setOpen(indexManager.isAvailable(sourceName));

            if (sessions != null) {
                LogIndexSessionDecorator indexSessionDecorator =
                    (LogIndexSessionDecorator) sessions.get(sourceName);

                if (indexSessionDecorator != null) {
					robotBean.setWorking(indexSessionDecorator.isWorking());    
                    List indexationResults = new ArrayList();
                    
                    I_Content currentContent = indexSessionDecorator.getCurrentContent();
                    if(currentContent != null){
                        //String csName = currentContent.getContentSource().getName();
                        String name = getFullName(currentContent); 
                        indexationResults.add( new MessageKeyRaramPair( CURRENT_CONTENT, name));
                    }
                    indexationResults.add( new MessageKeyRaramPair( DOCS_READ_KEY, new Integer( indexSessionDecorator.getContentsProcessed() )));
                    indexationResults.add( new MessageKeyRaramPair( DOCS_INDEXED_KEY, new Integer( indexSessionDecorator.getContentsIndexed() )));
                    indexationResults.add( new MessageKeyRaramPair( DOCS_SKIPPED_KEY, new Integer( indexSessionDecorator.getContentsSkipped() )));
                    indexationResults.add( new MessageKeyRaramPair( ERRORS_KEY, new Integer( indexSessionDecorator.getCurrentErrors() )));

                    if (indexSessionDecorator
                        .getStatus()
                        .equals(I_IndexConstants.INDEXATION_FINISHOK)) {

                        robotBean.setStatus( STATUS_FINISHED_OK_KEY );
                        robotBean.setLog( indexationResults );

                    } else if (
                        indexSessionDecorator.getStatus().equals(
                            I_IndexConstants.INDEXATION_FAIL)) {

                        String message;
                        if (indexSessionDecorator.getFatalException() != null)
                            message =
                                indexSessionDecorator
                                    .getFatalException()
                                    .toString();
                        else
                            message = "null";

                        List results = new ArrayList();
                        results.add( new MessageKeyRaramPair( FATAL_EXCEPTION_KEY, message ));
                        results.addAll(indexationResults);

                        robotBean.setStatus( STATUS_FATAL_EXCEPTION_KEY );

                        robotBean.setLog(results);
                    } else if (
                        indexSessionDecorator.getStatus().equals(
                            I_IndexConstants.INDEXATION_RUNNING)) {

                        robotBean.setStatus( STATUS_INDEXING_KEY );
                        robotBean.setLog(indexationResults);
                    } else if (
                        indexSessionDecorator.getStatus().equals(
                            I_IndexConstants.INDEXATION_COMMITING)) {

                        robotBean.setStatus( STATUS_UPDATING_INDEX_KEY );
                        robotBean.setLog(indexationResults);
                    } else if (
                        indexSessionDecorator.getStatus().equals(
                            I_IndexConstants.INDEXATION_STOPPING)) {

                        robotBean.setStatus( STATUS_STOPING_KEY );
                        robotBean.setLog(indexationResults);
                    } else if (
                        indexSessionDecorator.getStatus().equals(
                            I_IndexConstants.INDEXATION_STOPPED)) {

                        robotBean.setStatus( STATUS_STOPPED_KEY );
                        robotBean.setLog(indexationResults);
                    }
                }
            } else {
                robotBean.setStatus( STATUS_IDLE_KEY );
            }
            robotsBean.add(robotBean);
        }
        return robotsBean;
    }

    private boolean sessionRunning(HashMap sessions) {
        boolean running = false;
        if (sessions == null) {
            return false;
        }
        for (Iterator itSessions = sessions.values().iterator();
            itSessions.hasNext() && !running;
            ) {
            LogIndexSessionDecorator session =
                (LogIndexSessionDecorator) itSessions.next();
            running =
                session.getStatus().equals(I_IndexConstants.INDEXATION_RUNNING)
                    || session.getStatus().equals(
                        I_IndexConstants.INDEXATION_STOPPING)
                    || session.getStatus().equals(
                        I_IndexConstants.INDEXATION_COMMITING);
        }
        return running;
    }

    /**
     * @return
     */
    public IndexManager getIndexManager() {
        return indexManager;
    }

    /**
     * @param manager
     */
    public void setIndexManager(IndexManager manager) {
        indexManager = manager;
    }

    protected LogIndexSessionDecorator runRobot(
        String basePath,
        String source,
        boolean rebuild) {

        I_IndexSession indexSession =
            (I_IndexSession) indexManager.getIndexSession(
                source,
                !rebuild);

        LogIndexSessionDecorator indexSessionDecorator =
            new LogIndexSessionDecorator( indexSession );

        indexSessionDecorator.doIndex();

        return indexSessionDecorator;
    }
    
    private String getFullName(I_Content content){
    	String result;
    	
    	if(content instanceof I_TreeContent){
    		I_TreeContent treeContent = (I_TreeContent) content;
    		result = treeContent.getAbsolutePath();
    	}else{
    		result = content.getName();
    	}
    	return result;
    }

}




See more files for this project here

Kneobase

Kneobase is an enterprise search engine, based upon the Lucene search engine and the Spring framework. It allows to perform full-text search across many different content sources. It is highly adaptable out-of-the-box and has a pluggable architecture.

Project homepage: http://sourceforge.net/projects/kneobase
Programming language(s): Java,XML
License: other

  MessageKeyRaramPair.java
  RobotIndexBean.java
  RobotsCommand.java
  RobotsController.java