001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.platforms.stores.base;
009:
010: //base classes
011: import java.math.BigDecimal;
012: import java.sql.Connection;
013: import java.sql.PreparedStatement;
014: import java.sql.ResultSet;
015: import java.sql.SQLException;
016: import java.sql.Statement;
017: import java.util.ArrayList;
018: import javax.naming.Context;
019: import javax.naming.InitialContext;
020: import javax.naming.NamingException;
021: import javax.rmi.PortableRemoteObject;
022: import javax.sql.DataSource;
023:
024: //project specific classes
025: import org.jfolder.common.UnexpectedSystemException;
026: import org.jfolder.common.tagging.RootConceptTagHolder;
027: import org.jfolder.common.utils.misc.MiscHelper;
028: import org.jfolder.config.instance.ConfigInstanceTagHelper;
029:
030: //other classes
031:
032: public abstract class SystemStore {
033:
034: public final static String CURRENT_VERSION = "1.1";
035: public final static String SYSTEM_DATASOURCE = "/jdbc/JFolder/System";
036:
037: private final static String INITIALIZE_QUERY = "select "
038: + SystemDatabase.C_VERSION_T_SYSTEM + ", "
039: + SystemDatabase.C_DATABASE_T_SYSTEM + ", "
040: + SystemDatabase.C_APP_SERVER_T_SYSTEM + ", "
041: + SystemDatabase.C_SESSION_LOCATION_T_SYSTEM + ", "
042: + SystemDatabase.C_WRONG_DEPLOYS_T_SYSTEM + ", "
043: + SystemDatabase.C_ADMIN_CONTEXT_PATH_T_SYSTEM + ", "
044: + SystemDatabase.C_MACH_OF_ORIG_T_SYSTEM + ", "
045: + SystemDatabase.C_PERM_DIR_T_SYSTEM + ", "
046: + SystemDatabase.C_ORIG_TIME_T_SYSTEM + ", "
047: + SystemDatabase.C_CONFIG_RESET_T_SYSTEM + " from "
048: + getActualTableName(SystemDatabase.T_SYSTEM);
049:
050: //private Connection conn = null;
051: private static String version = null;
052: private static String database = null;
053: private static String appServer = null;
054: private static String sessionLocation = null;
055: private static String wrongDeploys = null;
056: private static String config = null;
057: //
058: private static String adminContextPath = null;
059: //
060: private static String machOfOrig = null;
061: private static String permDir = null;
062: private static BigDecimal origTime = null;
063: private static boolean configReset = false;
064: //
065: private static Boolean initialized = null;
066: private static boolean privilagedAccess = false;
067: //
068:
069: //
070: private StringBuffer currentStatement = null;
071: private int currentMargin = 0;
072:
073: protected SystemStore() {
074: //this.conn = getConnection();
075: }
076:
077: public static String getDisplayName() {
078: throw UnexpectedSystemException.notImplemented();
079: }
080:
081: public synchronized final static boolean isPrivaledgedAccess() {
082: return SystemStore.privilagedAccess;
083: }
084:
085: protected synchronized final static String getSessionLocation() {
086: return SystemStore.sessionLocation;
087: }
088:
089: public synchronized final static String getWrongDeploys() {
090: return SystemStore.wrongDeploys;
091: }
092:
093: protected synchronized final static String getAdminContextPath() {
094: return SystemStore.adminContextPath;
095: }
096:
097: public synchronized final static String getMachineOfOrigin() {
098: return SystemStore.machOfOrig;
099: }
100:
101: public synchronized final static String getPermissibleDirectories() {
102: return SystemStore.permDir;
103: }
104:
105: public synchronized final static BigDecimal getOrigTime() {
106: return SystemStore.origTime;
107: }
108:
109: public synchronized final static boolean isConfigReset() {
110: return SystemStore.configReset;
111: }
112:
113: public synchronized final static String getApplicationServer() {
114: return SystemStore.appServer;
115: }
116:
117: protected synchronized final static String getDatabaseClass() {
118: return SystemStore.database;
119: }
120:
121: public synchronized final static String getConfig() {
122:
123: return SystemStore.config;
124: }
125:
126: //public final static WorkflowServer getWorkflowServer() {
127: //}
128:
129: protected final static String convertToList(ArrayList inList) {
130: return convertToList(inList, ", ");
131: }
132:
133: protected final static String convertToList(ArrayList inList,
134: String inToken) {
135:
136: StringBuffer outValue = new StringBuffer();
137:
138: for (int j = 0; j < inList.size(); j++) {
139: if (j > 0) {
140: outValue.append(inToken);
141: }
142: outValue.append(inList.get(j));
143: }
144:
145: return outValue.toString();
146: }
147:
148: protected final static String convertToParenthesis(int inCode) {
149:
150: StringBuffer outValue = new StringBuffer();
151:
152: if (inCode > 0) {
153: for (int i = 0; i < inCode; i++) {
154: outValue.insert(0, "(");
155: }
156: } else if (inCode < 0) {
157: for (int i = 0; i < (-inCode); i++) {
158: outValue.append(")");
159: }
160: } else {
161: //do nothing
162: }
163:
164: return outValue.toString();
165: }
166:
167: protected void startStatement() {
168: this .currentStatement = new StringBuffer();
169: this .currentMargin = 0;
170: }
171:
172: private String getMargin() {
173:
174: StringBuffer outValue = new StringBuffer();
175:
176: for (int i = 0; i < this .currentMargin; i++) {
177: outValue.append(' ');
178: }
179:
180: return outValue.toString();
181: }
182:
183: protected ResultSet endStatement(Connection inConn,
184: ArrayList inTypes, ArrayList inValues,
185: PreparedStatement inPs[], int inUpdateCount[],
186: boolean inGetResultSet) throws SQLException {
187:
188: ResultSet outValue = null;
189:
190: PreparedStatement ps = null;
191: try {
192: if (inConn != null) {
193: //MiscHelper.println("<!--------------------------------->");
194: //MiscHelper.println("STATEMENT =\n" + this.currentStatement);
195: //MiscHelper.println("<!--------------------------------->");
196: ps = inConn.prepareStatement(this .currentStatement
197: .toString());
198: prepareParameters(ps, inTypes, inValues);
199: //ps.execute();
200:
201: //
202: if (inGetResultSet) {
203: //MiscHelper.println("Will get result set");
204: outValue = ps.executeQuery();
205: } else {
206: ps.execute();
207: }
208:
209: //
210: if (inPs != null) {
211: inPs[0] = ps;
212: }
213:
214: //
215: if (inUpdateCount != null) {
216: inUpdateCount[0] = ps.getUpdateCount();
217: }
218: }
219: } finally {
220: if (inPs == null) {
221: closeSQLObjects(null, ps, null);
222: }
223: }
224:
225: return outValue;
226: }
227:
228: protected abstract void prepareParameters(PreparedStatement inPs,
229: ArrayList inTypes, ArrayList inValues) throws SQLException;
230:
231: protected void justIndent() {
232: this .currentMargin = this .currentMargin + 4;
233: }
234:
235: protected void justRevert() {
236: this .currentMargin = this .currentMargin - 4;
237: }
238:
239: protected void printAndIndent(String inContent) {
240: this .currentStatement.append(getMargin());
241: this .currentStatement.append(inContent);
242: this .currentStatement.append('\n');
243: justIndent();
244: }
245:
246: protected void revertAndPrint(String inContent) {
247: justRevert();
248: this .currentStatement.append(getMargin());
249: this .currentStatement.append(inContent);
250: this .currentStatement.append('\n');
251: }
252:
253: protected void simpleAndPrint(String inContent) {
254: this .currentStatement.append(getMargin());
255: this .currentStatement.append(inContent);
256: this .currentStatement.append('\n');
257: }
258:
259: //
260: public abstract void execute(SystemStatement inWs, Connection inConn);
261:
262: //
263: public abstract void commit(Connection inConn);
264:
265: //
266: public synchronized final static boolean isInitialized() {
267:
268: return (SystemStore.initialized != null);
269: }
270:
271: public synchronized final static boolean isCorrectlyInitialized() {
272:
273: return (SystemStore.initialized != null && SystemStore.initialized
274: .booleanValue());
275: }
276:
277: public synchronized final static void initialize() {
278:
279: Connection conn = null;
280: PreparedStatement ps = null;
281: ResultSet rs = null;
282: boolean afterGetConnection = false;
283:
284: try {
285: conn = getConnection();
286:
287: afterGetConnection = true;
288:
289: ps = conn.prepareStatement(INITIALIZE_QUERY);
290: MiscHelper.println("SysStor InitQuery = "
291: + INITIALIZE_QUERY);
292: rs = ps.executeQuery();
293:
294: if (rs.next()) {
295: //
296: SystemStore.version = rs
297: .getString(SystemDatabase.C_VERSION_T_SYSTEM);
298: SystemStore.database = rs
299: .getString(SystemDatabase.C_DATABASE_T_SYSTEM);
300: SystemStore.appServer = rs
301: .getString(SystemDatabase.C_APP_SERVER_T_SYSTEM);
302: SystemStore.sessionLocation = rs
303: .getString(SystemDatabase.C_SESSION_LOCATION_T_SYSTEM);
304: SystemStore.wrongDeploys = rs
305: .getString(SystemDatabase.C_WRONG_DEPLOYS_T_SYSTEM);
306: SystemStore.adminContextPath = rs
307: .getString(SystemDatabase.C_ADMIN_CONTEXT_PATH_T_SYSTEM);
308: SystemStore.machOfOrig = rs
309: .getString(SystemDatabase.C_MACH_OF_ORIG_T_SYSTEM);
310: SystemStore.permDir = rs
311: .getString(SystemDatabase.C_PERM_DIR_T_SYSTEM);
312: SystemStore.origTime = rs
313: .getBigDecimal(SystemDatabase.C_ORIG_TIME_T_SYSTEM);
314: SystemStore.configReset = rs
315: .getBoolean(SystemDatabase.C_CONFIG_RESET_T_SYSTEM);
316: //
317: //
318: SystemStore ws = SystemStoreFactory.getSystemStore();
319: CreateStatement systemTable = SystemDatabase
320: .newInstance().createSystemTable();
321: SelectStatement wss = SelectStatement.createOrderById(
322: systemTable, null);
323: wss.addSelectColumn(SystemDatabase.C_CONFIG_T_SYSTEM,
324: systemTable, null);
325: //
326: ws.execute(wss, conn);
327: SystemResultSet wrs = wss.getResults();
328: //MiscHelper.println("wrs = " + wrs);
329: SystemStore.config = wrs.getLString(0, 1);
330: //
331: //
332: //SystemStore.adminContextPath = inContextPath;
333: //
334: SystemStore.initialized = new Boolean(true);
335: SystemStore.privilagedAccess = true;
336: }
337:
338: //MiscHelper.println("<!-------------------------------->");
339: //MiscHelper.println("VERSION = " + WorkflowStore.version);
340: //MiscHelper.println("DATABASE = " + WorkflowStore.database);
341: //MiscHelper.println("APPSERV = " + WorkflowStore.appServer);
342: //MiscHelper.println("<!-------------------------------->");
343: //try {
344: // Thread.sleep(20000);
345: //}
346: //catch (Exception e) {
347: //}
348: } catch (UnexpectedSystemException use) {
349: //MiscHelper.println(use.getMessage());
350: use.printStackTrace();
351: if (afterGetConnection) {
352: SystemStore.initialized = null;
353: } else {
354: SystemStore.initialized = new Boolean(false);
355: }
356: } catch (SQLException sqle) {
357: //MiscHelper.println(sqle.getMessage());
358: sqle.printStackTrace();
359: if (afterGetConnection) {
360: SystemStore.initialized = null;
361: } else {
362: SystemStore.initialized = new Boolean(false);
363: }
364: } finally {
365: closeSQLObjects(conn, ps, rs);
366: }
367: }
368:
369: //
370: public abstract void prepareConnection(Connection inConn)
371: throws SQLException;
372:
373: //
374: private final static Connection getConnection() {
375: return getConnection(null);
376: }
377:
378: //
379: public final static Connection getConnection(SystemStore inSs) {
380:
381: try {
382: Connection outValue = null;
383:
384: outValue = getGeneralConnection(SYSTEM_DATASOURCE);
385: if (inSs != null) {
386: inSs.prepareConnection(outValue);
387: }
388:
389: return outValue;
390: } catch (SQLException sqle) {
391: throw new UnexpectedSystemException(sqle);
392: }
393:
394: //try {
395: // //MiscHelper.writeMessage(SystemStore.class, new Exception());
396: // Connection outValue = null;
397: //
398: // Context context = new InitialContext();
399: // Object dsObject = context.lookup(SYSTEM_DATASOURCE);
400: // //Context context = new InitialContext("java:comp/env/");
401: // //Object dsObject = context.lookup(
402: // // "java:comp/env/jdbc/WorkflowLifecycle/DBStore");
403: // DataSource ds = (DataSource)PortableRemoteObject.narrow(
404: // dsObject, DataSource.class);
405: //
406: // outValue = ds.getConnection();
407: // if (inSs != null) {
408: // inSs.prepareConnection(outValue);
409: // }
410: //
411: // return outValue;
412: //}
413: //catch (SQLException sqle) {
414: // throw new UnexpectedSystemException(sqle);
415: //}
416: //catch (NamingException ne) {
417: // throw new UnexpectedSystemException(ne);
418: //}
419: }
420:
421: public final static Connection getGeneralConnection(
422: String inLocation) {
423:
424: try {
425: //MiscHelper.writeMessage(SystemStore.class, new Exception());
426: Connection outValue = null;
427:
428: Context context = new InitialContext();
429: Object dsObject = context.lookup("java:comp/env"
430: + inLocation);
431: //Context context = new InitialContext("java:comp/env/");
432: //Object dsObject = context.lookup(
433: // "java:comp/env/jdbc/WorkflowLifecycle/DBStore");
434: DataSource ds = (DataSource) PortableRemoteObject.narrow(
435: dsObject, DataSource.class);
436:
437: outValue = ds.getConnection();
438:
439: return outValue;
440: } catch (SQLException sqle) {
441: throw new UnexpectedSystemException(sqle);
442: } catch (NamingException ne) {
443: throw new UnexpectedSystemException(ne);
444: }
445: }
446:
447: //
448: public final static void closeSQLObjects(Connection inConn,
449: Statement inStatement, ResultSet inRs) {
450:
451: SQLException endSqle = null;
452:
453: //
454: try {
455: if (inRs != null) {
456: inRs.close();
457: }
458: } catch (SQLException sqle) {
459: endSqle = sqle;
460: }
461: //
462: try {
463: if (inStatement != null) {
464: inStatement.close();
465: }
466: } catch (SQLException sqle) {
467: endSqle = sqle;
468: }
469: //
470: try {
471: if (inConn != null) {
472: inConn.close();
473: }
474: } catch (SQLException sqle) {
475: endSqle = sqle;
476: }
477:
478: //
479: if (endSqle != null) {
480: throw new UnexpectedSystemException(endSqle);
481: }
482: }
483:
484: //
485: public final static String getActualTableName(String inName) {
486: return ("T_JF_" + inName);
487: }
488:
489: //
490: public final static String getActualSeriesSequenceName(String inName) {
491: return ("SS_JF_" + inName);
492: }
493:
494: //
495: //
496: //
497: //
498: //
499: //
500: public final static void createSystemEntry(SystemDatabase inSd,
501: SystemStore inSs, Connection inConn,
502: String inActualDatabaseClassName,
503: String inActualAppServerClassName,
504: String inSessionLocation, String inWrongDeploys,
505: String inAdminContextPath, String inMachOfOrig,
506: String inPermDir, BigDecimal inOrigTime,
507: boolean inConfigReset) {
508:
509: //
510: CreateStatement wcs = inSd.createSystemTable();
511:
512: //
513: InsertStatement wis = SystemStatement.newInsertStatement(wcs);
514: //
515: wis.setSStringColumn(SystemDatabase.C_VERSION_T_SYSTEM,
516: SystemStore.CURRENT_VERSION);
517: wis.setSStringColumn(SystemDatabase.C_DATABASE_T_SYSTEM,
518: inActualDatabaseClassName);
519: wis.setSStringColumn(SystemDatabase.C_APP_SERVER_T_SYSTEM,
520: inActualAppServerClassName);
521: wis.setSStringColumn(
522: SystemDatabase.C_SESSION_LOCATION_T_SYSTEM,
523: inSessionLocation);
524: wis.setSStringColumn(SystemDatabase.C_WRONG_DEPLOYS_T_SYSTEM,
525: inWrongDeploys);
526: wis.setSStringColumn(
527: SystemDatabase.C_ADMIN_CONTEXT_PATH_T_SYSTEM,
528: inAdminContextPath);
529: wis.setSStringColumn(SystemDatabase.C_MACH_OF_ORIG_T_SYSTEM,
530: inMachOfOrig);
531: wis.setSStringColumn(SystemDatabase.C_PERM_DIR_T_SYSTEM,
532: inPermDir);
533: wis.setDecimalColumn(SystemDatabase.C_ORIG_TIME_T_SYSTEM,
534: inOrigTime);
535: wis.setBooleanColumn(SystemDatabase.C_CONFIG_RESET_T_SYSTEM,
536: new Boolean(inConfigReset));
537: //
538: RootConceptTagHolder configRcth = ConfigInstanceTagHelper
539: .getDefault();
540: wis.setLStringColumn(SystemDatabase.C_CONFIG_T_SYSTEM,
541: configRcth.getXMLRepresentation(true));
542: //
543: inSs.execute(wis, inConn);
544: }
545: }
|