KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > WebManPublish


1 import java.io.*;
2 import java.util.*;
3
4 public class WebManPublish
5 {
6     public static final String JavaDoc IS_DYNAMIC_ONLINE = "intershop.enfinity.ecs.dynamic.online.directory";
7     public static final String JavaDoc IS_DYNAMIC_PREVIEW = "intershop.enfinity.ecs.dynamic.preview.directory";
8     public static final String JavaDoc IS_STATIC_ONLINE = "intershop.enfinity.ecs.static.online.directory";
9     public static final String JavaDoc IS_STATIC_PREVIEW = "intershop.enfinity.ecs.static.preview.directory";
10
11     public static final String JavaDoc WM_GENERATOR_OUTPUT = "webman.generator.output";
12     public static final String JavaDoc _SOURCE = "source";
13     public static final String JavaDoc _DYNAMIC = "dynamic";
14     public static final String JavaDoc _STATIC = "static";
15
16     private static int CHUNK_SIZE = 512;
17
18     public WebManPublish ()
19     {
20         // NOP
21
}
22
23     public final synchronized void publish (Properties properties)
24         throws Exception JavaDoc
25     {
26         publish(properties, true);
27     }
28
29     public final synchronized void publish (Properties properties, boolean preview)
30         throws Exception JavaDoc
31     {
32         // Get target directories.
33
File dynamicTarget = new File(preview ? properties.getProperty(IS_DYNAMIC_PREVIEW) : properties.getProperty(IS_DYNAMIC_ONLINE));
34         File staticTarget = new File(preview ? properties.getProperty(IS_STATIC_PREVIEW) : properties.getProperty(IS_STATIC_ONLINE));
35
36         // Clean targets.
37
if (dynamicTarget.exists())
38         {
39             delete(dynamicTarget);
40         }
41
42         if (staticTarget.exists())
43         {
44             delete(staticTarget);
45         }
46
47         // Get number of generator directories.
48
int output = Integer.parseInt(properties.getProperty(WM_GENERATOR_OUTPUT, "0"));
49
50         for (int i = 0; i < output; i++)
51         {
52             // Get sources.
53
String JavaDoc sources = properties.getProperty(WM_GENERATOR_OUTPUT + '.' + i + '.' + _SOURCE).trim();
54             String JavaDoc types = null;
55
56             // Get dynamic types.
57
types = properties.getProperty(WM_GENERATOR_OUTPUT + '.' + i + '.' + _DYNAMIC);
58
59             if (types != null && types.trim().length() > 0)
60             {
61                 // Publish i-th generator dynamic source.
62
copy(sources, dynamicTarget, getFileFilter(types));
63             }
64
65             // Get static types.
66
types = properties.getProperty(WM_GENERATOR_OUTPUT + '.' + i + '.' + _STATIC);
67
68             if (types != null && types.trim().length() > 0)
69             {
70                 // Publish i-th generator static source.
71
copy(sources, staticTarget, getFileFilter(types));
72             }
73         }
74     }
75
76     private void copy (String JavaDoc sources, File target, FileFilter filter)
77         throws Exception JavaDoc
78     {
79         if (sources.endsWith("*"))
80         {
81             File[] list = (new File(sources.substring(0, sources.length() - 2))).listFiles(filter);
82
83             for (int i = 0; i < list.length; i++)
84             {
85                 copy(list[i], target, filter);
86             }
87         }
88         else
89         {
90             copy(new File(sources), target, filter);
91         }
92     }
93
94     private void copy (File source, File target, FileFilter filter)
95         throws Exception JavaDoc
96     {
97         if (! source.exists())
98         {
99             throw new FileNotFoundException("File '" + source + "' doesn't exist.");
100         }
101
102         if (source.isDirectory())
103         {
104             if (target.exists() && ! target.isDirectory())
105             {
106                 throw new IOException("Can't copy directory into file.");
107             }
108
109             target = new File(target, source.getName());
110             target.mkdirs();
111
112             int index = 0;
113             File file = null;
114             File[] list = source.listFiles(filter);
115
116             while (index < list.length)
117             {
118                 file = list[index++];
119                 copy(file, target, filter);
120             }
121         }
122         else
123         {
124             if (target.exists() && target.isDirectory())
125             {
126                 target = new File(target, source.getName());
127             }
128
129             byte[] chunk = new byte[CHUNK_SIZE];
130             int read = 0;
131             FileInputStream in = new FileInputStream(source);
132             FileOutputStream out = new FileOutputStream(target);
133
134             while (true)
135             {
136                 read = in.read(chunk);
137
138                 if (read > 0)
139                 {
140                     out.write(chunk, 0, read);
141                     out.flush();
142                 }
143                 else
144                 {
145                     break;
146                 }
147             }
148
149             in.close();
150             out.close();
151         }
152     }
153
154     private void delete (String JavaDoc sources)
155         throws Exception JavaDoc
156     {
157         if (sources.endsWith("*"))
158         {
159             File[] list = (new File(sources.substring(0, sources.length() - 2))).listFiles();
160
161             for (int i = 0; i < list.length; i++)
162             {
163                 delete(list[i]);
164             }
165         }
166         else
167         {
168             delete(new File(sources));
169         }
170     }
171
172     private void delete (File source)
173         throws Exception JavaDoc
174     {
175         if (! source.exists())
176         {
177             throw new FileNotFoundException("File '" + source + "' doesn't exist.");
178         }
179
180         if (source.isDirectory())
181         {
182             int index = 0;
183             File[] list = source.listFiles();
184
185             while (index < list.length)
186             {
187                 delete(list[index++]);
188             }
189         }
190
191         source.delete();
192     }
193
194     private FileFilter getFileFilter (String JavaDoc types)
195     {
196         FileFilter filter = null;
197
198         if (! types.equals("*"))
199         {
200             int index = 0;
201             StringTokenizer tokens = new StringTokenizer(types);
202             String JavaDoc[] suffixes = new String JavaDoc[tokens.countTokens()];
203
204             while (tokens.hasMoreTokens())
205             {
206                 suffixes[index++] = tokens.nextToken();
207             }
208
209             filter = new WebManPublish.RecursiveFilter(suffixes);
210         }
211
212         return filter;
213     }
214
215     public class RecursiveFilter
216         implements FileFilter
217     {
218
219         private String JavaDoc[] suffixes = null;
220
221         public RecursiveFilter (String JavaDoc[] suffixes)
222         {
223             this.suffixes = suffixes;
224         }
225
226         public boolean accept (File file)
227         {
228             if (file.isDirectory())
229             {
230                 return file.listFiles(this).length > 0;
231             }
232
233             int index = 0;
234
235             while (index < suffixes.length)
236             {
237                 if (file.getName().endsWith(suffixes[index++]))
238                 {
239                     return true;
240                 }
241             }
242
243             return false;
244         }
245     }
246
247     public static void main (String JavaDoc[] args)
248     {
249         int code = 0;
250
251         if (args.length == 0 || args.length > 2)
252         {
253             usage();
254             System.exit(1);
255         }
256
257         try
258         {
259             // Load configuration.
260
Properties properties = new Properties();
261             properties.load(WebManPublish.class.getResourceAsStream(args[0]));
262
263             // Determine mode.
264
boolean mode = args.length == 2
265                                        ? args[1].equalsIgnoreCase("-preview")
266                                        : false;
267
268             // Create object and start publishing.
269
(new WebManPublish()).publish(properties, mode);
270         }
271         catch (Exception JavaDoc e)
272         {
273             code = -1;
274         }
275
276         System.exit(code);
277     }
278
279     public static final void usage ()
280     {
281         System.out.println("Usage: WebManPublish <properties> [-online|-preview]" +
282                            System.getProperty("line.separator") +
283                            " properties - name of property file" +
284                            System.getProperty("line.separator") +
285                            " online - publishes online (default)" +
286                            System.getProperty("line.separator") +
287                            " preview - publishes preview");
288     }
289
290 }
Popular Tags