001: package org.enhydra.shark.webclient;
002:
003: import java.io.File;
004: import java.util.ArrayList;
005: import java.util.List;
006:
007: import javax.transaction.UserTransaction;
008:
009: import org.enhydra.shark.client.utilities.LimitStruct;
010: import org.enhydra.shark.client.utilities.SharkInterfaceWrapper;
011: import org.enhydra.shark.utilities.MiscUtilities;
012: import org.enhydra.shark.webclient.business.DeadlineCheckerWrapper;
013: import org.enhydra.shark.webclient.business.LimitCheckerWrapper;
014: import org.enhydra.shark.webclient.business.QuartzInitializer;
015: import org.enhydra.shark.webclient.business.SharkUtils;
016:
017: import com.lutris.appserver.server.ApplicationException;
018: import com.lutris.appserver.server.Enhydra;
019: import com.lutris.appserver.server.StandardApplication;
020: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
021: import com.lutris.logging.Logger;
022: import com.lutris.util.Config;
023:
024: /**
025: * The application object. Application-wide data would go here.
026: */
027: public class SharkWebClient extends StandardApplication {
028:
029: public static SharkWebClient me;
030:
031: /**
032: * Shark config file
033: */
034: protected static String SHARK_CONFIG_FILE = "";
035:
036: protected DeadlineCheckerWrapper deadlineChecker = null;
037:
038: protected LimitCheckerWrapper limitChecker = null;
039:
040: protected boolean doProfile = false;
041:
042: /**
043: * Request pre-processing profiling level Negative - profile everything Zero - profile
044: * nothing Positive - profile only processing that lasted longer than specified number
045: * of milliseconds
046: */
047: protected int profileLevel = 0;
048:
049: /*
050: * A few methods you might want to add to. See StandardApplication for more details.
051: */
052:
053: public void startup(Config appConfig) throws ApplicationException {
054: super .startup(appConfig);
055: try {
056: Enhydra.register(this );
057: // Here is where you would read application-specific settings from
058: // your config file.
059:
060: // Profiling Initialization
061: try {
062: SharkWebClient.SHARK_CONFIG_FILE = appConfig.getString(
063: "SharkWebClient.Shark.confFile", "");
064: profileLevel = appConfig.getInt("TimeProfiler", 0);
065: if (profileLevel != 0) {
066: doProfile = true;
067: }
068:
069: } catch (Exception ex) {
070: }
071:
072: try {
073:
074: if (SHARK_CONFIG_FILE != null
075: && !SHARK_CONFIG_FILE.equals("")) {
076: File cFile = new File(SHARK_CONFIG_FILE);
077: // if file don't exist, try to resolve relative path
078: if (!cFile.exists()) {
079: cFile = new File(appConfig.getConfigFile()
080: .getFile().getParentFile()
081: .getParentFile().getCanonicalPath()
082: + "/" + SHARK_CONFIG_FILE);
083: }
084:
085: if (!cFile.exists())
086: throw new ApplicationException(
087: "Configuration file does not exist:"
088: + cFile);
089:
090: String cfp = cFile.getAbsolutePath();
091: SharkInterfaceWrapper.setProperties(cfp, false);
092: SharkInterfaceWrapper.getShark();
093:
094: } else {
095:
096: throw new ApplicationException(
097: "Shark configuration file is not defined");
098: }
099:
100: UserTransaction ut = SharkInterfaceWrapper
101: .getUserTransaction();
102: ut.begin();
103: SharkUtils.uploadPackage(appConfig);
104: ut.commit();
105:
106: if (appConfig.getBoolean(
107: "SharkWebClient.StartDeadlineChecker", false)) {
108: double dcTime = appConfig.getDouble(
109: "SharkWebClient.DeadlinePollingTime", 5);
110: long dt = (int) (dcTime * 60000);
111: int dcipt = appConfig
112: .getInt(
113: "SharkWebClient.DeadlineCheck.InstancesPerTransaction",
114: 100);
115: int dcfti = appConfig
116: .getInt(
117: "SharkWebClient.DeadlineCheck.FailuresToIgnore",
118: 10);
119: String[] lcStructs = appConfig
120: .getStrings("SharkWebClient.DeadlineStruct");
121: List lcStrLst = new ArrayList();
122: if (lcStructs != null) {
123: for (int i = 0; i < lcStructs.length; i++) {
124: String lcstruct = lcStructs[i];
125: try {
126: String[] hma = MiscUtilities.tokenize(
127: lcstruct, ":");
128: String h = hma[0];
129: String m = hma[1];
130: String a = hma[2];
131: lcStrLst.add(new LimitStruct(Integer
132: .parseInt(h), Integer
133: .parseInt(m), Integer
134: .parseInt(a)));
135: } catch (Exception ex) {
136: logChannel
137: .write(
138: Logger.ERROR,
139: "Deadline struct parameter "
140: + lcstruct
141: + " is invalid - it won't be evaluated!",
142: ex);
143: }
144: }
145: }
146:
147: deadlineChecker = new DeadlineCheckerWrapper(this ,
148: SharkInterfaceWrapper.getDefaultUser(),
149: lcStrLst, dt, dcipt, dcfti, true);
150: }
151:
152: if (appConfig.getBoolean(
153: "SharkWebClient.StartLimitChecker", false)) {
154: double dcTime = appConfig.getDouble(
155: "SharkWebClient.LimitPollingTime", 5);
156: long dt = (int) (dcTime * 60000);
157: int dcipt = appConfig
158: .getInt(
159: "SharkWebClient.LimitCheck.InstancesPerTransaction",
160: 100);
161: int dcfti = appConfig
162: .getInt(
163: "SharkWebClient.LimitCheck.FailuresToIgnore",
164: 10);
165: String[] lcStructs = appConfig
166: .getStrings("SharkWebClient.LimitStruct");
167: List lcStrLst = new ArrayList();
168: if (lcStructs != null) {
169: for (int i = 0; i < lcStructs.length; i++) {
170: String lcstruct = lcStructs[i];
171: try {
172: String[] hma = MiscUtilities.tokenize(
173: lcstruct, ":");
174: String h = hma[0];
175: String m = hma[1];
176: String a = hma[2];
177: lcStrLst.add(new LimitStruct(Integer
178: .parseInt(h), Integer
179: .parseInt(m), Integer
180: .parseInt(a)));
181: } catch (Exception ex) {
182: logChannel
183: .write(
184: Logger.ERROR,
185: "Limit struct parameter "
186: + lcstruct
187: + " is invalid - it won't be evaluated!",
188: ex);
189: }
190: }
191: }
192: limitChecker = new LimitCheckerWrapper(this ,
193: SharkInterfaceWrapper.getDefaultUser(),
194: lcStrLst, dt, dcipt, dcfti, true);
195: }
196:
197: SharkUtils.init(appConfig);
198:
199: me = this ;
200:
201: try {
202: if (appConfig.getBoolean("startQuartz")) {
203: QuartzInitializer
204: .startQuartzScheduler(appConfig
205: .getString("SharkWebClient.quartzConfPath"));
206: }
207: } catch (Exception ex) {
208: }
209:
210: } catch (Exception ex) {
211: throw new ApplicationException(
212: "Shark initialization failed!", ex);
213: }
214:
215: } finally {
216: Enhydra.unRegister();
217: }
218:
219: }
220:
221: public void shutdown() {
222: super .shutdown();
223: if (deadlineChecker != null) {
224: deadlineChecker.stopChecker();
225: }
226: if (limitChecker != null) {
227: limitChecker.stopChecker();
228: }
229:
230: QuartzInitializer.stopQuartzScheduler();
231: }
232:
233: public boolean requestPreprocessor(HttpPresentationComms comms)
234: throws Exception {
235: return super .requestPreprocessor(comms);
236: }
237:
238: /**
239: * This is an optional function, used only by the Multiserver's graphical
240: * administration. This bit of HTML appears in the status page for this application.
241: * You could add extra status info, for example a list of currently logged in users.
242: *
243: * @return HTML that is displayed in the status page of the Multiserver.
244: */
245: public String toHtml() {
246: return "This is <I>SharkWebClient</I>";
247: }
248:
249: public boolean isProfilingTurnedOn() {
250: return doProfile;
251: }
252:
253: public int getProfileLevel() {
254: return profileLevel;
255: }
256:
257: }
|