📄 indexserver.java
字号:
package org.apache.lucene.server;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Lucene" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Lucene", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.lucene.search.RemoteSearchable;
import org.apache.lucene.search.Searchable;
import org.apache.lucene.server.exceptions.IndexConfigurationErrorException;
import org.apache.lucene.server.exceptions.IndexNotFoundException;
import org.apache.lucene.server.exceptions.MultipleIndexNameException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class IndexServer extends UnicastRemoteObject implements IndexService
{
private Hashtable IndexTable = new Hashtable();
private Logger logger=null;
private XFileFactory XFFactory;
private Vector timers=new Vector();
private int port=0;
private Registry registry=null;
public IndexServer(String ConfigurationFileName)
throws IOException,
SAXException,
ParserConfigurationException,
IndexConfigurationErrorException,
MultipleIndexNameException
{
Init(ConfigurationFileName);
}
/**
* Initiates a new instance using the specified configuration file.
*/
public void Init(String ConfigurationFileName)
throws IOException,
SAXException,
ParserConfigurationException,
IndexConfigurationErrorException,
MultipleIndexNameException
{
File f = new File(ConfigurationFileName);
DocumentBuilderFactory DBF=DocumentBuilderFactory.newInstance();
DBF.setValidating(true);
DocumentBuilder DB = DBF.newDocumentBuilder();
DB.setErrorHandler(new ConfigurationErrorHandler());
Document DOM = DB.parse(f);
try
{
String sport=DOM.getDocumentElement().getAttribute("port");
port = Integer.parseInt(sport);
registry = java.rmi.registry.LocateRegistry.createRegistry(port);
}
catch (NumberFormatException e1)
{
}
NodeList nl = DOM.getElementsByTagName("LogFile");
Element logelt = (Element)nl.item(0);
if (logelt==null)
{
System.out.println("IndexServer FATAL ERROR : LogFile tag is required.");
return;
}
try
{
logger = new Logger(logelt.getFirstChild().getNodeValue());
}
catch (Exception e)
{
System.out.println("IndexServer FATAL ERROR : Logger could not be created");
return;
}
nl = DOM.getElementsByTagName("XFileFactoryClass");
Element elt = (Element)nl.item(0);
if (elt!=null)
{
String factoryname = elt.getFirstChild().getNodeValue();
try
{
XFFactory = (XFileFactory)Class.forName(factoryname).newInstance();
}
catch (Exception e)
{
System.out.println("IndexServer FATAL ERROR : unable to instanciate Class "+factoryname);
return;
}
}
else
{
XFFactory = new SimpleXFileFactory();
}
///
nl = DOM.getElementsByTagName("IndexManager");
for (int i=0;i<nl.getLength();i++)
{
Element IndexManagerElement = (Element)nl.item(i);
IndexManager manager = new IndexManager(IndexManagerElement,this);
if (manager != null)
{
if (IndexTable.get(manager.getIndexName()) != null)
{
throw new MultipleIndexNameException(manager.getIndexName());
}
else
{
IndexTable.put(manager.getIndexName(),manager);
}
}
}
}
/*
*
*/
public static void main(String args[])
{
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
if (Array.getLength(args) <1)
{
System.out.println("IndexServer : missing argument #1 ConfigurationFileName. ");
return;
}
try
{
IndexServer server = new IndexServer(args[0]);
if (server.getRegistry() != null)
{
server.getRegistry().rebind("IndexServer",server);
}
else
{
Naming.rebind("//localhost/IndexServer", server);
}
System.out.println("IndexServer bound");
String[] list = server.IndexesNames();
try
{
for (int i=0;;i++)
{
try
{
server.BindNewSearcher(list[i]);
}
catch (IOException e1)
{
System.out.println("Error while binding searcher for index "+list[i]);
System.out.println(e1.getMessage());
}
}
}
catch (ArrayIndexOutOfBoundsException e)
{ }
}
catch (MultipleIndexNameException E)
{
System.out.println("IndexServer FATAL ERROR : Index "+E.getIndexName()+"is declared more than once.");
}
catch (Exception E)
{
System.out.println(E.getMessage());
E.printStackTrace();
}
}
/**
*
* @param s
*/
public void log(String s)
{
java.util.Date d = new java.util.Date();
logger.log(d.toString()+" : "+this.getClass().getName() +" : "+ s);
}
public void log (Exception e)
{
logger.log(e);
}
/**
*
* @author Administrateur
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public String[] IndexesNames() throws RemoteException
{
String[] names=null;
try
{
names = (String[])Array.newInstance(Class.forName("java.lang.String"),IndexTable.size());
} catch (NegativeArraySizeException e1) {
} catch (ClassNotFoundException e1) {
}
int i = 0;
for (Enumeration e=IndexTable.keys();e.hasMoreElements();)
{
String name = (String)e.nextElement();
Array.set(names,i,name);
i++;
}
return names;
}
/**
*
* @author Administrateur
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public String Test()
{
return "Test OK !";
}
/**
*
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public void BuildIndexFromScratch(String IndexName) throws RemoteException
{
// stop search service on this index
try
{
if (registry != null)
{
Remote rs = registry.lookup(IndexName+"_searcher");
((Searchable)rs).close();
registry.unbind(IndexName+"_searcher");
}
else
{
Remote rs = Naming.lookup("//localhost/"+IndexName+"_searcher");
((Searchable)rs).close();
Naming.unbind("//localhost/"+IndexName+"_searcher");
}
log ("Search service stopped for index "+IndexName);
}
catch (NotBoundException e)
{
}
catch (Exception e1)
{
log("Error while stopping search service for index "+IndexName);
log (e1);
}
IndexManager manager = (IndexManager)IndexTable.get(IndexName);
try
{
log ("building index "+IndexName);
manager.BuildIndexFromScratch();
log ("Index "+IndexName+" succesfully built");
BindNewSearcher(IndexName);
}
catch (NullPointerException e)
{
throw new RemoteException();
}
catch (Exception e)
{
log (e.getMessage());
log (e);
}
}
/**
*
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
private IndexManager getIndexByName(String name) throws IndexNotFoundException
{
IndexManager manager = (IndexManager)IndexTable.get(name);
if (manager == null)
{
throw new IndexNotFoundException(name);
}
return manager;
}
/**
*
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public Logger getLogger()
{
return logger;
}
/**
*
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public void BindNewSearcher(String indexname) throws IOException,IndexNotFoundException
{
RemoteSearchable searcher = getIndexByName(indexname).getRemoteSearchable();
if (registry != null)
{
registry.rebind(indexname+"_searcher",searcher);
}
else
{
Naming.rebind("//localhost/"+indexname+"_searcher", searcher);
}
System.out.println(indexname+"_searcher bound");
}
/**
*
*/
public Class getAnalyzerClass(String indexname) throws RemoteException
{
try {
return getIndexByName(indexname).getAnalyzerClass();
} catch (IndexNotFoundException e) {
// TODO Auto-generated catch block
throw new RemoteException();
}
}
/**
*
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public XFileFactory getXFileFactory()
{
return XFFactory;
}
/**
*
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public void addTask(TimerTask t,Date d,int period)
{
Timer timer = new Timer();
timer.schedule(t,d,period);
timers.add(timer);
}
/**
*
* @author rcocula
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public Registry getRegistry()
{
return registry;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -