KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > xml > EarDescriptorHandler


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive.xml;
25
26 // XMLC
27
import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31 import org.enhydra.xml.io.DOMParser;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 // ToolBox
36
import org.enhydra.tool.archive.ArchiveException;
37 //import org.enhydra.tool.archive.EjbBuilder;
38
import org.enhydra.tool.archive.WarBuilder;
39 import org.enhydra.tool.archive.Descriptor;
40 import org.enhydra.tool.archive.Module;
41 import org.enhydra.tool.archive.WebApplication;
42 import org.enhydra.tool.common.PathHandle;
43
44 // JDK
45
import java.io.InputStreamReader JavaDoc;
46 import java.io.BufferedReader JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.io.IOException JavaDoc;
49
50 //
51
public class EarDescriptorHandler extends AbstractDescriptorHandler {
52     private final String JavaDoc ALT_DD = "alt-dd";
53     private final String JavaDoc APP = "application";
54     private final String JavaDoc DESC_NAME = "description";
55     private final String JavaDoc DISPLAY_NAME = "display-name";
56     private final String JavaDoc EJB = "ejb";
57     private final String JavaDoc MODULE = "module";
58     private final String JavaDoc WEB = "web";
59     private final String JavaDoc WEB_URI = "web-uri";
60     private final String JavaDoc CONTEXT_ROOT = "context-root";
61     private final String JavaDoc PUBLIC_ID =
62         "-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN";
63     private final String JavaDoc SYSTEM_ID =
64         "http://java.sun.com/j2ee/dtds/application_1_2.dtd";
65
66     //
67
private String JavaDoc appDesc = "Application description"; // default
68
private String JavaDoc appName = new String JavaDoc();
69     private Module[] mods = new Module[0];
70     private InputStream JavaDoc[] mergeDescriptors = new InputStream JavaDoc[0];
71     private boolean altDD = true;
72
73     //
74
//
75
public String JavaDoc getAppName() {
76         return appName;
77     }
78
79     public void setAppName(String JavaDoc n) {
80         appName = n;
81     }
82
83     public String JavaDoc getDescription() {
84         return appDesc;
85     }
86
87     public void setDescription(String JavaDoc d) {
88         appDesc = d;
89     }
90
91     public Module[] getAllModules() {
92         return mods;
93     }
94
95     public void setAllModules(Module[] m) {
96         mods = m;
97     }
98
99     public InputStream JavaDoc[] getMergeDescriptors() {
100         return mergeDescriptors;
101     }
102
103     public void setMergeDescriptors(InputStream JavaDoc[] m) {
104         mergeDescriptors = m;
105     }
106
107     public boolean isAlt() {
108         return altDD;
109     }
110
111     public void setAlt(boolean a) {
112         altDD = a;
113     }
114
115     public static void main(String JavaDoc[] args) {
116         EarDescriptorHandler prep = new EarDescriptorHandler();
117
118         prep.setSource("d:/test/simple/test2.xml");
119         prep.setOutStream(System.out);
120         try {
121             prep.prep();
122         } catch (Exception JavaDoc e) {
123             e.printStackTrace();
124         }
125     }
126
127     protected Document JavaDoc getDocument() throws ArchiveException {
128         DOMParser parser = new DOMParser();
129         Document JavaDoc d = null;
130         NodeList JavaDoc list = null;
131
132         d = parser.newDocument();
133         d.appendChild(d.createElement(APP));
134         for (int i = 0; i < mergeDescriptors.length; i++) {
135             Document JavaDoc mergeDoc = null;
136             InputSource JavaDoc inputSource = null;
137             DocTypeFilterReader reader = null;
138
139             reader =
140                 new DocTypeFilterReader(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(mergeDescriptors[i])));
141             inputSource = new InputSource JavaDoc(reader);
142             try {
143                 mergeDoc = parser.parse(inputSource);
144             } catch (IOException JavaDoc e) {
145                 throw new ArchiveException(e,
146                                            "Unable to parse merge descriptor");
147             } catch (SAXException JavaDoc e) {
148                 throw new ArchiveException(e,
149                                            "Unable to parse merge descriptor");
150             }
151             list = mergeDoc.getChildNodes();
152             for (int j = 0; j < list.getLength(); j++) {
153                 Node JavaDoc child = null;
154
155                 child = list.item(j);
156                 if (child instanceof Element JavaDoc) {
157                     Element JavaDoc element = (Element JavaDoc) child;
158
159                     if (element.getNodeName().equals("module")) {
160                         d.appendChild(element);
161                     }
162                 }
163             }
164         }
165         return d;
166     }
167
168     //
169
protected String JavaDoc getSystemID() {
170         return SYSTEM_ID;
171     }
172
173     protected String JavaDoc getPublicID() {
174         return PUBLIC_ID;
175     }
176
177     protected void prepElements() {
178         prepDisplayName();
179         prepDescription();
180         prepModules();
181     }
182
183     private void prepDescription() {
184         Element JavaDoc element = null;
185
186         element = lookup(getDoc().getDocumentElement(), DESC_NAME);
187         if (element == null) {
188             element = getDoc().createElement(DESC_NAME);
189             element.appendChild(getDoc().createTextNode(getDescription()));
190             getDoc().getDocumentElement().appendChild(element);
191         }
192     }
193
194     private void prepDisplayName() {
195         Element JavaDoc element = null;
196
197         element = lookup(getDoc().getDocumentElement(), DISPLAY_NAME);
198         if (element == null) {
199             element = getDoc().createElement(DISPLAY_NAME);
200             element.appendChild(getDoc().createTextNode(getAppName()));
201             getDoc().getDocumentElement().appendChild(element);
202         }
203     }
204
205     private void prepModules() {
206         PathHandle ph = null;
207
208         for (int i = 0; i < mods.length; i++) {
209             Element JavaDoc modElement = null;
210
211             if (mods[i] instanceof WebApplication) {
212                 modElement = appendWar((WebApplication) mods[i]);
213             } else if (mods[i].getName().toLowerCase().endsWith(".jar")) {
214                 modElement = appendEjbJar(mods[i]);
215             }
216             if ((modElement != null) && isAlt()) {
217                 appendAlt(modElement, mods[i]);
218             }
219         }
220     }
221
222     private Element JavaDoc appendWar(WebApplication webApp) {
223         Element JavaDoc mod = null;
224         Element JavaDoc web = null;
225         Element JavaDoc uri = null;
226         Element JavaDoc ctx = null;
227
228         mod = getDoc().createElement(MODULE);
229         web = getDoc().createElement(WEB);
230         uri = getDoc().createElement(WEB_URI);
231         ctx = getDoc().createElement(CONTEXT_ROOT);
232         mod.appendChild(web);
233         web.appendChild(uri);
234         web.appendChild(ctx);
235         uri.appendChild(getDoc().createTextNode(webApp.getName()));
236         ctx.appendChild(getDoc().createTextNode(webApp.getContextRoot()));
237         getDoc().getDocumentElement().appendChild(mod);
238         return mod;
239     }
240
241     private void appendAlt(Element JavaDoc modElement, Module mod) {
242         Element JavaDoc alt = null;
243         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
244
245         buf.append(mod.getArchive());
246         buf.append('/');
247         if (mod instanceof WebApplication) {
248             buf.append(Descriptor.getArchivePath(Descriptor.WEB));
249         } else {
250             buf.append(Descriptor.getArchivePath(Descriptor.EJB));
251         }
252         alt = getDoc().createElement(ALT_DD);
253         alt.appendChild(getDoc().createTextNode(buf.toString()));
254         modElement.appendChild(alt);
255     }
256
257     private Element JavaDoc appendEjbJar(Module ejbMod) {
258         Element JavaDoc mod = null;
259         Element JavaDoc ejb = null;
260
261         mod = getDoc().createElement(MODULE);
262         ejb = getDoc().createElement(EJB);
263         mod.appendChild(ejb);
264         ejb.appendChild(getDoc().createTextNode(ejbMod.getName()));
265         getDoc().getDocumentElement().appendChild(mod);
266         return mod;
267     }
268
269 }
270
Popular Tags