1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package persist; 10 11import java.io.File; 12import java.io.FileNotFoundException; 13 14import com.sleepycat.db.DatabaseException; 15import com.sleepycat.db.Environment; 16import com.sleepycat.db.EnvironmentConfig; 17import com.sleepycat.persist.EntityCursor; 18import com.sleepycat.persist.EntityStore; 19import com.sleepycat.persist.PrimaryIndex; 20import com.sleepycat.persist.StoreConfig; 21import com.sleepycat.persist.model.EntityMetadata; 22import com.sleepycat.persist.model.EntityModel; 23import com.sleepycat.persist.raw.RawObject; 24import com.sleepycat.persist.raw.RawStore; 25import com.sleepycat.persist.raw.RawType; 26 27/** 28 * Dumps a store or all stores to standard output in raw XML format. This 29 * sample is intended to be modifed to dump in application specific ways. 30 * @see #usage 31 */ 32public class DplDump { 33 34 private File envHome; 35 private String storeName; 36 private boolean dumpMetadata; 37 private Environment env; 38 39 public static void main(String[] args) { 40 try { 41 DplDump dump = new DplDump(args); 42 dump.open(); 43 dump.dump(); 44 dump.close(); 45 } catch (Throwable e) { 46 e.printStackTrace(); 47 System.exit(1); 48 } 49 } 50 51 private DplDump(String[] args) { 52 53 for (int i = 0; i < args.length; i += 1) { 54 String name = args[i]; 55 String val = null; 56 if (i < args.length - 1 && !args[i + 1].startsWith("-")) { 57 i += 1; 58 val = args[i]; 59 } 60 if (name.equals("-h")) { 61 if (val == null) { 62 usage("No value after -h"); 63 } 64 envHome = new File(val); 65 } else if (name.equals("-s")) { 66 if (val == null) { 67 usage("No value after -s"); 68 } 69 storeName = val; 70 } else if (name.equals("-meta")) { 71 dumpMetadata = true; 72 } else { 73 usage("Unknown arg: " + name); 74 } 75 } 76 77 if (storeName == null) { 78 usage("-s not specified"); 79 } 80 if (envHome == null) { 81 usage("-h not specified"); 82 } 83 } 84 85 private void usage(String msg) { 86 87 if (msg != null) { 88 System.out.println(msg); 89 } 90 91 System.out.println 92 ("usage:" + 93 "\njava " + DplDump.class.getName() + 94 "\n -h <envHome>" + 95 "\n # Environment home directory" + 96 "\n [-meta]" + 97 "\n # Dump metadata; default: false" + 98 "\n -s <storeName>" + 99 "\n # Store to dump"); 100 101 System.exit(2); 102 } 103 104 private void open() 105 throws DatabaseException, FileNotFoundException { 106 107 EnvironmentConfig envConfig = new EnvironmentConfig(); 108 envConfig.setInitializeCache(true); 109 envConfig.setInitializeLocking(true); 110 env = new Environment(envHome, envConfig); 111 } 112 113 private void close() 114 throws DatabaseException { 115 116 env.close(); 117 } 118 119 private void dump() 120 throws DatabaseException { 121 122 StoreConfig storeConfig = new StoreConfig(); 123 storeConfig.setReadOnly(true); 124 RawStore store = new RawStore(env, storeName, storeConfig); 125 126 EntityModel model = store.getModel(); 127 for (String clsName : model.getKnownClasses()) { 128 EntityMetadata meta = model.getEntityMetadata(clsName); 129 if (meta != null) { 130 if (dumpMetadata) { 131 for (RawType type : model.getAllRawTypeVersions(clsName)) { 132 System.out.println(type); 133 } 134 } else { 135 PrimaryIndex<Object,RawObject> index = 136 store.getPrimaryIndex(clsName); 137 EntityCursor<RawObject> entities = index.entities(); 138 for (RawObject entity : entities) { 139 System.out.println(entity); 140 } 141 entities.close(); 142 } 143 } 144 } 145 146 store.close(); 147 } 148} 149