Source Code Cross Referenced for Backup.java in  » Content-Management-System » openedit » com » openedit » modules » update » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » openedit » com.openedit.modules.update 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on May 17, 2006
003:         */
004:        package com.openedit.modules.update;
005:
006:        import java.io.File;
007:        import java.io.FileOutputStream;
008:        import java.io.FilenameFilter;
009:        import java.io.IOException;
010:        import java.text.SimpleDateFormat;
011:        import java.util.ArrayList;
012:        import java.util.Collections;
013:        import java.util.Date;
014:        import java.util.Iterator;
015:        import java.util.List;
016:
017:        import org.apache.commons.logging.Log;
018:        import org.apache.commons.logging.LogFactory;
019:
020:        import com.openedit.OpenEditException;
021:        import com.openedit.page.manage.PageManager;
022:        import com.openedit.util.FileUtils;
023:        import com.openedit.util.PageZipUtil;
024:        import com.openedit.util.ZipUtil;
025:
026:        public class Backup {
027:            protected PageManager fieldPageManager;
028:            protected SimpleDateFormat fieldFormat = new SimpleDateFormat(
029:                    "yyyy_MM_dd_HH_mm_ss");
030:            protected File fieldRoot;
031:            protected String fieldIncludePath = "/";
032:            protected FileUtils fieldUtils = new FileUtils();
033:            protected List fieldExcludes = new ArrayList();
034:            private static final Log log = LogFactory.getLog(Backup.class);
035:
036:            public PageManager getPageManager() {
037:                return fieldPageManager;
038:            }
039:
040:            public void setPageManager(PageManager inPageManager) {
041:                fieldPageManager = inPageManager;
042:            }
043:
044:            protected File backupCurrentSite(String inName)
045:                    throws OpenEditException {
046:                PageZipUtil zip = new PageZipUtil(getPageManager());
047:                zip.setRoot(getRoot());
048:                for (Iterator iter = getExcludes().iterator(); iter.hasNext();) {
049:                    String exclude = (String) iter.next();
050:                    zip.addExclude(exclude);
051:                }
052:                //		zip.addExclude("*/WEB-INF/*");
053:                //		zip.addExclude("*/.versions/*");
054:                //		zip.addExclude("*/WEB-INF/trash/*");
055:                //		zip.addExclude("*/WEB-INF/tmp/*");
056:                //		zip.addExclude("*/WEB-INF/log*");
057:
058:                inName = inName.replace(" ", "_");
059:                inName = inName.replace("/", "_");
060:                inName = inName.replace("\\", "_");
061:
062:                String id = fieldFormat.format(new Date()) + "_" + inName;
063:                String outpath = "/WEB-INF/versions/" + id + ".zip";
064:                zip.addExclude(outpath);
065:                File out = new File(getRoot(), outpath);
066:                log.info("Backing up " + out);
067:                try {
068:                    out.getParentFile().mkdirs();
069:                    FileOutputStream stream = new FileOutputStream(out);
070:                    try {
071:                        zip.zipFile(getIncludePath(), stream);
072:                    } finally {
073:                        FileUtils.safeClose(stream);
074:                    }
075:                    return out;
076:                } catch (IOException ex) {
077:                    throw new OpenEditException(ex);
078:                }
079:            }
080:
081:            public File getRoot() {
082:                return fieldRoot;
083:            }
084:
085:            public void setRoot(File inRoot) {
086:                fieldRoot = inRoot;
087:            }
088:
089:            public List listSiteVersions() {
090:                File verdir = new File(getRoot(), "WEB-INF/versions");
091:                verdir.mkdirs();
092:
093:                File[] children = verdir.listFiles(new FilenameFilter() {
094:                    public boolean accept(File inDir, String inName) {
095:                        return inName.endsWith(".zip");
096:                    }
097:                });
098:                List list = new ArrayList();
099:                if (children != null) {
100:                    for (int i = 0; i < children.length; i++) {
101:                        File child = children[i];
102:                        list.add(child);
103:                    }
104:                }
105:                Collections.sort(list);
106:                return list;
107:            }
108:
109:            public File loadVersion(String inName) {
110:                List list = listSiteVersions();
111:                for (Iterator iter = list.iterator(); iter.hasNext();) {
112:                    File version = (File) iter.next();
113:                    if (version.getName().equals(inName)) {
114:                        return version;
115:                    }
116:                }
117:                return null;
118:            }
119:
120:            protected void restoreBackup(File inVersion)
121:                    throws OpenEditException {
122:                log.info("Restoring " + inVersion.getName());
123:                File tmp = null;
124:                try {
125:                    tmp = new File(getRoot(), "WEB-INF/trash/new"
126:                            + fieldFormat.format(new Date()));
127:                    tmp.mkdirs();
128:
129:                    ZipUtil utils = new ZipUtil();
130:                    //unzip the zip file in a tmp directory
131:                    utils.unzip(inVersion, tmp);
132:                } catch (IOException ex) {
133:                    throw new OpenEditException("No harm done", ex);
134:                }
135:                File old = new File(getRoot(), "WEB-INF/trash/old"
136:                        + fieldFormat.format(new Date()));
137:
138:                try {
139:                    replaceDirectories(tmp, getRoot(), old);
140:                } catch (IOException ex) {
141:                    throw new OpenEditException(ex);
142:                }
143:
144:                //		try
145:                //		{
146:                //		File tmpold = null;
147:                //		}
148:                //		finally
149:                //		{
150:                //			if( !tmp.renameTo(getRoot()) ) 
151:                //			{
152:                //				//copy it
153:                //				try
154:                //				{
155:                //					log.error("Could not rename");
156:                //					//org.apache.commons.io.FileUtils.copyFile(tmp, getRoot(), true);
157:                //					new FileUtils().copyFiles(tmp, getRoot() );
158:                //				}
159:                //				catch ( IOException ex)
160:                //				{
161:                //					throw new OpenEditException(ex);
162:                //				}
163:                //			}
164:                //			//bring back the versions
165:                //			File versions = new File( tmpold , "WEB-INF/versions");
166:                //			File newversions = new File( getRoot(), "WEB-INF/versions");
167:                //			versions.renameTo(newversions);
168:                //		}
169:            }
170:
171:            /**
172:             * This method does a replacement of top level directories. 
173:             * One exception is the WEB-INF directory that it will go into
174:             * @param inNewDirs
175:             * @param inRoot
176:             * @param inSubPath
177:             * @param inOldDirectory
178:             * @throws IOException
179:             */
180:            protected void replaceDirectories(File inNewDirs, File inRoot,
181:                    File inOldDirectory) throws IOException {
182:                //move the existing content to tmp2
183:                //		tmpold = File.createTempFile("upgradeold", "");
184:                //		tmpold.delete();
185:                //		tmpold.mkdirs();
186:                File[] children = inNewDirs.listFiles();
187:                for (int i = 0; i < children.length; i++) {
188:                    File child = children[i];
189:                    File existing = new File(inRoot, child.getName());
190:                    if (existing.exists()) {
191:                        //Then move it into away
192:
193:                        if (child.getName().equals("WEB-INF")) {
194:                            //replaceDirectories(child, existing, inOldDirectory);
195:                            continue;
196:                        } else {
197:                            if (existing.isDirectory()) {
198:                                fieldUtils.move(existing, new File(
199:                                        inOldDirectory, existing.getName()));
200:                            } else {
201:                                //this is an existing file in the inNewDirs directory
202:                                File backup = new File(inOldDirectory, child
203:                                        .getName());
204:                                if (!existing.renameTo(backup)) {
205:                                    throw new IOException("Could not move "
206:                                            + existing.getPath() + " to "
207:                                            + backup.getPath());
208:                                }
209:                            }
210:                        }
211:                    }
212:                    //Now replace it
213:                    fieldUtils
214:                            .move(child, new File(getRoot(), child.getName()));
215:                    //child.renameTo(new File( getRoot(), child.getName() ));
216:
217:                }
218:                //TODO: Do this in smaller parts so we can exclude WEB-INF/logs/
219:                //		if ( !getRoot().renameTo(tmpold) )
220:                //		{
221:                //			log.info("Had to copy manually");
222:                //			FileUtils fu = new FileUtils();
223:                //			fu.copyFiles(getRoot(), tmpold );
224:                //			fu.deleteAll(getRoot());
225:                //		}
226:                //		File[] all = tmpold.listFiles();
227:                //		if ( all.length == 0)
228:                //		{
229:                //			throw new OpenEditException("Problem: Could not move entire existing site. Some files moved to: " + tmpold.getPath());
230:                //		}
231:
232:            }
233:
234:            public String getIncludePath() {
235:                return fieldIncludePath;
236:            }
237:
238:            public void setIncludePath(String inIncludePath) {
239:                fieldIncludePath = inIncludePath;
240:            }
241:
242:            public List getExcludes() {
243:                if (fieldExcludes == null) {
244:                    fieldExcludes = new ArrayList();
245:                }
246:                return fieldExcludes;
247:            }
248:
249:            public void setExcludes(List inExcludes) {
250:                this .fieldExcludes = inExcludes;
251:            }
252:
253:            public void addExclude(String inExclude) {
254:                getExcludes().add(inExclude);
255:            }
256:
257:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.