01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.client;
17:
18: import java.io.Externalizable;
19: import java.io.IOException;
20: import java.io.ObjectInput;
21: import java.io.ObjectOutput;
22: import java.io.ObjectStreamException;
23:
24: public class EJBObjectProxyHandle implements Externalizable {
25:
26: public static ThreadLocal<Resolver> resolver = new DefaultedThreadLocal<Resolver>(
27: new ClientSideResovler());
28:
29: EJBObjectHandler handler;
30:
31: public EJBObjectProxyHandle() {
32: }
33:
34: public EJBObjectProxyHandle(EJBObjectHandler handler) {
35: this .handler = handler;
36: }
37:
38: public void writeExternal(ObjectOutput out) throws IOException {
39: // write out the version of the serialized data for future use
40: out.writeByte(1);
41:
42: handler.client.writeExternal(out);
43:
44: handler.ejb.writeExternal(out);
45:
46: handler.server.writeExternal(out);
47: out.writeObject(handler.primaryKey);
48: }
49:
50: public void readExternal(ObjectInput in) throws IOException,
51: ClassNotFoundException {
52: byte version = in.readByte(); // future use
53:
54: ClientMetaData client = new ClientMetaData();
55: EJBMetaDataImpl ejb = new EJBMetaDataImpl();
56: ServerMetaData server = new ServerMetaData();
57:
58: client.readExternal(in);
59:
60: ejb.readExternal(in);
61:
62: server.readExternal(in);
63:
64: Object primaryKey = in.readObject();
65:
66: handler = EJBObjectHandler.createEJBObjectHandler(ejb, server,
67: client, primaryKey);
68:
69: }
70:
71: private Object readResolve() throws ObjectStreamException {
72: return resolver.get().resolve(handler);
73: }
74:
75: public static interface Resolver {
76: Object resolve(EJBObjectHandler handler);
77: }
78:
79: public static class ClientSideResovler implements Resolver {
80: public Object resolve(EJBObjectHandler handler) {
81: return handler.createEJBObjectProxy();
82: }
83: }
84: }
|