Currently trying to work my way through Chapter 4 of SAMS - Teach Yourself CORBA in 14 Days.
The code from the book seems to work, except rather than stopping as expected, it spits out "Couldn't bind StockServer: "
In an attempt to resolve this issue, I modified the book to use POA and I started tnameserv, but the issue remains.
Here is the code I typed and modified, instead of "compiled" (generated) with idlj:
// StockServerImpl.java
package StockMarket;
import java.util.Vector;
import org.omg.CORBA.*;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
/**
* StockMarket/StockServer.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from StockMarket.idl
* Thursday, 30 September 2010 14:40:36 o'clock CEST
*/
// StockServerImpl implements the StockServer IDL interface
public class StockServerImpl
extends StockServerPOA
//extends _StockServerImplBase
implements StockServer
{
/**
*
*/
private static final long serialVersionUID = 1L;
// Stock symbols and their respective values.
private Vector<String> myStockSymbols;
private Vector<Float> myStockValues;
// Characters from which StockSymbol names are built.
private static char ourCharacters[] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
// Path name for StockServer objects.
private static String ourPathName = "StockServer";
// Create a new StockServerImpl.
public StockServerImpl()
{
myStockSymbols = new Vector<String>();
myStockValues = new Vector<Float>();
// Initialize the symbols and values with some random values.
for (int i=0; i<10; i++)
{
// Generate a string of four random characters.
StringBuffer stockSymbol = new StringBuffer(" ");
for (int j=0; j<4; j++)
{ stockSymbol.setCharAt(j, ourCharacters[(int) (Math.random() * 26f)]); }
myStockSymbols.addElement(stockSymbol.toString());
// Give the stock a value between 0 and 100. In this example,
// the stock will retain this value for the duration of the
// application.
myStockValues.addElement(new Float(Math.random() * 100f));
}
// Print out the stock symbols generated above.
System.out.println("Generated stock symbols:");
for (int i=0; i<10; i++)
{
System.out.println(
" " + myStockSymbols.elementAt(i)
+ " " + myStockValues.elementAt(i)
);
}
System.out.println();
}
// Return the current value for the given StockSymbol.
@Override
public float getStockValue(String symbol) throws InvalidStockSymbolException
{
// Try to find the given symbol.
int stockIndex = myStockSymbols.indexOf(symbol);
if (stockIndex != -1)
{
// Symbol found; return its value.
return (myStockValues.elementAt(stockIndex).floatValue());
}
else
{
// Symbol was not found.
throw new InvalidStockSymbolException();
}
}
// Return a sequence of all StockSymbols known by this StockServer.
@Override
public String[] getStockSymbols()
{
String[] symbols = new String[myStockSymbols.size()];
myStockSymbols.copyInto(symbols);
return symbols;
}
/**
* @param ourPathName the ourPathName to set
*/
public static void setOurPathName(String ourPathName) {
StockServerImpl.ourPathName = ourPathName;
}
/**
* @return the ourPathName
*/
public static String getOurPathName() {
return ourPathName;
}
// Create and initialize a StockServer object.
public static void main (String args[])
{
NameComponent nameComponent = null;
NamingContext namingContext = null;
ORB orb = null;
org.omg.CORBA.Object obj = null;
StockServerImpl stockServer = null;
try { orb = ORB.init(args, null); } // Initialize the ORB.
catch (Exception ex) { System.err.println("Can't initialize ORB: " + ex.getMessage());}
try { stockServer = new StockServerImpl(); } // Create a StockServerImpl object and register it with the ORB.
catch (Exception ex) { System.err.println("Can't create StockServer: " + ex.getMessage()); }
try { orb.connect(stockServer); }
catch (Exception ex) { System.err.println("Can't connect ORB to StockServer: " + ex.getMessage()); ex.printStackTrace(); }
try { obj = orb.resolve_initial_references("NameService"); } // Get the root naming context.
catch (Exception ex) { System.err.println("Can't resolve NameServeice: " + ex.getMessage()); ex.printStackTrace();}
try { namingContext = NamingContextHelper.narrow(obj); }
catch (Exception ex) { System.err.println("Can't narrow NamingContext: " + ex.getMessage()); }
try { nameComponent = new NameComponent (ourPathName, ""); } // Bind the StockServer object reference in the naming context
catch (Exception ex) { System.err.println("Can't create NameComponent: " + ex.getMessage()); }
NameComponent path[] = { nameComponent };
try { namingContext.rebind(path, stockServer); }
catch (Exception ex) { System.err.println("Can't rebind NameComponent to StockServer: " + ex.getMessage()); ex.printStackTrace(); }
// Wait for invocations from clients.
java.lang.Object waitOnMe = new java.lang.Object();
synchronized(waitOnMe)
{
try { waitOnMe.wait(); }
catch (InterruptedException ex)
{
System.err.println("Can't wait: " + ex.getMessage());
ex.printStackTrace();
}
}
}
@Override
public Request _create_request(Context ctx, String operation,
NVList arg_list, NamedValue result) {
// TODO Auto-generated method stub
return null;
}
@Override
public Request _create_request(Context ctx, String operation,
NVList arg_list, NamedValue result, ExceptionList exclist,
ContextList ctxlist) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object _duplicate() {
// TODO Auto-generated method stub
return null;
}
@Override
public DomainManager[] _get_domain_managers() {
// TODO Auto-generated method stub
return null;
}
@Override
public Policy _get_policy(int policy_type) {
// TODO Auto-generated method stub
return null;
}
@Override
public int _hash(int maximum) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean _is_equivalent(Object other) {
// TODO Auto-generated method stub
return false;
}
@Override
public void _release() {
// TODO Auto-generated method stub
}
@Override
public Request _request(String operation) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object _set_policy_override(Policy[] policies,
SetOverrideType set_add) {
// TODO Auto-generated method stub
return null;
}
} // interface StockServer
Here is my console output, including stacktraces:
Generated stock symbols:
WION 56.691833
KQEJ 40.678604
HCBM 82.15452
VERC 30.731018
LEAR 11.632088
QLCV 58.973534
FJDO 57.708836
SVPS 29.638231
NNGN 27.48113
UAWE 65.20851
Can't connect ORB to StockServer:
org.omg.CORBA.OBJ_ADAPTER: vmcid: SUN minor code: 202 completed: No
at com.sun.corba.se.impl.logging.ORBUtilSystemException.orbConnectError(Unknown Source)
at com.sun.corba.se.impl.logging.ORBUtilSystemException.orbConnectError(Unknown Source)
at com.sun.corba.se.impl.orb.ORBImpl.connect(Unknown Source)
at StockMarket.StockServerImpl.main(StockServerImpl.java:137)
Caused by: org.omg.CORBA.BAD_OPERATION: vmcid: SUN minor code: 240 completed: No
at com.sun.corba.se.impl.logging.ORBUtilSystemException.getTypeIdsRequiresStub(Unknown Source)
at com.sun.corba.se.impl.logging.ORBUtilSystemException.getTypeIdsRequiresStub(Unknown Source)
at com.sun.corba.se.spi.presentation.rmi.StubAdapter.getTypeIds(Unknown Source)
at com.sun.corba.se.impl.oa.toa.TOAImpl.connect(Unknown Source)
... 2 more
Can't rebind NameComponent to StockServer:
org.omg.CORBA.BAD_PARAM: vmcid: SUN minor code: 206 completed: No
at com.sun.corba.se.impl.logging.ORBUtilSystemException.localObjectNotAllowed(Unknown Source)
at com.sun.corba.se.impl.logging.ORBUtilSystemException.localObjectNotAllowed(Unknown Source)
at com.sun.corba.se.impl.orbutil.ORBUtility.getIOR(Unknown Source)
at com.sun.corba.se.impl.orbutil.ORBUtility.connectAndGetIOR(Unknown Source)
at com.sun.corba.se.impl.encoding.CDROutputStream_1_0.write_Object(Unknown Source)
at com.sun.corba.se.impl.encoding.CDROutputStream.write_Object(Unknown Source)
at org.omg.CORBA.ObjectHelper.write(Unknown Source)
at org.omg.CosNaming._NamingContextExtStub.rebind(Unknown Source)
at StockMarket.StockServerImpl.main(StockServerImpl.java:151)