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 SoftwareFoundation, 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.types;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.LineConfigException;
034: import com.caucho.el.Expr;
035: import com.caucho.naming.Jndi;
036: import com.caucho.util.L10N;
037: import com.caucho.webbeans.manager.WebBeansContainer;
038:
039: import javax.annotation.PostConstruct;
040: import javax.naming.InitialContext;
041: import javax.naming.NamingException;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * Configuration for the env-entry pattern.
047: */
048: public class EnvEntry implements Validator {
049: private static final L10N L = new L10N(EnvEntry.class);
050: private static final Logger log = Logger.getLogger(EnvEntry.class
051: .getName());
052:
053: private String _location = "";
054:
055: private String _name;
056: private Class _type;
057: private String _value;
058: private String _description;
059:
060: private InjectionTarget _injectionTarget;
061:
062: public void setId(String id) {
063: }
064:
065: /**
066: * Sets the description.
067: */
068: public void setDescription(String description) {
069: _description = description;
070: }
071:
072: /**
073: * Sets the configuration location.
074: */
075: public void setConfigLocation(String filename, int line) {
076: _location = filename + ":" + line + " ";
077: }
078:
079: /**
080: * Sets the env-entry-name
081: */
082: public void setEnvEntryName(String name) {
083: _name = name;
084: }
085:
086: /**
087: * Sets the injection-target
088: */
089: public void setInjectionTarget(InjectionTarget injectionTarget) {
090: _injectionTarget = injectionTarget;
091: }
092:
093: /**
094: * Gets the env-entry-name
095: */
096: public String getEnvEntryName() {
097: return _name;
098: }
099:
100: /**
101: * Sets the env-entry-type
102: */
103: public void setEnvEntryType(Class type) {
104: _type = type;
105: }
106:
107: /**
108: * Gets the env-entry-type
109: */
110: public Class getEnvEntryType() {
111: return _type;
112: }
113:
114: /**
115: * Sets the env-entry-value
116: */
117: public void setEnvEntryValue(String value) {
118: _value = value;
119: }
120:
121: /**
122: * Gets the env-entry-value
123: */
124: public String getEnvEntryValue() {
125: return _value;
126: }
127:
128: /**
129: * Gets the injection-target
130: */
131: public InjectionTarget getInjectionTarget() {
132: return _injectionTarget;
133: }
134:
135: /**
136: * Gets the env-entry-value
137: */
138: // XXX: ejb/0fd0 vs ejb/0g03
139: // PostConstruct called from com.caucho.ejb.cfg.EjbSessionBean.
140: @PostConstruct
141: public void init() throws Exception {
142: if (_name == null)
143: throw new ConfigException(L
144: .l("env-entry needs 'env-entry-name' attribute"));
145: if (_type == null)
146: throw new ConfigException(L
147: .l("env-entry needs 'env-entry-type' attribute"));
148:
149: // actually, should register for validation
150: if (_value == null)
151: return;
152:
153: Object value = _value;
154:
155: if (_type.equals(String.class)) {
156: } else if (_type.equals(Boolean.class))
157: value = new Boolean(Expr.toBoolean(_value, null));
158: else if (_type.equals(Byte.class))
159: value = new Byte((byte) Expr.toLong(_value, null));
160: else if (_type.equals(Short.class))
161: value = new Short((short) Expr.toLong(_value, null));
162: else if (_type.equals(Integer.class))
163: value = new Integer((int) Expr.toLong(_value, null));
164: else if (_type.equals(Long.class))
165: value = new Long(Expr.toLong(_value, null));
166: else if (_type.equals(Float.class))
167: value = new Float((float) Expr.toDouble(_value, null));
168: else if (_type.equals(Double.class))
169: value = new Double(Expr.toDouble(_value, null));
170: else if (_type.equals(Character.class)) {
171: String v = Expr.toString(_value, null);
172:
173: if (v == null || v.length() == 0)
174: value = null;
175: else
176: value = new Character(v.charAt(0));
177: }
178:
179: WebBeansContainer webBeans = WebBeansContainer.create();
180: webBeans.addSingleton(value, _name);
181:
182: Jndi.bindDeepShort("java:comp/env/" + _name, value);
183: }
184:
185: /**
186: * Validates the resource-ref, i.e. checking that it exists in
187: * JNDI.
188: */
189: public void validate() throws ConfigException {
190: Object obj = null;
191:
192: try {
193: obj = new InitialContext().lookup("java:comp/env/" + _name);
194: } catch (NamingException e) {
195: log.log(Level.FINER, e.toString(), e);
196: }
197:
198: if (obj == null)
199: throw error(L
200: .l(
201: "env-entry '{0}' was not configured. All resources defined by <env-entry> tags must be defined in a configuration file.",
202: _name));
203: }
204:
205: public ConfigException error(String msg) {
206: if (_location != null)
207: return new LineConfigException(_location + msg);
208: else
209: return new ConfigException(msg);
210: }
211:
212: public String toString() {
213: return "EnvEntry[" + _name + "]";
214: }
215: }
|