KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > service > LocalConnection


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.remote.service;
21
22 import java.io.Serializable JavaDoc;
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 /**
33  * A ClientConnection that connects to a DataChannel. Used as an emulator of a remote
34  * service. Emulation includes serialization/deserialization of objects.
35  *
36  * @since 1.2
37  * @author Andrus Adamchik
38  */

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     /**
49      * Creates LocalConnector with specified handler and no serialization.
50      */

51     public LocalConnection(DataChannel handler) {
52         this(handler, NO_SERIALIZATION);
53     }
54
55     /**
56      * Creates a LocalConnector with specified handler and serialization policy. Valid
57      * policies are defined as final static int field in this class.
58      */

59     public LocalConnection(DataChannel handler, int serializationPolicy) {
60         this.channel = handler;
61
62         // convert invalid policy to NO_SER..
63
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     /**
75      * Returns wrapped DataChannel.
76      */

77     public DataChannel getChannel() {
78         return channel;
79     }
80
81     /**
82      * Returns null.
83      */

84     public EventBridge getServerEventBridge() {
85         return null;
86     }
87
88     /**
89      * Does nothing.
90      */

91     protected void beforeSendMessage(ClientMessage message) {
92         // noop
93
}
94
95     /**
96      * Dispatches a message to an internal handler.
97      */

98     protected Object JavaDoc 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 JavaDoc ex) {
121             throw new CayenneRuntimeException("Error serializing message", ex);
122         }
123
124         Serializable JavaDoc result = (Serializable JavaDoc) 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 JavaDoc ex) {
140             throw new CayenneRuntimeException("Error deserializing result", ex);
141         }
142
143     }
144 }
145
Popular Tags