1 5 package ve.luz.ica.jackass.ref; 6 7 import java.util.Map ; 8 import java.io.UnsupportedEncodingException ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.apache.commons.collections.LRUMap; 13 14 import org.omg.CORBA.Object ; 15 import org.omg.CORBA.Any ; 16 import org.omg.CORBA.ORB ; 17 import org.omg.CORBA.ObjectHelper ; 18 import org.omg.IOP.IOR ; 19 import org.omg.IOP.IORHelper ; 20 import org.omg.IOP.TaggedProfile ; 21 import org.omg.IOP.TAG_INTERNET_IOP ; 22 import org.omg.IOP.TaggedComponent ; 23 import org.omg.IOP.CodecFactory ; 24 import org.omg.IOP.CodecFactoryHelper ; 25 import org.omg.IOP.Encoding ; 26 import org.omg.IOP.ENCODING_CDR_ENCAPS ; 27 import org.omg.IOP.Codec ; 28 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding ; 29 import org.omg.IIOP.ProfileBody_1_1Helper; 30 import org.omg.IIOP.ProfileBody_1_1; 31 32 36 public final class RefUtil 37 { 38 39 public static final int JACKASS_COMPONENT_ID_TAG = 100; 40 41 public static final String JACKASS_COMPONENT_ID_ENCODING = "UTF-8"; 42 43 public static final Encoding DEF_ENCODING = new Encoding (ENCODING_CDR_ENCAPS.value, (byte) 1, (byte) 0); 44 45 private static final Log LOG = LogFactory.getLog(RefUtil.class); 46 private static final String ERR_INIT = "Error initializing"; 47 private static final String ERR_NULL_ARGS = "Illegal null arguments"; 49 private static final String ERR_ENCONDING = "Unsupported enconding: " + JACKASS_COMPONENT_ID_ENCODING; 50 private static final String ERR_CORBA_ENCODING = "Error using CORBA enconding"; 51 private static final String ERR_NOT_1_1_REFERENCE = "It is required an IIOP 1.1 or greater reference"; 52 53 private ORB orb; 54 private CodecFactory codecFactory; 55 private Codec codec; 56 private Map cache; 57 58 62 public RefUtil(ORB nOrb) 63 { 64 try 65 { 66 orb = nOrb; 67 codecFactory = CodecFactoryHelper.narrow(orb.resolve_initial_references("CodecFactory")); 68 codec = codecFactory.create_codec(DEF_ENCODING); 69 cache = new LRUMap(100); 70 } 71 catch (Exception e) 72 { 73 if (LOG.isErrorEnabled()) LOG.error(ERR_INIT, e); 74 throw new IllegalStateException (ERR_INIT + e); 75 } 76 } 77 78 83 public boolean isJackassComponent(Object ref) 84 { 85 CacheEntry entry = updateCache(ref); 86 return entry.isJackassComponent; 87 } 88 89 94 public String getComponentID(Object ref) 95 { 96 CacheEntry entry = updateCache(ref); 97 return entry.componentID; 98 } 99 100 107 public Object createJackassComponentReference(Object objectRef, String componentID) 108 { 109 byte[] cid = null; 110 try 111 { 112 cid = componentID.getBytes(JACKASS_COMPONENT_ID_ENCODING); 113 } 114 catch (UnsupportedEncodingException e) 115 { 116 if (LOG.isErrorEnabled()) LOG.error(ERR_ENCONDING, e); 117 throw new IllegalStateException (ERR_ENCONDING); 118 } 119 120 IORData iorData = rip(objectRef); 121 if (!iorData.isIIOP_1_1()) 122 { 123 throw new IllegalArgumentException (ERR_NOT_1_1_REFERENCE); 124 } 125 126 final int TC_LEN = iorData.profileBody.components.length; 127 TaggedComponent [] tcs = new TaggedComponent [TC_LEN + 1]; 128 System.arraycopy(iorData.profileBody.components, 0, tcs, 0, TC_LEN); 129 tcs[TC_LEN] = new TaggedComponent (JACKASS_COMPONENT_ID_TAG, cid); 130 iorData.profileBody.components = tcs; 131 132 Any anyProfBody = orb.create_any(); 133 ProfileBody_1_1Helper.insert(anyProfBody, iorData.profileBody); 134 135 try 136 { 137 iorData.ior.profiles[iorData.inetProfileIndex].profile_data = codec.encode_value(anyProfBody); 138 } 139 catch (InvalidTypeForEncoding e) 140 { 141 if (LOG.isErrorEnabled()) LOG.error(ERR_CORBA_ENCODING, e); 142 throw new IllegalStateException (ERR_CORBA_ENCODING); 143 } 144 145 Any iorAny = orb.create_any(); 146 IORHelper.insert(iorAny, iorData.ior); 147 return ObjectHelper.extract(iorAny); 148 } 149 150 156 private CacheEntry updateCache(Object ref) 157 { 158 CacheEntry ret = (CacheEntry) cache.get(ref); 159 if (ret != null) 160 { 161 return ret; 162 } 163 164 IORData iorData = rip(ref); 165 if (iorData.isIIOP_1_1()) 166 { 167 TaggedComponent [] components = iorData.profileBody.components; 168 for (int j = 0; j < components.length; j++) 169 { 170 if (components[j].tag == JACKASS_COMPONENT_ID_TAG) 171 { 172 try 173 { 174 ret = new CacheEntry(true, new String (components[j].component_data, 175 JACKASS_COMPONENT_ID_ENCODING)); 176 } 177 catch (java.io.UnsupportedEncodingException e) 178 { 179 if (LOG.isErrorEnabled()) LOG.error(ERR_ENCONDING, e); 180 throw new IllegalStateException (ERR_ENCONDING); 181 } 182 cache.put(ref, ret); 183 return ret; 184 } 185 } 186 } 187 188 ret = new CacheEntry(false, null); 189 cache.put(ref, ret); 190 return ret; 191 } 192 193 198 IORData rip(Object ref) 199 { 200 if (ref == null) 201 { 202 throw new NullPointerException (ERR_NULL_ARGS); 203 } 204 Any any = orb.create_any(); 205 any.insert_Object(ref); 206 207 IOR ior = IORHelper.extract(any); 208 for (int i = 0; i < ior.profiles.length; i++) 209 { 210 TaggedProfile profile = ior.profiles[i]; 211 if (profile.tag == TAG_INTERNET_IOP.value) 212 { 213 try 214 { 215 Any anyProfBody = codec.decode_value(profile.profile_data, ProfileBody_1_1Helper.type()); 216 ProfileBody_1_1 profBody = ProfileBody_1_1Helper.extract(anyProfBody); 217 if (LOG.isDebugEnabled()) LOG.debug("Successfully decoded reference as IIOP 1.1"); 218 return new IORData(ior, profBody, i); 219 } 220 catch (Exception e) 221 { 222 if (LOG.isInfoEnabled()) LOG.info("Could not decode reference as IIOP 1.1 or greater"); 223 } 224 } 225 } 226 227 return new IORData(ior); 228 } 229 230 238 private static final class CacheEntry 239 { 240 private final boolean isJackassComponent; 241 private final String componentID; 242 243 248 CacheEntry(boolean isJackass, String cid) 249 { 250 this.isJackassComponent = isJackass; 251 this.componentID = cid; 252 } 253 } 254 255 } 256 | Popular Tags |