KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > j2ee > deployserver > DeploymentPlan


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.j2ee.deployserver;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.types.RawString;
34 import com.caucho.util.L10N;
35 import com.caucho.vfs.Vfs;
36 import com.caucho.vfs.WriteStream;
37 import com.caucho.xml.XmlPrinter;
38
39 import org.w3c.dom.Node JavaDoc;
40
41 import javax.annotation.PostConstruct;
42 import java.io.IOException JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 /**
47  * Plan for the deployment.
48  */

49 public class DeploymentPlan {
50   private static final L10N L = new L10N(DeploymentPlan.class);
51
52   private String JavaDoc _archiveType;
53   private String JavaDoc _name;
54   private String JavaDoc _metaInf;
55
56   private ArrayList JavaDoc<PlanFile> _fileList = new ArrayList JavaDoc<PlanFile>();
57
58   /**
59    * Sets the deployment type.
60    */

61   public void setArchiveType(String JavaDoc type)
62     throws ConfigException
63   {
64     if (type.equals("war")) {
65       _metaInf = "WEB-INF/";
66     }
67     else if (type.equals("ear")) {
68       _metaInf = "META-INF/";
69     }
70     else if (type.equals("rar")) {
71       _metaInf = "META-INF/";
72     }
73     else
74       throw new ConfigException(L.l("'{0}' is an unknown archive type.", type));
75
76     _archiveType = type;
77   }
78
79   /**
80    * Gets the deployment type.
81    */

82   public String JavaDoc getArchiveType()
83   {
84     return _archiveType;
85   }
86
87   /**
88    * Sets the name
89    */

90   public void setName(String JavaDoc name)
91   {
92     _name = name;
93   }
94
95   /**
96    * Gets the name
97    */

98   public String JavaDoc getName()
99   {
100     return _name;
101   }
102
103   @PostConstruct
104   public void init()
105   {
106     if (_archiveType == null)
107       throw new ConfigException(L.l("`{0}' is required", "archive-type"));
108
109     if (_name == null)
110       throw new ConfigException(L.l("`{0}' is required", "name"));
111   }
112
113   /**
114    * An ExtFile is an Xml file that is written into the META-INF
115    * (or WEB-INF for a war) * directory.
116    */

117   public ExtFile createExtFile()
118   {
119     return new ExtFile();
120   }
121
122   public void addExtFile(ExtFile extFile)
123   {
124     _fileList.add(extFile);
125   }
126
127
128   /**
129    * A RawFile is any format file that is written into a file
130    * specified by the path, relative to the root of the deployed archive.
131    */

132   public RawFile createRawFile()
133   {
134     return new RawFile();
135   }
136
137   public void addRawFile(RawFile rawFile)
138   {
139     _fileList.add(rawFile);
140   }
141
142   /**
143    * Returns the list of ext and raw files.
144    */

145   public ArrayList JavaDoc<PlanFile> getFileList()
146   {
147     return _fileList;
148   }
149
150   abstract public class PlanFile {
151     abstract public String JavaDoc getPath();
152     abstract public void writeToStream(OutputStream JavaDoc os)
153       throws IOException JavaDoc;
154     public String JavaDoc toString()
155     {
156       return "DeploymentPlan$" + getClass().getSimpleName() + "[" + getPath() + "]";
157     }
158
159   }
160
161   public class ExtFile
162     extends PlanFile
163   {
164     private String JavaDoc _name;
165     private Node JavaDoc _data;
166
167     /**
168      * Sets the file name.
169      */

170     public void setName(String JavaDoc name)
171     {
172       if (name.startsWith("/"))
173         throw new ConfigException(L.l("name `{0}' cannot start with /", name));
174
175       _name = name;
176     }
177
178     public void setData(Node JavaDoc data)
179     {
180       _data = data.getFirstChild();
181     }
182
183     @PostConstruct
184     public void init()
185     {
186       if (_name == null)
187         throw new ConfigException(L.l("`{0}' is required", "name"));
188
189       if (_data == null)
190         throw new ConfigException(L.l("`{0}' is required", "data"));
191     }
192
193     public String JavaDoc getPath()
194     {
195       return _metaInf + _name;
196     }
197
198     public void writeToStream(OutputStream JavaDoc os)
199       throws IOException JavaDoc
200     {
201       XmlPrinter xmlPrinter = new XmlPrinter(os);
202       xmlPrinter.setPretty(true);
203       xmlPrinter.printXml(_data);
204     }
205   }
206
207   public class RawFile
208     extends PlanFile
209   {
210     private String JavaDoc _path;
211     private String JavaDoc _data;
212
213     /**
214      * Sets the file name.
215      */

216     public void setPath(String JavaDoc path)
217     {
218       if (path.startsWith("/"))
219         throw new ConfigException(L.l("path `{0}' cannot start with /", path));
220
221       _path = path;
222     }
223
224     public void setData(RawString data)
225     {
226       _data = data.getValue();
227     }
228
229     @PostConstruct
230     public void init()
231     {
232       if (_path == null)
233         throw new ConfigException(L.l("`{0}' is required", "path"));
234
235       if (_data == null)
236         throw new ConfigException(L.l("`{0}' is required", "data"));
237     }
238
239     public String JavaDoc getPath()
240     {
241       return _path;
242     }
243
244     public void writeToStream(OutputStream JavaDoc os)
245       throws IOException JavaDoc
246     {
247       WriteStream writeStream = Vfs.openWrite(os);
248
249       try {
250         writeStream.print(_data);
251       }
252       finally {
253         writeStream.close();
254       }
255     }
256   }
257 }
258
259
Popular Tags