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.tomcat.common;
18:
19: import org.apache.naming.EjbRef;
20: import org.apache.openejb.core.ivm.EjbObjectInputStream;
21: import org.apache.openejb.core.ivm.IntraVmCopyMonitor;
22: import static org.apache.openejb.tomcat.common.NamingUtil.DEPLOYMENT_ID;
23: import static org.apache.openejb.tomcat.common.NamingUtil.EXTERNAL;
24: import static org.apache.openejb.tomcat.common.NamingUtil.LOCAL;
25: import static org.apache.openejb.tomcat.common.NamingUtil.REMOTE;
26: import static org.apache.openejb.tomcat.common.NamingUtil.getProperty;
27: import static org.apache.openejb.tomcat.common.NamingUtil.isPropertyTrue;
28:
29: import javax.naming.Context;
30: import javax.naming.Name;
31: import javax.naming.NamingException;
32: import javax.naming.Reference;
33: import java.io.ByteArrayInputStream;
34: import java.io.ByteArrayOutputStream;
35: import java.io.ObjectInputStream;
36: import java.io.ObjectOutputStream;
37: import java.util.Hashtable;
38:
39: public class EjbFactory extends AbstractObjectFactory {
40: public Object getObjectInstance(Object object, Name name,
41: Context context, Hashtable environment) throws Exception {
42: // ignore non ejb-refs
43: if (!(object instanceof EjbRef)) {
44: return null;
45: }
46:
47: // lookup the value
48: Object value = super .getObjectInstance(object, name, context,
49: environment);
50:
51: // if this is an external reference, copy it into the local class loader
52: if (isPropertyTrue((Reference) object, EXTERNAL)) {
53: value = copy(value);
54: }
55:
56: // done
57: return value;
58: }
59:
60: protected String buildJndiName(Reference reference)
61: throws NamingException {
62: String jndiName;// get and verify deploymentId
63: String deploymentId = getProperty(reference, DEPLOYMENT_ID);
64: if (deploymentId == null)
65: throw new NamingException("ejb-ref deploymentId is null");
66:
67: // get and verify interface type
68: String interfaceType = getProperty(reference, REMOTE);
69: if (interfaceType == null) {
70: interfaceType = getProperty(reference, LOCAL);
71: }
72: if (interfaceType == null)
73: throw new NamingException("ejb-ref interface type is null");
74:
75: // build jndi name using the deploymentId and interface type
76: jndiName = "java:openejb/Deployment/" + deploymentId + "/"
77: + interfaceType;
78: return jndiName;
79: }
80:
81: private static Object copy(Object source) throws Exception {
82: IntraVmCopyMonitor.preCrossClassLoaderOperation();
83: try {
84: ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
85: ObjectOutputStream out = new ObjectOutputStream(baos);
86: out.writeObject(source);
87: out.close();
88:
89: ByteArrayInputStream bais = new ByteArrayInputStream(baos
90: .toByteArray());
91: ObjectInputStream in = new EjbObjectInputStream(bais);
92: Object copy = in.readObject();
93: return copy;
94: } finally {
95: IntraVmCopyMonitor.postCrossClassLoaderOperation();
96: }
97: }
98: }
|