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: $Header:$
018: */package org.apache.beehive.controls.system.ejb.internal;
019:
020: import java.util.List;
021:
022: import org.apache.beehive.controls.system.ejb.EJBInfo;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Node;
026:
027: /**
028: *
029: */
030: public class WebDescriptorHandler {
031:
032: public static WebDescriptorHandler getInstance() {
033: return new WebDescriptorHandler();
034: }
035:
036: private WebDescriptorHandler() {
037: }
038:
039: public void assemble(Document document, EJBInfo ejbInfo,
040: String ejbLinkValue) {
041: Element webAppType = document.getDocumentElement();
042:
043: if (ejbInfo.isLocal())
044: insertEJBLocalRefInWebApp(webAppType, ejbInfo,
045: ejbLinkValue, document);
046: else
047: insertEJBRefInWebApp(webAppType, ejbInfo, ejbLinkValue,
048: document);
049: }
050:
051: private void insertEJBRefInWebApp(Element webAppType, EJBInfo ei,
052: String ejbLinkValue, Document webAppDoc) {
053: List ejbRefArray = DomUtils.getChildElementsByName(webAppType,
054: "ejb-ref");
055:
056: String insertedEjbRefName = ei.getRefName();
057: Node nextSibling = null;
058: for (int j = ejbRefArray.size() - 1; j >= 0; j--) {
059: Element ejbRef = (Element) ejbRefArray.get(j);
060: String ejbRefName = DomUtils.getChildElementText(ejbRef,
061: "ejb-ref-name");
062: if (insertedEjbRefName.equals(ejbRefName)) {
063: nextSibling = ejbRef.getNextSibling();
064: webAppType.removeChild(ejbRef);
065: break;
066: }
067: }
068:
069: // insert a new <ejb-ref> entry and fill in the values
070: Element insertedEjbRef = webAppDoc.createElement("ejb-ref");
071: if (nextSibling != null)
072: webAppType.insertBefore(insertedEjbRef, nextSibling);
073: else
074: webAppType.insertBefore(insertedEjbRef,
075: findEjbRefInsertPoint(webAppType));
076:
077: Element ejbRefName = webAppDoc.createElement("ejb-ref-name");
078: ejbRefName.setTextContent(insertedEjbRefName);
079: insertedEjbRef.appendChild(ejbRefName);
080:
081: Element ejbRefType = webAppDoc.createElement("ejb-ref-type");
082: ejbRefType.setTextContent(ei.getBeanType());
083: insertedEjbRef.appendChild(ejbRefType);
084:
085: Element homeType = webAppDoc.createElement("home");
086: homeType.setTextContent(ei.getBeanInterface().getName());
087: insertedEjbRef.appendChild(homeType);
088:
089: Element remoteType = webAppDoc.createElement("remote");
090: remoteType.setTextContent(ei.getBeanInterface().getName());
091: insertedEjbRef.appendChild(remoteType);
092:
093: Element ejbLink = webAppDoc.createElement("ejb-link");
094: ejbLink.setTextContent(ejbLinkValue);
095: insertedEjbRef.appendChild(ejbLink);
096: }
097:
098: private void insertEJBLocalRefInWebApp(Element webAppType,
099: EJBInfo ei, String ejbLinkValue, Document webAppDoc) {
100: List ejbLocalRefArray = DomUtils.getChildElementsByName(
101: webAppType, "ejb-local-ref");
102: String insertedEjbRefName = ei.getRefName();
103:
104: Node nextSibling = null;
105: for (int j = ejbLocalRefArray.size() - 1; j >= 0; j--) {
106: Element ejbLocalRef = (Element) ejbLocalRefArray.get(j);
107: String ejbRefName = DomUtils.getChildElementText(
108: ejbLocalRef, "ejb-ref-name");
109: if (insertedEjbRefName.equals(ejbRefName)) {
110: nextSibling = ejbLocalRef.getNextSibling();
111: webAppType.removeChild(ejbLocalRef);
112: break;
113: }
114: }
115:
116: // insert a new <ejb-local-ref> entry and fill in the values
117: Element insertedEjbLocalRef = webAppDoc
118: .createElement("ejb-local-ref");
119: if (nextSibling != null)
120: webAppType.insertBefore(insertedEjbLocalRef, nextSibling);
121: else
122: webAppType.insertBefore(insertedEjbLocalRef,
123: findEjbLocalRefInsertPoint(webAppType));
124:
125: Element ejbRefName = webAppDoc.createElement("ejb-ref-name");
126: ejbRefName.setTextContent(insertedEjbRefName);
127: insertedEjbLocalRef.appendChild(ejbRefName);
128:
129: Element ejbRefType = webAppDoc.createElement("ejb-ref-type");
130: ejbRefType.setTextContent(ei.getBeanType());
131: insertedEjbLocalRef.appendChild(ejbRefType);
132:
133: Element homeType = webAppDoc.createElement("local-home");
134: homeType.setTextContent(ei.getHomeInterface().getName());
135: insertedEjbLocalRef.appendChild(homeType);
136:
137: Element localType = webAppDoc.createElement("local");
138: localType.setTextContent(ei.getBeanInterface().getName());
139: insertedEjbLocalRef.appendChild(localType);
140:
141: Element ejbLink = webAppDoc.createElement("ejb-link");
142: ejbLink.setTextContent(ejbLinkValue);
143: insertedEjbLocalRef.appendChild(ejbLink);
144: }
145:
146: private Node findEjbRefInsertPoint(Element parent) {
147: Element e = DomUtils.getChildElementByName(parent,
148: "ejb-local-ref");
149: if (e != null)
150: return e;
151:
152: return findEjbLocalRefInsertPoint(parent);
153: }
154:
155: private Node findEjbLocalRefInsertPoint(Element parent) {
156:
157: Element e = DomUtils.getChildElementByName(parent,
158: "service-ref");
159: if (e != null)
160: return e;
161:
162: e = DomUtils.getChildElementByName(parent, "resource-ref");
163: if (e != null)
164: return e;
165:
166: e = DomUtils.getChildElementByName(parent, "resource-env-ref");
167: if (e != null)
168: return e;
169:
170: e = DomUtils.getChildElementByName(parent,
171: "message-destination-ref");
172: if (e != null)
173: return e;
174:
175: e = DomUtils.getChildElementByName(parent,
176: "message-destination");
177: if (e != null)
178: return e;
179:
180: e = DomUtils.getChildElementByName(parent,
181: "locale-encoding-mapping-list");
182: if (e != null)
183: return e;
184:
185: return null;
186: }
187: }
|