1 19 20 package org.apache.cayenne.remote.service; 21 22 import java.io.Serializable ; 23 24 import org.apache.cayenne.CayenneRuntimeException; 25 import org.apache.cayenne.DataChannel; 26 import org.apache.cayenne.event.EventBridge; 27 import org.apache.cayenne.remote.BaseConnection; 28 import org.apache.cayenne.remote.ClientMessage; 29 import org.apache.cayenne.remote.hessian.service.HessianUtil; 30 import org.apache.cayenne.util.Util; 31 32 39 public class LocalConnection extends BaseConnection { 40 41 public static final int NO_SERIALIZATION = 0; 42 public static final int JAVA_SERIALIZATION = 1; 43 public static final int HESSIAN_SERIALIZATION = 2; 44 45 protected DataChannel channel; 46 protected int serializationPolicy; 47 48 51 public LocalConnection(DataChannel handler) { 52 this(handler, NO_SERIALIZATION); 53 } 54 55 59 public LocalConnection(DataChannel handler, int serializationPolicy) { 60 this.channel = handler; 61 62 this.serializationPolicy = serializationPolicy == JAVA_SERIALIZATION 64 || serializationPolicy == HESSIAN_SERIALIZATION 65 ? serializationPolicy 66 : NO_SERIALIZATION; 67 } 68 69 public boolean isSerializingMessages() { 70 return serializationPolicy == JAVA_SERIALIZATION 71 || serializationPolicy == HESSIAN_SERIALIZATION; 72 } 73 74 77 public DataChannel getChannel() { 78 return channel; 79 } 80 81 84 public EventBridge getServerEventBridge() { 85 return null; 86 } 87 88 91 protected void beforeSendMessage(ClientMessage message) { 92 } 94 95 98 protected Object doSendMessage(ClientMessage message) throws CayenneRuntimeException { 99 100 ClientMessage processedMessage; 101 102 try { 103 switch (serializationPolicy) { 104 case HESSIAN_SERIALIZATION: 105 processedMessage = (ClientMessage) HessianUtil 106 .cloneViaClientServerSerialization(message, channel 107 .getEntityResolver()); 108 break; 109 110 case JAVA_SERIALIZATION: 111 processedMessage = (ClientMessage) Util 112 .cloneViaSerialization(message); 113 break; 114 115 default: 116 processedMessage = message; 117 } 118 119 } 120 catch (Exception ex) { 121 throw new CayenneRuntimeException("Error serializing message", ex); 122 } 123 124 Serializable result = (Serializable ) DispatchHelper.dispatch( 125 channel, 126 processedMessage); 127 128 try { 129 switch (serializationPolicy) { 130 case HESSIAN_SERIALIZATION: 131 return HessianUtil.cloneViaServerClientSerialization(result, channel 132 .getEntityResolver()); 133 case JAVA_SERIALIZATION: 134 return Util.cloneViaSerialization(result); 135 default: 136 return result; 137 } 138 } 139 catch (Exception ex) { 140 throw new CayenneRuntimeException("Error deserializing result", ex); 141 } 142 143 } 144 } 145 | Popular Tags |