001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.factory;
018:
019: import java.util.Hashtable;
020: import javax.naming.Name;
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023: import javax.naming.NamingException;
024: import javax.naming.Reference;
025: import javax.naming.RefAddr;
026: import javax.naming.spi.ObjectFactory;
027: import org.apache.naming.EjbRef;
028:
029: /**
030: * Object factory for EJBs.
031: *
032: * @author Remy Maucherat
033: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:54 $
034: */
035:
036: public class EjbFactory implements ObjectFactory {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: // -------------------------------------------------------------- Constants
041:
042: // ----------------------------------------------------- Instance Variables
043:
044: // --------------------------------------------------------- Public Methods
045:
046: // -------------------------------------------------- ObjectFactory Methods
047:
048: /**
049: * Crete a new EJB instance.
050: *
051: * @param obj The reference object describing the DataSource
052: */
053: public Object getObjectInstance(Object obj, Name name,
054: Context nameCtx, Hashtable environment) throws Exception {
055:
056: if (obj instanceof EjbRef) {
057: Reference ref = (Reference) obj;
058:
059: // If ejb-link has been specified, resolving the link using JNDI
060: RefAddr linkRefAddr = ref.get(EjbRef.LINK);
061: if (linkRefAddr != null) {
062: // Retrieving the EJB link
063: String ejbLink = linkRefAddr.getContent().toString();
064: Object beanObj = (new InitialContext()).lookup(ejbLink);
065: // Load home interface and checking if bean correctly
066: // implements specified home interface
067: /*
068: String homeClassName = ref.getClassName();
069: try {
070: Class home = Class.forName(homeClassName);
071: if (home.isInstance(beanObj)) {
072: System.out.println("Bean of type "
073: + beanObj.getClass().getName()
074: + " implements home interface "
075: + home.getName());
076: } else {
077: System.out.println("Bean of type "
078: + beanObj.getClass().getName()
079: + " doesn't implement home interface "
080: + home.getName());
081: throw new NamingException
082: ("Bean of type " + beanObj.getClass().getName()
083: + " doesn't implement home interface "
084: + home.getName());
085: }
086: } catch (ClassNotFoundException e) {
087: System.out.println("Couldn't load home interface "
088: + homeClassName);
089: }
090: */
091: return beanObj;
092: }
093:
094: ObjectFactory factory = null;
095: RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
096: if (factoryRefAddr != null) {
097: // Using the specified factory
098: String factoryClassName = factoryRefAddr.getContent()
099: .toString();
100: // Loading factory
101: ClassLoader tcl = Thread.currentThread()
102: .getContextClassLoader();
103: Class factoryClass = null;
104: if (tcl != null) {
105: try {
106: factoryClass = tcl.loadClass(factoryClassName);
107: } catch (ClassNotFoundException e) {
108: }
109: } else {
110: try {
111: factoryClass = Class.forName(factoryClassName);
112: } catch (ClassNotFoundException e) {
113: }
114: }
115: if (factoryClass != null) {
116: try {
117: factory = (ObjectFactory) factoryClass
118: .newInstance();
119: } catch (Throwable t) {
120: }
121: }
122: } else {
123: String javaxEjbFactoryClassName = System.getProperty(
124: "javax.ejb.Factory",
125: Constants.OPENEJB_EJB_FACTORY);
126: try {
127: factory = (ObjectFactory) Class.forName(
128: javaxEjbFactoryClassName).newInstance();
129: } catch (Throwable t) {
130: }
131: }
132:
133: if (factory != null) {
134: return factory.getObjectInstance(obj, name, nameCtx,
135: environment);
136: } else {
137: throw new NamingException(
138: "Cannot create resource instance");
139: }
140:
141: }
142:
143: return null;
144:
145: }
146:
147: }
|