001: /*
002: * PJwrapper.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): suhler.
022: *
023: * Version: 1.5
024: * Created by suhler on 99/01/05
025: * Last modified by suhler on 00/05/31 13:52:32
026: */
027:
028: package sunlabs.brazil.util;
029:
030: import java.lang.reflect.Method;
031: import java.lang.reflect.InvocationTargetException;
032:
033: /**
034: * Wrap a pJama invocation, to allow for persistent operation.
035: * pJama is an implementation of orthoginal persistance, allowin session
036: * state to be recovered after server shut-downs.
037: * See <a href=http://www.sun.com/research/forest/>here</a>
038: * for more information.
039: * This utility class allows a server application to run with the pJama
040: * persistent VM by dynamically locating and binding the classes at run time.
041: * If pJama is not available, a standard <b>transient</b> implementation is
042: * used instead.
043: *
044: * @author Stephen Uhler
045: * @version @(#) PJwrapper.java 1.4 99/08/06 12:31:42
046: *
047: */
048:
049: /*
050: * To use with the persistent store, identify a "root" object for persistence.
051: * This is normally a static reference:
052: * Static Type root= null; // <Type> is the objects class
053: *
054: * When you are ready to start the store:
055: * if (root == null) {
056: * root = initStore("some identifying string", Type.class)
057: * }
058: *
059: * Any time you want stabilization, call:
060: * stabilize()
061: *
062: * If the store didn't start, you still get a valid instance of <root>,
063: * but isPersistent() returns false, and getError() returns why.
064: */
065:
066: public class PJwrapper {
067: private static boolean persistent = false; // is persistence on?
068: private static String error = null; // last error message
069: private static Class pClass;
070:
071: private PJwrapper() {
072: }
073:
074: /**
075: * See if we are using persistence.
076: * @return true, is persistence is enabled.
077: */
078:
079: public static boolean isPersistent() {
080: return persistent;
081: }
082:
083: /**
084: * Return the cause of most recent failure.
085: */
086:
087: public static String getError() {
088: return error;
089: }
090:
091: /**
092: * Do a one-time store initialization,
093: * @param The string name to identify this store
094: * @param The type of the object to be the store root
095: * @return The root object. If one didn't exist, it is
096: * created with newInstance()
097: */
098:
099: public static Object initStore(String name, Class type) {
100: Object root = null;
101: Object store = getStore();
102: if (store == null) {
103: try {
104: root = (Object) (type.newInstance());
105: } catch (Exception e) {
106: error = e.toString();
107: }
108: return root;
109: }
110: try {
111: Class[] rootParams = { java.lang.String.class };
112: Object[] rootArgs = { name };
113: Method rootMethod = pClass
114: .getMethod("getPRoot", rootParams);
115: root = (Object) rootMethod.invoke(store, rootArgs);
116: if (root == null) {
117: Class[] newParams = { java.lang.String.class,
118: java.lang.Object.class };
119: Object[] newArgs = { name, null };
120: root = (Object) type.newInstance();
121: newArgs[1] = root;
122: Method newMethod = pClass.getMethod("newPRoot",
123: newParams);
124: newMethod.invoke(store, newArgs);
125: }
126: persistent = true;
127: } catch (Exception e) {
128: error = e.toString();
129: try {
130: root = (Object) (type.newInstance());
131: } catch (Exception e2) {
132: error += " " + e2.toString();
133: }
134: }
135: return root;
136: }
137:
138: /**
139: * Stabilize the store
140: * @return true if successful. Use getError() to extract
141: * reason for failure
142: */
143:
144: public static boolean stabilize() {
145: boolean result = false;
146: if (!persistent) {
147: return false;
148: }
149: try {
150: Object store = getStore();
151: Class[] stabilizeParams = new Class[0];
152: Object[] stabilizeArgs = new Object[0];
153: Method stableMethod = pClass.getMethod("stabilizeAll",
154: stabilizeParams);
155: stableMethod.invoke(store, stabilizeArgs);
156: result = true;
157: } catch (Exception e) {
158: error = e.toString();
159: }
160: return result;
161: }
162:
163: /**
164: * get an initial store reference
165: * @return null, if no store available
166: */
167:
168: private static Object getStore() {
169: Object store = null;
170: try {
171: pClass = Class.forName("org.opj.store.PJStoreImpl");
172: Class[] storeParams = new Class[0];
173: Object[] storeArgs = new Object[0];
174: Method storeMethod = pClass.getMethod("getStore",
175: storeParams);
176: store = storeMethod.invoke(null, storeArgs);
177: persistent = true;
178: } catch (InvocationTargetException e) {
179: error = e.getTargetException().toString();
180: } catch (Exception e) {
181: error = e.toString();
182: }
183: return store;
184: }
185: }
|