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    
022    import org.apache.geronimo.deployment.plugin.DConfigBeanSupport;
023    import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
024    import org.apache.geronimo.xbeans.geronimo.GerConnectiondefinitionInstanceType;
025    import org.apache.geronimo.xbeans.geronimo.GerConnectionmanagerType;
026    import org.apache.xmlbeans.SchemaTypeLoader;
027    
028    /**
029     *
030     *
031     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
032     *
033     **/
034    public class ConnectionDefinitionDConfigBean extends DConfigBeanSupport {
035    
036        private ConnectionDefinitionInstance[] instances = new ConnectionDefinitionInstance[0];
037    
038        public ConnectionDefinitionDConfigBean(DDBean ddBean, GerConnectionDefinitionType connectionDefinition) {
039            super(ddBean, connectionDefinition);
040            String connectionfactoryInterface = ddBean.getText("connectionfactory-interface")[0];
041            if (connectionDefinition.getConnectionfactoryInterface() == null) {
042                connectionDefinition.setConnectionfactoryInterface(connectionfactoryInterface);
043            } else {
044                assert connectionfactoryInterface.equals(connectionDefinition.getConnectionfactoryInterface());
045            }
046            // Get initial list of instances
047            instances = new ConnectionDefinitionInstance[getConnectionDefinition().getConnectiondefinitionInstanceArray().length];
048            for (int i = 0; i < instances.length; i++) {
049                instances[i] = new ConnectionDefinitionInstance();
050                instances[i].initialize(getConnectionDefinition().getConnectiondefinitionInstanceArray(i), this);
051            }
052        }
053    
054        GerConnectionDefinitionType getConnectionDefinition() {
055            return (GerConnectionDefinitionType) getXmlObject();
056        }
057    
058        public ConnectionDefinitionInstance[] getConnectionDefinitionInstance() {
059            return instances;
060        }
061    
062        public void setConnectionDefinitionInstance(ConnectionDefinitionInstance[] instances) {
063            ConnectionDefinitionInstance[] old = getConnectionDefinitionInstance();
064            this.instances = instances;
065            for (int i = 0; i < instances.length; i++) { // catch additions
066                ConnectionDefinitionInstance instance = instances[i];
067                if (!instance.hasParent()) {
068                    GerConnectiondefinitionInstanceType xmlObject = getConnectionDefinition().addNewConnectiondefinitionInstance();
069                    xmlObject.setConnectionmanager(GerConnectionmanagerType.Factory.newInstance());
070                    instance.initialize(xmlObject, this);
071                }
072            }
073            for (int i = 0; i < old.length; i++) { // catch removals
074                ConnectionDefinitionInstance instance = old[i];
075                boolean found = false;
076                for (int j = 0; j < instances.length; j++) {
077                    if (instances[j] == instance) {
078                        found = true;
079                        break;
080                    }
081                }
082                if (!found) {
083                    // remove the XmlBean
084                    for (int j = 0; j < getConnectionDefinition().getConnectiondefinitionInstanceArray().length; j++) {
085                        GerConnectiondefinitionInstanceType test = getConnectionDefinition().getConnectiondefinitionInstanceArray(j);
086                        if (test == instance.getConnectiondefinitionInstance()) {
087                            getConnectionDefinition().removeConnectiondefinitionInstance(j);
088                            break;
089                        }
090                    }
091                    // clean up the removed JavaBean
092                    instance.dispose();
093                }
094            }
095            pcs.firePropertyChange("connectionDefinitionInstance", old, instances);
096        }
097    
098        protected SchemaTypeLoader getSchemaTypeLoader() {
099            return ResourceAdapterDConfigRoot.SCHEMA_TYPE_LOADER;
100        }
101    
102    }