KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > EnvEntry


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * 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 SoftwareFoundation, 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.types;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.LineConfigException;
34 import com.caucho.el.Expr;
35 import com.caucho.naming.Jndi;
36 import com.caucho.util.L10N;
37
38 import javax.annotation.PostConstruct;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Configuration for the env-entry pattern.
46  */

47 public class EnvEntry implements Validator {
48   private static final L10N L = new L10N(EnvEntry.class);
49   private static final Logger JavaDoc log = Logger.getLogger(EnvEntry.class.getName());
50
51   private String JavaDoc _location = "";
52   
53   private String JavaDoc _name;
54   private Class JavaDoc _type;
55   private String JavaDoc _value;
56   private String JavaDoc _description;
57
58   public void setId(String JavaDoc id)
59   {
60   }
61   
62   /**
63    * Sets the description.
64    */

65   public void setDescription(String JavaDoc description)
66   {
67     _description = description;
68   }
69
70   /**
71    * Sets the configuration location.
72    */

73   public void setConfigLocation(String JavaDoc filename, int line)
74   {
75     _location = filename + ":" + line + " ";
76   }
77
78   /**
79    * Sets the env-entry-name
80    */

81   public void setEnvEntryName(String JavaDoc name)
82   {
83     _name = name;
84   }
85
86   /**
87    * Gets the env-entry-name
88    */

89   public String JavaDoc getEnvEntryName()
90   {
91     return _name;
92   }
93
94   /**
95    * Sets the env-entry-type
96    */

97   public void setEnvEntryType(Class JavaDoc type)
98   {
99     _type = type;
100   }
101
102   /**
103    * Gets the env-entry-type
104    */

105   public Class JavaDoc getEnvEntryType()
106   {
107     return _type;
108   }
109
110   /**
111    * Sets the env-entry-value
112    */

113   public void setEnvEntryValue(String JavaDoc value)
114   {
115     _value = value;
116   }
117
118   /**
119    * Gets the env-entry-value
120    */

121   public String JavaDoc getEnvEntryValue()
122   {
123     return _value;
124   }
125
126   /**
127    * Gets the env-entry-value
128    */

129   @PostConstruct
130   public void init()
131     throws Exception JavaDoc
132   {
133     if (_name == null)
134       throw new ConfigException(L.l("env-entry needs `env-entry-name' attribute"));
135     if (_type == null)
136       throw new ConfigException(L.l("env-entry needs `env-entry-type' attribute"));
137
138     /*
139     if (_value == null)
140       throw new BeanBuilderException(L.l("env-entry needs `env-entry-value' attribute"));
141     */

142
143     // actually, should register for validation
144
if (_value == null)
145       return;
146
147     Object JavaDoc value = _value;
148
149     if (_type.equals(String JavaDoc.class)) {
150     }
151     else if (_type.equals(Boolean JavaDoc.class))
152       value = new Boolean JavaDoc(Expr.toBoolean(_value, null));
153     else if (_type.equals(Byte JavaDoc.class))
154       value = new Byte JavaDoc((byte) Expr.toLong(_value, null));
155     else if (_type.equals(Short JavaDoc.class))
156       value = new Short JavaDoc((short) Expr.toLong(_value, null));
157     else if (_type.equals(Integer JavaDoc.class))
158       value = new Integer JavaDoc((int) Expr.toLong(_value, null));
159     else if (_type.equals(Long JavaDoc.class))
160       value = new Long JavaDoc(Expr.toLong(_value, null));
161     else if (_type.equals(Float JavaDoc.class))
162       value = new Float JavaDoc((float) Expr.toDouble(_value, null));
163     else if (_type.equals(Double JavaDoc.class))
164       value = new Double JavaDoc(Expr.toDouble(_value, null));
165     else if (_type.equals(Character JavaDoc.class)) {
166       String JavaDoc v = Expr.toString(_value, null);
167
168       if (v == null || v.length() == 0)
169     value = null;
170       else
171     value = new Character JavaDoc(v.charAt(0));
172     }
173
174     if (_name.startsWith("java:comp"))
175       Jndi.bindDeep(_name, value);
176     else
177       Jndi.bindDeep("java:comp/env/" + _name, value);
178   }
179
180   /**
181    * Validates the resource-ref, i.e. checking that it exists in
182    * JNDI.
183    */

184   public void validate()
185     throws ConfigException
186   {
187     Object JavaDoc obj = null;
188     
189     try {
190       obj = new InitialContext JavaDoc().lookup("java:comp/env/" + _name);
191     } catch (NamingException JavaDoc e) {
192       log.log(Level.FINER, e.toString(), e);
193     }
194
195     if (obj == null)
196       throw error(L.l("env-entry '{0}' was not configured. All resources defined by <env-entry> tags must be defined in a configuration file.",
197               _name));
198   }
199
200   public ConfigException error(String JavaDoc msg)
201   {
202     if (_location != null)
203       return new LineConfigException(_location + msg);
204     else
205       return new ConfigException(msg);
206   }
207
208   public String JavaDoc toString()
209   {
210     return "EnvEntry[" + _name + "]";
211   }
212 }
213
214
Popular Tags