View Javadoc

1   /*
2    * $Header: /usr/local/cvsroot/dev/madcache/src/com/macvu/tiles/xmlDefinition/CacheDefinitionSaveFactory.java,v 1.3 2004/04/18 00:27:44 macvu Exp $
3    * $Revision: 1.3 $
4    * $Date: 2004/04/18 00:27:44 $
5    *
6    * ====================================================================
7    *
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution, if
26   *    any, must include the following acknowlegement:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowlegement may appear in the software itself,
30   *    if and wherever such third-party acknowlegements normally appear.
31   *
32   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
33   *    Foundation" must not be used to endorse or promote products derived
34   *    from this software without prior written permission. For written
35   *    permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache"
38   *    nor may "Apache" appear in their names without prior written
39   *    permission of the Apache Group.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   *
60   */
61  package com.macvu.tiles.xmlDefinition;
62  import com.macvu.tiles.CacheAttribute;
63  import com.macvu.tiles.CacheComponentDefinition;
64  import com.macvu.tiles.CacheInformation;
65  import com.macvu.tiles.cache.CacheObjectWrapper;
66  import org.apache.struts.tiles.ComponentDefinition;
67  import org.apache.struts.tiles.DefinitionsFactoryException;
68  import org.apache.struts.tiles.DefinitionsUtil;
69  import org.jdom.Document;
70  import org.jdom.Element;
71  import org.jdom.JDOMException;
72  import org.jdom.input.SAXBuilder;
73  import org.jdom.output.Format;
74  import org.jdom.output.XMLOutputter;
75  import org.xml.sax.helpers.DefaultHandler;
76  
77  import javax.servlet.ServletContext;
78  import javax.servlet.http.HttpServletRequest;
79  import java.io.*;
80  import java.util.ArrayList;
81  import java.util.Iterator;
82  import java.util.List;
83  
84  /***
85   * Unfortunate that we have to do the saving in a separate Class from reading but we are hacking existing code in trying
86   * to get the functionality as close to the orginial as possible.
87   */
88  public class CacheDefinitionSaveFactory {
89      boolean initialized = false;
90      List filenames;
91  
92      HttpServletRequest request;
93      ServletContext servletContext;
94  
95      public CacheDefinitionSaveFactory() {
96          initialized = false;
97      }
98  
99      public void initDefintionFiles(HttpServletRequest request, ServletContext servletContext) {
100         this.request = request;
101         this.servletContext = servletContext;
102 
103         filenames = CacheObjectWrapper.getTileDefinitionFileList(servletContext);
104         if (filenames == null) {
105             throw new IllegalStateException("Tile definition has not been put into the servlet context yet.");
106         }
107 
108         initialized = true;
109     }
110 
111     public List getFilenames() {
112         if (!initialized) {
113             throw new IllegalStateException("call init definition files first");
114         }
115 
116         return filenames;
117     }
118 
119     public void setFilenames(List filenames) {
120         this.filenames = filenames;
121     }
122 
123     public void updateFile(String filename) {
124         saveChangeToFile(filename, filename);
125     }
126 
127     public void saveChangeToFile(String filename, String destination) {
128         if (!initialized) {
129             throw new IllegalStateException("call init definition files first");
130         }
131 
132         try {
133             FileInputStream ifileStream = new FileInputStream(servletContext.getRealPath(filename));
134             FileOutputStream ofileStream = new FileOutputStream(destination);
135 
136             saveChange(ifileStream, ofileStream);
137         } catch (FileNotFoundException e) {
138             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
139         }
140     }
141 
142     public void saveChange(InputStream instream, OutputStream ostream) {
143         if (!initialized) {
144             throw new IllegalStateException("call init definition files first");
145         }
146 
147         Document document;
148         SAXBuilder parser = new SAXBuilder();
149         parser.setValidation(false);
150         parser.setDTDHandler(new DefaultHandler());
151 
152         try {
153             document = parser.build(instream);
154 
155             Element root = document.getRootElement();
156             List definitionList = root.getChildren();
157             Iterator definitionItr = definitionList.iterator();
158             while (definitionItr.hasNext()) {
159                 Element definitionElement = (Element) definitionItr.next();
160                 updateDefinitionElement(definitionElement);
161             }
162 
163             ObjectOutputStream oStream = new ObjectOutputStream(ostream);
164 
165             XMLOutputter writer = new XMLOutputter(Format.getPrettyFormat());
166             writer.output(document, oStream);
167         } catch (JDOMException e) {
168             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
169         } catch (IOException e) {
170             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
171         }
172 
173     }
174 
175     private void updateDefinitionElement(Element definitionElement) {
176         String tileName = definitionElement.getAttributeValue("name");
177 
178         ComponentDefinition definition;
179 
180         try {
181             definition = DefinitionsUtil.getDefinition(tileName, request, servletContext);
182             if (definition instanceof CacheComponentDefinition) {
183                 CacheInformation cacheInfo = ((CacheComponentDefinition) definition).getCacheInformation();
184                 if (cacheInfo == null || cacheInfo.isDefault()) {
185                     definitionElement.removeChildren("cacheInformation");
186                 } else {
187                     Element cacheInformation = definitionElement.getChild("cacheInformation");
188                     if (cacheInformation == null) {
189                         cacheInformation = new Element("cacheInformation");
190                         // definitionElement.getChildren().add(cacheInfo);
191                     }
192 
193                     cacheInformation.setAttribute("cacheEnabled", String.valueOf(cacheInfo.getCacheEnabled()));
194                     cacheInformation.setAttribute("repositoryName", cacheInfo.getRepositoryName());
195                     cacheInformation.setAttribute("repositoryFactory", cacheInfo.getRepositoryFactory());
196                     cacheInformation.setAttribute("keyFactory", cacheInfo.getKeyFactory());
197 
198                     addCacheKeysToCacheInformationElement(cacheInformation, cacheInfo.getCacheAttributes());
199 
200                     definitionElement.removeChild("cacheInformation");
201                     definitionElement.addContent(cacheInformation);
202                 }
203             }
204         } catch (DefinitionsFactoryException e) {
205             // Unable to find definition.  Do Nothing.
206         }
207     }
208 
209     private void addCacheKeysToCacheInformationElement(Element cacheInformationElement, List cacheAttributes) {
210         List list = new ArrayList();
211 
212         Iterator attIter = cacheAttributes.iterator();
213 
214         while (attIter.hasNext()) {
215             CacheAttribute attr = (CacheAttribute) attIter.next();
216             Element key = new Element("key");
217             key.setAttribute("name", attr.getName());
218             key.setAttribute("scope", attr.getScope());
219 
220             list.add(key);
221         }
222 
223         cacheInformationElement.setContent(list);
224     }
225 }
226