Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

RPCExample.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: RPCExample.java,v 12.2 2005/06/16 20:22:47 bostic Exp $
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  * An example of a program configuring a database environment as an RPC client.
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         // Do something interesting...
00028         // Your application goes here.
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         // Create an environment object and initialize it for error reporting.
00041         EnvironmentConfig config = new EnvironmentConfig();
00042         config.setErrorStream(errs);
00043         config.setErrorPrefix(progname);
00044 
00045         //
00046         // We want to specify the shared memory buffer pool cachesize,
00047         // but everything else is the default.
00048         //
00049         config.setCacheSize(64 * 1024);
00050 
00051         // Open the environment with full transactional support.
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         // open is declared to throw a FileNotFoundException, which normally
00061         // shouldn't occur when allowCreate is set.
00062         //
00063         Environment dbenv = new Environment(home, config);
00064 
00065         try {
00066             // Start your application.
00067             runApplication(dbenv);
00068         } finally {
00069             // Close the environment.  Doing this in the finally block ensures
00070             // it is done, even if an error is thrown.
00071             dbenv.close();
00072         }
00073     }
00074 
00075     private static void teardownEnvironment(File home,
00076                                             OutputStream errs)
00077         throws DatabaseException, FileNotFoundException {
00078 
00079         // Remove the shared database regions.
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 }

Generated on Sun Dec 25 12:14:27 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2