01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.injection;
23:
24: import org.jboss.logging.Logger;
25: import org.jboss.naming.Util;
26:
27: /**
28: * Comment
29: *
30: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
31: * @version $Revision: 60233 $
32: */
33: public class EnvEntryEncInjector implements EncInjector {
34: private static final Logger log = Logger
35: .getLogger(EnvEntryEncInjector.class);
36:
37: private String name;
38: private String entryType;
39: private String value;
40:
41: public EnvEntryEncInjector(String encName, String entryType,
42: String value) {
43: this .name = encName;
44: this .entryType = entryType;
45: this .value = value;
46: }
47:
48: public void inject(InjectionContainer container) {
49: try {
50: Util.rebind(container.getEnc(), name, getEnvEntryValue());
51: } catch (Exception e) {
52: throw new RuntimeException("Invalid <env-entry> name: "
53: + name, e);
54: }
55: }
56:
57: protected Object getEnvEntryValue() throws ClassNotFoundException {
58: Class type = Thread.currentThread().getContextClassLoader()
59: .loadClass(entryType);
60: if (type == String.class) {
61: return value;
62: } else if (type == Integer.class) {
63: return new Integer(value);
64: } else if (type == Long.class) {
65: return new Long(value);
66: } else if (type == Double.class) {
67: return new Double(value);
68: } else if (type == Float.class) {
69: return new Float(value);
70: } else if (type == Byte.class) {
71: return new Byte(value);
72: } else if (type == Character.class) {
73: String input = value;
74: if (input == null || input.length() == 0) {
75: return new Character((char) 0);
76: } else {
77: if (input.length() > 1)
78: // TODO: Add deployment context
79: log
80: .warn("Warning character env-entry is too long: binding="
81: + name + " value=" + input);
82: return new Character(input.charAt(0));
83: }
84: } else if (type == Short.class) {
85: return new Short(value);
86: } else if (type == Boolean.class) {
87: return new Boolean(value);
88: } else {
89: return value;
90: }
91: }
92: }
|