1 28 29 package com.caucho.ejb.protocol; 30 31 import com.caucho.ejb.AbstractServer; 32 import com.caucho.log.Log; 33 import com.caucho.util.Base64; 34 import com.caucho.util.CharBuffer; 35 import com.caucho.util.L10N; 36 import com.caucho.util.RandomUtil; 37 38 import java.util.logging.Logger ; 39 40 43 public class HandleEncoder { 44 private static final L10N L = new L10N(HandleEncoder.class); 45 private static final Logger log = Log.open(HandleEncoder.class); 46 47 private final String _serverId; 48 private AbstractServer _server; 49 50 public HandleEncoder(String serverId) 51 { 52 _serverId = serverId; 53 } 54 55 public HandleEncoder(AbstractServer server, String serverId) 56 { 57 this(serverId); 58 59 setServer(server); 60 } 61 62 public String getServerId() 63 { 64 return _serverId; 65 } 66 67 protected void setServer(AbstractServer server) 68 { 69 _server = server; 70 } 71 72 protected AbstractServer getServer() 73 { 74 return _server; 75 } 76 77 80 public AbstractHomeHandle createHomeHandle() 81 { 82 if (_server != null) { 83 try { 84 return new HomeHandleImpl(_server.getEJBHome(), _serverId); 85 } catch (Throwable e) { 86 } 87 } 88 89 return new HomeHandleImpl(_serverId); 90 } 91 92 95 public String getURL() 96 { 97 return _serverId; 98 } 99 100 103 public String getURL(String primaryKey) 104 { 105 return _serverId + "?id=" + primaryKey; 106 } 107 108 111 public AbstractHandle createHandle(String objectId) 112 { 113 return new HandleImpl(_serverId, objectId); 114 } 115 116 119 public String createRandomStringKey() 120 { 121 long id = RandomUtil.getRandomLong(); 122 123 CharBuffer cb = new CharBuffer(); 124 Base64.encode(cb, id); 125 for (int i = 1; i < cb.length(); i++) { 126 if (cb.charAt(i) == '/') 127 cb.setCharAt(i, '-'); 128 } 129 130 return cb.toString(); 131 } 132 133 136 protected String encodePrimaryKey(Object primaryKey) 137 { 138 if (_server != null) 139 return _server.encodeId(primaryKey); 140 else 141 return String.valueOf(primaryKey); 142 } 143 144 public Object objectIdToKey(Object id) 145 { 146 return id; 147 } 148 } 149 | Popular Tags |