1 56 package org.objectstyle.cayenne.distribution; 57 58 import org.objectstyle.cayenne.client.CayenneClientException; 59 import org.objectstyle.cayenne.util.Util; 60 61 import com.caucho.hessian.client.HessianProxyFactory; 62 import com.caucho.hessian.client.HessianRuntimeException; 63 import com.caucho.hessian.io.HessianProtocolException; 64 65 73 public class HessianConnector implements CayenneConnector { 74 75 protected String url; 76 protected String userName; 77 protected String password; 78 protected String sessionId; 79 80 protected HessianService service; 81 82 public HessianConnector(String url, String userName, String password) { 83 this.url = url; 84 this.userName = userName; 85 this.password = password; 86 } 87 88 91 public void connect() throws CayenneClientException { 92 try { 94 this.service = (HessianService) new HessianProxyFactory() 95 .create(HessianService.class, url); 96 } 97 catch (Throwable th) { 98 th = unwindThrowable(th); 99 String message = buildExceptionMessage("URL error", th); 100 throw new CayenneClientException(message, th); 101 } 102 103 try { 105 this.sessionId = service.establishSession(userName, password); 106 } 107 catch (Throwable th) { 108 th = unwindThrowable(th); 109 String message = buildExceptionMessage("Error establishing remote session", 110 th); 111 throw new CayenneClientException(message, th); 112 } 113 } 114 115 public Object sendMessage(ClientMessage message) throws CayenneClientException { 116 if (sessionId == null) { 118 throw new CayenneClientException("Not connected, can't call 'invokeRemote'."); 119 } 120 121 try { 122 return service.processMessage(sessionId, message); 123 } 124 catch (Throwable th) { 125 th = unwindThrowable(th); 126 String errorMessage = buildExceptionMessage("Remote error", th); 127 throw new CayenneClientException(errorMessage, th); 128 } 129 } 130 131 protected String buildExceptionMessage(String message, Throwable th) { 132 133 StringBuffer buffer = new StringBuffer (message); 134 buffer.append(". URL - ").append(url); 135 136 String thMessage = th.getMessage(); 137 if (!Util.isEmptyString(thMessage)) { 138 buffer.append("; CAUSE - ").append(thMessage); 139 } 140 141 return buffer.toString(); 142 } 143 144 148 protected Throwable unwindThrowable(Throwable th) { 149 if (th instanceof HessianProtocolException) { 150 Throwable cause = ((HessianProtocolException) th).getRootCause(); 151 152 if (cause != null) { 153 return unwindThrowable(cause); 154 } 155 } 156 else if (th instanceof HessianRuntimeException) { 157 Throwable cause = ((HessianRuntimeException) th).getRootCause(); 158 159 if (cause != null) { 160 return unwindThrowable(cause); 161 } 162 } 163 164 return Util.unwindException(th); 165 } 166 } | Popular Tags |