View Javadoc

1   /*
2   MadCaching is a Tile Caching solution
3   Copyright (C) 2005  Mac Vu
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  package com.macvu.tiles.actions;
20  
21  import com.macvu.tiles.cache.CacheObjectWrapper;
22  import com.macvu.tiles.xmlDefinition.CacheDefinitionSaveFactory;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.struts.action.Action;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  
30  import javax.servlet.ServletContext;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /***
37   * User: MVu
38   */
39  public class SaveCacheTilesDefinitionAction extends Action {
40      protected static Log log = LogFactory.getLog(SaveCacheTilesDefinitionAction.class);
41  
42      public ActionForward execute(ActionMapping mapping,
43                                   ActionForm form,
44                                   HttpServletRequest request,
45                                   HttpServletResponse response)
46              throws Exception {
47  
48  
49          try {
50              List fileList = CacheObjectWrapper.getTileDefinitionFileList(getServlet().getServletContext());
51              Iterator fileItr = fileList.iterator();
52              while (fileItr.hasNext()) {
53                  String defFile = (String) fileItr.next();
54  
55                  String outFile = generateDefinitionFile(defFile, getServlet().getServletContext());
56                  if (outFile != null) {
57                      CacheDefinitionSaveFactory factory = new CacheDefinitionSaveFactory();
58  
59                      factory.initDefintionFiles(request, getServlet().getServletContext());
60                      factory.saveChangeToFile(defFile, outFile);
61  
62                      if (log.isDebugEnabled()) {
63                          log.debug("Saved " + defFile + " changes to " + outFile);
64                      }
65                  } else {
66                      log.error("Fail to save tile definition for invalid definition file: " + defFile);
67                  }
68              }
69          } catch (Exception e) {
70              log.error("Fail to save tile definition for invalid definition file: ", e);
71          }
72  
73          return mapping.findForward("success");
74      }
75  
76      /***
77       * Return null if the input file is invalid
78       *
79       * @param definitionFileName
80       * @param servletContext
81       * @return
82       */
83      protected String generateDefinitionFile(String definitionFileName, ServletContext servletContext) {
84          if (definitionFileName == null || definitionFileName.length() == 0) {
85              return null;
86          }
87  
88          String fullFileName = servletContext.getRealPath(definitionFileName);
89  
90          int index = fullFileName.lastIndexOf(".");
91          if (index < 0) {
92              return null;
93          }
94  
95          String newFileName = fullFileName.substring(0, index);
96          newFileName += "Save.xml";
97  
98          return newFileName;
99      }
100 }