View Javadoc

1   /********************************************************************************
2    *InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base.jdbs;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  
29  import org.apache.log4j.Logger;
30  import org.w3c.dom.Document;
31  
32  import base.util.FileUtil;
33  
34  public class BackupDescriptor {
35  	
36  	private static final transient Logger logger = Logger.getLogger(BackupDescriptor.class.getName());
37  	
38  	private final File crc32ChecksumFile;
39  	private final File md5DigestFile;
40  	private final File md5SignedFile;
41  	private final File x509CertificateFile;
42  	private final File infoFile;
43  	private final File backupFile;
44  	private final File artifactDirectory;
45  	private Backup backup;
46  	
47  	/***
48  	 * This is the default constructor.
49  	 * @param artifactDirectory The directory associated to a backup.
50  	 */
51  	public BackupDescriptor(File artifactDirectory) {
52  		
53  		this.artifactDirectory = artifactDirectory;
54  		
55  		this.crc32ChecksumFile = new File(artifactDirectory,JDBSConstant.CRC32_FILE_NAME);
56  		if(!crc32ChecksumFile.exists())logger.error("CRC32Checksum file doesn't exists...");
57  		
58  		this.md5DigestFile = new File(artifactDirectory, JDBSConstant.MD5_FILE_NAME);
59  		if(!md5DigestFile.exists())logger.error("MD5Digest file doesn't exists...");
60  		
61  		this.md5SignedFile = new File(artifactDirectory, JDBSConstant.MD5_FILE_NAME+JDBSConstant.DOTTED_SIGNED_EXTENSION);
62  		if(!md5SignedFile.exists())logger.error("MD5Digest signed file doesn't exists...");
63  	
64  		this.x509CertificateFile = new File(artifactDirectory, JDBSConstant.CERTIFICATE_FILE_NAME);
65  		if(!x509CertificateFile.exists())logger.error("X509Certificate file doesn't exists...");
66  
67  		this.infoFile = new File(artifactDirectory, JDBSConstant.XML_INFO_FILE_NAME);
68  		if(!infoFile.exists())logger.error("Info file doesn't exists...");
69  		
70  		this.backupFile = new File(""+artifactDirectory+File.separatorChar+artifactDirectory.getName()+JDBSConstant.DOTTED_ZIP_EXTENSION);
71  		if(!backupFile.exists())logger.error("Backup file doesn't exists...");
72  		
73  		
74  		
75  	}
76  
77  
78  	/***
79  	 * @return Returns the backupFile.
80  	 */
81  	public File getBackupFile() {
82  		return backupFile;
83  	}
84  
85  
86  	/***
87  	 * @return Returns the crc32ChecksumFile.
88  	 */
89  	public File getCrc32ChecksumFile() {
90  		return crc32ChecksumFile;
91  	}
92  	
93  	public String getCrc32ChecksumValue(){
94  		try {
95  			return FileUtil.fileContent(getCrc32ChecksumFile());
96  		} catch (IOException e) {
97  			// TODO Auto-generated catch block
98  			e.printStackTrace();
99  		}
100 		return null;
101 	}
102 
103 
104 	/***
105 	 * @return Returns the md5DigestFile.
106 	 */
107 	public File getMd5DigestFile() {
108 		return md5DigestFile;
109 	}
110 	
111 	public String getMd5DigestValue(){
112 		try {
113 			return FileUtil.fileContent(getMd5DigestFile());
114 		} catch (IOException e) {
115 			// TODO Auto-generated catch block
116 			e.printStackTrace();
117 		}
118 		return null;
119 	}
120 
121 
122 	/***
123 	 * @return Returns the md5SignedFile.
124 	 */
125 	public File getMd5SignedFile() {
126 		return md5SignedFile;
127 	}
128 
129 
130 	/***
131 	 * @return Returns the x509CertificateFile.
132 	 */
133 	public File getX509CertificateFile() {
134 		return x509CertificateFile;
135 	}
136 	
137 	/***
138 	 * @return Returns the infoFile.
139 	 */
140 	public File getInfoFile() {
141 		return infoFile;
142 	}
143 	
144 	public Backup getBackup(){
145 		if(this.backup == null && this.infoFile.exists()){
146 			try{
147 				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
148 				factory.setIgnoringComments(true);
149 				factory.setValidating(false);
150 				factory.setIgnoringElementContentWhitespace(true);
151 				DocumentBuilder docBuilder = factory.newDocumentBuilder();
152 				Document document = docBuilder.parse(new FileInputStream(infoFile));
153 				this.backup = new Backup(document);
154 			}catch(Exception ex){
155 				ex.printStackTrace();
156 			}
157 		}
158 		return this.backup;
159 	}
160 
161 	
162 
163 
164 	/* (non-Javadoc)
165 	 * @see java.lang.Object#toString()
166 	 */
167 	@Override
168 	public String toString() {
169 		StringBuffer stringBuffer = new StringBuffer();
170 		stringBuffer.append("CRC32ChecksumFile: "+this.getCrc32ChecksumFile() + " size in Byte: " + FileUtil.fileSizeInByte(this.getCrc32ChecksumFile()));
171 		stringBuffer.append("\n");
172 		stringBuffer.append("MD5DigestFile: "+this.getMd5DigestFile() + " size in Byte: " + FileUtil.fileSizeInByte(this.getMd5DigestFile()));
173 		stringBuffer.append("\n");
174 		stringBuffer.append("MD5SignedFile: "+this.getMd5SignedFile() + " size in Byte: " + FileUtil.fileSizeInByte(this.getMd5SignedFile()));
175 		stringBuffer.append("\n");
176 		stringBuffer.append("X509CertificateFile: "+this.getX509CertificateFile() + " size in Byte: " + FileUtil.fileSizeInByte(this.getX509CertificateFile()));
177 		stringBuffer.append("\n");
178 		stringBuffer.append("InfoFile: "+this.getInfoFile() + " size in Byte: " + FileUtil.fileSizeInByte(this.getInfoFile()));
179 		stringBuffer.append("\n");
180 		stringBuffer.append("BackupFile: "+this.getBackupFile() + " size in Byte: " + FileUtil.fileSizeInByte(this.getBackupFile()));
181 		stringBuffer.append("\n");
182 		stringBuffer.append("Total size in Byte: " + FileUtil.fileSizeInByte(new File(this.getBackupFile().getParent())));
183 		
184 		return stringBuffer.toString();
185 	}
186 
187 
188 	/***
189 	 * @return Returns the artifactDirectory.
190 	 */
191 	public File getArtifactDirectory() {
192 		return artifactDirectory;
193 	}
194 
195 }