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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.backends.jeb;
028 import org.opends.messages.Message;
029
030 import static org.opends.server.loggers.debug.DebugLogger.*;
031 import org.opends.server.loggers.debug.DebugTracer;
032 import org.opends.server.types.DebugLogLevel;
033 import static org.opends.messages.JebMessages.*;
034
035 import java.io.File;
036 import java.io.FilenameFilter;
037
038 /**
039 * A singleton class to manage the life-cycle of a JE database environment.
040 */
041 public class EnvManager
042 {
043 /**
044 * The tracer object for the debug logger.
045 */
046 private static final DebugTracer TRACER = getTracer();
047
048
049 /**
050 * A filename filter to match all kinds of JE files.
051 */
052 private static final FilenameFilter jeAllFilesFilter;
053
054 static
055 {
056 // A filename filter to match all kinds of JE files.
057 // JE has a com.sleepycat.je.log.JEFileFilter that would be useful
058 // here but is not public.
059 jeAllFilesFilter = new FilenameFilter()
060 {
061 public boolean accept(File d, String name)
062 {
063 return name.endsWith(".jdb") ||
064 name.endsWith(".del") ||
065 name.equals("je.lck");
066 }
067 };
068 }
069
070 /**
071 * Creates the environment home directory, deleting any existing data files
072 * if the directory already exists.
073 * The environment must not be open.
074 *
075 * @param homeDir The backend home directory.
076 * @throws JebException If an error occurs in the JE backend.
077 */
078 public static void createHomeDir(String homeDir)
079 throws JebException
080 {
081 File dir = new File(homeDir);
082
083 if (dir.exists())
084 {
085 if (!dir.isDirectory())
086 {
087 Message message = ERR_JEB_DIRECTORY_INVALID.get(homeDir);
088 throw new JebException(message);
089 }
090 removeFiles(homeDir);
091 }
092 else
093 {
094 try
095 {
096 dir.mkdir();
097 }
098 catch (Exception e)
099 {
100 if (debugEnabled())
101 {
102 TRACER.debugCaught(DebugLogLevel.ERROR, e);
103 }
104 Message message = ERR_JEB_CREATE_FAIL.get(e.getMessage());
105 throw new JebException(message, e);
106 }
107 }
108 }
109
110 /**
111 * Deletes all the data files associated with the environment.
112 * The environment must not be open.
113 *
114 * @param homeDir The backend home directory
115 * @throws JebException If an error occurs in the JE backend or if the
116 * specified home directory does not exist.
117 */
118 public static void removeFiles(String homeDir)
119 throws JebException
120 {
121 File dir = new File(homeDir);
122 if (!dir.exists())
123 {
124 Message message = ERR_JEB_DIRECTORY_DOES_NOT_EXIST.get(homeDir);
125 throw new JebException(message);
126 }
127 if (!dir.isDirectory())
128 {
129 Message message = ERR_JEB_DIRECTORY_INVALID.get(homeDir);
130 throw new JebException(message);
131 }
132
133 try
134 {
135 File[] jdbFiles = dir.listFiles(jeAllFilesFilter);
136 for (File f : jdbFiles)
137 {
138 f.delete();
139 }
140 }
141 catch (Exception e)
142 {
143 if (debugEnabled())
144 {
145 TRACER.debugCaught(DebugLogLevel.ERROR, e);
146 }
147 Message message = ERR_JEB_REMOVE_FAIL.get(e.getMessage());
148 throw new JebException(message, e);
149 }
150 }
151
152 }