001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.config.types;
030:
031: import com.caucho.config.program.ConfigProgram;
032: import com.caucho.config.j2ee.*;
033: import com.caucho.config.program.FieldValueProgram;
034: import com.caucho.config.program.MethodValueProgram;
035: import com.caucho.ejb.*;
036: import com.caucho.naming.Jndi;
037: import com.caucho.util.L10N;
038: import com.caucho.vfs.Path;
039:
040: import javax.annotation.PostConstruct;
041: import java.lang.reflect.*;
042:
043: /**
044: * Configuration for the init-param pattern.
045: */
046: public class ResourceEnvRef extends BaseRef {
047: private static L10N L = new L10N(ResourceEnvRef.class);
048:
049: private String _name;
050: private Class _type;
051:
052: public ResourceEnvRef() {
053: }
054:
055: public ResourceEnvRef(Path modulePath, String sourceEjbName) {
056: super (modulePath, sourceEjbName);
057: }
058:
059: /**
060: * Sets the description.
061: */
062: public void setDescription(String description) {
063: }
064:
065: /**
066: * Sets the name
067: */
068: public void setResourceEnvRefName(String name) {
069: _name = name;
070: }
071:
072: /**
073: * Sets the type
074: */
075: public void setResourceEnvRefType(Class type) {
076: _type = type;
077: }
078:
079: /**
080: * Initialize the resource.
081: */
082: // XXX TCK: @PostConstruct, needs QA, called from EjbConfig.
083: public void initBinding(AbstractServer ejbServer) throws Exception {
084: String jndiName = null;
085:
086: // XXX TCK: merge this code with the one in InjectIntrospector.
087: if (javax.ejb.SessionContext.class.equals(_type)) {
088: jndiName = "java:comp/env/sessionContext";
089: }
090:
091: Object targetValue = null;
092:
093: if (jndiName != null) {
094: targetValue = Jndi.lookup(jndiName);
095: }
096:
097: // XXX TCK, needs QA
098: if (_injectionTarget != null && targetValue != null) {
099: String className = _injectionTarget
100: .getInjectionTargetClass();
101: String fieldName = _injectionTarget
102: .getInjectionTargetName();
103:
104: if (ejbServer != null) {
105: Class cl = getJavaClass(className);
106:
107: AccessibleObject field = getFieldOrMethod(cl, fieldName);
108:
109: if (field != null) {
110: ConfigProgram program;
111:
112: if (field instanceof Method)
113: program = new MethodValueProgram(
114: (Method) field, targetValue);
115: else
116: program = new FieldValueProgram((Field) field,
117: targetValue);
118:
119: ejbServer.getInitProgram().addProgram(program);
120: }
121: }
122: }
123: }
124:
125: public String toString() {
126: return "ResourceEnvRef[" + _name + "]";
127: }
128: }
|