001: package com.completex.objective.persistency.examples;
002:
003: import com.completex.objective.components.persistency.core.adapter.DefaultPersistencyAdapter;
004: import com.completex.objective.components.persistency.transact.Transaction;
005: import com.completex.objective.components.persistency.OdalPersistencyException;
006: import com.completex.objective.components.log.Log;
007: import com.completex.objective.components.log.adapter.StdOutputLogAdapter;
008: import com.completex.objective.util.PropertyMap;
009: import com.completex.objective.tools.generators.PersistentDescriptorGenerator;
010:
011: import java.io.*;
012: import java.util.Properties;
013: import java.sql.SQLException;
014:
015: /**
016: * @author Gennady Krizhevsky
017: */
018: public class ExamplesHelper {
019:
020: public static final Log logger = StdOutputLogAdapter
021: .newLogInstance();
022:
023: public static final String CREATE_TABLES_FILE = "create-tables.sql";
024: public static final String CREATE_SEQ_FILE = "create-sequences.sql";
025:
026: public static final String TABLE = "TABLE";
027: public static final String SEQUENCE = "SEQUENCE";
028:
029: public static final String BASE = "examples/src/java/";
030: public static final String CONFIG_BASE = "examples/src/config/";
031: public static final String PD_FILE_NAME = "persistent-descriptor-config.properties";
032: public static final String PO_FILE_NAME = "persistent-object-config.sdl";
033: public static final String CPX_FILE_NAME = "composite-po-config.sdl";
034: public static final String BEAN_FILE_NAME = "bean-config.sdl";
035: public static final String CTL_FILE_NAME = "lifecycle-ctl-config.properties";
036:
037: public static String configPath(Class exampleClass, String fileName) {
038: return CONFIG_BASE + exToken(exampleClass) + "/" + fileName;
039: }
040:
041: public static String pdConfigPath(Class exampleClass) {
042: return CONFIG_BASE + exToken(exampleClass) + "/" + PD_FILE_NAME;
043: }
044:
045: public static String poConfigPath(Class exampleClass) {
046: return CONFIG_BASE + exToken(exampleClass) + "/" + PO_FILE_NAME;
047: }
048:
049: public static String cpxConfigPath(Class exampleClass) {
050: return CONFIG_BASE + exToken(exampleClass) + "/"
051: + CPX_FILE_NAME;
052: }
053:
054: public static String beanConfigPath(Class exampleClass) {
055: return CONFIG_BASE + exToken(exampleClass) + "/"
056: + BEAN_FILE_NAME;
057: }
058:
059: public static String ctlConfigPath(Class exampleClass) {
060: return CONFIG_BASE + exToken(exampleClass) + "/"
061: + CTL_FILE_NAME;
062: }
063:
064: public static String exToken(Class exampleClass) {
065: Package pkg = exampleClass.getPackage();
066: String packageName = pkg.getName();
067: String[] tokens = packageName.split("\\.");
068: if (tokens.length == 0) {
069: throw new RuntimeException("Cannot split package name "
070: + packageName);
071: }
072: return tokens[tokens.length - 1];
073: }
074:
075: public static final String[] deletePaths = new String[] {
076: "./testdb.lck", "./testdb.data", "./testdb.backup",
077: "./testdb.log", "./testdb.properties", "./testdb.script", };
078: public static final String EXAMPLES = "examples/";
079:
080: public static void resetDatabase() {
081: for (int i = 0; i < deletePaths.length; i++) {
082: String deletePath = deletePaths[i];
083: deleteFile(deletePath);
084: }
085: }
086:
087: private static void deleteFile(String pathname) {
088: File file = new File(pathname);
089: if (file.exists()) {
090: file.delete();
091: }
092: }
093:
094: public static String resolveConfigPath(String filePath)
095: throws FileNotFoundException {
096: File file = new File(filePath);
097: if (file.exists()) {
098: return filePath;
099: } else {
100: if (filePath.startsWith(EXAMPLES)) {
101: filePath = filePath.substring(EXAMPLES.length());
102: } else {
103: throw new FileNotFoundException(
104: "Cannot find config file " + file);
105: }
106: file = new File(filePath);
107: if (file.exists()) {
108: return filePath;
109: } else {
110: throw new FileNotFoundException(
111: "Cannot find config file " + file);
112: }
113: }
114: }
115:
116: public static void removeLock() {
117: deleteFile("./testdb.lck");
118: }
119:
120: public static String outPath(Class aClass) {
121: String outPath = aClass.getPackage().getName();
122: outPath = BASE + outPath.replaceAll("\\.", "/");
123: return outPath;
124: }
125:
126: private static Properties createTables0(Class clazz)
127: throws IOException, SQLException {
128: String configPath = pdConfigPath(clazz);
129: Properties properties = new Properties();
130: properties.load(new FileInputStream(configPath));
131: DefaultPersistencyAdapter persistency = new DefaultPersistencyAdapter(
132: properties);
133: Transaction transaction = persistency.getTransactionManager()
134: .begin();
135: String outPath = outPath(clazz);
136: recreateObjects(CREATE_TABLES_FILE, persistency, outPath);
137: recreateObjects(CREATE_SEQ_FILE, persistency, outPath);
138: persistency.getTransactionManager().commit(transaction);
139: return properties;
140: }
141:
142: public static void recreateObjects(String fileName,
143: DefaultPersistencyAdapter persistency, String out)
144: throws IOException, OdalPersistencyException {
145: String[] sentences = toSentences(fileName, out);
146: createObjects(sentences, persistency);
147: }
148:
149: protected static String[] toSentences(String create, String out)
150: throws IOException {
151: String pathname = out + "/" + create;
152: File file = new File(pathname);
153: FileReader reader = new FileReader(file);
154: LineNumberReader lineNumberReader = new LineNumberReader(reader);
155: String fileText = "";
156: String line;
157: while ((line = lineNumberReader.readLine()) != null) {
158: fileText += line;
159: }
160: lineNumberReader.close();
161: return fileText.split(";");
162: }
163:
164: private static void createObjects(String[] sentences,
165: DefaultPersistencyAdapter persistency)
166: throws OdalPersistencyException {
167: for (int i = 0; i < sentences.length; i++) {
168: String sentence = sentences[i];
169: try {
170: persistency.executeUpdate(sentence);
171: System.out.println("[" + i + "]" + sentence);
172: } catch (OdalPersistencyException e) {
173: logger.error("Cannot create " + i + "-th table as "
174: + sentence, e);
175: throw e;
176: }
177: }
178: }
179:
180: public static void generateDesriptors(Properties properties)
181: throws Exception {
182: PropertyMap descProperties = PropertyMap
183: .toPropertyMap(properties);
184: PersistentDescriptorGenerator descriptorGenerator = new PersistentDescriptorGenerator(
185: descProperties);
186: descriptorGenerator.generate();
187: descriptorGenerator.dispose();
188: }
189:
190: public static void createTablesAndGenerateDescriptors(Class clazz)
191: throws Exception {
192: Properties properties = createTables(clazz);
193: generateDesriptors(properties);
194: }
195:
196: public static Properties createTables(Class clazz)
197: throws IOException, SQLException {
198: resetDatabase();
199: return createTables0(clazz);
200: }
201:
202: }
|