/*
* NzdlServiceFactory.java
* Copyright (C) 2001 New Zealand Digital Library Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// the package we're in
package org.nzdl.gsdl.util;
// java libraries we're using
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Serializable;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
import java.util.Hashtable;
// local libraries
import org.nzdl.gsdl.service.NzdlCollectionInfo;
import org.nzdl.gsdl.service.NzdlQuery;
import org.nzdl.gsdl.service.NzdlRequest;
import org.nzdl.gsdl.service.NzdlResponse;
import org.nzdl.gsdl.service.NzdlResultSet;
import org.nzdl.gsdl.service.NzdlService;
import org.nzdl.gsdl.service.NzdlServiceClient;
import org.nzdl.gsdl.service.NzdlCacheWrapper;
import org.nzdl.gsdl.service.NzdlLogWrapper;
import org.nzdl.gsdl.service.NzdlDocSaveWrapper;
import org.nzdl.gsdl.service.NzdlMultiWayWrapper;
/**
* Class NzdlServiceFactory
*
* A repositry for IORs of various NZDL servers we've seen.
* Finds IORs from sources such as: URL, file, string, or server based
* list of known IORs.
*
* @author stuart yeates (say1@cs.waikato.ac.nz)
* @author Brett Sheeran (bsheeran@xtra.co.nz)
* @author Dave Nichols (daven@cs.waikato.ac.nz)
* @author Gordon Paynter (paynter@cs.waikato.ac.nz)
* @version $Revision: 2183 $
* @see org.nzdl.gsdl.SimpleServer
* @see org.nzdl.gsdl.service.NzdlCollectionInfo
* @see org.nzdl.gsdl.service.NzdlQuery
* @see org.nzdl.gsdl.service.NzdlRequest
* @see org.nzdl.gsdl.service.NzdlResponse
* @see org.nzdl.gsdl.service.NzdlResultSet
* @see org.nzdl.gsdl.service.NzdlService
* @see org.nzdl.gsdl.service.NzdlServiceClient
* @see gnu.getopt.Getopt
* @see gnu.getopt.LongOpt
* @see The New Zealand Digital Library Project
* @see Greenstone Digital Library Software
*/
public class NzdlServiceFactory implements Cloneable, Serializable {
/** The collection of CORBA IORs we know about */
protected static Properties knownIORs = null;
/** The name of the properties file */
static String propertiesFileName = "knownIORs.properties";
/** The name of the file we look for IOR's in */
public static String IORFileName = "/tmp/localcorba.objid";
/** default places wit look for IORs ... */
static String[] urlsCompiledIn =
{
"http://nikau.cs.waikato.ac.nz/~say1/gsdl/cgi-bin/getior",
"http://www.nzdl.org/cgi-bin/getior"
};
/** Are we using a caching NzdlService ? */
static public boolean caching = true;
/** Are we logging before the cache ? */
static public boolean logBefore = true;
/** Are we logging after the cache ? */
static public boolean logAfter = true;
/** Are we saving documents ? */
static public boolean saveDocs = false;
/** Are we trying for multiple servers ? */
static public boolean doMultiple = false;
/** Are we logging after individual servers ? */
static public boolean logMultiple = false;
/**
* Get the first known-good IOR
* @see java.lang.String
* @return the string containing the IOR
*/
public static String getFirstValid() {
validate(); // implies checkLoaded();
synchronized (knownIORs) {
Enumeration e = knownIORs.keys();
while (e.hasMoreElements()) {
String IOR = (String) e.nextElement();
if (attemptToInitialise(null, null, IOR) != null) {
return IOR;
}
}
return null;
}
}
/**
* Get the all known-good IOR
* @see java.lang.String
* @return the string[] containing the IORs
*/
public static String[] getAllValid() {
checkLoaded();
Vector vec = new Vector();
synchronized (knownIORs) {
Enumeration e = knownIORs.keys();
while (e.hasMoreElements()) {
String IOR = (String) e.nextElement();
if (attemptToInitialise(null, null, IOR) != null) {
vec.add(IOR);
}
}
}
String[] strings = new String[vec.size()];
Enumeration e = vec.elements();
int i = 0;
while (e.hasMoreElements()) {
strings[i++] = (String) e.nextElement();
}
return strings;
}
/**
* Check that the class has been correctly initialised
*/
static protected void checkLoaded() {
// if we're already initialised we can bail now and
// save synchronization overhead ...
if (knownIORs != null)
return;
Properties blarg = new Properties();
// take a lock on blarg. currently it's local, but
// won't be soon...
synchronized (blarg) {
try {
synchronized (Class.forName("org.nzdl.gsdl.util.NzdlServiceFactory")) {
if (knownIORs != null)
return;
knownIORs = blarg;
}
} catch (ClassNotFoundException exception) {
System.err.println("Exception locking class:" + exception);
}
try {
File file = new File(propertiesFileName);
if (file.exists()) {
knownIORs.load(new FileInputStream(file));
}
} catch (IOException exception) {
System.err.println("Exception reading properties file:" + exception);
}
}
getIORsfromFile(IORFileName);
}
/**
* Save the IORs back to the underlying file
* @see java.io.File
*/
static protected void checkSave() {
synchronized (knownIORs) {
if (knownIORs == null)
throw new Error("saving null property list");
File file = new File(propertiesFileName);
try {
knownIORs.store(new FileOutputStream(file),
"NZDL IOR's see: org.nzdl.gsdl.util.NzdlServiceFactory");
} catch (IOException i) {
System.err.println("Error reading properties file:" + i);
}
}
}
/**
* Find a valid IOR to initialise from.
* @param _args the command line arguments
* @param _URL a URL to look for an IOR in
* @param _filename a local filename to look for an IOR in
* @param _IOR a string to look for an IOR in
* @see org.omg.CORBA.ORB
*/
public static NzdlService findIOR(String [] _args,
Properties _props,
String _URL,
String _filename,
String _IOR) {
// read our prevously seed IORs ...
String IOR = null;
NzdlService client = null;
// try a URL from the command line
if ( _URL != null) {
String[] IORs = NzdlServiceFactory.getIORsfromURL(_URL);
for (int i=0;( i