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: */
17:
18: package org.apache.naming.factory;
19:
20: import org.apache.naming.EjbRef;
21:
22: import javax.naming.Context;
23: import javax.naming.InitialContext;
24: import javax.naming.Name;
25: import javax.naming.Reference;
26: import javax.naming.RefAddr;
27: import javax.naming.spi.ObjectFactory;
28: import java.util.Hashtable;
29: import java.util.Properties;
30:
31: /**
32: * Object factory for EJBs.
33: *
34: * @author Jacek Laskowski
35: * @author Remy Maucherat
36: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
37: */
38: public class OpenEjbFactory implements ObjectFactory {
39:
40: // -------------------------------------------------------------- Constants
41:
42: protected static final String DEFAULT_OPENEJB_FACTORY = "org.openejb.client.LocalInitialContextFactory";
43:
44: // -------------------------------------------------- ObjectFactory Methods
45:
46: /**
47: * Crete a new EJB instance using OpenEJB.
48: *
49: * @param obj The reference object describing the DataSource
50: */
51: public Object getObjectInstance(Object obj, Name name,
52: Context nameCtx, Hashtable environment) throws Exception {
53:
54: Object beanObj = null;
55:
56: if (obj instanceof EjbRef) {
57:
58: Reference ref = (Reference) obj;
59:
60: String factory = DEFAULT_OPENEJB_FACTORY;
61: RefAddr factoryRefAddr = ref.get("openejb.factory");
62: if (factoryRefAddr != null) {
63: // Retrieving the OpenEJB factory
64: factory = factoryRefAddr.getContent().toString();
65: }
66:
67: Properties env = new Properties();
68: env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
69:
70: RefAddr linkRefAddr = ref.get("openejb.link");
71: if (linkRefAddr != null) {
72: String ejbLink = linkRefAddr.getContent().toString();
73: beanObj = (new InitialContext(env)).lookup(ejbLink);
74: }
75:
76: }
77:
78: return beanObj;
79:
80: }
81:
82: }
|