1 22 package org.jboss.iiop; 23 24 import java.util.Hashtable ; 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.Name ; 28 import javax.naming.NamingException ; 29 import javax.naming.Reference ; 30 import javax.naming.spi.ObjectFactory ; 31 32 import org.apache.avalon.framework.configuration.Configuration; 33 import org.jboss.logging.Logger; 34 import org.jboss.system.ServiceMBeanSupport; 35 import org.omg.CORBA.ORB ; 36 import org.omg.CORBA.Policy ; 37 import org.omg.CosNaming.Binding ; 38 import org.omg.CosNaming.BindingHolder ; 39 import org.omg.CosNaming.BindingIteratorHolder ; 40 import org.omg.CosNaming.BindingListHolder ; 41 import org.omg.CosNaming.BindingType ; 42 import org.omg.CosNaming.NameComponent ; 43 import org.omg.CosNaming.NamingContext ; 44 import org.omg.CosNaming.NamingContextExt ; 45 import org.omg.CosNaming.NamingContextHelper ; 46 import org.omg.CosNaming.NamingContextExtHelper ; 47 import org.omg.PortableServer.IdAssignmentPolicyValue ; 48 import org.omg.PortableServer.LifespanPolicyValue ; 49 import org.omg.PortableServer.POA ; 50 51 58 public class CorbaNamingService 59 extends ServiceMBeanSupport 60 implements CorbaNamingServiceMBean, ObjectFactory 61 { 62 public static String NAMING_NAME = "JBossCorbaNaming"; 64 65 67 68 private POA namingPOA; 69 70 72 73 private static NamingContextExt namingService; 74 75 79 public String list() 80 { 81 StringBuffer buf = new StringBuffer (); 82 rlist(namingService, new NameComponent [0], buf); 83 return buf.toString(); 84 } 85 86 88 protected void startService() 89 throws Exception 90 { 91 Context ctx; 92 ORB orb; 93 POA rootPOA; 94 95 try { 96 ctx = new InitialContext (); 97 } 98 catch (NamingException e) { 99 throw new RuntimeException ("Cannot get intial JNDI context: " + e); 100 } 101 try { 102 orb = (ORB )ctx.lookup("java:/" + CorbaORBService.ORB_NAME); 103 } 104 catch (NamingException e) { 105 throw new RuntimeException ("Cannot lookup java:/" 106 + CorbaORBService.ORB_NAME + ": " + e); 107 } 108 try { 109 rootPOA = (POA )ctx.lookup("java:/" + CorbaORBService.POA_NAME); 110 } 111 catch (NamingException e) { 112 throw new RuntimeException ("Cannot lookup java:/" 113 + CorbaORBService.POA_NAME + ": " + e); 114 } 115 116 Policy [] policies = new Policy [2]; 118 policies[0] = 119 rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID); 120 policies[1] = 121 rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT); 122 namingPOA = rootPOA.create_POA("Naming", null, policies); 123 namingPOA.the_POAManager().activate(); 124 125 org.jacorb.naming.NamingContextImpl.init(orb, rootPOA); 127 NamingContextImpl ns = new NamingContextImpl(namingPOA); 128 Configuration config = ((org.jacorb.orb.ORB)orb).getConfiguration(); 129 ns.configure(config); byte[] rootContextId = "root".getBytes(); 131 namingPOA.activate_object_with_id(rootContextId, ns); 132 namingService = NamingContextExtHelper.narrow( 133 namingPOA.create_reference_with_id(rootContextId, 134 "IDL:omg.org/CosNaming/NamingContextExt:1.0")); 135 bind(NAMING_NAME, "org.omg.CosNaming.NamingContextExt"); 136 getLog().info("CORBA Naming Started"); 137 getLog().debug("Naming: ["+orb.object_to_string(namingService)+"]"); 138 } 139 140 protected void stopService() 141 { 142 try { 144 unbind(NAMING_NAME); 145 } catch (Exception e) { 146 log.error("Exception while stopping CORBA naming service", e); 147 } 148 149 try { 151 namingPOA.destroy(false, false); 152 } catch (Exception e) { 153 log.error("Exception while stopping CORBA naming service", e); 154 } 155 } 156 157 159 public Object getObjectInstance(Object obj, Name name, 160 Context nameCtx, Hashtable environment) 161 throws Exception 162 { 163 String s = name.toString(); 164 if (getLog().isTraceEnabled()) 165 getLog().trace("getObjectInstance: obj.getClass().getName=\"" + 166 obj.getClass().getName() + 167 "\n name=" + s); 168 if (NAMING_NAME.equals(s)) 169 return namingService; 170 else 171 return null; 172 } 173 174 176 private void bind(String name, String className) 177 throws Exception 178 { 179 Reference ref = new Reference (className, getClass().getName(), null); 180 new InitialContext ().bind("java:/"+name, ref); 181 } 182 183 private void unbind(String name) 184 throws Exception 185 { 186 new InitialContext ().unbind("java:/"+name); 187 } 188 189 private static void rlist(NamingContext ctx, NameComponent [] base, 190 StringBuffer buf) 191 { 192 BindingListHolder listHolder = new BindingListHolder (new Binding [0]); 193 BindingIteratorHolder iterHolder = new BindingIteratorHolder (); 194 ctx.list(0, listHolder, iterHolder); 195 BindingHolder bindingHolder = new BindingHolder (); 196 197 if (iterHolder.value == null ) 198 return; 199 200 NameComponent [] name = new NameComponent [base.length + 1]; 201 for (int i = 0; i < base.length; i++) 202 name[i] = base[i]; 203 204 while (iterHolder.value.next_one(bindingHolder)) 205 { 206 Binding binding = bindingHolder.value; 207 name[name.length - 1] = binding.binding_name[0]; 208 try 209 { 210 String stringName = namingService.to_string(name); 211 buf.append(stringName); 212 } 213 catch(Exception e) 214 { 215 buf.append(e.getMessage()); 216 } 217 218 if (binding.binding_type.value() == BindingType._ncontext) 219 { 220 buf.append('/'); 224 buf.append('\n'); 225 226 try 228 { 229 NamingContext subCtx = 230 NamingContextHelper.narrow(ctx.resolve(binding.binding_name)); 231 rlist(subCtx, name, buf); 232 } 233 catch(Exception e) 234 { 235 buf.append(e.getMessage()); 236 } 237 } 238 else 239 { 240 buf.append('\n'); 241 } 242 } 243 } 244 245 247 256 static class NamingContextImpl 257 extends org.jacorb.naming.NamingContextImpl 258 { 259 private POA poa; 260 private int childCount = 0; 261 private static final Logger logger = 262 Logger.getLogger(NamingContextImpl.class); 263 264 NamingContextImpl(POA poa) 265 { 266 this.poa = poa; 267 } 268 269 public NamingContext new_context() 270 { 271 try { 272 NamingContextImpl newContextImpl = new NamingContextImpl(poa); 273 byte[] oid = (new String (poa.servant_to_id(this)) + 274 "/ctx" + (++childCount)).getBytes(); 275 poa.activate_object_with_id(oid, newContextImpl); 276 return NamingContextExtHelper.narrow( 277 poa.create_reference_with_id(oid, 278 "IDL:omg.org/CosNaming/NamingContextExt:1.0")); 279 } 280 catch (Exception e) { 281 logger.error("Cannot create CORBA naming context", e); 282 return null; 283 } 284 } 285 } 286 287 } 288 | Popular Tags |