001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.tool.archive;
024:
025: // ToolBox imports
026: import org.enhydra.tool.common.ResUtil;
027: import org.enhydra.tool.archive.xml.WarDescriptorHandler;
028:
029: // Standard imports
030: import java.io.IOException;
031: import java.io.File;
032: import java.util.ResourceBundle;
033: import java.util.jar.Manifest;
034: import java.util.jar.Attributes;
035:
036: //
037: public class WarBuilder extends JarBuilder {
038:
039: // not to be resourced
040: private final String WEB_INF = "WEB-INF"; // nores
041: private final String CLASSES = "classes"; // nores
042:
043: //
044: public WarBuilder() {
045: super ();
046: }
047:
048: public WarPlan getWarPlan() {
049: WarPlan wp = null;
050:
051: if (getPlan() instanceof WarPlan) {
052: wp = (WarPlan) getPlan();
053: }
054: return wp;
055: }
056:
057: // override JarBuilder
058: public File buildArchive() throws ArchiveException {
059: File war = null;
060: String root = null;
061: File[] files = new File[0];
062: Descriptor[] dd = new Descriptor[0];
063:
064: war = openJarStream(getWarPlan().getArchivePath());
065: root = getWarPlan().getContentRoot();
066: dd = getWarPlan().getDescriptors();
067: files = getWarPlan().getContentFileArray();
068: addWarClasses();
069: addLibraries();
070: addContent();
071: for (int i = 0; i < dd.length; i++) {
072: addDescriptor(dd[i], root, files);
073: }
074: closeJarStream();
075: return war;
076: }
077:
078: protected Manifest buildManifest(Manifest m) {
079: Manifest manifest = super .buildManifest(m);
080:
081: manifest.getMainAttributes().put(
082: new Attributes.Name(CONTENT_ROOT),
083: getWarPlan().getContentRoot());
084: return manifest;
085: }
086:
087: private void addContent() throws ArchiveException {
088: File[] files = getWarPlan().getContentFileArray();
089:
090: for (int i = 0; i < files.length; i++) {
091: addContentFile(files[i]);
092: }
093: }
094:
095: private void addContentFile(File source) throws ArchiveException {
096: StringBuffer entryPath = new StringBuffer();
097: String contentRoot = null;
098: String relative = null;
099:
100: contentRoot = getWarPlan().getContentRoot();
101: relative = source.getAbsolutePath().substring(
102: contentRoot.length() + 1);
103: relative = relative.replace('\\', '/');
104: entryPath.append(relative);
105: try {
106: createEntry(source, entryPath.toString());
107: } catch (IOException e) {
108: throw new ArchiveException(e, ResUtil.format(res
109: .getString("WarBuilder_Unable_to"), source));
110: }
111: }
112:
113: private void addLibraries() throws ArchiveException {
114: File[] files = getWarPlan().getLibFileArray();
115:
116: for (int i = 0; i < files.length; i++) {
117: addLibFile(files[i]);
118: }
119: }
120:
121: private void addLibFile(File source) throws ArchiveException {
122: StringBuffer entryPath = new StringBuffer();
123:
124: //
125: entryPath.append("WEB-INF");
126: entryPath.append('/');
127: entryPath.append("lib");
128: entryPath.append('/');
129: entryPath.append(source.getName());
130:
131: //
132: try {
133: createEntry(source, entryPath.toString());
134: } catch (IOException e) {
135: String exMess = res.getString("WarBuilder_Unable_to");
136:
137: exMess = ResUtil.format(exMess, source);
138: throw new ArchiveException(e, exMess);
139: }
140: }
141:
142: private void addWarClasses() throws ArchiveException {
143: File[] files = getWarPlan().getClassFileArray();
144:
145: for (int i = 0; i < files.length; i++) {
146: addWarClassesFile(files[i]);
147: }
148: }
149:
150: private void addWarClassesFile(File source) throws ArchiveException {
151: StringBuffer entryPath = new StringBuffer();
152: String classRoot = null;
153: String relative = null;
154:
155: //
156: classRoot = getWarPlan().getClassRoot();
157: relative = source.getAbsolutePath().substring(
158: 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 e) {
172: throw new ArchiveException(e, ResUtil.format(res
173: .getString("WarBuilder_Unable_to1"), source));
174: }
175: }
176:
177: }
|