KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > WarBuilder


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;
25
26 // ToolBox imports
27
import org.enhydra.tool.common.ResUtil;
28 import org.enhydra.tool.archive.xml.WarDescriptorHandler;
29
30 // Standard imports
31
import java.io.IOException JavaDoc;
32 import java.io.File JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import java.util.jar.Manifest JavaDoc;
35 import java.util.jar.Attributes JavaDoc;
36
37 //
38
public class WarBuilder extends JarBuilder {
39
40     // not to be resourced
41
private final String JavaDoc WEB_INF = "WEB-INF"; // nores
42
private final String JavaDoc CLASSES = "classes"; // nores
43

44     //
45
public WarBuilder() {
46         super();
47     }
48
49     public WarPlan getWarPlan() {
50         WarPlan wp = null;
51
52         if (getPlan() instanceof WarPlan) {
53             wp = (WarPlan) getPlan();
54         }
55         return wp;
56     }
57
58     // override JarBuilder
59
public File JavaDoc buildArchive() throws ArchiveException {
60         File JavaDoc war = null;
61         String JavaDoc root = null;
62         File JavaDoc[] files = new File JavaDoc[0];
63         Descriptor[] dd = new Descriptor[0];
64
65         war = openJarStream(getWarPlan().getArchivePath());
66         root = getWarPlan().getContentRoot();
67         dd = getWarPlan().getDescriptors();
68         files = getWarPlan().getContentFileArray();
69         addWarClasses();
70         addLibraries();
71         addContent();
72         for (int i = 0; i < dd.length; i++) {
73             addDescriptor(dd[i], root, files);
74         }
75         closeJarStream();
76         return war;
77     }
78
79     protected Manifest JavaDoc buildManifest(Manifest JavaDoc m) {
80         Manifest JavaDoc manifest = super.buildManifest(m);
81
82         manifest.getMainAttributes().put(new Attributes.Name JavaDoc(CONTENT_ROOT),
83                                          getWarPlan().getContentRoot());
84         return manifest;
85     }
86
87     private void addContent() throws ArchiveException {
88         File JavaDoc[] files = getWarPlan().getContentFileArray();
89
90         for (int i = 0; i < files.length; i++) {
91             addContentFile(files[i]);
92         }
93     }
94
95     private void addContentFile(File JavaDoc source) throws ArchiveException {
96         StringBuffer JavaDoc entryPath = new StringBuffer JavaDoc();
97         String JavaDoc contentRoot = null;
98         String JavaDoc relative = null;
99
100         contentRoot = getWarPlan().getContentRoot();
101         relative = source.getAbsolutePath().substring(contentRoot.length()
102                 + 1);
103         relative = relative.replace('\\', '/');
104         entryPath.append(relative);
105         try {
106             createEntry(source, entryPath.toString());
107         } catch (IOException JavaDoc e) {
108             throw new ArchiveException(e,
109                                        ResUtil.format(res.getString("WarBuilder_Unable_to"),
110                                                       source));
111         }
112     }
113
114     private void addLibraries() throws ArchiveException {
115         File JavaDoc[] files = getWarPlan().getLibFileArray();
116
117         for (int i = 0; i < files.length; i++) {
118             addLibFile(files[i]);
119         }
120     }
121
122     private void addLibFile(File JavaDoc source) throws ArchiveException {
123         StringBuffer JavaDoc entryPath = new StringBuffer JavaDoc();
124
125         //
126
entryPath.append("WEB-INF");
127         entryPath.append('/');
128         entryPath.append("lib");
129         entryPath.append('/');
130         entryPath.append(source.getName());
131
132         //
133
try {
134             createEntry(source, entryPath.toString());
135         } catch (IOException JavaDoc e) {
136             String JavaDoc exMess = res.getString("WarBuilder_Unable_to");
137
138             exMess = ResUtil.format(exMess, source);
139             throw new ArchiveException(e, exMess);
140         }
141     }
142
143     private void addWarClasses() throws ArchiveException {
144         File JavaDoc[] files = getWarPlan().getClassFileArray();
145
146         for (int i = 0; i < files.length; i++) {
147             addWarClassesFile(files[i]);
148         }
149     }
150
151     private void addWarClassesFile(File JavaDoc source) throws ArchiveException {
152         StringBuffer JavaDoc entryPath = new StringBuffer JavaDoc();
153         String JavaDoc classRoot = null;
154         String JavaDoc relative = null;
155
156         //
157
classRoot = getWarPlan().getClassRoot();
158         relative = source.getAbsolutePath().substring(classRoot.length() + 1);
159         relative = relative.replace('\\', '/');
160
161         //
162
entryPath.append(WEB_INF);
163         entryPath.append('/');
164         entryPath.append(CLASSES);
165         entryPath.append('/');
166         entryPath.append(relative);
167
168         //
169
try {
170             createEntry(source, entryPath.toString());
171         } catch (IOException JavaDoc e) {
172             throw new ArchiveException(e,
173                                        ResUtil.format(res.getString("WarBuilder_Unable_to1"),
174                                                       source));
175         }
176     }
177
178 }
179
Popular Tags