001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2006-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2014-2015 ForgeRock AS 026 */ 027package org.opends.server.backends.jeb; 028import org.forgerock.i18n.LocalizableMessage; 029 030import org.forgerock.i18n.slf4j.LocalizedLogger; 031import static org.opends.messages.BackendMessages.*; 032 033import java.io.File; 034import java.io.FilenameFilter; 035 036/** 037 * A singleton class to manage the life-cycle of a JE database environment. 038 */ 039public class EnvManager 040{ 041 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 042 043 044 /** 045 * A filename filter to match all kinds of JE files. 046 */ 047 private static final FilenameFilter jeAllFilesFilter; 048 049 static 050 { 051 // A filename filter to match all kinds of JE files. 052 // JE has a com.sleepycat.je.log.JEFileFilter that would be useful 053 // here but is not public. 054 jeAllFilesFilter = new FilenameFilter() 055 { 056 public boolean accept(File d, String name) 057 { 058 return name.endsWith(".jdb") || 059 name.endsWith(".del") || 060 name.startsWith("je."); 061 } 062 }; 063 } 064 065 /** 066 * Creates the environment home directory, deleting any existing data files 067 * if the directory already exists. 068 * The environment must not be open. 069 * 070 * @param homeDir The backend home directory. 071 * @throws JebException If an error occurs in the JE backend. 072 */ 073 public static void createHomeDir(String homeDir) 074 throws JebException 075 { 076 File dir = new File(homeDir); 077 078 if (dir.exists()) 079 { 080 if (!dir.isDirectory()) 081 { 082 throw new JebException(ERR_DIRECTORY_INVALID.get(homeDir)); 083 } 084 removeFiles(homeDir); 085 } 086 else 087 { 088 try 089 { 090 dir.mkdir(); 091 } 092 catch (Exception e) 093 { 094 logger.traceException(e); 095 throw new JebException(ERR_CREATE_FAIL.get(e.getMessage()), e); 096 } 097 } 098 } 099 100 /** 101 * Deletes all the data files associated with the environment. 102 * The environment must not be open. 103 * 104 * @param homeDir The backend home directory 105 * @throws JebException If an error occurs in the JE backend or if the 106 * specified home directory does not exist. 107 */ 108 public static void removeFiles(String homeDir) 109 throws JebException 110 { 111 File dir = new File(homeDir); 112 if (!dir.exists()) 113 { 114 LocalizableMessage message = ERR_DIRECTORY_DOES_NOT_EXIST.get(homeDir); 115 throw new JebException(message); 116 } 117 if (!dir.isDirectory()) 118 { 119 throw new JebException(ERR_DIRECTORY_INVALID.get(homeDir)); 120 } 121 122 try 123 { 124 File[] jdbFiles = dir.listFiles(jeAllFilesFilter); 125 for (File f : jdbFiles) 126 { 127 f.delete(); 128 } 129 } 130 catch (Exception e) 131 { 132 logger.traceException(e); 133 throw new JebException(ERR_REMOVE_FAIL.get(e.getMessage()), e); 134 } 135 } 136 137}