1 29 30 package com.caucho.iiop; 31 32 import com.caucho.log.Log; 33 34 import org.omg.CORBA.NO_IMPLEMENT ; 35 36 import java.lang.reflect.Method ; 37 import java.util.HashMap ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 public class IiopSkeleton extends DummyObjectImpl { 42 private static final Logger log = Log.open(IiopSkeleton.class); 43 44 private static HashMap <String ,String > _knownClasses; 45 46 private ClassLoader _loader; 47 private Class _remoteClass; 48 49 private Object _obj; 50 51 IiopSkeleton(Object obj, Class apiClass, ClassLoader loader, 52 String host, int port, String oid) 53 { 54 super(new IOR(apiClass, host, port, oid)); 55 56 if (obj == null) 57 throw new NullPointerException (); 58 59 _obj = obj; 60 _remoteClass = apiClass; 61 _loader = loader; 62 } 63 64 Object getObject() 65 { 66 return _obj; 67 } 68 69 Class getRemoteClass() 70 { 71 return _remoteClass; 72 } 73 74 void service(Object obj, IiopReader reader, IiopWriter writer) 75 throws Throwable 76 { 77 String op = reader.getOperation().toString(); 78 79 Method method = null; 80 SkeletonMethod skelMethod = null; 81 82 if (log.isLoggable(Level.FINE)) 83 log.fine("IIOP-call: " + _remoteClass.getName() + "." + op); 84 85 if ((method = getMethod(op)) != null) { 86 boolean isJava = !_remoteClass.getName().startsWith("com.caucho.iiop"); 87 88 skelMethod = new SkeletonMethod(this, method, isJava); 89 Thread thread = Thread.currentThread(); 90 ClassLoader oldLoader = thread.getContextClassLoader(); 91 try { 92 thread.setContextClassLoader(_loader); 93 94 skelMethod.service(obj, reader, writer); 95 } finally { 96 thread.setContextClassLoader(oldLoader); 97 } 98 } 99 else if (serviceSystemMethod(obj, op, reader, writer)) { 100 } 101 else { 102 throw new NO_IMPLEMENT ("no such method: " + op + " for " + _remoteClass.getName()); 103 } 104 } 105 106 Method getMethod(String name) 107 { 108 Method []methods = _remoteClass.getMethods(); 109 for (int i = 0; i < methods.length; i++) 110 if (methods[i].getName().equals(name)) 111 return methods[i]; 112 113 return null; 114 } 115 116 boolean serviceSystemMethod(Object obj, String op, IiopReader reader, IiopWriter writer) 117 throws Exception 118 { 119 if (op.equals("_is_a")) { 120 String name = reader.read_string(); 121 122 String className = _knownClasses.get(name); 123 124 if (className != null) { 125 } 126 else if (name.startsWith("RMI:")) { 127 className = name.substring(4); 128 int p = className.indexOf(':'); 129 if (p > 0) 130 className = className.substring(0, p); 131 } 132 else 133 className = name; 134 135 Class cl = obj.getClass(); 136 137 boolean value = isA(cl, className); 138 139 if (log.isLoggable(Level.FINE)) 140 log.fine("IIOP _is_a: " + obj.getClass() + " " + className + " " + value); 141 142 writer.startReplyOk(reader.getRequestId()); 143 writer.write_boolean(value); 144 145 return true; 146 } 147 else 148 return false; 149 } 150 151 private boolean isA(Class cl, String className) 152 { 153 for (; cl != null; cl = cl.getSuperclass()) { 154 if (cl.getName().equals(className)) 155 return true; 156 157 Class []ifs = cl.getInterfaces(); 158 for (int i = 0; i < ifs.length; i++) { 159 if (isA(ifs[i], className)) 160 return true; 161 } 162 } 163 164 return false; 165 } 166 167 public String toString() 168 { 169 return "IiopSkeleton[" + _remoteClass.getName() + "]"; 170 } 171 172 static { 173 _knownClasses = new HashMap <String ,String >(); 174 _knownClasses.put("IDL:omg.org/CosNaming/NamingContext:1.0", 175 "org.omg.CosNaming.NamingContext"); 176 } 177 } 178 | Popular Tags |