001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.naming.deployment;
020    
021    import java.util.Map;
022    
023    import javax.xml.namespace.QName;
024    
025    import org.apache.geronimo.common.DeploymentException;
026    import org.apache.geronimo.gbean.GBeanInfo;
027    import org.apache.geronimo.gbean.GBeanInfoBuilder;
028    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
029    import org.apache.geronimo.j2ee.deployment.Module;
030    import org.apache.geronimo.j2ee.deployment.NamingBuilder;
031    import org.apache.geronimo.kernel.config.Configuration;
032    import org.apache.geronimo.kernel.repository.Environment;
033    import org.apache.geronimo.naming.reference.KernelReference;
034    import org.apache.geronimo.xbeans.j2ee.EnvEntryType;
035    import org.apache.xmlbeans.QNameSet;
036    import org.apache.xmlbeans.XmlObject;
037    
038    /**
039     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
040     */
041    public class EnvironmentEntryBuilder extends AbstractNamingBuilder {
042    
043        private final QNameSet envEntryQNameSet;
044    
045        public EnvironmentEntryBuilder(String[] eeNamespaces) {
046            envEntryQNameSet = buildQNameSet(eeNamespaces, "env-entry");
047        }
048        public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) {
049        }
050    
051        public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
052        }
053    
054        public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException {
055            XmlObject[] envEntriesUntyped = specDD.selectChildren(envEntryQNameSet);
056            for (int i = 0; i < envEntriesUntyped.length; i++) {
057                EnvEntryType envEntry = (EnvEntryType) envEntriesUntyped[i].copy().changeType(EnvEntryType.type);
058                String name = envEntry.getEnvEntryName().getStringValue().trim();
059                String type = envEntry.getEnvEntryType().getStringValue().trim();
060                String text = envEntry.getEnvEntryValue().getStringValue().trim();
061                try {
062                    Object value;
063                    if (text == null) {
064                        if ("org.apache.geronimo.kernel.Kernel".equals(type)) {
065                            value = new KernelReference();
066                        } else {
067                            value = null;
068                        }
069                    } else if ("java.lang.String".equals(type)) {
070                        value = text;
071                    } else if ("java.lang.Character".equals(type)) {
072                        value = new Character(text.charAt(0));
073                    } else if ("java.lang.Boolean".equals(type)) {
074                        value = Boolean.valueOf(text);
075                    } else if ("java.lang.Byte".equals(type)) {
076                        value = Byte.valueOf(text);
077                    } else if ("java.lang.Short".equals(type)) {
078                        value = Short.valueOf(text);
079                    } else if ("java.lang.Integer".equals(type)) {
080                        value = Integer.valueOf(text);
081                    } else if ("java.lang.Long".equals(type)) {
082                        value = Long.valueOf(text);
083                    } else if ("java.lang.Float".equals(type)) {
084                        value = Float.valueOf(text);
085                    } else if ("java.lang.Double".equals(type)) {
086                        value = Double.valueOf(text);
087                    } else {
088                        throw new DeploymentException("unrecognized type: " + type);
089                    }
090                    getJndiContextMap(componentContext).put(ENV + name, value);
091                } catch (NumberFormatException e) {
092                    throw new DeploymentException("Invalid env-entry value for name: " + name, e);
093                }
094            }
095    
096        }
097    
098        public QNameSet getSpecQNameSet() {
099            return envEntryQNameSet;
100        }
101    
102        public QNameSet getPlanQNameSet() {
103            return QNameSet.EMPTY;
104        }
105    
106        public static final GBeanInfo GBEAN_INFO;
107    
108        static {
109            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(EnvironmentEntryBuilder.class, NameFactory.MODULE_BUILDER);
110            infoBuilder.addAttribute("eeNamespaces", String[].class, true, true);
111            infoBuilder.setConstructor(new String[] {"eeNamespaces"});
112    
113            GBEAN_INFO = infoBuilder.getBeanInfo();
114        }
115    
116        public static GBeanInfo getGBeanInfo() {
117            return GBEAN_INFO;
118        }
119    
120    }