package de.unimuenchen.informatik.mnm.masa.agentSystem.nameservice;
import java.io.*;
import java.net.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
/**
* Class NamingWebServer implements a startroutine for
* a CORBA-NameService and WWW-server to replay the IOR
* of the NamingContext-Object of the CORBA-NameService.
* So a the NameService of a special ORB can be used by another ORB.
*
*/
public class NamingWebServer{
private String factory_name; // Factory's name, necessary for VisiBroker's Naming Service
private String logfile; // Name of the logfile, necessary for VisiBroker's Naming Service
private Thread thr_vb_nameservice; // Thread of the NameService (VisiBroker)
private Process proc_jdk_nameservice; // Process of the NameService (JDK1.2)
private String ior; // IOR-String
private String nameORB; // ORB name
private int port; // Port of WWW-Server
private String host; // Hostname
/**
* Method to get the IOR from the NamingWebServer
*
* @return IOR of the NamingContext belonging to the NamingService started by the NamingWebServer
*
*/
public static String getIORWWW() throws Exception
{
String ior = null;
try{
// get host
String host = System.getProperty("namingwebserver.host");
// get portnumber
System.out.print("NamingWebServer.getIORWWW() get port...");
int port = new Integer (System.getProperty("namingwebserver.port")).intValue();
System.out.println("done.");
// Request server for IOR
URL url_name = new URL ("http",host,port,"/");
URLConnection con_name = url_name.openConnection();
InputStream in = con_name.getInputStream();
byte [] buffer = new byte[in.available()];
in.read(buffer);
ior = new String(buffer);
in.close();
// Extract IOR from HTTP-File
ior = ior.substring(ior.indexOf("IOR:"),ior.length());
System.out.println("NamingWebServer.getIORWWW(): " + ior);
} catch (Exception e){
e.printStackTrace();
throw e;
}
return ior;
}
/**
* Function to extract the pur IOR from a string
*
* @param ior the string sequenze containing the IOR
* @return the pur IOR
*/
private static String scanIOR(String ior){
int begin = ior.indexOf("IOR:");
String result = "IOR:";
int i=begin+4;
while(i<ior.length() &&
((47<ior.charAt(i) && ior.charAt(i)<58) ||
(96<ior.charAt(i) && ior.charAt(i)<103))){ //is hex number
result += ior.substring(i,i+1);
i++;
}//while
return result;
}
/**
* Constructor
*/
private NamingWebServer(String factory_name, String logfile) throws Exception
{
// Check property
nameORB = System.getProperty("orb.name");
host = System.getProperty("namingwebserver.host");
port = new Integer(System.getProperty("namingwebserver.port")).intValue();
if (nameORB == null || (!nameORB.equals("VisiBroker") &
!nameORB.equals("JDK1.2")))
throw new Exception ("No or wrong ORB name specified.\n " +
"Valid ORB's: VisiBroker, JDK1.2");
// Check arguments
if(nameORB.equals("VisiBroker")){
if (factory_name==null)
throw new Exception("No factory name specified!");
else this.factory_name = factory_name;
if(logfile==null)
throw new Exception("No logfile specified!");
else this.logfile = logfile;
}
System.out.println("NamingWebServer.<init>: ORB name=" + nameORB);
}
/**
* Wait for request and send as answer of HTTP-requests
* IOR of the NamingContext
*/
private void waitForRequests() throws Exception {
// check if localhost host for namingwebserver
System.out.println("NamingWebServer.waitForRequests(): localhost:" +
InetAddress.getLocalHost() + "\n"+
" configured host: " +
host);
if(!InetAddress.getLocalHost().equals(InetAddress.getByName(host))){
throw new Exception("NamingWebServer must be started on " + host +
" or the scripts must be re-compiled on localhost/"+
InetAddress.getLocalHost() + "!");
}
// Create Serversocket
ServerSocket acceptSocket = new ServerSocket(port);
while (true){
System.out.println("NamingWebServer.waitForRequests(): Wait for requests, listening at port " + port + ".");
// Accept client, wait for requests
Socket socket = acceptSocket.accept();
// Create inputstream
InputStream in = socket.getInputStream();
// Wait for request-input
java.lang.Object a = new java.lang.Object();
synchronized (a) {while(in.available() <= 0) a.wait(10);}
System.out.println("NamingWebServer.waitForRequests(): Request:");
byte [] buffer = new byte[in.available()];
in.read(buffer);
System.out.print(new String(buffer));
// Test whether nameservice still alive
if (thr_vb_nameservice != null && !thr_vb_nameservice.isAlive())
throw new Exception("NameService down!");
// Create outputstream, send ior over http
OutputStream out = socket.getOutputStream();
String answer = new String ("HTTP/1.0 200 OK \r\n" +
"Server: NamingWebServer \r\r" +
"Content_type: text/plain \r\n \r\n" + ior );
out.write(answer.getBytes());
out.flush();
in.close();
out.close();
socket.close();
}
}
/**
* initWebServer() starts the NameService and gets the IOR from it
*/
private void initWebServer()throws Exception{
OutputStream out = System.out; // dummy initialisation, just to satisfy the compiler
InputStream in = System.in;
PrintStream stdout = System.out;;
if (nameORB.equals("VisiBroker")){
///////////////////////////////////////////
// VisiBroker specific BEGIN
// Create PipedInput- and OutputStreams for threadcommunication
out = new PipedOutputStream();
in = new PipedInputStream((PipedOutputStream) out);
// Redirect stdout to Outputstream, stdout saved
PrintStream ptrout = new PrintStream(out,true);
System.setOut(ptrout);
// create thread for nameservice
thr_vb_nameservice = new Thread(new ThreadNameService());
// Start nameService
thr_vb_nameservice.start();
//VisiBroker specific END
///////////////////////////////////////////
} else if (nameORB.equals("JDK1.2")){
///////////////////////////////////////////
// JDK specific BEGIN
// start NameService
System.out.println("NamingWebServer.initWebServer(): Start tnameserv in runtime.");
Runtime runt_nameservice = Runtime.getRuntime();
String [] cmd = {"/proj/java/jdk1.2-solaris/jdk1.2beta4/bin/tnameserv",
"-ORBInitialPort","14050"};
proc_jdk_nameservice = runt_nameservice.exec(cmd);
// Get I/O-Stream of NameService-Process
out = proc_jdk_nameservice.getOutputStream();
in = proc_jdk_nameservice.getInputStream();
System.out.println("NamingWebServer.initWebServer(): NameService starting.");
// JDK specific END
///////////////////////////////////////////
}
// Wait for Nameservice output (=IOR-String)
while(in.available() <=0) {
java.lang.Object a = new java.lang.Object();
// artifical deadlock for waiting,
// i.e. nameservice has chance to write in stream
synchronized (a) {a.wait(1);
}
}
// Read IOR-String
byte [] buffer = new byte[in.available()];
in.read(buffer);
// Close streams
out.close();
in.close();
// extract the IOR from the read string
String iorExtFact = scanIOR(new String(buffer));
if (nameORB.equals("VisiBroker")){
///////////////////////////////////////////
// VisiBroker specific BEGIN
// Reset stdout
System.setOut(stdout);
System.out.println("NamingWebServerinitWebServer(): " + iorExtFact);
// Get IOR of NameService,
// The IOR returned by the NameService is
// the IOR of the ExtendedNamingContextFactory
ORB orb = ORB.init();
org.omg.CORBA.Object refNameContFact =
orb.string_to_object(iorExtFact);
ExtendedNamingContextFactory nameContFact =
ExtendedNamingContextFactoryHelper.narrow(refNameContFact);
NamingContext nameService = nameContFact.root_context();
ior = orb.object_to_string(nameService);
System.out.println("NamingWebServerinitWebServer(): ior length" + ior.length());
//VisiBroker specific END
///////////////////////////////////////////
}else if(nameORB.equals("JDK1.2")){
///////////////////////////////////////////
// JDK specific BEGIN
System.out.println("NamingWebServerinitWebServer(): " + iorExtFact);
ior = iorExtFact;
System.out.println("NamingWebServerinitWebServer(): ior length" + ior.length());
// JDK specific END
///////////////////////////////////////////
}
}
/**
* Stops the NamingWebServer
*/
private void stopWebServer()
{
// Stop NameService
if (thr_vb_nameservice != null && thr_vb_nameservice.isAlive()) {
System.out.println("NamingWebServer.stopWebServer(): Stop NameService (VisiBorker).");
thr_vb_nameservice.interrupt();
} else if (proc_jdk_nameservice != null){
System.out.println("NamingWebServer.stopWebServer(): Stop NameService (JDK1.2).");
proc_jdk_nameservice.destroy();
}
}
/**
* Destruktor
*/
protected void finalize()
{
stopWebServer();
}
//////////////////////////////////////////////
// Main
/////////////////////////////////////////////
public static void main (String[] args) throws Exception {
NamingWebServer nws = null;
try {
nws = new NamingWebServer(args[0],args[1]);
nws.initWebServer();
nws.waitForRequests();
} catch (Exception e){
if (nws != null) nws.stopWebServer();
e.printStackTrace();
}
}
/**
* Thread of the NamingService of VisiBroker 3.0
*/
private class ThreadNameService implements Runnable {
public void run(){
////////////////////////////////////////
// VisiBroker specific BEGIN
// Prepare arguments for namegservice
String[] args_naming = {factory_name,logfile};
// nameservice VisiBroker in an own thread
com.visigenic.vbroker.services.CosNaming.ExtFactory.main(args_naming);
// VisiBroker specific END
///////////////////////////////////////
}
}
}