    1   /* EnhancedEnvironment.java
    2    *
    3    * Created on February 18. 2007
    4    *
    5    * Copyright (C) 2007 Internet Archive.
    6    *
    7    * This file is part of the Heritrix web crawler (crawler.archive.org).
    8    *
    9    * Heritrix is free software; you can redistribute it and/or modify
    10   * it under the terms of the GNU Lesser Public License as published by
    11   * the Free Software Foundation; either version 2.1 of the License, or
    12   * any later version.
    13   *
    14   * Heritrix is distributed in the hope that it will be useful,
    15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17   * GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License
    20   * along with Heritrix; if not, write to the Free Software
    21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22   */
    23  package org.archive.util.bdbje;
    24
    25  import java.io.File;
    26
    27  import com.sleepycat.bind.serial.StoredClassCatalog;
    28  import com.sleepycat.je.Database;
    29  import com.sleepycat.je.DatabaseConfig;
    30  import com.sleepycat.je.DatabaseException;
    31  import com.sleepycat.je.Environment;
    32  import com.sleepycat.je.EnvironmentConfig;
    33
    34  /***
    35   * Version of BDB_JE Environment with additional convenience features, such as
    36   * a shared, cached StoredClassCatalog. (Additional convenience caching of
    37   * Databases and StoredCollections may be added later.)
    38   *
    39   * @author gojomo
    40   */
    41  public class EnhancedEnvironment extends Environment {
    42      StoredClassCatalog classCatalog;
    43      Database classCatalogDB;
    44
    45      /***
    46       * Constructor
    47       *
    48       * @param envHome directory in which to open environment
    49       * @param envConfig config options
    50       * @throws DatabaseException
    51       */
    52      public EnhancedEnvironment(File envHome, EnvironmentConfig envConfig) throws DatabaseException {
    53          super(envHome, envConfig);
    54      }
    55
    56      /***
    57       * Return a StoredClassCatalog backed by a Database in this environment,
    58       * either pre-existing or created (and cached) if necessary.
    59       *
    60       * @return the cached class catalog
    61       */
    62      public StoredClassCatalog getClassCatalog() {
    63          if(classCatalog == null) {
    64              DatabaseConfig dbConfig = new DatabaseConfig();
    65              dbConfig.setAllowCreate(true);
    66              try {
    67                  classCatalogDB = openDatabase(null, "classCatalog", dbConfig);
    68                  classCatalog = new StoredClassCatalog(classCatalogDB);
    69              } catch (DatabaseException e) {
    70                  // TODO Auto-generated catch block
    71                  throw new RuntimeException(e);
    72              }
    73          }
    74          return classCatalog;
    75      }
    76
    77      @Override
    78      public synchronized void close() throws DatabaseException {
    79          if(classCatalogDB!=null) {
    80              classCatalogDB.close();
    81          }
    82          super.close();
    83      }
    84
    85
    86
    87  }