001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: DplDump.java,v 1.1.2.2 2008/01/07 15:14:04 cwl Exp $
007: */
008:
009: package persist;
010:
011: import java.io.File;
012:
013: import com.sleepycat.je.DatabaseException;
014: import com.sleepycat.je.Environment;
015: import com.sleepycat.je.EnvironmentConfig;
016: import com.sleepycat.persist.EntityCursor;
017: import com.sleepycat.persist.EntityStore;
018: import com.sleepycat.persist.PrimaryIndex;
019: import com.sleepycat.persist.StoreConfig;
020: import com.sleepycat.persist.model.EntityMetadata;
021: import com.sleepycat.persist.model.EntityModel;
022: import com.sleepycat.persist.raw.RawObject;
023: import com.sleepycat.persist.raw.RawStore;
024: import com.sleepycat.persist.raw.RawType;
025:
026: /**
027: * Dumps a store or all stores to standard output in raw XML format. This
028: * sample is intended to be modifed to dump in application specific ways.
029: * @see #usage
030: */
031: public class DplDump {
032:
033: private File envHome;
034: private String storeName;
035: private boolean dumpMetadata;
036: private Environment env;
037:
038: public static void main(String[] args) {
039: try {
040: DplDump dump = new DplDump(args);
041: dump.open();
042: dump.execute();
043: dump.close();
044: } catch (Throwable e) {
045: e.printStackTrace();
046: System.exit(1);
047: }
048: }
049:
050: private DplDump(String[] args) {
051:
052: for (int i = 0; i < args.length; i += 1) {
053: String name = args[i];
054: String val = null;
055: if (i < args.length - 1 && !args[i + 1].startsWith("-")) {
056: i += 1;
057: val = args[i];
058: }
059: if (name.equals("-h")) {
060: if (val == null) {
061: usage("No value after -h");
062: }
063: envHome = new File(val);
064: } else if (name.equals("-s")) {
065: if (val == null) {
066: usage("No value after -s");
067: }
068: storeName = val;
069: } else if (name.equals("-meta")) {
070: dumpMetadata = true;
071: } else {
072: usage("Unknown arg: " + name);
073: }
074: }
075:
076: if (envHome == null) {
077: usage("-h not specified");
078: }
079: }
080:
081: private void usage(String msg) {
082:
083: if (msg != null) {
084: System.out.println(msg);
085: }
086:
087: System.out.println("usage:" + "\njava "
088: + DplDump.class.getName() + "\n -h <envHome>"
089: + "\n # Environment home directory"
090: + "\n [-meta]"
091: + "\n # Dump metadata; default: false"
092: + "\n [-s <storeName>]"
093: + "\n # Store to dump; default: dump all stores");
094:
095: System.exit(2);
096: }
097:
098: private void open() throws DatabaseException {
099:
100: EnvironmentConfig envConfig = new EnvironmentConfig();
101: envConfig.setReadOnly(true);
102: env = new Environment(envHome, envConfig);
103: }
104:
105: private void close() throws DatabaseException {
106:
107: env.close();
108: }
109:
110: private void execute() throws DatabaseException {
111:
112: if (storeName != null) {
113: dump();
114: } else {
115: for (String name : EntityStore.getStoreNames(env)) {
116: storeName = name;
117: dump();
118: }
119: }
120: }
121:
122: private void dump() throws DatabaseException {
123:
124: StoreConfig storeConfig = new StoreConfig();
125: storeConfig.setReadOnly(true);
126: RawStore store = new RawStore(env, storeName, storeConfig);
127:
128: EntityModel model = store.getModel();
129: for (String clsName : model.getKnownClasses()) {
130: EntityMetadata meta = model.getEntityMetadata(clsName);
131: if (meta != null) {
132: if (dumpMetadata) {
133: for (RawType type : model
134: .getAllRawTypeVersions(clsName)) {
135: System.out.println(type);
136: }
137: } else {
138: PrimaryIndex<Object, RawObject> index = store
139: .getPrimaryIndex(clsName);
140: EntityCursor<RawObject> entities = index.entities();
141: for (RawObject entity : entities) {
142: System.out.println(entity);
143: }
144: entities.close();
145: }
146: }
147: }
148:
149: store.close();
150: }
151: }
|