1 19 20 package org.apache.cayenne.remote.hessian; 21 22 import org.apache.cayenne.CayenneRuntimeException; 23 import org.apache.cayenne.event.EventBridge; 24 import org.apache.cayenne.remote.BaseConnection; 25 import org.apache.cayenne.remote.ClientMessage; 26 import org.apache.cayenne.remote.RemoteService; 27 import org.apache.cayenne.remote.RemoteSession; 28 import org.apache.cayenne.util.Util; 29 30 import com.caucho.hessian.client.HessianRuntimeException; 31 import com.caucho.hessian.io.HessianProtocolException; 32 33 43 public class HessianConnection extends BaseConnection { 44 45 public static final String [] CLIENT_SERIALIZER_FACTORIES = new String [] { 46 ClientSerializerFactory.class.getName(), EnumSerializerProxy.class.getName() 47 }; 48 49 protected String url; 50 protected String userName; 51 protected String password; 52 protected String sharedSessionName; 53 54 protected RemoteSession session; 55 protected RemoteService service; 56 57 61 public HessianConnection(String url) { 62 this(url, null, null, null); 63 } 64 65 70 public HessianConnection(String url, String userName, String password, 71 String sharedSessionName) { 72 if (url == null) { 73 throw new IllegalArgumentException ("URL of Cayenne service is null."); 74 } 75 76 this.url = url; 77 this.userName = userName; 78 this.password = password; 79 this.sharedSessionName = sharedSessionName; 80 } 81 82 85 public String getUrl() { 86 return url; 87 } 88 89 93 public String getUserName() { 94 return userName; 95 } 96 97 101 public String getPassword() { 102 return password; 103 } 104 105 public String getSharedSessionName() { 106 return sharedSessionName; 107 } 108 109 public EventBridge getServerEventBridge() throws CayenneRuntimeException { 110 if (session == null) { 111 connect(); 112 } 113 114 return session.isServerEventsEnabled() ? session.createServerEventBridge() : null; 115 } 116 117 120 RemoteSession getSession() { 121 return session; 122 } 123 124 127 protected void beforeSendMessage(ClientMessage message) 128 throws CayenneRuntimeException { 129 if (session == null) { 131 connect(); 132 } 133 } 134 135 138 protected Object doSendMessage(ClientMessage message) throws CayenneRuntimeException { 139 try { 140 return service.processMessage(message); 141 } 142 catch (Throwable th) { 143 th = unwindThrowable(th); 144 String errorMessage = buildExceptionMessage("Remote error", th); 145 throw new CayenneRuntimeException(errorMessage, th); 146 } 147 } 148 149 152 protected synchronized void connect() throws CayenneRuntimeException { 153 if (session != null) { 154 return; 155 } 156 157 long t0 = 0; 158 if (logger.isInfoEnabled()) { 159 t0 = System.currentTimeMillis(); 160 StringBuffer log = new StringBuffer ("Connecting to ["); 161 if (userName != null) { 162 log.append(userName); 163 164 if (password != null) { 165 log.append(":*******"); 166 } 167 168 log.append("@"); 169 } 170 171 log.append(url); 172 log.append("]"); 173 174 if (sharedSessionName != null) { 175 log.append(" - shared session '").append(sharedSessionName).append("'"); 176 } 177 else { 178 log.append(" - dedicated session."); 179 } 180 181 logger.info(log.toString()); 182 } 183 184 HessianProxyFactory factory = new HessianProxyFactory(this); 186 factory.setSerializerFactory(HessianConfig.createFactory( 187 CLIENT_SERIALIZER_FACTORIES, 188 null)); 189 factory.setUser(userName); 190 factory.setPassword(password); 191 try { 192 this.service = (RemoteService) factory.create(RemoteService.class, url); 193 } 194 catch (Throwable th) { 195 th = unwindThrowable(th); 196 String message = buildExceptionMessage("URL error", th); 197 throw new CayenneRuntimeException(message, th); 198 } 199 200 try { 202 session = (sharedSessionName != null) ? service 203 .establishSharedSession(sharedSessionName) : service 204 .establishSession(); 205 206 if (logger.isInfoEnabled()) { 207 long time = System.currentTimeMillis() - t0; 208 logger.info("=== Connected, session: " 209 + session 210 + " - took " 211 + time 212 + " ms."); 213 } 214 } 215 catch (Throwable th) { 216 th = unwindThrowable(th); 217 th.printStackTrace(); 218 String message = buildExceptionMessage( 219 "Error establishing remote session", 220 th); 221 throw new CayenneRuntimeException(message, th); 222 } 223 224 } 226 227 String buildExceptionMessage(String message, Throwable th) { 228 229 StringBuffer buffer = new StringBuffer (message); 230 buffer.append(". URL - ").append(url); 231 232 String thMessage = th.getMessage(); 233 if (!Util.isEmptyString(thMessage)) { 234 buffer.append("; CAUSE - ").append(thMessage); 235 } 236 237 return buffer.toString(); 238 } 239 240 244 Throwable unwindThrowable(Throwable th) { 245 if (th instanceof HessianProtocolException) { 246 Throwable cause = ((HessianProtocolException) th).getRootCause(); 247 248 if (cause != null) { 249 return unwindThrowable(cause); 250 } 251 } 252 else if (th instanceof HessianRuntimeException) { 253 Throwable cause = ((HessianRuntimeException) th).getRootCause(); 254 255 if (cause != null) { 256 return unwindThrowable(cause); 257 } 258 } 259 260 return Util.unwindException(th); 261 } 262 } 263 | Popular Tags |