GrxCorbaUtil.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 /*
00011  *  GrxConfigPane.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  *  2004/03/19
00018  */
00019 
00020 package com.generalrobotix.ui.util;
00021 
00022 import java.util.HashMap;
00023 
00024 import org.omg.CORBA.*;
00025 import org.omg.CORBA.ORBPackage.InvalidName;
00026 import org.omg.CosNaming.*;
00027 import org.omg.PortableServer.POA;
00028 
00029 import com.generalrobotix.ui.grxui.Activator;
00030 
00034 public class GrxCorbaUtil {
00035 
00036     private static ORB                            orb_               = null;
00037     private static HashMap<String, NamingContext> namingContextList_ = null;
00038 
00045     public static org.omg.CORBA.ORB getORB(String[] argv) {
00046         if (orb_ == null) {
00047             java.util.Properties props = null;
00048             props = System.getProperties();
00049             orb_ = ORB.init(argv, props);
00050         }
00051         return orb_;
00052     }
00053 
00058     public static org.omg.CORBA.ORB getORB() {
00059         return getORB(null);
00060     }
00061 
00066     public static int nsPort() {
00067         return GrxServerManager.NAME_SERVER_PORT_;
00068     }
00069 
00074     public static String nsHost() {
00075         return GrxServerManager.NAME_SERVER_HOST_;
00076     }
00077 
00082     public static NamingContext getNamingContext() {
00083         return getNamingContext(nsHost(), nsPort());
00084     }
00085 
00095     public static NamingContext getNamingContext(String nsHost, int nsPort) {
00096         getORB();
00097         String nameServiceURL = "corbaloc:iiop:" + nsHost + ":" + nsPort + "/NameService";
00098         NamingContext ncxt = getNamingContextList().get(nameServiceURL);
00099         try {
00100             if (ncxt == null) {
00101                 org.omg.CORBA.Object obj = orb_.string_to_object(nameServiceURL);
00102                 ncxt = NamingContextHelper.narrow(obj);
00103                 getNamingContextList().put(nameServiceURL, ncxt);
00104             }
00105             ncxt._non_existent();
00106         } catch (Exception excep) {
00107             ncxt = null;
00108             getNamingContextList().remove(nameServiceURL);
00109         }
00110         return ncxt;
00111     }
00112 
00117     private static HashMap<String, NamingContext> getNamingContextList() {
00118         if (namingContextList_ == null) {
00119             namingContextList_ = new HashMap<String, NamingContext>();
00120         }
00121         return namingContextList_;
00122     }
00123 
00130     public static org.omg.CORBA.Object getReference(String id) {
00131         return getReference(id, nsHost(), nsPort());
00132     }
00133 
00144     public static org.omg.CORBA.Object getReference(String id, String nsHost, int nsPort) {
00145         return getReference(id, "", nsHost, nsPort);
00146     }
00147     
00148     public static org.omg.CORBA.Object getReference(String id, String kind, String nsHost, int nsPort){
00149         NamingContext namingContext = getNamingContext(nsHost, nsPort);
00150         org.omg.CORBA.Object obj = null;
00151         try {
00152             NameComponent[] nc = new NameComponent[1];
00153             nc[0] = new NameComponent(id, kind);
00154             obj = namingContext.resolve(nc);
00155         } catch (Exception excep) {
00156             obj = null;
00157             GrxDebugUtil.printErr("getReference:NG(" + id + "," + nsHost + "," + nsPort + ")");
00158         }
00159         return obj;
00160     }
00161 
00162     public static org.omg.CORBA.Object getReferenceURL(String id, String nsHost, int nsPort) {
00163         org.omg.CORBA.Object obj = null;
00164         try {
00165             obj = getORB().string_to_object("corbaloc:iiop:" + nsHost + ":" + nsPort + "/" + id);
00166         } catch (Exception e) {
00167             obj = null;
00168             GrxDebugUtil.printErr("getReferenceURL:NG" + id + "," + nsHost + "," + nsPort + ")");
00169         }
00170         return obj;
00171     }
00172 
00173     public static String getIOR(org.omg.CORBA.Object obj) {
00174         return getORB().object_to_string(obj);
00175     }
00176 
00177     static String getIOR(String id, String nsHost, int nsPort) {
00178         return getIOR(getReference(id, nsHost, nsPort));
00179     }
00180 
00181     public static POA getRootPOA()
00182             throws InvalidName {
00183         org.omg.CORBA.Object CORBA_obj = getORB().resolve_initial_references("RootPOA");
00184         org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(CORBA_obj);
00185         return rootPOA;
00186     }
00187 
00188     public static org.omg.PortableServer.POAManager getRootPOAManager()
00189             throws InvalidName {
00190         org.omg.PortableServer.POAManager manager = getRootPOA().the_POAManager();
00191         return manager;
00192     }
00193 
00194     public static boolean isConnected(org.omg.CORBA.Object obj) {
00195         try {
00196             obj._non_existent();
00197         } catch (Exception ex) {
00198             return false;
00199         }
00200         return true;
00201     }
00202 
00203     public static boolean isConnected(String id, String nsHost, int nsPort) {
00204         org.omg.CORBA.Object obj = GrxCorbaUtil.getReference(id, nsHost, nsPort);
00205         return isConnected(obj);
00206     }
00207 
00208     public static String[] getObjectNameList() {
00209         return _getObjectNameList(getNamingContext());
00210     }
00211 
00212     public static void removeNameServiceFromList() {
00213         StringBuffer nsHost = new StringBuffer("");
00214         StringBuffer nsPort = new StringBuffer("");
00215         Activator.refNSHostPort(nsHost, nsPort);
00216         namingContextList_.remove("corbaloc:iiop:" + nsHost + ":" + nsPort + "/NameService");
00217     }
00218 
00219     public static void clearOrb() {
00220         orb_ = null;
00221     }
00222 
00223     public static String[] getObjectNameList(String nsHost, int nsPort) {
00224         return _getObjectNameList(getNamingContext(nsHost, nsPort));
00225     }
00226 
00227     public static boolean isAliveNameService() {
00228         boolean ret = false;
00229         try{
00230             ORB orb = getORB();
00231             String nameServiceURL = "corbaloc:iiop:" + nsHost() + ":" + nsPort() + "/NameService";
00232             org.omg.CORBA.Object obj = orb.string_to_object(nameServiceURL);
00233             ret = !obj._non_existent();
00234         }catch (Exception ex){
00235             System.out.println("[GrxCorbaUtil] Name server is not alive!");
00236         }
00237         return ret;
00238     }
00239 
00240     
00241     private static String[] _getObjectNameList(NamingContext cxt) {
00242         int a = 100;
00243         BindingListHolder bl = new BindingListHolder();
00244         BindingIteratorHolder bi = new BindingIteratorHolder();
00245         String[] ret = null;
00246         try {
00247             cxt.list(a, bl, bi);
00248             ret = new String[bl.value.length];
00249             for (int i = 0; i < bl.value.length; i++) {
00250                 ret[i] = bl.value[i].binding_name[0].id;
00251             }
00252         } catch (Exception ex) {
00253             ret = null;
00254             GrxDebugUtil.printErr("getObjectList:NG", ex);
00255         }
00256 
00257         return ret;
00258     }
00259 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16