new simplified Siena interface for micro-oracle.

valetto [2001-11-13 04:31:54]
new simplified Siena interface for micro-oracle.
Filename
OracleSienaInterface.java
diff --git a/OracleSienaInterface.java b/OracleSienaInterface.java
index 3c91956..8d7ded7 100644
--- a/OracleSienaInterface.java
+++ b/OracleSienaInterface.java
@@ -5,8 +5,8 @@
 * Copyright (c) 2000: The Trustees of Columbia University and the City of New York.
 *							   All Rights Reserved.
 * Company:		<p>
-* @author Kanan Naik
-* @version 1.0
+* @author Peppo Valetto
+* @version 2.0
 */

 package psl.oracle;
@@ -19,316 +19,151 @@ import java.util.*;
 import siena.*;
 import psl.kx.*;

-public class OracleSienaInterface implements Runnable, Notifiable
-{
-    static Siena si = null;
-    static String hostname = null;
-    static String sienaPort = null;
-    PrintWriter log = null;
-    static String rootPath = null;
-    static String propFile = null;
-
-    //static String oraclePath = null;
-    // PrintWriter log = new PrintWriter(System.err);
-
-    /**
-     * Constructor. Set the specified Siena object as the default.
-     */
-    public OracleSienaInterface(Siena s)
-    {
-	this.si = s;
-    }
-
-    /**
-     * Constructor. It sets a Siena master server running at localhost and
-     * 4321 port.
-     */
-    public OracleSienaInterface()
-    {
-	File file = new File(rootPath + File.separator + "oracle.prop");
-	propFile = rootPath + File.separator + "oracle.prop";
-	// File file = new File("oracle.prop");
-	if(!file.exists())
-            {
-                file = new File(rootPath + File.separator + "psl" +File.separator+ "oracle" + File.separator + "oracle.prop");
-                if(!file.exists())
-                    {
-                        System.out.println ("File 'oracle.prop' does not exist");
-                        System.exit(1);
-                    }
-		    propFile = rootPath + File.separator + "psl" +File.separator+ "oracle" + File.separator + "oracle.prop";
-	    }
-	Properties property = new Properties();
-	try
-	    {
-	        property.load(new FileInputStream(file));
-	    }
-	catch(FileNotFoundException ffe)
-	    {
-	        System.out.println("Exception: " + ffe);
-	        System.exit(0);
-	    }
-	catch(IOException ioe)
-	    {
-	        System.out.println("Exception: "+ ioe);
-                System.exit(1);
-            }
-	sienaPort = property.getProperty("sienaPort");
-	if(sienaPort == null || sienaPort.length() < 1)
-	    {
-	        System.out.println("Parameter 'sienaPort' must be set in 'oracle.prop' file.");
-	        System.exit(0);
-	    }
-	String master = "senp://localhost:4321";
-	HierarchicalDispatcher hd = new HierarchicalDispatcher();
-	try
-	    {
-	      	hd.setMaster(master);
-	       	System.out.println("Oracle master is " + master);
-	    }
-	catch(siena.InvalidSenderException e)
-	    {
-	       	e.printStackTrace();
-	    }
-	catch(IOException ex)
-	    {
-	       	ex.printStackTrace();
-	    }
-	OracleSienaInterface osi = new OracleSienaInterface(hd);
-	Thread t = new Thread(osi);
-	t.start();
-    }
-
-
-    /**
-     * Siena master server can be specified as command line argument.
-     * If no argument is specified then it will start it at localhost,
-     * port 4321.
-     */
-    public static void main(String[] args)
-    {
-	if((args.length < 1) || (args.length > 2) )
-	    {
-		System.out.println("Usage: java psl.oracle.OracleSienaInterface <root path> [senp://<host>:<port>]");
-		System.exit(1);
-	    }
-	rootPath = args[0];
-	File file = new File(rootPath + File.separator + "oracle.prop");
-	propFile = rootPath + File.separator + "oracle.prop";
-	if(!file.exists())
-	    {
-		propFile = rootPath + File.separator + "psl" + File.separator + "oracle" + File.separator + "oracle.prop";
-		file = new File(propFile);
-		if(!file.exists())
-		    {
-			System.out.println("File " + propFile + " does not exist" + propFile);
-			System.exit(2);
-		    }
-	    }
-	String senp = null;
-	if(args.length == 2)
-	    senp = args[1];
-	if(senp == null || senp.length() < 1)
-	    {
-		senp = "senp://localhost:4321";
-	    }
-	InetAddress addr = null;
-	try
-	    {
-  	      	addr = InetAddress.getLocalHost();
-	    }
-	catch(Exception e)
-	    {
-           	System.err.println("Exception occurred: " + e);
-	    }
-	hostname = addr.getHostName();
-	SendWorklet sw = new SendWorklet(hostname, "OracleRegistry");
-	String master = senp;
-	final HierarchicalDispatcher hd = new HierarchicalDispatcher();
-	try
-	    {
-	      	hd.setMaster(master);
-	       	System.out.println("Oracle master is " + master);
-	    }
-	catch(siena.InvalidSenderException e)
-	    {
-	       	e.printStackTrace();
-	    }
-	catch(IOException ex)
-	    {
-	       	ex.printStackTrace();
-	    }
-	OracleSienaInterface osi = new OracleSienaInterface(hd);
-	Thread t = new Thread(osi);
-	t.start();
-	Runtime.getRuntime().addShutdownHook(new Thread()
-	    {
-	       	public void run()
-	      	{
-		    System.out.println("Unsubscribing Siena..");
-		    hd.shutdown();
-	       	}
-	    });;
+public class OracleSienaInterface implements Runnable, Notifiable {
+  static Siena si = null;
+  static MicroOracle microOracleHandle = null;
+  static String sienaHost = "localhost";
+  String hostname = null;
+  static int sienaPort = 7130;
+  PrintWriter log = null;
+
+  /**
+   * Constructor. It sets a Siena master server running at localhost and
+   * 4321 port.
+   */
+  public OracleSienaInterface(String sienaURL) {
+	String master;
+	if (sienaURL == null || sienaURL.equals("") || sienaURL.equals("default"))
+    	master = "senp://" + sienaHost + ":" + sienaPort;
+    else
+    	master = sienaURL;
+    HierarchicalDispatcher hd = new HierarchicalDispatcher();
+    try {
+      hd.setMaster(master);
+      System.out.println("Oracle master is " + master);
+    } catch(siena.InvalidSenderException e) {
+      e.printStackTrace();
     }
-
-
-    /**
-     * Subscribes to an event generated by Metaparser.
-     */
-
-    public void run()
-    {
-	Filter f = new Filter();
-	f.addConstraint("Source", "psl.metaparser.ParserThread");
-	f.addConstraint("Type", "MPQuery");
-	try
-	    {
-		si.subscribe(f, this);
-	    }
-	catch(siena.SienaException se)
-	    {
-		se.printStackTrace();
-	    }
-	System.out.println("Oracle subscribed to " + f);
-    }
-
-    /**
-     * This method is called when an event is received from Metaparser.
-     * It will call getFragment(String query) method of Oracle. It will
-     * generate a Siena event once a reply is received from Oracle.
-     */
-
-    public void processMPQuery(String query, String MPHost, int MPSourceID, String MPRequestID)
-    {
-	Hashtable publishEvent = new Hashtable();
-	SchemaFragment fragment = new SchemaFragment();
-	Vector moduleName = new Vector();
-	Oracle oracle = new Oracle(propFile);
-	String msg = null;
-	boolean result = false;
-	publishEvent.put("Hostname", new AttributeValue(hostname));
-	int srcID = this.hashCode();
-	publishEvent.put("MPRequestID", new AttributeValue(MPRequestID));
-	publishEvent.put("MPHostname", new AttributeValue(MPHost));
-    try
-	    {
-		fragment = oracle.getFragment(query, rootPath);
-	    }
-	catch (UnknownTagException ex)
-	    {
-		msg = "Exception occured at Oracle: " + ex;
-	    }
-	catch (InvalidQueryFormatException e)
-	    {
-		msg = "Exception occured at Oracle: " + e;
-	    }
-	catch (InvalidSchemaFormatException e)
-	    {
-		msg = "Exception occured at Oracle: " + e;
-	    }
-	catch (Exception e)
-	    {
-		msg = "Exception occured at Oracle: " + e;
-	    }
-	if(msg == null) //no exception occurred while retrieving a fragment
-	    {
-		result = true;
-		moduleName = fragment.getModuleName();
-		SchemaFragmentToXML sfx = new SchemaFragmentToXML();
-		msg = sfx.toXML(fragment);
-	    }
-	publishEvent.put("Value", new AttributeValue(msg));
-
-	//create a notification using KXNotification
-
-	// System.err.println("before sending reply, publishEvent =");
-	// System.err.println(" " + publishEvent);
-	Notification n = KXNotification.OracleOutputKXNotification("psl.oracle.OracleSienaInterface",srcID,MPSourceID,result,publishEvent);
-	try
-	    {
-		System.out.println("Oracle is sending back a reply: " + n);
-		si.publish(n);
-	    }
-	catch(siena.SienaException se)
-	    {
-		se.printStackTrace();
-	    }
-	if(!moduleName.isEmpty())
-	    {
-		SendOracleReply sor = new SendOracleReply();
-		sor.sendReply(MPRequestID, MPHost, MPRequestID, moduleName);
-	    }
+    catch(IOException ex) {
+      ex.printStackTrace();
     }
-
-
-
-    /**
-     * This method will generate an event for TriKX. It should be
-     * called when Oracle wants to send a notification to TriKX
-     * about a portal update.
-     */
-
-    public void generateTriKXEvent(String event)
-    {
-	Hashtable publishEvent = new Hashtable();
-	publishEvent.put("Hostname", hostname);
-	publishEvent.put("Type", "trikxEvent");
-	publishEvent.put("TrikxEvent", event);
-	int srcID = this.hashCode();
-	Notification n1 = KXNotification.EDInputKXNotification("psl.oracle.OracleSienaInterface",srcID,publishEvent);
-	System.out.println("Oracle is sending an update event to TriKX");
-	try
-	    {
-		si.publish(n1);
-	    }
-	catch(siena.SienaException se)
-	    {
-		se.printStackTrace();
-	    }
+
+    si = hd;
+
+    try {
+    	hostname = InetAddress.getLocalHost().getHostName();
+    } catch (Exception e) {
+       System.err.println("Exception occurred: " + e);
+     }
+  }
+
+  public OracleSienaInterface() {
+  	this("default");
+  }
+
+  /**
+   * Subscribes to an event generated by Metaparser.
+   */
+
+  public void run() {
+    Filter f = new Filter();
+    f.addConstraint("Source", "psl.metaparser.ParserThread");
+    f.addConstraint("Type", "MPQuery");
+    try {
+      si.subscribe(f, this);
+    } catch(siena.SienaException se) {
+      se.printStackTrace();
     }
-
-    /**
-     * Writes messages to a log file
-     */
-
-    public void printLog(String msg)
-    {
-	log.println("OracleSienaInterface: " + msg);
+    System.out.println("Oracle subscribed to " + f);
+  }
+
+	static public void setOracleHandle (MicroOracle mo) {
+		System.err.println ("setting micro-oracle ...");
+		microOracleHandle = mo;
+		System.err.println  ("micro-oracle is " + mo.toString());
+	}
+	static public MicroOracle getOracleHandle () { return microOracleHandle; }
+
+
+  /**
+   * This method will generate an event for TriKX. It should be
+   * called when Oracle wants to send a notification to TriKX
+   * about a portal update.
+   */
+
+  public void generateTriKXEvent(String event) {
+    Hashtable publishEvent = new Hashtable();
+    publishEvent.put("Hostname", hostname);
+    publishEvent.put("Type", "trikxEvent");
+    publishEvent.put("TrikxEvent", event);
+    int srcID = this.hashCode();
+    Notification n1 = KXNotification.EDInputKXNotification("psl.oracle.OracleSienaInterface",srcID,publishEvent);
+    System.out.println("Oracle is sending an update event to TriKX");
+    try {
+      si.publish(n1);
+    } catch(siena.SienaException se) {
+      se.printStackTrace();
     }
-
-
-    /**
-     * This method will receive the events from the subscribed
-     * components.
-     */
-    public void notify(Notification n)
-    {
-	AttributeValue av = n.getAttribute("MPQuery");
-	if (av != null)
-	    {
-		String query = av.stringValue();
-		System.out.println("Oracle got a query !!");
-		av = n.getAttribute("Hostname");
-		String MPHost = av.stringValue();
-		av = n.getAttribute("Source");
-		String MPSource = av.stringValue();
-		av = n.getAttribute("SourceID");
-		int MPSourceID = av.intValue();
-		av = n.getAttribute("RequestID");
-		String MPRequestID = av.stringValue();
-		processMPQuery(query, MPHost, MPSourceID, MPRequestID);
-	    }
-	else
-	    {
-		System.out.println("Oracle error: MP query is null.");
-	    }
+  }
+
+  /**
+   * Writes messages to a log file
+   */
+
+  public void printLog(String msg) {
+    log.println("OracleSienaInterface: " + msg);
+  }
+
+  	private void processHint (String hint, String MPHost, int source, String request) {
+  		String schema = microOracleHandle.getSchemaURL (hint);
+  		String tagprocessor = microOracleHandle.getProcessorURL (hint);
+
+  		Hashtable publishEvent = new Hashtable();
+  		publishEvent.put("Hostname", new AttributeValue(hostname));
+	    int srcID = this.hashCode();
+    	publishEvent.put("MPRequestID", new AttributeValue(request));
+    	publishEvent.put("MPHostname", new AttributeValue(MPHost));
+    	publishEvent.put("Value", new AttributeValue(schema));
+
+  		Notification n = KXNotification.OracleOutputKXNotification("psl.oracle.OracleSienaInterface",srcID,source,true,publishEvent);
+   		n.putAttribute("TPModule", tagprocessor);
+   		 try {
+     		 System.out.println("Oracle is sending back a reply: " + n);
+    		  si.publish(n);
+   		 } catch(siena.SienaException se) {
+      		se.printStackTrace();
+    	}
+	}
+
+
+  /**
+   * This method will receive the events from the subscribed
+   * components.
+   */
+  public void notify(Notification n) {
+	AttributeValue av = n.getAttribute("Hostname");
+   	String MPHost = av.stringValue();
+ 	av = n.getAttribute("Source");
+	String MPSource = av.stringValue();
+	av = n.getAttribute("SourceID");
+	int MPSourceID = av.intValue();
+	av = n.getAttribute("RequestID");
+	String MPRequestID = av.stringValue();
+
+  	AttributeValue hintAttr = n.getAttribute("useHint");
+  	if (hintAttr != null) {
+  		String hint = hintAttr.stringValue();
+  		System.out.println("Oracle got a hint !! " + hint);
+  		processHint (hint, MPHost, MPSourceID, MPRequestID);
+
+  	}
+  	else {
+    	System.out.println("Oracle error: MP query is null.");
     }
+
+  }

-
-
-    public void notify(Notification[] no)
-    {
-    }
+  public void notify(Notification[] no) {}

 }