001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.connector.deployment.dconfigbean;
019    
020    import javax.enterprise.deploy.model.DDBean;
021    import javax.enterprise.deploy.model.XpathEvent;
022    import javax.enterprise.deploy.model.XpathListener;
023    
024    import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
025    import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
026    import org.apache.xmlbeans.SchemaTypeLoader;
027    import org.apache.xmlbeans.XmlBeans;
028    
029    /**
030     * @version $Revision 1.0$  $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
031     */
032    public class ConfigPropertySettings extends XmlBeanSupport {
033        private final static SchemaTypeLoader SCHEMA_TYPE_LOADER = XmlBeans.getContextTypeLoader();
034        private String type;
035        private DDBean ddBean;
036        private XpathListener typeListener;
037        private XpathListener nameListener;
038    
039        public ConfigPropertySettings() {
040            super(null);
041        }
042    
043        void initialize(GerConfigPropertySettingType xmlObject, DDBean configPropertyBean) {
044            setXmlObject(xmlObject);
045            ddBean = configPropertyBean;
046            DDBean[] child = configPropertyBean.getChildBean("config-property-type");
047            if (child.length == 1) {
048                setConfigPropertyType(child[0]);
049            }
050            child = configPropertyBean.getChildBean("config-property-name");
051            if (child.length == 1) {
052                setConfigPropertyName(child[0]);
053            }
054            configPropertyBean.addXpathListener("config-property-type", typeListener = new XpathListener() {
055                public void fireXpathEvent(XpathEvent xpe) {
056                    if (xpe.isChangeEvent() || xpe.isAddEvent()) {
057                        setConfigPropertyType(xpe.getBean());
058                    } else if (xpe.isRemoveEvent()) {
059                        setConfigPropertyType((String) null);
060                    }
061                }
062            });
063            configPropertyBean.addXpathListener("config-property-name", nameListener = new XpathListener() {
064                public void fireXpathEvent(XpathEvent xpe) {
065                    if (xpe.isChangeEvent() || xpe.isAddEvent()) {
066                        setConfigPropertyName(xpe.getBean());
067                    } else if (xpe.isRemoveEvent()) {
068                        setConfigPropertyName((String) null);
069                    }
070                }
071            });
072        }
073    
074        boolean matches(DDBean target) {
075            return target.equals(ddBean);
076        }
077    
078        void dispose() {
079            if (ddBean != null) {
080                ddBean.removeXpathListener("config-property-type", typeListener);
081                ddBean.removeXpathListener("config-property-name", nameListener);
082            }
083            nameListener = null;
084            typeListener = null;
085            ddBean = null;
086        }
087    
088        GerConfigPropertySettingType getConfigPropertySetting() {
089            return (GerConfigPropertySettingType) getXmlObject();
090        }
091    
092        public String getConfigPropertyName() {
093            return getConfigPropertySetting().getName();
094        }
095    
096        private void setConfigPropertyName(DDBean configPropertyBean) {
097            if (configPropertyBean == null) {
098                setConfigPropertyName((String) null);
099            } else {
100                setConfigPropertyName(configPropertyBean.getText());
101            }
102        }
103    
104        private void setConfigPropertyName(String name) {
105            String old = getConfigPropertyName();
106            getConfigPropertySetting().setName(name);
107            pcs.firePropertyChange("configPropertyName", old, name);
108        }
109    
110        public String getConfigPropertyType() {
111            return type;
112        }
113    
114        private void setConfigPropertyType(DDBean configPropertyBean) {
115            if (configPropertyBean == null) {
116                setConfigPropertyType((String) null);
117            } else {
118                setConfigPropertyType(configPropertyBean.getText());
119            }
120        }
121    
122        private void setConfigPropertyType(String type) {
123            String old = getConfigPropertyType();
124            this.type = type;
125            pcs.firePropertyChange("configPropertyType", old, type);
126        }
127    
128        public String getConfigPropertyValue() {
129            return getConfigPropertySetting().getStringValue();
130        }
131    
132        public void setConfigPropertyValue(String configPropertyValue) {
133            String old = getConfigPropertyValue();
134            getConfigPropertySetting().setStringValue(configPropertyValue);
135            pcs.firePropertyChange("configPropertyValue", old, configPropertyValue);
136        }
137    
138        public String toString() {
139            return "Property "+getConfigPropertyName();
140        }
141    }