/**
*
# cat NSlist.sh
#!/bin/ksh
java -classpath /
./:/
/vendorpackages/PrismTech/JACORB/lib/avalon-framework.jar:/
/vendorpackages/PrismTech/JACORB/lib/jacorb.jar:/
/vendorpackages/PrismTech/JACORB/lib/logkit.jar:/
/vendorpackages/TimesTen/lib/classes15.jar /
-Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB /
-Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton /
-DORBInitRef.NameService=corbaloc::localhost:34000/NameService /
-Dnamelist=HeartBeatFactory,ReplicationManager,IpInitial /
-DCheckInterval=10 /
NSList
*
*
* above three RED lines must be defined to pass into ORB.init(...)
*/
import org.omg.CORBA.ORB;
import org.omg.CosNaming.Binding;
import org.omg.CosNaming.BindingHolder;
import org.omg.CosNaming.BindingIteratorHolder;
import org.omg.CosNaming.BindingListHolder;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class wraps the CORBA Naming Service with methods to make it more
* 'userfriendly'.
*
*/
public class NSList{
private static final Logger logger = Logger.getLogger( NSList.class.getName());
private NamingContextExt context = null;
private ORB orb = null;
// private static final ORB orb = getOrb();
private NSList() {
}
private void initOrb(String[] args) {
Properties props = System.getProperties();
//props.put(ORB_PROPERTY, orbClass);
//props.put(ORB_SINGLETON_PROPERTY, orbSingleton);
//props.put("ORBInitRef.NameService", namingServiceStr);
// props contain all properties defined from environment and command line with -D...
orb = ORB.init(args, null);
// or, orb = ORB.init(args, props)
// if some properties hadnot defined at environment variable, or -D... command line,
// user must add it here, so that ORB.init(...) can find them.
org.omg.CORBA.Object obj = null;
/**
* either these 4 lines or bellow 4 lines are available.
*
try {
obj = orb.string_to_object(System.getProperty("ORBInitRef.NameService", "corbaloc::localhost:34000/NameService"));
context = NamingContextExtHelper.narrow(obj);
} catch (Exception e) {}
*/
try {
obj = orb.resolve_initial_references("NameService");
context = NamingContextExtHelper.narrow(obj);
} catch (org.omg.CORBA.ORBPackage.InvalidName e) {}
// Above two methods are both used to get NamingService, either is available.
// 1. orb.string_to_object(...);
// 2. orb.resolve_initial_references("NamingService"); here
// ORBInitRef.NameService must be defined into properties before.
}
// =============================================================================
// ==================== public wrapped methods ===============================
// =============================================================================
public void bind(String name, org.omg.CORBA.Object obj)
throws NotFound, CannotProceed, InvalidName, AlreadyBound
{
context.bind(context.to_name(name), obj);
}
public void unbind(String name)
throws NotFound, CannotProceed, InvalidName
{
context.unbind(context.to_name(name));
}
public void rebind(String name, org.omg.CORBA.Object obj)
throws NotFound, CannotProceed, InvalidName
{
context.rebind(context.to_name(name), obj);
}
// ===================== public tool functions =========================
// =====================================================================
/**
* Get an IOR for object identified by name.
*
* @param name of object.
* @return IOR of the object name.
*/
public String resolve(String name)
{
int loopUntil = 5;
org.omg.CORBA.Object obj;
for (int i=0; i<loopUntil;i++){
try {
obj = context.resolve_str(name);
return orb.object_to_string(obj);
} catch (NotFound e) {
if(i>=loopUntil){
return null;
}
// Sleep a while
synchronized (this) {
try{
wait(1000);
} catch (Exception ex) {
// ignore
}
}
} catch (CannotProceed e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidName e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null; // Will never happen, makes the compiler to shut up.
}
/**
* List all names within a naming service.
*
* @return List of names
*/
private List<String> list()
{
List<String> rets = new ArrayList<String>();
try
{
BindingListHolder blsoh =
new BindingListHolder(new Binding[0]);
BindingIteratorHolder bioh =
new BindingIteratorHolder();
context.list( 0, blsoh, bioh );
BindingHolder bh = new BindingHolder();
if( bioh.value == null )
return rets;
while( bioh.value.next_one( bh ))
{
String stringName = context.to_string(bh.value.binding_name);
rets.add(stringName);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return rets;
}
/**
* List all initial services.
*
*/
public void listInitialServices()
{
String services[] = orb.list_initial_services();
if (services != null){
System.out.println("There are total "+services.length+" initial service");
for (int i=0;i<services.length;i++)
System.out.println(" initial service["+i+"] = ["+services[i]+"]");
}
}
public static void main(String args[])
{
NSList wrapper = new NSList();
wrapper.initOrb(args);
// wrapper.listInitialServices();
int interval = 10;
try {
interval = Integer.parseInt(System.getProperty("CheckInterval", "10"));
} catch (Exception e) {
interval = 10;
}
System.out.println("Naming Service check interval is ["+interval+"] seconds.");
// get all registered name objects.
String namelist = System.getProperty("namelist", "");
List<String> names = null;
if (namelist == null || namelist.length() <= 0)
names = wrapper.list();
else {
names = new ArrayList<String>();
String namevalues[] = namelist.split(",");
for (int i=0;i<namevalues.length;i++)
names.add(namevalues[i].trim());
}
System.out.println("Naming Service check list is "+names.toString()+".");
String name = null;
String ior = null;
Date dat = new Date();
while (true) {
for (int i=0;i<names.size();i++)
{
name = names.get(i);
try {
ior = wrapper.resolve(name);
dat.setTime(System.currentTimeMillis());
if (ior == null || ior.equals(""))
System.out.println("Date "+dat+" : IOR["+name+"] = [null]");
else
System.out.println("Date "+dat+" : IOR["+name+"] = ["+ior+"]");
} catch (Exception e) {
System.out.println("Date "+dat+": IOR["+name+"] = [null]");
}
}
try {
Thread.sleep(interval*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // while
}
}