001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.corba.deployment;
022    
023    import java.net.URI;
024    import java.util.Map;
025    
026    import javax.xml.namespace.QName;
027    
028    import org.apache.geronimo.common.DeploymentException;
029    import org.apache.geronimo.gbean.AbstractName;
030    import org.apache.geronimo.gbean.AbstractNameQuery;
031    import org.apache.geronimo.gbean.GBeanData;
032    import org.apache.geronimo.gbean.GBeanInfo;
033    import org.apache.geronimo.gbean.GBeanInfoBuilder;
034    import org.apache.geronimo.j2ee.deployment.Module;
035    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
036    import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
037    import org.apache.geronimo.kernel.GBeanNotFoundException;
038    import org.apache.geronimo.kernel.repository.Environment;
039    import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
040    import org.apache.geronimo.naming.deployment.ENCConfigBuilder;
041    import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
042    import org.apache.geronimo.corba.TSSLinkGBean;
043    import org.apache.geronimo.corba.xbeans.csiv2.tss.TSSTssDocument;
044    import org.apache.xmlbeans.QNameSet;
045    import org.apache.xmlbeans.XmlObject;
046    import org.apache.xmlbeans.XmlString;
047    
048    /**
049     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
050     */
051    public class TSSLinkBuilder extends AbstractNamingBuilder {
052        private static final String TSS_NAMESPACE = TSSTssDocument.type.getDocumentElementName().getNamespaceURI();
053        private static final QName TSS_LINK_QNAME = new QName(TSS_NAMESPACE, "tss-link");
054        private static final QName TSS_QNAME = new QName(TSS_NAMESPACE, "tss");
055        private static final QName JNDI_NAME_QNAME = new QName(TSS_NAMESPACE, "jndi-name");
056    
057        public TSSLinkBuilder() {
058        }
059    
060        public TSSLinkBuilder(Environment defaultEnvironment) {
061            super(defaultEnvironment);
062        }
063    
064        protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
065            return true;
066    //        return hasTssLinks(plan);
067        }
068    
069    //    static boolean hasTssLinks(XmlObject plan) {
070    //        return plan != null && (plan.selectChildren(TSS_LINK_QNAME).length > 0 ||
071    //                plan.selectChildren(TSS_QNAME).length > 0);
072    //    }
073    
074        public void buildNaming(XmlObject specDD, XmlObject plan, Module module, Map componentContext) throws DeploymentException {
075            if (plan == null) {
076                return;
077            }
078    
079            AbstractName ejbName = getGBeanName(componentContext);
080    
081            String[] tssLinks = toStringArray(plan.selectChildren(TSS_LINK_QNAME));
082            XmlObject[] tsss = plan.selectChildren(TSS_QNAME);
083            String[] jndiNames = toStringArray(plan.selectChildren(JNDI_NAME_QNAME));
084            for (int i = 0; i < tssLinks.length; i++) {
085                String tssLink = tssLinks[i];
086                URI moduleURI = module.getModuleURI();
087                String moduleString = moduleURI == null ? null : moduleURI.toString();
088                AbstractNameQuery tssBeanName = ENCConfigBuilder.buildAbstractNameQuery(null, moduleString, tssLink, NameFactory.EJB_MODULE, NameFactory.EJB_MODULE);
089                try {
090                    module.getEarContext().findGBean(tssBeanName);
091                } catch (GBeanNotFoundException e) {
092                    tssBeanName = ENCConfigBuilder.buildAbstractNameQuery(null, null, tssLink, null, NameFactory.EJB_MODULE);
093                    try {
094                        module.getEarContext().findGBean(tssBeanName);
095                    } catch (GBeanNotFoundException e1) {
096                        throw new DeploymentException("No tss bean found", e);
097                    }
098                }
099                AbstractName tssLinkName = module.getEarContext().getNaming().createChildName(ejbName, "tssLink" + i, "TSSLink");
100                GBeanData tssLinkData = new GBeanData(tssLinkName, TSSLinkGBean.GBEAN_INFO);
101                tssLinkData.setAttribute("jndiNames", jndiNames);
102                tssLinkData.setReferencePattern("EJB", ejbName);
103                tssLinkData.setReferencePattern("TSSBean", tssBeanName);
104                try {
105                    module.getEarContext().addGBean(tssLinkData);
106                } catch (GBeanAlreadyExistsException e) {
107                    throw new DeploymentException("tss link gbean already present", e);
108                }
109            }
110            for (int i = 0; i < tsss.length; i++) {
111                GerPatternType tss = (GerPatternType) tsss[i];
112                AbstractNameQuery tssBeanName = ENCConfigBuilder.buildAbstractNameQuery(tss, NameFactory.CORBA_TSS, NameFactory.EJB_MODULE, null);
113                AbstractName tssLinkName = module.getEarContext().getNaming().createChildName(ejbName, "tssRef" + i, "TSSLink");
114                GBeanData tssLinkData = new GBeanData(tssLinkName, TSSLinkGBean.GBEAN_INFO);
115                tssLinkData.setAttribute("jndiNames", jndiNames);
116                tssLinkData.setReferencePattern("EJB", ejbName);
117                tssLinkData.setReferencePattern("TSSBean", tssBeanName);
118                try {
119                    module.getEarContext().addGBean(tssLinkData);
120                } catch (GBeanAlreadyExistsException e) {
121                    throw new DeploymentException("tss link gbean already present", e);
122                }
123            }
124    
125        }
126    
127        public QNameSet getSpecQNameSet() {
128            return QNameSet.EMPTY;
129        }
130    
131        public QNameSet getPlanQNameSet() {
132            return QNameSet.singleton(TSS_LINK_QNAME);
133        }
134    
135        private String[] toStringArray(XmlObject[] xmlObjects) {
136            String[] result = new String[xmlObjects.length];
137            for (int i = 0; i < result.length; i++) {
138                // toString() and xmlText() insert tags around the value, which
139                // is most definitely NOT what we want.
140                result[i] = ((XmlString)xmlObjects[i]).getStringValue();
141            }
142            return result;
143        }
144    
145        public static final GBeanInfo GBEAN_INFO;
146    
147        static {
148            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(TSSLinkBuilder.class, NameFactory.MODULE_BUILDER); //TODO decide what type this should be
149            infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
150    
151            infoBuilder.setConstructor(new String[] {"defaultEnvironment"});
152    
153            GBEAN_INFO = infoBuilder.getBeanInfo();
154        }
155    
156        public static GBeanInfo getGBeanInfo() {
157            return GBEAN_INFO;
158        }
159    
160    }
161    
162