##### Coverage Report - org.owasp.dependencycheck.data.cpe.BaseIndex

Classes in this File | Line Coverage | Branch Coverage | Complexity
BaseIndex |
73% | 17/23
25% | 1/4
| 1.6666666666666667;1.667

1 |  |
     /*
2 |  |
      * This file is part of dependency-check-core.
3 |  |
      *
4 |  |
      * Dependency-check-core is free software: you can redistribute it and/or modify it
5 |  |
      * under the terms of the GNU General Public License as published by the Free
6 |  |
      * Software Foundation, either version 3 of the License, or (at your option) any
7 |  |
      * later version.
8 |  |
      *
9 |  |
      * Dependency-check-core is distributed in the hope that it will be useful, but
10 |  |
      * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 |  |
      * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 |  |
      * details.
13 |  |
      *
14 |  |
      * You should have received a copy of the GNU General Public License along with
15 |  |
      * dependency-check-core. If not, see http://www.gnu.org/licenses/.
16 |  |
      *
17 |  |
      * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
18 |  |
      */
19 |  |
     package org.owasp.dependencycheck.data.cpe;
20 |  |
21 |  |
     import java.io.File;
22 |  |
     import java.io.IOException;
23 |  |
     import java.util.logging.Level;
24 |  |
     import java.util.logging.Logger;
25 |  |
     import org.apache.lucene.store.Directory;
26 |  |
     import org.apache.lucene.store.FSDirectory;
27 |  |
     import org.owasp.dependencycheck.utils.Settings;
28 |  |
29 |  |
     /**
30 |  |
      * The Base Index class used to access the CPE Index.
31 |  |
      *
32 |  |
      * @author Jeremy Long (jeremy.long@owasp.org)
33 |  |
      */
34 | 16 |
     public abstract class BaseIndex {
35 |  |
36 |  |
         /**
37 |  |
          * The Lucene directory containing the index.
38 |  |
          */
39 |  |
         private Directory directory;
40 |  |
         /**
41 |  |
          * Indicates whether or not the Lucene Index is open.
42 |  |
          */
43 | 16 |
         private boolean indexOpen = false;
44 |  |
45 |  |
         /**
46 |  |
          * Gets the directory.
47 |  |
          *
48 |  |
          * @return the directory
49 |  |
          */
50 |  |
         public Directory getDirectory() {
51 | 16 |
             return directory;
52 |  |
         }
53 |  |
54 |  |
         /**
55 |  |
          * Opens the CPE Index.
56 |  |
          *
57 |  |
          * @throws IOException is thrown if an IOException occurs opening the index.
58 |  |
          */
59 |  |
         public void open() throws IOException {
60 | 16 |
             directory = this.openDirectory();
61 | 16 |
             indexOpen = true;
62 | 16 |
         }
63 |  |
64 |  |
         /**
65 |  |
          * Closes the CPE Index.
66 |  |
          */
67 |  |
         public void close() {
68 |  |
             try {
69 | 16 |
                 directory.close();
70 | 0 |
             } catch (IOException ex) {
71 | 0 |
                 final String msg = "Unable to update database due to an IO error.";
72 | 0 |
                 Logger.getLogger(BaseIndex.class.getName()).log(Level.SEVERE, msg);
73 | 0 |
                 Logger.getLogger(BaseIndex.class.getName()).log(Level.FINE, null, ex);
74 |  |
             } finally {
75 | 16 |
                 directory = null;
76 | 16 |
             }
77 | 16 |
             indexOpen = false;
78 |  |
79 | 16 |
         }
80 |  |
81 |  |
         /**
82 |  |
          * Returns the status of the data source - is the index open.
83 |  |
          *
84 |  |
          * @return true or false.
85 |  |
          */
86 |  |
         public boolean isOpen() {
87 | 8 |
             return indexOpen;
88 |  |
         }
89 |  |
90 |  |
         /**
91 |  |
          * Returns the Lucene directory object for the CPE Index.
92 |  |
          *
93 |  |
          * @return the Lucene Directory object for the CPE Index.
94 |  |
          * @throws IOException is thrown if an IOException occurs.
95 |  |
          */
96 |  |
         protected Directory openDirectory() throws IOException {
97 | 16 |
             final File path = getDataDirectory();
98 | 16 |
             return FSDirectory.open(path);
99 |  |
         }
100 |  |
101 |  |
         /**
102 |  |
          * Retrieves the directory that the JAR file exists in so that we can ensure
103 |  |
          * we always use a common data directory.
104 |  |
          *
105 |  |
          * @return the data directory for this index.
106 |  |
          * @throws IOException is thrown if an IOException occurs of course...
107 |  |
          */
108 |  |
         public static File getDataDirectory() throws IOException {
109 | 17 |
             final File path = Settings.getFile(Settings.KEYS.CPE_DATA_DIRECTORY);
110 | 17 |
             if (!path.exists()) {
111 | 0 |
                 if (!path.mkdirs()) {
112 | 0 |
                     throw new IOException("Unable to create CPE Data directory");
113 |  |
                 }
114 |  |
             }
115 | 17 |
             return path;
116 |  |
         }
117 |  |
     }