| Line | Hits | Source |
|---|---|---|
| 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 | 1 | |
| 36 | 0 | private static final transient Logger logger = Logger |
| 37 | 1 | .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 | 1 | |
| 45 | /** The repositorys' backup collection.* */ | |
| 46 | 0 | private Hashtable<String, BackupDescriptor> backup = new Hashtable<String, BackupDescriptor>(); |
| 47 | ||
| 48 | /** | |
| 49 | * This is the default constructor. | |
| 50 | * | |
| 51 | 1 | * @param location |
| 52 | 1 | * The location on filesytem associated to the JDBS local |
| 53 | 1 | * repository. |
| 54 | 1 | * @param declaredAvailableSpace |
| 55 | * The declared repository's available space. | |
| 56 | */ | |
| 57 | 0 | public Repository(File location, long declaredAvailableSpace) { |
| 58 | 0 | this.location = location; |
| 59 | 0 | this.declaredAvailableSpace = declaredAvailableSpace; |
| 60 | 0 | } |
| 61 | 0 | |
| 62 | 0 | /** |
| 63 | 0 | * This is the xml repository constructor. |
| 64 | 0 | * |
| 65 | 0 | * @param document |
| 66 | 0 | * The document where the repository data has been stored. |
| 67 | */ | |
| 68 | 0 | public Repository(Document document) { |
| 69 | 0 | NodeList nodeList = document.getElementsByTagName("Repository"); |
| 70 | 0 | for (int i = 0; i < nodeList.item(0).getChildNodes().getLength(); i++) { |
| 71 | 0 | if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals( |
| 72 | "Location")) | |
| 73 | 0 | this.location = new File(nodeList.item(0).getChildNodes().item( |
| 74 | 1 | i).getAttributes().getNamedItem("value").getNodeValue() |
| 75 | 1 | .toString()); |
| 76 | 1 | if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals( |
| 77 | 1 | "DeclaredAvailableSpace")) |
| 78 | 5 | this.declaredAvailableSpace = new Integer(nodeList.item(0) |
| 79 | 4 | .getChildNodes().item(i).getAttributes().getNamedItem( |
| 80 | 4 | "value").getNodeValue().toString()); |
| 81 | 2 | } |
| 82 | 2 | } |
| 83 | 2 | |
| 84 | 0 | /** |
| 85 | 0 | * This method creates an index of backups building it from this |
| 86 | * repository's location. | |
| 87 | 1 | */ |
| 88 | 1 | public void indexRepository() { |
| 89 | 0 | this.setChanged(); |
| 90 | 0 | // We must clear the backups indexed so far. |
| 91 | 2 | this.backup.clear(); |
| 92 | 0 | logger.debug("Indexing the repository: " + location); |
| 93 | 0 | if (location.exists() && location.isDirectory()) { |
| 94 | 0 | File[] artifact = location.listFiles(); |
| 95 | 0 | for (int i = 0; i < artifact.length; i++) { |
| 96 | 0 | logger.debug("Item: " + artifact[i] + " isDirectory: " |
| 97 | + artifact[i].isDirectory()); | |
| 98 | 0 | if (artifact[i].isDirectory()) { |
| 99 | 0 | logger.debug("Found: " + artifact[i]); |
| 100 | 0 | BackupDescriptor backupDescriptor = new BackupDescriptor( |
| 101 | artifact[i]); | |
| 102 | 0 | this.addBackupArtifact(backupDescriptor); |
| 103 | } | |
| 104 | } | |
| 105 | 2 | } |
| 106 | 0 | this.notifyObservers(); |
| 107 | 0 | } |
| 108 | ||
| 109 | protected String createArtifactKey(BackupDescriptor backupDescriptor) { | |
| 110 | 0 | return backupDescriptor.getArtifactDirectory().toString(); |
| 111 | } | |
| 112 | 0 | |
| 113 | /** | |
| 114 | 0 | * @return Returns the declaredAvailableSpace. |
| 115 | */ | |
| 116 | 2 | public long getDeclaredAvailableSpace() { |
| 117 | 2 | return declaredAvailableSpace; |
| 118 | 2 | } |
| 119 | 2 | |
| 120 | 0 | /** |
| 121 | 0 | * @return Returns the indexed backup descriptors.. |
| 122 | */ | |
| 123 | public BackupDescriptor[] getBackupDescriptor() { | |
| 124 | 0 | return backup.values().toArray(new BackupDescriptor[0]); |
| 125 | 0 | } |
| 126 | ||
| 127 | 0 | /** |
| 128 | 0 | * @return Returns the location. |
| 129 | 0 | */ |
| 130 | 0 | public File getLocation() { |
| 131 | 0 | return location; |
| 132 | 0 | } |
| 133 | 0 | |
| 134 | 0 | private void addBackupArtifact(BackupDescriptor backupDescriptor) { |
| 135 | 0 | this.setChanged(); |
| 136 | 0 | this.backup.put(createArtifactKey(backupDescriptor), backupDescriptor); |
| 137 | 0 | this.notifyObservers(); |
| 138 | 0 | } |
| 139 | ||
| 140 | /* | |
| 141 | * (non-Javadoc) | |
| 142 | 0 | * |
| 143 | 0 | * @see base.IXMLSaveable#toXml(org.w3c.dom.Document) |
| 144 | 0 | */ |
| 145 | 0 | public Node toXml(Document document) { |
| 146 | 0 | Element repositoryElement = document.createElement("Repository"); |
| 147 | ||
| 148 | 0 | Element locationElement = document.createElement("Location"); |
| 149 | 0 | locationElement.setAttribute("value", this.location.toString()); |
| 150 | 0 | repositoryElement.appendChild(locationElement); |
| 151 | 0 | |
| 152 | 0 | Element availableSpaceElement = document |
| 153 | 0 | .createElement("DeclaredAvailableSpace"); |
| 154 | 0 | availableSpaceElement.setAttribute("value", "" |
| 155 | 0 | + this.declaredAvailableSpace); |
| 156 | 0 | repositoryElement.appendChild(availableSpaceElement); |
| 157 | 0 | |
| 158 | 0 | return repositoryElement; |
| 159 | 0 | } |
| 160 | ||
| 161 | /** | |
| 162 | * @param declaredAvailableSpace | |
| 163 | * The declaredAvailableSpace to set. | |
| 164 | */ | |
| 165 | 0 | public void setDeclaredAvailableSpace(long declaredAvailableSpace) { |
| 166 | 0 | this.declaredAvailableSpace = declaredAvailableSpace; |
| 167 | 0 | } |
| 168 | 0 | |
| 169 | 0 | /** |
| 170 | * @param location | |
| 171 | 0 | * The location to set. |
| 172 | */ | |
| 173 | public void setLocation(File location) { | |
| 174 | 0 | this.location = location; |
| 175 | 0 | } |
| 176 | ||
| 177 | public void removeBackupDescriptor(BackupDescriptor backupDescriptor) { | |
| 178 | 0 | this.setChanged(); |
| 179 | 0 | this.backup.remove(createArtifactKey(backupDescriptor)); |
| 180 | 0 | this.notifyObservers(); |
| 181 | ||
| 182 | 0 | } |
| 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 | 0 | for (int i = 0; i < this.getBackupDescriptor().length; i++) { |
| 195 | 0 | if (this.getBackupDescriptor()[i].getBackup().getGuId() |
| 196 | .equals(guId)) | |
| 197 | 0 | return this.getBackupDescriptor()[i]; |
| 198 | } | |
| 199 | 0 | return null; |
| 200 | } | |
| 201 | ||
| 202 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |