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 java.io.IOException;
021    import java.io.InputStream;
022    
023    import javax.enterprise.deploy.model.DDBean;
024    import javax.enterprise.deploy.model.DDBeanRoot;
025    import javax.enterprise.deploy.spi.DConfigBean;
026    import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
027    
028    import org.apache.geronimo.deployment.plugin.DConfigBeanRootSupport;
029    import org.apache.geronimo.xbeans.geronimo.GerConnectorDocument;
030    import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType;
031    import org.apache.xmlbeans.SchemaTypeLoader;
032    import org.apache.xmlbeans.XmlBeans;
033    import org.apache.xmlbeans.XmlException;
034    import org.apache.xmlbeans.XmlObject;
035    
036    /**
037     *
038     *
039     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
040     *
041     * */
042    public class ResourceAdapterDConfigRoot extends DConfigBeanRootSupport {
043        static final SchemaTypeLoader SCHEMA_TYPE_LOADER = XmlBeans.typeLoaderUnion(new SchemaTypeLoader[] {
044            XmlBeans.typeLoaderForClassLoader(org.apache.geronimo.xbeans.j2ee.String.class.getClassLoader()),
045            XmlBeans.typeLoaderForClassLoader(GerConnectorDocument.class.getClassLoader())
046        });
047    
048        private static String[][] XPATHS = {
049            {"connector", "resourceadapter"}
050        };
051    
052        private ResourceAdapterDConfigBean resourceAdapterDConfigBean;
053    
054        public ResourceAdapterDConfigRoot(DDBeanRoot ddBean) {
055            super(ddBean, loadDefaultData(ddBean));
056            replaceResourceAdapterDConfigBean(getConnectorDocument().getConnector().getResourceadapterArray()[0]);
057        }
058    
059        private static XmlObject loadDefaultData(DDBeanRoot root) {
060            InputStream in = root.getDeployableObject().getEntry("META-INF/geronimo-ra.xml");
061            if(in == null) {
062                GerConnectorDocument doc = GerConnectorDocument.Factory.newInstance();
063                doc.addNewConnector().addNewResourceadapter();
064                return doc;
065            } else {
066                try {
067                    XmlObject result =  GerConnectorDocument.Factory.parse(in);
068                    in.close();
069                    return result;
070                } catch (XmlException e) {
071                    throw new RuntimeException("Unable to load default Geronimo RA data", e);
072                } catch (IOException e) {
073                    throw new RuntimeException("Unable to load default Geronimo RA data", e);
074                }
075            }
076        }
077    
078        private void replaceResourceAdapterDConfigBean(GerResourceadapterType resourceAdapter) {
079            DDBean ddBean = getDDBean();
080            String path = getXpaths()[0];
081            System.out.println("********** Searching XPath "+path+" -- "+ddBean.getChildBean(path));
082            DDBean childDDBean = ddBean.getChildBean(path)[0];
083            resourceAdapterDConfigBean = new ResourceAdapterDConfigBean(childDDBean, resourceAdapter);
084        }
085    
086        GerConnectorDocument getConnectorDocument() {
087            return (GerConnectorDocument) getXmlObject();
088        }
089    
090        public String[] getXpaths() {
091            return getXPathsForJ2ee_1_4(XPATHS);
092        }
093    
094        public DConfigBean getDConfigBean(DDBean bean) throws ConfigurationException {
095            if (getXpaths()[0].equals(bean.getXpath())) {
096                return resourceAdapterDConfigBean;
097            }
098            return null;
099        }
100    
101        public void fromXML(InputStream inputStream) throws XmlException, IOException {
102            super.fromXML(inputStream);
103            //TODO this is so totally wrong...
104            replaceResourceAdapterDConfigBean(getConnectorDocument().getConnector().getResourceadapterArray()[0]);
105        }
106    
107        protected SchemaTypeLoader getSchemaTypeLoader() {
108            return SCHEMA_TYPE_LOADER;
109        }
110    }