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    package org.apache.geronimo.connector.deployment.jsr88;
018    
019    import java.util.Set;
020    import java.util.HashSet;
021    import java.util.Iterator;
022    import javax.enterprise.deploy.model.DDBean;
023    import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
024    import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType;
025    import org.apache.geronimo.xbeans.geronimo.GerOutboundResourceadapterType;
026    import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
027    import org.apache.xmlbeans.SchemaTypeLoader;
028    
029    /**
030     * Represents /connector/resourceadapter in the Geronimo Connector deployment plan.
031     * Note: is not a DConfigBean, because there may be more than one ResourceAdapter
032     * in the Geronimo plan per ResourceAdapter in the J2EE deployment descriptor.
033     *
034     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
035     */
036    public class ResourceAdapter extends XmlBeanSupport {
037        private DDBean resourceAdapter;
038        private ConnectionDefinition[] instances = new ConnectionDefinition[0];
039        private ResourceAdapterInstance resourceAdapterInstance;
040    
041        public ResourceAdapter() {
042            super(null);
043        }
044    
045        public ResourceAdapter(DDBean resourceAdapter, GerResourceadapterType ra) {
046            super(null);
047            configure(resourceAdapter, ra);
048        }
049    
050        protected GerResourceadapterType getResourceAdapter() {
051            return (GerResourceadapterType)getXmlObject();
052        }
053    
054        void configure(DDBean resourceAdapter, GerResourceadapterType ra) {
055            this.resourceAdapter = resourceAdapter;
056            setXmlObject(ra);
057            if(ra.isSetResourceadapterInstance()) {
058                resourceAdapterInstance = new ResourceAdapterInstance(resourceAdapter, ra.getResourceadapterInstance());
059            } else {
060                resourceAdapterInstance = null;
061            }
062            if(ra.isSetOutboundResourceadapter()) {
063                DDBean[] test = resourceAdapter.getChildBean("outbound-resourceadapter");
064                if(test != null && test.length > 0) {
065                    GerOutboundResourceadapterType outbound = ra.getOutboundResourceadapter();
066                    GerConnectionDefinitionType[] defs = outbound.getConnectionDefinitionArray();
067                    if(defs != null) {
068                        instances = new ConnectionDefinition[defs.length];
069                        for (int i = 0; i < defs.length; i++) {
070                            GerConnectionDefinitionType def = defs[i];
071                            instances[i] = new ConnectionDefinition(resourceAdapter, def);
072                        }
073                    }
074                } else {
075                    //todo: clean up the Geronimo deployment info since there's no J2EE outbound RA
076                }
077            }
078        }
079    
080        // ----------------------- JavaBean Properties for /connector/resourceadapter ----------------------
081    
082        public ConnectionDefinition[] getConnectionDefinition() {
083            return instances;
084        }
085    
086        public ConnectionDefinition getConnectionDefinition(int index) {
087            return instances[index];
088        }
089    
090        public void setConnectionDefinition(ConnectionDefinition[] definitions) {
091            ConnectionDefinition[] old = getConnectionDefinition();
092    
093            if(definitions != null && definitions.length > 0) {
094                if(!getResourceAdapter().isSetOutboundResourceadapter()) {
095                    getResourceAdapter().addNewOutboundResourceadapter();
096                }
097            } else {
098                if(getResourceAdapter().isSetOutboundResourceadapter()) {
099                    getResourceAdapter().unsetOutboundResourceadapter();
100                }
101            }
102            Set before = new HashSet();
103            for (int i = 0; i < old.length; i++) {
104                before.add(old[i]);
105            }
106            instances = definitions;
107            // Handle current or new definitions
108            for (int i = 0; i < definitions.length; i++) {
109                ConnectionDefinition definition = definitions[i];
110                if(definition.getConnectionDefinition() == null) {
111                    definition.configure(resourceAdapter, getResourceAdapter().getOutboundResourceadapter().addNewConnectionDefinition());
112                } else {
113                    before.remove(definition);
114                }
115            }
116            // Handle removed definitions
117            for (Iterator it = before.iterator(); it.hasNext();) {
118                ConnectionDefinition definition = (ConnectionDefinition) it.next();
119                GerConnectionDefinitionType all[] = getResourceAdapter().isSetOutboundResourceadapter() ? getResourceAdapter().getOutboundResourceadapter().getConnectionDefinitionArray() : new GerConnectionDefinitionType[0];
120                for (int i = 0; i < all.length; i++) {
121                    if(all[i] == definition) {
122                        getResourceAdapter().getOutboundResourceadapter().removeConnectionDefinition(i);
123                        break;
124                    }
125                }
126            }
127            pcs.firePropertyChange("connectionDefinition", old, instances);
128        }
129    
130        public void setConnectionDefinition(int index, ConnectionDefinition definition) {
131            ConnectionDefinition[] old = instances;
132            instances[index] = definition;
133            if(definition.getConnectionDefinition() == null) {
134                definition.configure(resourceAdapter, getResourceAdapter().getOutboundResourceadapter().addNewConnectionDefinition());
135            }
136            pcs.firePropertyChange("connectionDefinition", old, instances);
137        }
138    
139        public ResourceAdapterInstance getResourceAdapterInstance() {
140            return resourceAdapterInstance;
141        }
142    
143        public void setResourceAdapterInstance(ResourceAdapterInstance resourceAdapterInstance) {
144            ResourceAdapterInstance old = this.resourceAdapterInstance;
145            this.resourceAdapterInstance = resourceAdapterInstance;
146            if(resourceAdapterInstance.getResourceAdapterInstance() == null) {
147                if(getResourceAdapter().isSetResourceadapterInstance()) {
148                    resourceAdapterInstance.configure(resourceAdapter, getResourceAdapter().getResourceadapterInstance());
149                } else {
150                    resourceAdapterInstance.configure(resourceAdapter, getResourceAdapter().addNewResourceadapterInstance());
151                }
152            }
153            pcs.firePropertyChange("resourceAdapterInstance", old, instances);
154        }
155    
156        // ----------------------- End of JavaBean Properties ----------------------
157    
158        protected SchemaTypeLoader getSchemaTypeLoader() {
159            return Connector15DCBRoot.SCHEMA_TYPE_LOADER;
160        }
161    }