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