00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package db;
00011
00012 import com.sleepycat.db.*;
00013 import java.io.File;
00014 import java.io.FileNotFoundException;
00015 import java.io.OutputStream;
00016
00017
00018
00019
00020 public class RPCExample {
00021 private static final String progname = "RPCExample";
00022 private static final File DATABASE_HOME = new File("TESTDIR");
00023
00024 private static void runApplication(Environment dbenv)
00025 throws DatabaseException, FileNotFoundException {
00026
00027
00028
00029 DatabaseConfig config = new DatabaseConfig();
00030 config.setAllowCreate(true);
00031 config.setType(DatabaseType.BTREE);
00032 Database db = dbenv.openDatabase(null, "test.db", null, config);
00033 db.close();
00034 }
00035
00036 private static void setupEnvironment(File home,
00037 OutputStream errs)
00038 throws DatabaseException, FileNotFoundException {
00039
00040
00041 EnvironmentConfig config = new EnvironmentConfig();
00042 config.setErrorStream(errs);
00043 config.setErrorPrefix(progname);
00044
00045
00046
00047
00048
00049 config.setCacheSize(64 * 1024);
00050
00051
00052 config.setAllowCreate(true);
00053 config.setInitializeCache(true);
00054 config.setTransactional(true);
00055 config.setInitializeLocking(true);
00056
00057 config.setRPCServer("localhost", 0, 0);
00058
00059
00060
00061
00062
00063 Environment dbenv = new Environment(home, config);
00064
00065 try {
00066
00067 runApplication(dbenv);
00068 } finally {
00069
00070
00071 dbenv.close();
00072 }
00073 }
00074
00075 private static void teardownEnvironment(File home,
00076 OutputStream errs)
00077 throws DatabaseException, FileNotFoundException {
00078
00079
00080 EnvironmentConfig config = new EnvironmentConfig();
00081
00082 config.setErrorStream(errs);
00083 config.setErrorPrefix(progname);
00084 config.setRPCServer("localhost", 0, 0);
00085 Environment.remove(home, true, config);
00086 }
00087
00088 public static void main(String[] args) {
00089 File home = DATABASE_HOME;
00090
00091 try {
00092 System.out.println("Setup env");
00093 setupEnvironment(home, System.err);
00094
00095 System.out.println("Teardown env");
00096 teardownEnvironment(home, System.err);
00097 } catch (DatabaseException dbe) {
00098 System.err.println(progname + ": environment open: " + dbe.toString());
00099 dbe.printStackTrace(System.err);
00100 System.exit (1);
00101 } catch (FileNotFoundException fnfe) {
00102 System.err.println(progname + ": unexpected open environment error " + fnfe);
00103 System.exit (1);
00104 }
00105 }
00106
00107 }