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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson;
028: */
029:
030: package com.caucho.config.program;
031:
032: import com.caucho.config.program.ValueGenerator;
033: import com.caucho.config.ConfigContext;
034: import com.caucho.config.ConfigException;
035: import com.caucho.config.program.ConfigProgram;
036: import com.caucho.util.L10N;
037:
038: import javax.rmi.PortableRemoteObject;
039: import java.lang.reflect.*;
040: import java.util.logging.Logger;
041:
042: public class MethodGeneratorProgram extends ConfigProgram {
043: private static final Logger log = Logger
044: .getLogger(MethodGeneratorProgram.class.getName());
045: private static final L10N L = new L10N(MethodGeneratorProgram.class);
046:
047: private Method _method;
048: private ValueGenerator _gen;
049:
050: public MethodGeneratorProgram(Method method, ValueGenerator gen) {
051: _method = method;
052: _method.setAccessible(true);
053:
054: _gen = gen;
055: }
056:
057: String getName() {
058: return _method.getName();
059: }
060:
061: Class getType() {
062: return _method.getParameterTypes()[0];
063: }
064:
065: Class getDeclaringClass() {
066: return _method.getDeclaringClass();
067: }
068:
069: @Override
070: public void inject(Object bean, ConfigContext env)
071: throws ConfigException {
072: try {
073: Class type = getType();
074:
075: Object value = _gen.create();
076:
077: // XXX TCK: ejb30/bb/session/stateless/sessioncontext/descriptor/getBusinessObjectLocal1, needs QA
078: if (!type.isAssignableFrom(value.getClass())) {
079: value = PortableRemoteObject.narrow(value, getType());
080: }
081:
082: if (!type.isAssignableFrom(value.getClass())) {
083:
084: throw new ConfigException(
085: location()
086: + L
087: .l(
088: "Resource type {0} is not assignable to method '{1}' of type {2}.",
089: value.getClass()
090: .getName(),
091: _method.getName(), type
092: .getName()));
093: }
094:
095: _method.invoke(bean, value);
096: } catch (InvocationTargetException e) {
097: throw new ConfigException(location()
098: + e.getCause().getMessage(), e.getCause());
099: } catch (Exception e) {
100: throw new ConfigException(location() + e.getMessage(), e);
101: }
102: }
103:
104: private String location() {
105: return _method.getDeclaringClass().getName() + "."
106: + _method.getName() + ": ";
107: }
108: }
|