001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.j2ee.deployserver;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.types.RawString;
034: import com.caucho.util.L10N;
035: import com.caucho.vfs.Vfs;
036: import com.caucho.vfs.WriteStream;
037: import com.caucho.xml.XmlPrinter;
038:
039: import org.w3c.dom.Node;
040:
041: import javax.annotation.PostConstruct;
042: import java.io.IOException;
043: import java.io.OutputStream;
044: import java.util.ArrayList;
045:
046: /**
047: * Plan for the deployment.
048: */
049: public class DeploymentPlan {
050: private static final L10N L = new L10N(DeploymentPlan.class);
051:
052: private String _archiveType;
053: private String _name;
054: private String _metaInf;
055:
056: private ArrayList<PlanFile> _fileList = new ArrayList<PlanFile>();
057:
058: /**
059: * Sets the deployment type.
060: */
061: public void setArchiveType(String type) throws ConfigException {
062: if (type.equals("war")) {
063: _metaInf = "WEB-INF/";
064: } else if (type.equals("ear")) {
065: _metaInf = "META-INF/";
066: } else if (type.equals("rar")) {
067: _metaInf = "META-INF/";
068: } else
069: throw new ConfigException(L.l(
070: "'{0}' is an unknown archive type.", type));
071:
072: _archiveType = type;
073: }
074:
075: /**
076: * Gets the deployment type.
077: */
078: public String getArchiveType() {
079: return _archiveType;
080: }
081:
082: /**
083: * Sets the name
084: */
085: public void setName(String name) {
086: _name = name;
087: }
088:
089: /**
090: * Gets the name
091: */
092: public String getName() {
093: return _name;
094: }
095:
096: @PostConstruct
097: public void init() {
098: if (_archiveType == null)
099: throw new ConfigException(L.l("`{0}' is required",
100: "archive-type"));
101:
102: if (_name == null)
103: throw new ConfigException(L.l("`{0}' is required", "name"));
104: }
105:
106: /**
107: * An ExtFile is an Xml file that is written into the META-INF
108: * (or WEB-INF for a war) * directory.
109: */
110: public ExtFile createExtFile() {
111: return new ExtFile();
112: }
113:
114: public void addExtFile(ExtFile extFile) {
115: _fileList.add(extFile);
116: }
117:
118: /**
119: * A RawFile is any format file that is written into a file
120: * specified by the path, relative to the root of the deployed archive.
121: */
122: public RawFile createRawFile() {
123: return new RawFile();
124: }
125:
126: public void addRawFile(RawFile rawFile) {
127: _fileList.add(rawFile);
128: }
129:
130: /**
131: * Returns the list of ext and raw files.
132: */
133: public ArrayList<PlanFile> getFileList() {
134: return _fileList;
135: }
136:
137: abstract public class PlanFile {
138: abstract public String getPath();
139:
140: abstract public void writeToStream(OutputStream os)
141: throws IOException;
142:
143: public String toString() {
144: return "DeploymentPlan$" + getClass().getSimpleName() + "["
145: + getPath() + "]";
146: }
147:
148: }
149:
150: public class ExtFile extends PlanFile {
151: private String _name;
152: private Node _data;
153:
154: /**
155: * Sets the file name.
156: */
157: public void setName(String name) {
158: if (name.startsWith("/"))
159: throw new ConfigException(L.l(
160: "name `{0}' cannot start with /", name));
161:
162: _name = name;
163: }
164:
165: public void setData(Node data) {
166: _data = data.getFirstChild();
167: }
168:
169: @PostConstruct
170: public void init() {
171: if (_name == null)
172: throw new ConfigException(L.l("`{0}' is required",
173: "name"));
174:
175: if (_data == null)
176: throw new ConfigException(L.l("`{0}' is required",
177: "data"));
178: }
179:
180: public String getPath() {
181: return _metaInf + _name;
182: }
183:
184: public void writeToStream(OutputStream os) throws IOException {
185: XmlPrinter xmlPrinter = new XmlPrinter(os);
186: xmlPrinter.setPretty(true);
187: xmlPrinter.printXml(_data);
188: }
189: }
190:
191: public class RawFile extends PlanFile {
192: private String _path;
193: private String _data;
194:
195: /**
196: * Sets the file name.
197: */
198: public void setPath(String path) {
199: if (path.startsWith("/"))
200: throw new ConfigException(L.l(
201: "path `{0}' cannot start with /", path));
202:
203: _path = path;
204: }
205:
206: public void setData(RawString data) {
207: _data = data.getValue();
208: }
209:
210: @PostConstruct
211: public void init() {
212: if (_path == null)
213: throw new ConfigException(L.l("`{0}' is required",
214: "path"));
215:
216: if (_data == null)
217: throw new ConfigException(L.l("`{0}' is required",
218: "data"));
219: }
220:
221: public String getPath() {
222: return _path;
223: }
224:
225: public void writeToStream(OutputStream os) throws IOException {
226: WriteStream writeStream = Vfs.openWrite(os);
227:
228: try {
229: writeStream.print(_data);
230: } finally {
231: writeStream.close();
232: }
233: }
234: }
235: }
|