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.util.Hashtable;
24  import java.util.Observable;
25  
26  import org.apache.log4j.Logger;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  import org.w3c.dom.Node;
30  import org.w3c.dom.NodeList;
31  
32  import base.IXMLSaveable;
33  
34  public class Repository extends Observable implements IXMLSaveable {
35  
36  	private static final transient Logger logger = Logger
37  			.getLogger(Repository.class.getName());
38  
39  	/*** The repositorys' available space in Mega Bytes.* */
40  	private long declaredAvailableSpace;
41  
42  	/*** The repositorys' physical location, a folder on the file system.* */
43  	private File location;
44  
45  	/*** The repositorys' backup collection.* */
46  	private Hashtable<String, BackupDescriptor> backup = new Hashtable<String, BackupDescriptor>();
47  
48  	/***
49  	 * This is the default constructor.
50  	 * 
51  	 * @param location
52  	 *            The location on filesytem associated to the JDBS local
53  	 *            repository.
54  	 * @param declaredAvailableSpace
55  	 *            The declared repository's available space.
56  	 */
57  	public Repository(File location, long declaredAvailableSpace) {
58  		this.location = location;
59  		this.declaredAvailableSpace = declaredAvailableSpace;
60  	}
61  
62  	/***
63  	 * This is the xml repository constructor.
64  	 * 
65  	 * @param document
66  	 *            The document where the repository data has been stored.
67  	 */
68  	public Repository(Document document) {
69  		NodeList nodeList = document.getElementsByTagName("Repository");
70  		for (int i = 0; i < nodeList.item(0).getChildNodes().getLength(); i++) {
71  			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
72  					"Location"))
73  				this.location = new File(nodeList.item(0).getChildNodes().item(
74  						i).getAttributes().getNamedItem("value").getNodeValue()
75  						.toString());
76  			if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
77  					"DeclaredAvailableSpace"))
78  				this.declaredAvailableSpace = new Integer(nodeList.item(0)
79  						.getChildNodes().item(i).getAttributes().getNamedItem(
80  								"value").getNodeValue().toString());
81  		}
82  	}
83  
84  	/***
85  	 * This method creates an index of backups building it from this
86  	 * repository's location.
87  	 */
88  	public void indexRepository() {
89  		this.setChanged();
90  		// We must clear the backups indexed so far.
91  		this.backup.clear();
92  		logger.debug("Indexing the repository: " + location);
93  		if (location.exists() && location.isDirectory()) {
94  			File[] artifact = location.listFiles();
95  			for (int i = 0; i < artifact.length; i++) {
96  				logger.debug("Item: " + artifact[i] + " isDirectory: "
97  						+ artifact[i].isDirectory());
98  				if (artifact[i].isDirectory()) {
99  					logger.debug("Found: " + artifact[i]);
100 					BackupDescriptor backupDescriptor = new BackupDescriptor(
101 							artifact[i]);
102 					this.addBackupArtifact(backupDescriptor);
103 				}
104 			}
105 		}
106 		this.notifyObservers();
107 	}
108 
109 	protected String createArtifactKey(BackupDescriptor backupDescriptor) {
110 		return backupDescriptor.getArtifactDirectory().toString();
111 	}
112 
113 	/***
114 	 * @return Returns the declaredAvailableSpace.
115 	 */
116 	public long getDeclaredAvailableSpace() {
117 		return declaredAvailableSpace;
118 	}
119 
120 	/***
121 	 * @return Returns the indexed backup descriptors..
122 	 */
123 	public BackupDescriptor[] getBackupDescriptor() {
124 		return backup.values().toArray(new BackupDescriptor[0]);
125 	}
126 
127 	/***
128 	 * @return Returns the location.
129 	 */
130 	public File getLocation() {
131 		return location;
132 	}
133 
134 	private void addBackupArtifact(BackupDescriptor backupDescriptor) {
135 		this.setChanged();
136 		this.backup.put(createArtifactKey(backupDescriptor), backupDescriptor);
137 		this.notifyObservers();
138 	}
139 
140 	/*
141 	 * (non-Javadoc)
142 	 * 
143 	 * @see base.IXMLSaveable#toXml(org.w3c.dom.Document)
144 	 */
145 	public Node toXml(Document document) {
146 		Element repositoryElement = document.createElement("Repository");
147 
148 		Element locationElement = document.createElement("Location");
149 		locationElement.setAttribute("value", this.location.toString());
150 		repositoryElement.appendChild(locationElement);
151 
152 		Element availableSpaceElement = document
153 				.createElement("DeclaredAvailableSpace");
154 		availableSpaceElement.setAttribute("value", ""
155 				+ this.declaredAvailableSpace);
156 		repositoryElement.appendChild(availableSpaceElement);
157 
158 		return repositoryElement;
159 	}
160 
161 	/***
162 	 * @param declaredAvailableSpace
163 	 *            The declaredAvailableSpace to set.
164 	 */
165 	public void setDeclaredAvailableSpace(long declaredAvailableSpace) {
166 		this.declaredAvailableSpace = declaredAvailableSpace;
167 	}
168 
169 	/***
170 	 * @param location
171 	 *            The location to set.
172 	 */
173 	public void setLocation(File location) {
174 		this.location = location;
175 	}
176 
177 	public void removeBackupDescriptor(BackupDescriptor backupDescriptor) {
178 		this.setChanged();
179 		this.backup.remove(createArtifactKey(backupDescriptor));
180 		this.notifyObservers();
181 
182 	}
183 
184 	/***
185 	 * This method retrieves a specified BackupDescriptor from a specified guId.
186 	 * 
187 	 * @param guId
188 	 *            The guId of the BackupDescriptor to retrieve.
189 	 * @return The BackupDescriptor whose global unique identifier is guId, null
190 	 *         if there isn't a BackupDescriptor that matched the specified
191 	 *         guId.
192 	 */
193 	public BackupDescriptor getBackupDescriptorByGuId(String guId) {
194 		for (int i = 0; i < this.getBackupDescriptor().length; i++) {
195 			if (this.getBackupDescriptor()[i].getBackup().getGuId()
196 					.equals(guId))
197 				return this.getBackupDescriptor()[i];
198 		}
199 		return null;
200 	}
201 
202 }