01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.core.ivm.naming;
18:
19: import org.apache.openejb.core.ivm.EjbObjectInputStream;
20: import org.apache.openejb.core.ivm.IntraVmCopyMonitor;
21:
22: import java.io.ByteArrayInputStream;
23: import java.io.ByteArrayOutputStream;
24: import java.io.ObjectInputStream;
25: import java.io.ObjectOutputStream;
26:
27: public class CrossClassLoaderJndiReference extends IntraVmJndiReference {
28: public CrossClassLoaderJndiReference(String jndiName) {
29: super (jndiName);
30: }
31:
32: public Object getObject() throws javax.naming.NamingException {
33: Object o = super .getObject();
34: try {
35: o = copy(o);
36: } catch (Exception e) {
37: throw new NamingException(
38: "Error copying object into local class loader", e);
39: }
40: return o;
41: }
42:
43: private static Object copy(Object source) throws Exception {
44: IntraVmCopyMonitor.preCrossClassLoaderOperation();
45: try {
46: ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
47: ObjectOutputStream out = new ObjectOutputStream(baos);
48: out.writeObject(source);
49: out.close();
50:
51: ByteArrayInputStream bais = new ByteArrayInputStream(baos
52: .toByteArray());
53: ObjectInputStream in = new EjbObjectInputStream(bais);
54: Object copy = in.readObject();
55: return copy;
56: } finally {
57: IntraVmCopyMonitor.postCrossClassLoaderOperation();
58: }
59: }
60: }
|