KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > extra > StaticExporter


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2005 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.extra;
24
25 import java.io.*;
26 import java.net.*;
27 import javax.servlet.http.*;
28 import org.meshcms.core.*;
29 import org.meshcms.util.*;
30
31 /**
32  * Performs the export of a website in static files (for use with the Apache
33  * web server or any other one).
34  */

35 public class StaticExporter extends DirectoryParser {
36   public static final String JavaDoc REQUEST_ATTRIBUTE_CHECK = "meshcms-is-exporting";
37   public static final String JavaDoc USER_AGENT_HEADER = "User-Agent";
38   
39   public static final String JavaDoc USER_AGENT = WebSite.APP_NAME + ' ' +
40       WebSite.VERSION_ID + " (" +
41       System.getProperty("java.version") + ' ' +
42       System.getProperty("java.vendor") + ", " +
43       System.getProperty("os.name") + ' ' +
44       System.getProperty("os.version") + ' ' +
45       System.getProperty("os.arch") + ')';
46
47   File staticDir;
48   URL contextURL;
49   boolean checkDates = true;
50   Writer writer;
51   WebSite webSite;
52
53   /**
54    * Creates an instance.
55    *
56    * @param staticDir the directory where files should be exported
57    */

58   public StaticExporter(WebSite webSite, URL contextURL, File staticDir) {
59     super();
60     setProcessStartDir(true);
61     setRecursive(true);
62     setInitialDir(webSite.getRootFile());
63     setStaticDir(staticDir);
64     this.webSite = webSite;
65     this.contextURL = contextURL;
66   }
67
68   /**
69    * Sets the static directory.
70    */

71   public void setStaticDir(File staticDir) {
72     this.staticDir = staticDir;
73   }
74
75   /**
76    * Returns the static directory.
77    */

78   public File getStaticDir() {
79     return staticDir;
80   }
81
82   /**
83    * Sets the context URL.
84    */

85   public void setContextURL(URL contextURL) {
86     this.contextURL = contextURL;
87   }
88
89   /**
90    * Returns the context URL.
91    */

92   public URL getContextURL() {
93     return contextURL;
94   }
95
96   /**
97    * Sets the date check to on or off. If the date check is on, only newer
98    * files are copied. HTML are always recreated regardless of this option.
99    * Default is true (recommended).
100    */

101   public void setCheckDates(boolean checkDates) {
102     this.checkDates = checkDates;
103   }
104
105   /**
106    * Returns the value of the date check option.
107    */

108   public boolean getCheckDates() {
109     return checkDates;
110   }
111
112   /**
113    * Sets the writer for logging (usually the writer of the web page).
114    */

115   public void setWriter(Writer writer) {
116     this.writer = writer;
117   }
118
119   /**
120    * Returns the writer (if any).
121    */

122   public Writer getWriter() {
123     return writer;
124   }
125
126   protected void postProcess() {
127     MainWebSite mainWebSite = null;
128     
129     if (webSite instanceof VirtualWebSite) {
130       mainWebSite = ((VirtualWebSite) webSite).getMainWebSite();
131     }
132     
133     write("");
134     StaticExportCopier copier = new StaticExportCopier(staticDir);
135     copier.setInitialDir(initialDir);
136     copier.setWriter(writer);
137     copier.setCheckDates(checkDates);
138     copier.process();
139     
140     write("");
141     StaticExportCleaner cleaner = new StaticExportCleaner(initialDir);
142     cleaner.setInitialDir(staticDir);
143     cleaner.setWriter(writer);
144     
145     if (mainWebSite != null) {
146       cleaner.setProtectedPath(webSite.getAdminPath());
147     }
148     
149     cleaner.process();
150
151     if (mainWebSite != null) {
152       File adminStaticDir = webSite.getAdminPath().getFile(staticDir);
153       File adminDir = mainWebSite.getFile(mainWebSite.getAdminPath());
154
155       write("");
156       StaticExportCopier adminCopier = new StaticExportCopier(adminStaticDir);
157       adminCopier.setMkDirs(true);
158       adminCopier.setInitialDir(adminDir);
159       adminCopier.setWriter(writer);
160       adminCopier.setCheckDates(checkDates);
161       adminCopier.process();
162       
163       write("");
164       StaticExportCleaner adminCleaner = new StaticExportCleaner(adminDir);
165       adminCleaner.setInitialDir(adminStaticDir);
166       adminCleaner.setWriter(writer);
167       adminCleaner.process();
168     }
169   }
170
171   protected boolean preProcessDirectory(File file, Path path) {
172     File dir = path.getFile(staticDir);
173
174     if (isExportable(path)) {
175       dir.mkdirs();
176     } else {
177       new DirectoryRemover(dir).process();
178     }
179
180     return dir.isDirectory();
181   }
182
183   protected void processFile(File file, Path path) {
184     File staticFile = path.getFile(staticDir);
185     String JavaDoc fileName = file.getName();
186
187     try {
188       if (FileTypes.isPage(fileName)) {
189         long time = System.currentTimeMillis();
190         URL url = new URL(contextURL, path.toString());
191         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
192         connection.setRequestProperty(USER_AGENT_HEADER, USER_AGENT);
193         InputStream in = connection.getInputStream();
194         OutputStream out = new FileOutputStream(staticFile);
195         Utils.copyStream(in, out, true);
196         connection.disconnect();
197         write(path + " page generated in " + (System.currentTimeMillis() - time) + " ms");
198       }
199     } catch (IOException ex) {
200       ex.printStackTrace();
201     }
202   }
203
204   void write(String JavaDoc message) {
205     if (writer != null) {
206       try {
207         writer.write(message);
208         writer.write('\n');
209         writer.flush();
210       } catch (IOException ex) {}
211     }
212   }
213   
214   public boolean isExportable(Path path) {
215     if (path == null || path.isRelative()) {
216       return false;
217     }
218     
219     if (path.isRoot()) {
220       return true;
221     }
222
223     if (webSite.getAdminPath() == null ||
224         !(path.equals(webSite.getPrivatePath()) ||
225           path.equals(webSite.getVirtualSitesPath()))) {
226       String JavaDoc level1 = path.getElementAt(0).toLowerCase();
227       return !(level1.equals("web-inf") || level1.equals("meta-inf"));
228     }
229
230     return false;
231   }
232
233   public void process() {
234     // Avoid to store generated pages in cache
235
Configuration conf = webSite.getConfiguration();
236     int cacheType = conf.getCacheType();
237     conf.setCacheType(Configuration.NO_CACHE);
238     
239     write("base url: " + contextURL);
240
241     // Update the site map to make sure everything is up to date
242
long time = System.currentTimeMillis();
243     webSite.updateSiteMap(true);
244     write("site map updated in " + (System.currentTimeMillis() - time) + " ms");
245
246     write("");
247     super.process();
248     
249     // Restore cache
250
conf.setCacheType(cacheType);
251   }
252   
253   public static boolean isExportRequest(HttpServletRequest request) {
254     // return USER_AGENT.equals(request.getHeader("User-Agent"));
255
Boolean JavaDoc bo = (Boolean JavaDoc) request.getAttribute(REQUEST_ATTRIBUTE_CHECK);
256     
257     if (bo == null) {
258       String JavaDoc ua = Utils.noNull(request.getHeader(USER_AGENT_HEADER));
259       bo = Boolean.valueOf(ua.startsWith(WebSite.APP_NAME));
260       request.setAttribute(REQUEST_ATTRIBUTE_CHECK, bo);
261     }
262     
263     return bo.booleanValue();
264   }
265 }
266
Popular Tags