View Javadoc
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  public abstract class BaseUpdater {
32  
33      /**
34       * Static logger.
35       */
36      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      private CveDB cveDB = null;
45  
46      protected CveDB getCveDB() {
47          return cveDB;
48      }
49  
50      protected DatabaseProperties getProperties() {
51          return properties;
52      }
53  
54      /**
55       * Closes the CVE and CPE data stores.
56       */
57      protected void closeDataStores() {
58          if (cveDB != null) {
59              try {
60                  cveDB.close();
61                  cveDB = null;
62                  properties = null;
63              } catch (Throwable ignore) {
64                  LOGGER.trace("Error closing the database", ignore);
65              }
66          }
67      }
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          if (cveDB != null) {
76              return;
77          }
78          try {
79              cveDB = new CveDB();
80              cveDB.open();
81              properties = cveDB.getDatabaseProperties();
82          } catch (DatabaseException ex) {
83              closeDataStores();
84              LOGGER.debug("Database Exception opening databases", ex);
85              throw new UpdateException("Error updating the database, please see the log file for more details.");
86          }
87      }
88  }