Coverage details for base.jdbs.Repository

LineHitsSource
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 {
351 
360    private static final transient Logger logger = Logger
371            .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;
441 
45     /** The repositorys' backup collection.* */
460    private Hashtable<String, BackupDescriptor> backup = new Hashtable<String, BackupDescriptor>();
47  
48     /**
49      * This is the default constructor.
50      *
511     * @param location
521     * The location on filesytem associated to the JDBS local
531     * repository.
541     * @param declaredAvailableSpace
55      * The declared repository's available space.
56      */
570    public Repository(File location, long declaredAvailableSpace) {
580        this.location = location;
590        this.declaredAvailableSpace = declaredAvailableSpace;
600    }
610 
620    /**
630     * This is the xml repository constructor.
640     *
650     * @param document
660     * The document where the repository data has been stored.
67      */
680    public Repository(Document document) {
690        NodeList nodeList = document.getElementsByTagName("Repository");
700        for (int i = 0; i < nodeList.item(0).getChildNodes().getLength(); i++) {
710            if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
72                     "Location"))
730                this.location = new File(nodeList.item(0).getChildNodes().item(
741                        i).getAttributes().getNamedItem("value").getNodeValue()
751                        .toString());
761            if (nodeList.item(0).getChildNodes().item(i).getNodeName().equals(
771                    "DeclaredAvailableSpace"))
785                this.declaredAvailableSpace = new Integer(nodeList.item(0)
794                        .getChildNodes().item(i).getAttributes().getNamedItem(
804                                "value").getNodeValue().toString());
812        }
822    }
832 
840    /**
850     * This method creates an index of backups building it from this
86      * repository's location.
871     */
881    public void indexRepository() {
890        this.setChanged();
900        // We must clear the backups indexed so far.
912        this.backup.clear();
920        logger.debug("Indexing the repository: " + location);
930        if (location.exists() && location.isDirectory()) {
940            File[] artifact = location.listFiles();
950            for (int i = 0; i < artifact.length; i++) {
960                logger.debug("Item: " + artifact[i] + " isDirectory: "
97                         + artifact[i].isDirectory());
980                if (artifact[i].isDirectory()) {
990                    logger.debug("Found: " + artifact[i]);
1000                    BackupDescriptor backupDescriptor = new BackupDescriptor(
101                             artifact[i]);
1020                    this.addBackupArtifact(backupDescriptor);
103                 }
104             }
1052        }
1060        this.notifyObservers();
1070    }
108  
109     protected String createArtifactKey(BackupDescriptor backupDescriptor) {
1100        return backupDescriptor.getArtifactDirectory().toString();
111     }
1120 
113     /**
1140     * @return Returns the declaredAvailableSpace.
115      */
1162    public long getDeclaredAvailableSpace() {
1172        return declaredAvailableSpace;
1182    }
1192 
1200    /**
1210     * @return Returns the indexed backup descriptors..
122      */
123     public BackupDescriptor[] getBackupDescriptor() {
1240        return backup.values().toArray(new BackupDescriptor[0]);
1250    }
126  
1270    /**
1280     * @return Returns the location.
1290     */
1300    public File getLocation() {
1310        return location;
1320    }
1330 
1340    private void addBackupArtifact(BackupDescriptor backupDescriptor) {
1350        this.setChanged();
1360        this.backup.put(createArtifactKey(backupDescriptor), backupDescriptor);
1370        this.notifyObservers();
1380    }
139  
140     /*
141      * (non-Javadoc)
1420     *
1430     * @see base.IXMLSaveable#toXml(org.w3c.dom.Document)
1440     */
1450    public Node toXml(Document document) {
1460        Element repositoryElement = document.createElement("Repository");
147  
1480        Element locationElement = document.createElement("Location");
1490        locationElement.setAttribute("value", this.location.toString());
1500        repositoryElement.appendChild(locationElement);
1510 
1520        Element availableSpaceElement = document
1530                .createElement("DeclaredAvailableSpace");
1540        availableSpaceElement.setAttribute("value", ""
1550                + this.declaredAvailableSpace);
1560        repositoryElement.appendChild(availableSpaceElement);
1570 
1580        return repositoryElement;
1590    }
160  
161     /**
162      * @param declaredAvailableSpace
163      * The declaredAvailableSpace to set.
164      */
1650    public void setDeclaredAvailableSpace(long declaredAvailableSpace) {
1660        this.declaredAvailableSpace = declaredAvailableSpace;
1670    }
1680 
1690    /**
170      * @param location
1710     * The location to set.
172      */
173     public void setLocation(File location) {
1740        this.location = location;
1750    }
176  
177     public void removeBackupDescriptor(BackupDescriptor backupDescriptor) {
1780        this.setChanged();
1790        this.backup.remove(createArtifactKey(backupDescriptor));
1800        this.notifyObservers();
181  
1820    }
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) {
1940        for (int i = 0; i < this.getBackupDescriptor().length; i++) {
1950            if (this.getBackupDescriptor()[i].getBackup().getGuId()
196                     .equals(guId))
1970                return this.getBackupDescriptor()[i];
198         }
1990        return null;
200     }
201  
202 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.