001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.metadata;
023:
024: import javax.naming.Context;
025: import javax.naming.NamingException;
026:
027: import org.w3c.dom.Element;
028:
029: import org.jboss.deployment.DeploymentException;
030: import org.jboss.util.naming.Util;
031:
032: /**
033: * <description>
034: *
035: * @see <related>
036: * @author <a href="mailto:sebastien.alborini@m4x.org">Sebastien Alborini </a>
037: * @version $Revision: 57209 $
038: */
039: public class EnvEntryMetaData extends MetaData {
040: // Constants -----------------------------------------------------
041:
042: // Attributes ----------------------------------------------------
043: private String name;
044:
045: private String type;
046:
047: private String value;
048:
049: // Static --------------------------------------------------------
050:
051: // Constructors --------------------------------------------------
052: public EnvEntryMetaData() {
053: }
054:
055: // Public --------------------------------------------------------
056:
057: public String getName() {
058: return name;
059: }
060:
061: public String getType() {
062: return type;
063: }
064:
065: public String getValue() {
066: return value;
067: }
068:
069: public void importEjbJarXml(Element element)
070: throws DeploymentException {
071: name = getElementContent(getUniqueChild(element,
072: "env-entry-name"));
073: type = getElementContent(getUniqueChild(element,
074: "env-entry-type"));
075: // Strip any surrounding spaces
076: type = type.trim();
077: value = getElementContent(getUniqueChild(element,
078: "env-entry-value"));
079: }
080:
081: public static void bindEnvEntry(Context ctx, EnvEntryMetaData entry)
082: throws ClassNotFoundException, NamingException {
083: ClassLoader loader = EnvEntryMetaData.class.getClassLoader();
084: Class type = loader.loadClass(entry.getType());
085: if (type == String.class) {
086: Util.bind(ctx, entry.getName(), entry.getValue());
087: } else if (type == Integer.class) {
088: Util.bind(ctx, entry.getName(), new Integer(entry
089: .getValue()));
090: } else if (type == Long.class) {
091: Util.bind(ctx, entry.getName(), new Long(entry.getValue()));
092: } else if (type == Double.class) {
093: Util.bind(ctx, entry.getName(),
094: new Double(entry.getValue()));
095: } else if (type == Float.class) {
096: Util
097: .bind(ctx, entry.getName(), new Float(entry
098: .getValue()));
099: } else if (type == Byte.class) {
100: Util.bind(ctx, entry.getName(), new Byte(entry.getValue()));
101: } else if (type == Character.class) {
102: Object value = null;
103: String input = entry.getValue();
104: if (input == null || input.length() == 0) {
105: value = new Character((char) 0);
106: } else {
107: if (input.length() > 1)
108: // TODO: Add deployment context
109: log
110: .warn("Warning character env-entry is too long: binding="
111: + entry.getName()
112: + " value="
113: + input);
114: value = new Character(input.charAt(0));
115: }
116: Util.bind(ctx, entry.getName(), value);
117: } else if (type == Short.class) {
118: Util
119: .bind(ctx, entry.getName(), new Short(entry
120: .getValue()));
121: } else if (type == Boolean.class) {
122: Util.bind(ctx, entry.getName(), new Boolean(entry
123: .getValue()));
124: } else {
125: // Default to a String type
126: Util.bind(ctx, entry.getName(), entry.getValue());
127: }
128: }
129:
130: }
|