1 28 29 package com.caucho.ejb.burlap; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.ejb.AbstractServer; 33 import com.caucho.ejb.EJBExceptionWrapper; 34 import com.caucho.ejb.protocol.AbstractHandle; 35 import com.caucho.ejb.protocol.AbstractHomeHandle; 36 import com.caucho.ejb.protocol.HandleEncoder; 37 import com.caucho.util.CharBuffer; 38 39 import javax.ejb.EJBException ; 40 import java.lang.reflect.Field ; 41 42 45 public class BurlapHandleEncoder extends HandleEncoder { 46 private static char []serverEncode64; 47 private static int []serverHash64; 48 private static char []serverEncodeH2; 49 50 private Class _primaryKeyClass; 51 private Field []_fields; 52 private int []fieldTypes; 53 54 public BurlapHandleEncoder(String serverId, Class primaryKeyClass) 55 throws ConfigException 56 { 57 super(serverId); 58 59 _primaryKeyClass = primaryKeyClass; 60 61 if (_primaryKeyClass == null || 62 _primaryKeyClass.isPrimitive() || 63 _primaryKeyClass.getName().startsWith("java.lang.")) 64 return; 65 66 _fields = _primaryKeyClass.getFields(); 67 fieldTypes = new int[_fields.length]; 68 } 69 70 public BurlapHandleEncoder(AbstractServer server, String serverId, 71 Class primaryKeyClass) 72 throws ConfigException 73 { 74 this(serverId, primaryKeyClass); 75 76 setServer(server); 77 } 78 79 82 public AbstractHomeHandle createHomeHandle() 83 { 84 try { 85 return new BurlapHomeHandle(getServer().getEJBHome(), getServerId()); 86 } catch (Throwable e) { 87 return new BurlapHomeHandle(getServerId()); 88 } 89 } 90 91 94 public AbstractHandle createHandle(String objectId) 95 { 96 String url = getURL(objectId); 97 98 try { 99 return new BurlapHandle(url, getServer().getEJBObject(objectId)); 100 } catch (Throwable e) { 101 return new BurlapHandle(url); 102 } 103 } 104 105 108 public String createRandomStringKey() 109 { 110 String originalKey = super.createRandomStringKey(); 111 112 int srun = 0; 113 int tail = calculateHash(originalKey) * 65521; 114 115 CharBuffer cb = CharBuffer.allocate(); 116 cb.append(originalKey); 117 118 int d1 = (64 + srun - tail) & 0x3f; 119 cb.append(serverEncode64[d1]); 120 121 return cb.close(); 122 } 123 124 127 private int calculateHash(String key) 128 { 129 int hash = 137; 130 int len = key.length(); 131 for (int i = 0; i < len; i++) { 132 char ch = key.charAt(i); 133 hash = 65521 * hash + serverHash64[ch & 0xff]; 134 } 135 136 return hash; 137 } 138 139 142 public Object objectIdToKey(Object objectKey) 143 { 144 String objectId = (String ) objectKey; 145 146 if (_primaryKeyClass == null) 147 return objectId; 148 else if (_primaryKeyClass.equals(String .class)) 149 return objectId; 150 else if (_primaryKeyClass.equals(Integer .class) || 151 _primaryKeyClass.equals(int.class)) 152 return new Integer (objectId); 153 else if (_primaryKeyClass.equals(Long .class) || 154 _primaryKeyClass.equals(long.class)) 155 return new Long (objectId); 156 else if (_primaryKeyClass.equals(Float .class) || 157 _primaryKeyClass.equals(float.class)) 158 return new Float (objectId); 159 else if (_primaryKeyClass.equals(Double .class) || 160 _primaryKeyClass.equals(double.class)) 161 return new Double (objectId); 162 else if (Character .class.equals(_primaryKeyClass) || 163 char.class.equals(_primaryKeyClass)) 164 return objectId; 165 else if (_fields != null) { 166 Object obj; 167 168 try { 169 obj = _primaryKeyClass.newInstance(); 170 171 int length = objectId.length(); 172 int j = 0; 173 174 CharBuffer cb = new CharBuffer(); 175 for (int i = 0; i < _fields.length; i++) { 176 cb.clear(); 177 for (; j < length && objectId.charAt(j) != ','; j++) 178 cb.append(objectId.charAt(j)); 179 180 j++; 181 182 String field = cb.toString(); 183 Class fieldClass = _fields[i].getType(); 184 185 if (fieldClass.equals(String .class)) 186 _fields[i].set(obj, field); 187 else if (fieldClass.equals(int.class)) 188 _fields[i].setInt(obj, Integer.parseInt(field)); 189 else if (fieldClass.equals(Integer .class)) 190 _fields[i].set(obj, new Integer (field)); 191 else if (fieldClass.equals(long.class)) 192 _fields[i].setLong(obj, Long.parseLong(field)); 193 else if (fieldClass.equals(Long .class)) 194 _fields[i].set(obj, new Long (field)); 195 else if (fieldClass.equals(float.class)) 196 _fields[i].setFloat(obj, (float) Double.parseDouble(field)); 197 else if (fieldClass.equals(Float .class)) 198 _fields[i].set(obj, new Float (field)); 199 else if (fieldClass.equals(double.class)) 200 _fields[i].setDouble(obj, Double.parseDouble(field)); 201 else if (fieldClass.equals(Double .class)) 202 _fields[i].set(obj, new Double (field)); 203 else if (char.class.equals(fieldClass)) 204 _fields[i].setChar(obj, field.charAt(0)); 205 else if (Character .class.equals(fieldClass)) 206 _fields[i].set(obj, new Character (field.charAt(0))); 207 else 208 throw new RuntimeException (); 209 } 210 211 j++; 212 } catch (Exception e) { 213 throw EJBExceptionWrapper.create(e); 214 } 215 216 return obj; 217 } 218 else 219 throw new EJBException ("bad primary key class"); 220 } 221 222 225 static { 226 serverEncode64 = new char[64]; 227 for (int i = 0; i < 26; i++) { 228 serverEncode64[i] = (char) ('a' + i); 229 serverEncode64[i + 26] = (char) ('A' + i); 230 } 231 for (int i = 0; i < 10; i++) 232 serverEncode64[i + 52] = (char) ('0' + i); 233 serverEncode64[62] = '-'; 234 serverEncode64[63] = '_'; 235 236 serverHash64 = new int[256]; 237 238 for (int i = 0; i < 26; i++) { 239 serverHash64['a' + i] = i; 240 serverHash64['A' + i] = i + 26; 241 } 242 for (int i = 0; i < 10; i++) 243 serverHash64['0' + i] = 52 + i; 244 serverHash64['-'] = 62; 245 serverHash64['_'] = 63; 246 247 int j = 64; 248 for (int i = 0; i < 256; i++) { 249 if (serverHash64[i] == 0) 250 serverHash64[i] = j++; 251 } 252 253 serverEncodeH2 = new char[64]; 254 j = 0; 255 for (int i = 0; i < 64; i++) { 256 serverEncodeH2[j] = serverEncode64[i]; 257 j = (j + 49) % 64; 258 } 259 } 260 } 261 | Popular Tags |