import javax.naming.*;
import java.util.Hashtable;
import com.linar.jintegra.*;

public class ComToWebSphere
{
	String defaultLogicalId = "wsejb";
	String defaultDcomPort = "1350";
	
	public ComToWebSphere()
	{
	}

	public void registerJVMandSleep(String logicalId, String dcomPort, String corbaPort)
	{
		String passId = null;
		int    passDcomPort = -1;
		
		if (logicalId != null && logicalId != "")
			passId = logicalId;
		else
			passId = defaultLogicalId;
			 
		if (dcomPort != null && dcomPort != "")
			passDcomPort = Integer.parseInt(dcomPort);
		else
			passDcomPort = Integer.parseInt(defaultDcomPort);
			
			
	  	try {
		// For COM access to objects loaded via JNDI lookup
		//com.linar.jintegra.Log.logImmediately(3, System.err);
		// name, instanciator, port
        //	Jvm.register("wsejb", new EjbInstanciator(), defaultDcomPort);
        //	Jvm.register("wsejb", 1350);
			Jvm.register(passId, new EjbInstanciator(corbaPort), passDcomPort);

			System.out.println("Bridge Started");
			while (true) {
		  	Thread.sleep(5000);
			}
	  	}
	  	catch (Exception eX) {
			System.out.println("Exception Error: " + eX.getMessage());
	    }
	}
	 
	public static void main(String[] args)
	{
	  ComToWebSphere bridge = new ComToWebSphere();
	  
	  // logicalId, Dcom Port, Corba Port	   
	  if (args.length == 3)
	  	bridge.registerJVMandSleep(args[0], args[1], args[2]);
	  else
	  {
	  	if (args.length == 0)
			bridge.registerJVMandSleep(bridge.getDefaultLogicalId(), bridge.getDefaultDcomPort(), null);
	  	else
			System.out.println("Usage: ComToWebSphere logicalId DCOM-PORT CORBA-PORT");
	  }
	}

	public String getDefaultLogicalId()
	{
		return (defaultLogicalId);
	}
	
	public String getDefaultDcomPort ()
	{
		return (defaultDcomPort);
	}
}





import com.linar.jintegra.*;
import javax.naming.*;
import java.util.Hashtable;

public class EjbInstanciator implements Instanciator
{ 
	 Context ctx;
	 String defaultCorbaPort = "2809"; 
 
	EjbInstanciator(String overRidePort) throws NamingException
	{ 
		Hashtable env = new Hashtable(); 
		// prior version I think 4
		env.put(Context.INITIAL_CONTEXT_FACTORY, 
		  "com.ibm.websphere.naming.WsnInitialContextFactory");
	   // version 5.1 
       // env.put(Context.INITIAL_CONTEXT_FACTORY, 
       // "com.ibm.ws.naming.util.WsnInitCtxFactory");
		if (overRidePort != null && overRidePort != "") 
			env.put(Context.PROVIDER_URL, "corbaloc:iiop://localhost:" + overRidePort);
		else
			env.put(Context.PROVIDER_URL, "corbaloc:iiop://localhost:" + defaultCorbaPort);
       // env.put(Context.PROVIDER_URL, "corbaloc:rir:/NameServiceServerRoot");
	   // get InitialContext from webSphere 
		ctx = new InitialContext(env);
          
	}
	
	private boolean findSlash(String s)
	{
	  char [] characters = s.toCharArray();
	  for (int i = 0; i < characters.length; i++) {
		if (characters[i] == '/') {
		  return true;
		}// end if
	  } // end for
	  return false;
	}
	
	 public Object instanciate(String javaClass) throws AutomationException { 
		 try { 
			 System.out.println("Class requested is..." + javaClass); 
			 if (findSlash(javaClass)) { 
				 Object obj = ctx.lookup(javaClass); 
				System.out.println("found slash"); 
				 return obj; 
			 } 
			 System.out.println("before new instance");
			 return Class.forName(javaClass).newInstance(); 
              
		 } catch (Throwable t) { 
			 t.printStackTrace(); 
			 throw new AutomationException(new Exception("Unexpected: " + t)); 
		 }
	 } // end method	 
      
}  // end class