01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * Resin Open Source is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License as published by
11: * the Free Software Foundation; either version 2 of the License, or
12: * (at your option) any later version.
13: *
14: * Resin Open Source is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17: * of NON-INFRINGEMENT. See the GNU General Public License for more
18: * details.
19: *
20: * You should have received a copy of the GNU General Public License
21: * along with Resin Open Source; if not, write to the
22: *
23: * Free Software Foundation, Inc.
24: * 59 Temple Place, Suite 330
25: * Boston, MA 02111-1307 USA
26: *
27: * @author Scott Ferguson;
28: */
29:
30: package com.caucho.config.program;
31:
32: import com.caucho.config.program.ValueGenerator;
33: import com.caucho.config.ConfigContext;
34: import com.caucho.config.ConfigException;
35: import com.caucho.config.program.ConfigProgram;
36: import com.caucho.util.L10N;
37: import com.caucho.webbeans.context.DependentScope;
38:
39: import javax.rmi.PortableRemoteObject;
40: import java.lang.reflect.Field;
41: import java.util.logging.Logger;
42:
43: public class FieldGeneratorProgram extends ConfigProgram {
44: private static final Logger log = Logger
45: .getLogger(FieldGeneratorProgram.class.getName());
46: private static final L10N L = new L10N(FieldGeneratorProgram.class);
47:
48: private Field _field;
49: private ValueGenerator _gen;
50:
51: public FieldGeneratorProgram(Field field, ValueGenerator gen) {
52: _field = field;
53: _field.setAccessible(true);
54:
55: _gen = gen;
56: }
57:
58: String getName() {
59: return _field.getName();
60: }
61:
62: Class getType() {
63: return _field.getType();
64: }
65:
66: Class getDeclaringClass() {
67: return _field.getDeclaringClass();
68: }
69:
70: @Override
71: public void inject(Object bean, ConfigContext env)
72: throws ConfigException {
73: try {
74: Object value = _gen.create();
75:
76: // XXX TCK: ejb30/bb/session/stateless/sessioncontext/descriptor/getBusinessObjectLocal1, needs QA
77: if (value != null
78: && !_field.getType().isAssignableFrom(
79: value.getClass())
80: && !_field.getType().isPrimitive()) {
81: value = PortableRemoteObject.narrow(value, _field
82: .getType());
83:
84: }
85:
86: _field.set(bean, value);
87: } catch (ConfigException e) {
88: throw e;
89: } catch (Exception e) {
90: throw ConfigException.create(location(), e);
91: }
92: }
93:
94: private String location() {
95: return _field.getDeclaringClass().getName() + "."
96: + _field.getName() + ": ";
97: }
98: }
|