[Webfunds-commits] java/webfunds/ricardian SOXServer.java
Ian Grigg
iang@cypherpunks.ai
Fri, 6 Apr 2001 18:42:59 -0400 (AST)
iang 01/04/06 18:42:58
Modified: webfunds/ricardian SOXServer.java
Log:
1. Moved over to webfunds.util.IniFileReader and FormattedFileException.
2. fixed up to use CommsManager.
3. Deprecated by webfunds.sox.server.SSD;
Revision Changes Path
1.19 +110 -51 java/webfunds/ricardian/SOXServer.java
Index: SOXServer.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/SOXServer.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- SOXServer.java 2001/03/24 16:26:59 1.18
+++ SOXServer.java 2001/04/06 22:42:58 1.19
@@ -1,5 +1,5 @@
/*
- * $Id: SOXServer.java,v 1.18 2001/03/24 16:26:59 iang Exp $
+ * $Id: SOXServer.java,v 1.19 2001/04/06 22:42:58 iang Exp $
*
* Copyright (c) Systemics Ltd 1995-1999 on behalf of
* the WebFunds Development Team. All Rights Reserved.
@@ -16,7 +16,16 @@
import java.util.Vector;
import webfunds.utils.Diagnostics;
+
+import webfunds.util.Panic;
+import webfunds.util.IniFileReader;
+import webfunds.util.FormattedFileException;
+
import webfunds.comms.RawHttp;
+import webfunds.comms.CommsManager;
+import webfunds.comms.BasicCommsManager;
+import webfunds.comms.SingleRequestor;
+import webfunds.comms.RawException;
import webfunds.comms.RawReplyException;
import webfunds.comms.RawURLException;
import webfunds.comms.RawConnectException;
@@ -42,6 +51,7 @@
*
* NOTE that this might be better in SOX hierarchy.
* It should also have its own IniFile format reader.
+ * It is also misnamed, as it is a SOX Server _File_ manager.
*/
public class SOXServer
extends Encodable implements Diagnostics
@@ -81,7 +91,8 @@
public SOXServer(byte[] fileData, String name, PrintWriter bug)
throws SOXServerException
{
- this.bug = new PrintWriter(bug, true);
+ if (bug != null)
+ this.bug = new PrintWriter(bug, true);
this.fileData = fileData;
this.originalName = name;
init();
@@ -112,9 +123,9 @@
{
try {
serverFile = new IniFileReader(fileData);
- } catch (ContractException ex) {
+ } catch (FormattedFileException ex) {
ex.printStackTrace(System.err);
- throw new SOXPacketException("ContractEx: " + ex);
+ throw new SOXPacketException("FFEx: " + ex);
};
}
@@ -123,9 +134,9 @@
{
try {
serverFile = new IniFileReader(fileData);
- } catch (ContractException ex) {
+ } catch (FormattedFileException ex) {
ex.printStackTrace(System.err);
- throw new SOXServerException(ex.getMessage());
+ throw new SOXServerException("FFEx: " + ex);
};
refreshServerData();
}
@@ -195,12 +206,26 @@
* Create a SOXServer object given an URL
*
* @param url URL to read the object from
+ *
+ * XXX: deprecate!
*/
public static SOXServer getInstance(URL url, PrintWriter bug)
throws SOXServerException, SOXLaterException
{
- byte[] data;
+ CommsManager comms = new BasicCommsManager();
+ return getInstance(url, comms, bug);
+ }
+ /**
+ * Create a SOXServer object given an URL
+ *
+ * @param url URL to read the object from
+ * @param comms where to get a comms requestor from
+ */
+ public static SOXServer getInstance(URL url, CommsManager comms,
+ PrintWriter bug)
+ throws SOXServerException, SOXLaterException
+ {
String proto = url.getProtocol();
if (proto.equals("file"))
return getInstance(new File(url.getFile()), bug);
@@ -209,23 +234,33 @@
throw new SOXServerException("unknown proto " + proto +
" in URL " + url);
- RawHttp http;
- http = new RawHttp(url, bug);
+ // RawHttp http = new RawHttp(url, bug);
+ SingleRequestor sr = comms.getSingleRequestor(url);
+ byte[] getRequest = RawHttp.getGetData(url);
+ byte[] buf;
try {
// do a Get on the URL
- data = http.getPage();
- } catch (RawReplyException ex) {
- throw new SOXServerException(ex.getMessage() +
- "\n\nThe server file <<" + url + ">> is not found.\n" +
- "Check this URL (as found in the contract local file).");
+ buf = sr.get(getRequest);
} catch (RawConnectException ex) {
throw new SOXLaterException(ex.getErrno(), "no net?\n" + ex);
} catch (RawURLException ex) {
ex.printStackTrace();
throw new SOXServerException("bad url?\n" + ex);
+ } catch (RawException ex) {
+ ex.printStackTrace();
+ throw new Panic("Bad RawEx: " + ex);
}
+ byte[] data;
+ try {
+ data = RawHttp.getReply(buf);
+ } catch (RawReplyException ex) {
+ throw new SOXServerException(ex.getMessage() +
+ "\n\nThe server file <<" + url + ">> is not found.\n" +
+ "Check this URL (as found in the contract local file).");
+ }
+
return new SOXServer(data, url+"", bug) ;
}
@@ -250,40 +285,35 @@
/**
* Return an Issuer.
* Ready to go? Tested? Probably.
+ * XXX: deprecate, no CommsManager
*/
- public Issuer getIssuer(Contract con)
+ /*package*/ Issuer getIssuer(Contract con)
throws SOXIssuerException, SOXLaterException
{
- logmsg("getIssuer ( " + con + " ) == " + smart);
+ logmsg("DEPRECATED getIssuer ( " + con + " ) == " + smart);
if (smart == null)
- {
- //
- // Extract the URLs for the server(s).
- //
- String[] urls;
- urls = getArray("server_url");
+ smart = initSmartIssuer(con, new BasicCommsManager());
- //
- // Extract the cert.
- //
- Certificate cert;
- try {
- cert = con.getServerCert();
- } catch (ContractException ex) {
- throw new SOXIssuerException("contract cert is bad: " + con +
- "\n" + ex);
- }
+ logmsg("smart.getReady()");
+ smart.getReady();
+ logmsg("Ready for Action!");
- logmsg("smart with " + urls.length + " urls");
- smart = new SmartIssuer(urls, cert, getDebug());
+ // to go any further - re-read the SOXServer file - we need
+ // to know whether we are online.
- //
- // Pass the URLs of other non-SOX servers to the SmartIssuer
- // so that it can check the status of the net.
- //
- urls = getArray("nearby_urls");
- smart.setOtherURLs(urls);
- }
+ return (Issuer)smart;
+ }
+
+ /**
+ * Return an Issuer.
+ * Ready to go? Tested? Probably.
+ */
+ /*package*/ Issuer getIssuer(Contract con, CommsManager comms)
+ throws SOXIssuerException, SOXLaterException
+ {
+ logmsg("getIssuer ( " + con + " ) == " + smart);
+ if (smart == null)
+ smart = initSmartIssuer(con, comms);
logmsg("smart.getReady()");
smart.getReady();
@@ -295,8 +325,42 @@
return (Issuer)smart;
}
+ private SmartIssuer initSmartIssuer(Contract con, CommsManager comms)
+ throws SOXIssuerException, SOXLaterException
+ {
+ //
+ // Extract the URLs for the server(s).
+ //
+ String[] urls;
+ urls = getArray("server_url");
+
+ //
+ // Extract the cert.
+ //
+ Certificate cert;
+ try {
+ cert = con.getServerCert();
+ } catch (ContractException ex) {
+
+ throw new SOXIssuerException("contract cert is bad: " + con +
+ "\n" + ex);
+ }
+ logmsg("smart with " + urls.length + " urls");
+ SmartIssuer smart = new SmartIssuer(urls, cert, comms, getDebug());
+ //
+ // Pass the URLs of other non-SOX servers to the SmartIssuer
+ // so that it can check the status of the net.
+ //
+ urls = getArray("nearby_urls");
+ smart.setOtherURLs(urls);
+
+ return smart;
+ }
+
+
+
/////// Contents /////////////////////////////////////////////
static final String http = "http://";
@@ -362,8 +426,6 @@
return names ;
}
- // public String getIssuerName() { return getName() ; }
-
public URL[] getServerLocation()
{
@@ -410,16 +472,10 @@
if (s == null || "".equals(s) )
{
-System.err.println("couldn't find [" + section + "] " + item);
item = section + "_" + item;
s = serverFile.getSectionItemValue(section, item);
}
-if (s == null || "".equals(s) ) {
-System.err.println("couldn't find [" + section + "] " + item + " in\n\n" + serverFile);
-serverFile.dump();
-} else System.err.println("[" + section + "] " + item + " = " + s);
-
return s;
}
@@ -526,8 +582,10 @@
* With a bunch of URLs, go out and get a SOX Server file from the net.
* Should be called using the contract data by a Store.
* @except SOXServerException if the format is duff
+ *
+ * XXX: deprecate!
*/
- public static SOXServer getNewSOXServerFile(URL[] urls)
+ private static SOXServer getNewSOXServerFile(URL[] urls)
throws SOXServerException
{
@@ -603,13 +661,14 @@
String name = arg[0];
SOXServer sox = null;
PrintWriter bug = new PrintWriter(System.err);
+ CommsManager comms = new BasicCommsManager();
try
{
if (name.startsWith("http://"))
{
URL url = new URL(name);
- sox = SOXServer.getInstance(url, bug);
+ sox = SOXServer.getInstance(url, comms, bug);
}
else
{