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;
018    
019    import javax.xml.namespace.QName;
020    
021    import org.apache.geronimo.xbeans.geronimo.GerConnectorType;
022    import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType;
023    import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
024    import org.apache.geronimo.xbeans.geronimo.GerConnectiondefinitionInstanceType;
025    import org.apache.xmlbeans.XmlCursor;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
031     */
032    public class ConnectorPlanRectifier {
033    
034        private static final Log log = LogFactory.getLog(ConnectorPlanRectifier.class);
035    
036        private static final QName VERSION_QNAME = new QName("", "version");
037        private static final QName GLOBAL_JNDI_NAME_QNAME = new QName(ConnectorModuleBuilder.GERCONNECTOR_NAMESPACE, "global-jndi-name");
038        private static final QName CREDENTIAL_INTERFACE_QNAME = new QName(ConnectorModuleBuilder.GERCONNECTOR_NAMESPACE, "credential-interface");
039    
040    
041        static void rectifyPlan(GerConnectorType gerConnector) {
042            boolean updated = false;
043            XmlCursor cursor = gerConnector.newCursor();
044            try {
045                updated = cursor.removeAttribute(VERSION_QNAME);
046            } finally {
047                cursor.dispose();
048            }
049            GerResourceadapterType[] resourceAdapters = gerConnector.getResourceadapterArray();
050            for (int i = 0; i < resourceAdapters.length; i++) {
051                GerResourceadapterType resourceAdapter = resourceAdapters[i];
052                if (resourceAdapter.isSetOutboundResourceadapter()) {
053                GerConnectionDefinitionType[] connectionDefinitions = resourceAdapter.getOutboundResourceadapter().getConnectionDefinitionArray();
054                    for (int j = 0; j < connectionDefinitions.length; j++) {
055                        GerConnectionDefinitionType connectionDefinition = connectionDefinitions[j];
056                        GerConnectiondefinitionInstanceType[] connectiondefinitionInstances = connectionDefinition.getConnectiondefinitionInstanceArray();
057                        for (int k = 0; k < connectiondefinitionInstances.length; k++) {
058                            GerConnectiondefinitionInstanceType connectiondefinitionInstance = connectiondefinitionInstances[k];
059                            cursor = connectiondefinitionInstance.newCursor();
060                            try {
061                                if (cursor.toFirstChild()) {
062                                    if (cursor.toNextSibling(GLOBAL_JNDI_NAME_QNAME)) {
063                                        cursor.removeXml();
064                                        updated = true;
065                                    }
066                                    if (cursor.toNextSibling(CREDENTIAL_INTERFACE_QNAME)) {
067                                        cursor.removeXml();
068                                        updated = true;
069                                    }
070                                }
071                            } finally {
072                                cursor.dispose();
073                            }
074                        }
075                    }
076                }
077            }
078            if (updated) {
079                log.warn("Your connector plan has obsolete elements or attributes in it.  Please remove version attributes, global-jndi-name elements, and credential-interface elements");
080            }
081        }
082    
083    }