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: package org.apache.geronimo.corba.deployment;
021:
022: import java.net.URI;
023: import java.util.Map;
024:
025: import javax.xml.namespace.QName;
026:
027: import org.apache.geronimo.common.DeploymentException;
028: import org.apache.geronimo.gbean.AbstractName;
029: import org.apache.geronimo.gbean.AbstractNameQuery;
030: import org.apache.geronimo.gbean.GBeanData;
031: import org.apache.geronimo.gbean.GBeanInfo;
032: import org.apache.geronimo.gbean.GBeanInfoBuilder;
033: import org.apache.geronimo.j2ee.deployment.Module;
034: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
035: import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
036: import org.apache.geronimo.kernel.GBeanNotFoundException;
037: import org.apache.geronimo.kernel.repository.Environment;
038: import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
039: import org.apache.geronimo.naming.deployment.ENCConfigBuilder;
040: import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
041: import org.apache.geronimo.corba.TSSLinkGBean;
042: import org.apache.geronimo.corba.xbeans.csiv2.tss.TSSTssDocument;
043: import org.apache.xmlbeans.QNameSet;
044: import org.apache.xmlbeans.XmlObject;
045: import org.apache.xmlbeans.XmlString;
046:
047: /**
048: * @version $Rev: 535381 $ $Date: 2007-05-04 14:04:18 -0700 (Fri, 04 May 2007) $
049: */
050: public class TSSLinkBuilder extends AbstractNamingBuilder {
051: private static final String TSS_NAMESPACE = TSSTssDocument.type
052: .getDocumentElementName().getNamespaceURI();
053: private static final QName TSS_LINK_QNAME = new QName(
054: TSS_NAMESPACE, "tss-link");
055: private static final QName TSS_QNAME = new QName(TSS_NAMESPACE,
056: "tss");
057: private static final QName JNDI_NAME_QNAME = new QName(
058: TSS_NAMESPACE, "jndi-name");
059:
060: public TSSLinkBuilder() {
061: }
062:
063: public TSSLinkBuilder(Environment defaultEnvironment) {
064: super (defaultEnvironment);
065: }
066:
067: protected boolean willMergeEnvironment(XmlObject specDD,
068: XmlObject plan) {
069: return true;
070: // return hasTssLinks(plan);
071: }
072:
073: // static boolean hasTssLinks(XmlObject plan) {
074: // return plan != null && (plan.selectChildren(TSS_LINK_QNAME).length > 0 ||
075: // plan.selectChildren(TSS_QNAME).length > 0);
076: // }
077:
078: public void buildNaming(XmlObject specDD, XmlObject plan,
079: Module module, Map componentContext)
080: throws DeploymentException {
081: if (plan == null) {
082: return;
083: }
084:
085: AbstractName ejbName = getGBeanName(componentContext);
086:
087: String[] tssLinks = toStringArray(plan
088: .selectChildren(TSS_LINK_QNAME));
089: XmlObject[] tsss = plan.selectChildren(TSS_QNAME);
090: String[] jndiNames = toStringArray(plan
091: .selectChildren(JNDI_NAME_QNAME));
092: for (int i = 0; i < tssLinks.length; i++) {
093: String tssLink = tssLinks[i];
094: URI moduleURI = module.getModuleURI();
095: String moduleString = moduleURI == null ? null : moduleURI
096: .toString();
097: AbstractNameQuery tssBeanName = ENCConfigBuilder
098: .buildAbstractNameQuery(null, moduleString,
099: tssLink, NameFactory.EJB_MODULE,
100: NameFactory.EJB_MODULE);
101: try {
102: module.getEarContext().findGBean(tssBeanName);
103: } catch (GBeanNotFoundException e) {
104: tssBeanName = ENCConfigBuilder.buildAbstractNameQuery(
105: null, null, tssLink, null,
106: NameFactory.EJB_MODULE);
107: try {
108: module.getEarContext().findGBean(tssBeanName);
109: } catch (GBeanNotFoundException e1) {
110: throw new DeploymentException("No tss bean found",
111: e);
112: }
113: }
114: AbstractName tssLinkName = module.getEarContext()
115: .getNaming().createChildName(ejbName,
116: "tssLink" + i, "TSSLink");
117: GBeanData tssLinkData = new GBeanData(tssLinkName,
118: TSSLinkGBean.GBEAN_INFO);
119: tssLinkData.setAttribute("jndiNames", jndiNames);
120: tssLinkData.setReferencePattern("EJB", ejbName);
121: tssLinkData.setReferencePattern("TSSBean", tssBeanName);
122: try {
123: module.getEarContext().addGBean(tssLinkData);
124: } catch (GBeanAlreadyExistsException e) {
125: throw new DeploymentException(
126: "tss link gbean already present", e);
127: }
128: }
129: for (int i = 0; i < tsss.length; i++) {
130: GerPatternType tss = (GerPatternType) tsss[i];
131: AbstractNameQuery tssBeanName = ENCConfigBuilder
132: .buildAbstractNameQuery(tss, NameFactory.CORBA_TSS,
133: NameFactory.EJB_MODULE, null);
134: AbstractName tssLinkName = module.getEarContext()
135: .getNaming().createChildName(ejbName, "tssRef" + i,
136: "TSSLink");
137: GBeanData tssLinkData = new GBeanData(tssLinkName,
138: TSSLinkGBean.GBEAN_INFO);
139: tssLinkData.setAttribute("jndiNames", jndiNames);
140: tssLinkData.setReferencePattern("EJB", ejbName);
141: tssLinkData.setReferencePattern("TSSBean", tssBeanName);
142: try {
143: module.getEarContext().addGBean(tssLinkData);
144: } catch (GBeanAlreadyExistsException e) {
145: throw new DeploymentException(
146: "tss link gbean already present", e);
147: }
148: }
149:
150: }
151:
152: public QNameSet getSpecQNameSet() {
153: return QNameSet.EMPTY;
154: }
155:
156: public QNameSet getPlanQNameSet() {
157: return QNameSet.singleton(TSS_LINK_QNAME);
158: }
159:
160: private String[] toStringArray(XmlObject[] xmlObjects) {
161: String[] result = new String[xmlObjects.length];
162: for (int i = 0; i < result.length; i++) {
163: // toString() and xmlText() insert tags around the value, which
164: // is most definitely NOT what we want.
165: result[i] = ((XmlString) xmlObjects[i]).getStringValue();
166: }
167: return result;
168: }
169:
170: public static final GBeanInfo GBEAN_INFO;
171:
172: static {
173: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
174: TSSLinkBuilder.class, NameFactory.MODULE_BUILDER); //TODO decide what type this should be
175: infoBuilder.addAttribute("defaultEnvironment",
176: Environment.class, true, true);
177:
178: infoBuilder
179: .setConstructor(new String[] { "defaultEnvironment" });
180:
181: GBEAN_INFO = infoBuilder.getBeanInfo();
182: }
183:
184: public static GBeanInfo getGBeanInfo() {
185: return GBEAN_INFO;
186: }
187:
188: }
|