KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > injection > EnvEntryEncInjector


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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: 56518 $
32  */

33 public class EnvEntryEncInjector implements EncInjector
34 {
35    private static final Logger log = Logger.getLogger(EnvEntryEncInjector.class);
36
37    private String JavaDoc name;
38    private String JavaDoc entryType;
39    private String JavaDoc value;
40
41
42    public EnvEntryEncInjector(String JavaDoc encName, String JavaDoc entryType, String JavaDoc value)
43    {
44       this.name = encName;
45       this.entryType = entryType;
46       this.value = value;
47    }
48
49    public void inject(InjectionContainer container)
50    {
51       try
52       {
53          Util.rebind(container.getEnc(),
54                  name,
55                  getEnvEntryValue());
56       }
57       catch (Exception JavaDoc e)
58       {
59          throw new RuntimeException JavaDoc("Invalid <env-entry> name: " + name, e);
60       }
61    }
62
63
64    protected Object JavaDoc getEnvEntryValue() throws ClassNotFoundException JavaDoc
65    {
66       Class JavaDoc type = Thread.currentThread().getContextClassLoader().loadClass(entryType);
67       if (type == String JavaDoc.class)
68       {
69          return value;
70       }
71       else if (type == Integer JavaDoc.class)
72       {
73          return new Integer JavaDoc(value);
74       }
75       else if (type == Long JavaDoc.class)
76       {
77          return new Long JavaDoc(value);
78       }
79       else if (type == Double JavaDoc.class)
80       {
81          return new Double JavaDoc(value);
82       }
83       else if (type == Float JavaDoc.class)
84       {
85          return new Float JavaDoc(value);
86       }
87       else if (type == Byte JavaDoc.class)
88       {
89          return new Byte JavaDoc(value);
90       }
91       else if (type == Character JavaDoc.class)
92       {
93          String JavaDoc input = value;
94          if (input == null || input.length() == 0)
95          {
96             return new Character JavaDoc((char) 0);
97          }
98          else
99          {
100             if (input.length() > 1)
101                // TODO: Add deployment context
102
log.warn("Warning character env-entry is too long: binding="
103                        + name + " value=" + input);
104             return new Character JavaDoc(input.charAt(0));
105          }
106       }
107       else if (type == Short JavaDoc.class)
108       {
109          return new Short JavaDoc(value);
110       }
111       else if (type == Boolean JavaDoc.class)
112       {
113          return new Boolean JavaDoc(value);
114       }
115       else
116       {
117          return value;
118       }
119    }
120 }
121
Popular Tags