Coverage Report - org.owasp.dependencycheck.data.update.BaseUpdater
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseUpdater
70%
17/24
50%
2/4
2.5
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2015 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.update;
 19  
 
 20  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 21  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 22  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 23  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 24  
 import org.slf4j.Logger;
 25  
 import org.slf4j.LoggerFactory;
 26  
 
 27  
 /**
 28  
  *
 29  
  * @author Jeremy Long
 30  
  */
 31  4
 public abstract class BaseUpdater {
 32  
 
 33  
     /**
 34  
      * Static logger.
 35  
      */
 36  1
     private static final Logger LOGGER = LoggerFactory.getLogger(BaseUpdater.class);
 37  
     /**
 38  
      * Information about the timestamps and URLs for data that needs to be updated.
 39  
      */
 40  
     private DatabaseProperties properties;
 41  
     /**
 42  
      * Reference to the Cve Database.
 43  
      */
 44  4
     private CveDB cveDB = null;
 45  
 
 46  
     protected CveDB getCveDB() {
 47  1
         return cveDB;
 48  
     }
 49  
 
 50  
     protected DatabaseProperties getProperties() {
 51  1
         return properties;
 52  
     }
 53  
 
 54  
     /**
 55  
      * Closes the CVE and CPE data stores.
 56  
      */
 57  
     protected void closeDataStores() {
 58  3
         if (cveDB != null) {
 59  
             try {
 60  3
                 cveDB.close();
 61  3
                 cveDB = null;
 62  3
                 properties = null;
 63  0
             } catch (Throwable ignore) {
 64  0
                 LOGGER.trace("Error closing the database", ignore);
 65  3
             }
 66  
         }
 67  3
     }
 68  
 
 69  
     /**
 70  
      * Opens the data store.
 71  
      *
 72  
      * @throws UpdateException thrown if a data store cannot be opened
 73  
      */
 74  
     protected final void openDataStores() throws UpdateException {
 75  3
         if (cveDB != null) {
 76  0
             return;
 77  
         }
 78  
         try {
 79  3
             cveDB = new CveDB();
 80  3
             cveDB.open();
 81  3
             properties = cveDB.getDatabaseProperties();
 82  0
         } catch (DatabaseException ex) {
 83  0
             closeDataStores();
 84  0
             LOGGER.debug("Database Exception opening databases", ex);
 85  0
             throw new UpdateException("Error updating the database, please see the log file for more details.");
 86  3
         }
 87  3
     }
 88  
 }