001: package org.objectweb.salome_tmf.api;
002:
003: import java.io.File;
004: import java.net.URL;
005: import java.util.Properties;
006: import java.util.Vector;
007:
008: import org.objectweb.salome_tmf.api.data.ConnectionWrapper;
009: import org.objectweb.salome_tmf.api.sql.ISQLEngine;
010: import org.objectweb.salome_tmf.api.sql.ISQLObjectFactory;
011:
012: public class Api {
013: /* SQLENGINE */
014: static ISQLObjectFactory pISQLObjectFactory = null;
015: static ISQLEngine pISQLEngine = null;
016:
017: /* Config */
018: static Config pConfig = new Config();
019:
020: static boolean isLocked = false;
021: static boolean isAlreadyConnected = false;
022: static java.net.URL urlBase = null;
023: static java.net.URL urlCodeBase = null;
024:
025: /* Listener */
026: static ChangeDispatcher changeDispatcher = null;
027: //static PluginDispatcher pluginDispatcher = null;
028: static Thread listenerThread = null;
029: static ISafeThread pChangeListener = null;
030: static int _pid = -1;
031:
032: static String mail_host = "";
033: static String url;
034: static String user;
035: static String password;
036: static String driver;
037: static Object verrou = new Object();
038: static int nbConnect = 0;
039: //public final static String VERSION = "3.0";
040:
041: public final static String DEFAULT_LOGIN = "LoginSalome";
042: public final static String DEFAULT_PASSWORD = "LoginSalome";
043: static String strUsername = null;
044: static String strMD5Password = null;
045:
046: public static String getStrUsername() {
047: return strUsername;
048: }
049:
050: public static String getStrMD5Password() {
051: return strMD5Password;
052: }
053:
054: public static void setStrUsername(String strUsername) {
055: Api.strUsername = strUsername;
056: }
057:
058: public static void setStrMD5Password(String strMD5Password) {
059: Api.strMD5Password = strMD5Password;
060: }
061:
062: public static String getUrl() {
063: return url;
064: }
065:
066: public static String getUrlBase() {
067: return urlBase.toString();
068: }
069:
070: public static String getUser() {
071: return user;
072: }
073:
074: public static String getPassword() {
075: return password;
076: }
077:
078: public static String getDriver() {
079: return driver;
080: }
081:
082: static boolean isSOAP = false;
083:
084: public static boolean isSOAP() {
085: return isSOAP;
086: }
087:
088: /**
089: * Fonction qui établit la connextion à la base de données SalomeTMF
090: */
091: public static boolean openConnection(java.net.URL _urlBase) {
092: synchronized (verrou) {
093:
094: createTMP();
095: if (isAlreadyConnected) {
096: Util
097: .log("[API : openConnection] Connection already exist");
098: return isAlreadyConnected;
099: }
100: isAlreadyConnected = true;
101: //nbConnect ++;
102: try {
103: String url_txt = _urlBase.toString();
104: url_txt = url_txt
105: .substring(0, url_txt.lastIndexOf("/"));
106: //boolean key = false;
107: urlBase = new java.net.URL(url_txt);
108:
109: changeDispatcher = new ChangeDispatcher();
110: //pluginDispatcher = new PluginDispatcher();
111: _pid = (int) ((changeDispatcher.hashCode()) * (System
112: .currentTimeMillis()));
113:
114: if (pISQLEngine == null) {
115: // Ouverture du fichier "properties" contenant les paramètres de configuration pour la connexion à la BdD
116: Util.log("[Api] Applet base is " + urlBase);
117: java.net.URL url_salome = new java.net.URL(urlBase
118: + ApiConstants.CONFIG_FILE_PATH);
119: Util.log("[Api] Try to find properties on "
120: + url_salome);
121: Properties prop = null;
122: try {
123: prop = Util.getPropertiesFile(url_salome);
124: Util.log("[Api] Properties from Url is : "
125: + prop);
126: } catch (Exception e) {
127: Util
128: .log("[Api] WARNING JAR FILE PROPERTIES SELECTED : "
129: + e);
130: prop = Util
131: .getPropertiesFile(ApiConstants.CONFIG_FILE_PATH);
132: Util.log("[Api] Properties from jar is : "
133: + prop);
134: }
135: try {
136: String debug = prop.getProperty("Debug");
137: if (debug.equals("true")) {
138: pConfig.setDEBUG(true);
139: } else {
140: pConfig.setDEBUG(false);
141: }
142: Util.log("[Api] Debug value is " + debug);
143: } catch (Exception e) {
144: pConfig.setDEBUG(false);
145: e.printStackTrace();
146: }
147: //pConfig.setDEBUG(true);
148: String allow_plugins = prop
149: .getProperty("AllowPlugins");
150: if (allow_plugins.equals("true")) {
151: pConfig.setALLOW_PLUGINS(true);
152: } else {
153: pConfig.setALLOW_PLUGINS(false);
154: }
155:
156: String ide_dev = prop.getProperty("IDE_Dev");
157: if (ide_dev.equals("true")) {
158: pConfig.setIDE_DEV(true);
159: Permission.setPermission(254);
160: } else {
161: pConfig.setIDE_DEV(false);
162: }
163: Util.log("[Api] IDE_DEV value is " + ide_dev);
164:
165: String net_change_track = prop
166: .getProperty("NetChangeTrack");
167: if (net_change_track.equals("true")) {
168: pConfig.setNET_CHANGE_TRACK(true);
169: } else {
170: pConfig.setNET_CHANGE_TRACK(false);
171: }
172: Util.log("[Api] NetChangeTrack value is "
173: + net_change_track);
174:
175: try {
176: String with_ical = prop.getProperty("WithICAL");
177: if (with_ical.equals("true")) {
178: pConfig.setWITH_ICAL(true);
179: } else {
180: pConfig.setWITH_ICAL(false);
181: }
182: Util.log("[Api] WithICAL value is "
183: + with_ical);
184: } catch (Exception e) {
185: pConfig.setWITH_ICAL(false);
186: }
187:
188: try {
189: String mail_server = prop
190: .getProperty("MAIL_HOST");
191: mail_host = mail_server;
192: Util.log("[Api] MAIL_HOST value is "
193: + mail_server);
194: } catch (Exception e) {
195: mail_host = null;
196: }
197:
198: try {
199: String lockOnExeC = prop
200: .getProperty("LockOnTestExec");
201: if (lockOnExeC.equals("true")) {
202: pConfig.setLockOnExec(true);
203: } else {
204: pConfig.setLockOnExec(false);
205: }
206: Util.log("[Api] LockOnTestExec value is "
207: + lockOnExeC);
208: } catch (Exception e) {
209: pConfig.setLockOnExec(true);
210: }
211:
212: try {
213: int lock_meth = Integer.parseInt(prop
214: .getProperty("DBLOCK"));
215: //0 = NONE, 1 = mysql, 2 = salome
216: lock_meth--;
217: pConfig.setUSE_DB_LOCK(lock_meth);
218: Util.log("[Api] DBLOCK value is " + lock_meth);
219: } catch (Exception e) {
220: pConfig.setUSE_DB_LOCK(1);
221: }
222:
223: try {
224: String lockTestIfExecuted = prop
225: .getProperty("LockExecutedTest");
226: if (lockTestIfExecuted.equals("true")) {
227: pConfig.setLockExecutedTest(true);
228: } else {
229: pConfig.setLockExecutedTest(false);
230: }
231: Util.log("[Api] LockExecutedTest value is "
232: + lockTestIfExecuted);
233: } catch (Exception e) {
234: pConfig.setLockExecutedTest(true);
235: }
236:
237: try {
238: String useDynamicValueForDataSet = prop
239: .getProperty("DynamicDataSetValueFromEnv");
240: if (useDynamicValueForDataSet.equals("true")) {
241: pConfig.setDYNAMIC_VALUE_DATASET(true);
242: } else {
243: pConfig.setDYNAMIC_VALUE_DATASET(false);
244: }
245: Util
246: .log("[Api] DynamicDataSetValueFromEnv value is "
247: + useDynamicValueForDataSet);
248: } catch (Exception e) {
249: pConfig.setDYNAMIC_VALUE_DATASET(true);
250: }
251:
252: driver = prop.getProperty("DriverJDBC");
253: String engine = prop.getProperty("SQLEngine");
254: String engineSOAP = prop
255: .getProperty("SQLEngineSOAP");
256:
257: url = prop.getProperty("URL");
258: user = prop.getProperty("User");
259: password = prop.getProperty("Password");
260: if (!pConfig.isIDE_DEV()) {
261: Util.log("[Api] Decripte : " + password);
262: try {
263: MD5paswd.readkey(urlBase);
264: password = MD5paswd.decryptString(password);
265: Util.log("[Api] Password is crypted");
266: } catch (Exception e) {
267: Util.log("[Api] Password isn't crypted");
268: }
269: }
270: Util.log("[Api] Open connexion on " + url
271: + ", with pid : " + _pid);
272:
273: // Sélection de la factory en fonction de la version (SQL ou SOAP)
274: try {
275: pISQLObjectFactory = (ISQLObjectFactory) Class
276: .forName(engineSOAP).newInstance();
277: isSOAP = true;
278: Util.log("[Api] version : SOAP");
279: } catch (ClassNotFoundException e) {
280: pISQLObjectFactory = (ISQLObjectFactory) Class
281: .forName(engine).newInstance();
282: isSOAP = false;
283: Util.log("[Api] version : SQL");
284: }
285:
286: Util.log("[Api] get ISQLObjectFactory = "
287: + pISQLObjectFactory);
288: pISQLEngine = pISQLObjectFactory
289: .getInstanceOfSQLEngine(url, user,
290: password, changeDispatcher, _pid,
291: driver, pConfig.getUSE_DB_LOCK());
292: pConfig.init(pISQLObjectFactory);
293:
294: try {
295: urlCodeBase = new URL(prop
296: .getProperty("CodeBase"));
297: } catch (Exception e) {
298: //e.printStackTrace();
299: Util
300: .log("[API->openConnection] No boot strap found");
301: urlCodeBase = _urlBase;
302: }
303:
304: } else {
305: pISQLEngine = pISQLObjectFactory
306: .getInstanceOfSQLEngine(url, user,
307: password, changeDispatcher, _pid,
308: driver, pConfig.getUSE_DB_LOCK());
309: pConfig.init(pISQLObjectFactory);
310: }
311: nbConnect++;
312: }
313:
314: catch (Exception E) {
315: E.printStackTrace();
316: isAlreadyConnected = false;
317: }
318: }
319: return isAlreadyConnected;
320: }
321:
322: public static URL getCodeBase() {
323: return urlCodeBase;
324: }
325:
326: public static String getVersion() {
327: return pISQLObjectFactory.getSalomeVersion();
328: }
329:
330: public static boolean isConnected() {
331: return isAlreadyConnected;
332: }
333:
334: public static int getNbConnect() {
335: synchronized (verrou) {
336: return nbConnect;
337: }
338: }
339:
340: public static void closeConnection() {
341: synchronized (verrou) {
342: try {
343: Util
344: .log("[Api->closeConnection] Try to close connexion ");
345: if (pChangeListener != null) {
346: pChangeListener.safe_stop();
347: }
348: Thread.sleep(2000);
349: listenerThread = null;
350: //pluginDispatcher = null;
351: closeDB();
352: isAlreadyConnected = false;
353: nbConnect--;
354: } catch (Exception e) {
355: e.printStackTrace();
356: }
357: }
358: }
359:
360: public static int beginTransaction(int lock_code, int type)
361: throws Exception {
362: if (pISQLEngine != null) {
363: return pISQLEngine.beginTransDB(lock_code, type);
364: }
365: return -1;
366: }
367:
368: public static void commitTrans(int code) throws Exception {
369: if (pISQLEngine != null) {
370: pISQLEngine.commitTransDB(code);
371: }
372: }
373:
374: public static void forceRollBackTrans(int code) {
375: if (pISQLEngine != null) {
376: try {
377: pISQLEngine.rollForceBackTransDB(code);
378: } catch (Exception e) {
379: e.printStackTrace();
380: }
381: }
382: }
383:
384: public static synchronized void lockTestPlan() throws Exception {
385: if (isLocked == false) {
386: if (pConfig.isLockOnExec()) {
387: if (pISQLEngine != null) {
388: pISQLEngine.lockTPlan();
389: }
390: }
391: isLocked = true;
392: } else {
393: throw new Exception(
394: "[Api->lockTestPlan] Database is already locked");
395: }
396: }
397:
398: public static synchronized void unLockTestPlan() throws Exception {
399: if (isLocked == true) {
400: if (pConfig.isLockOnExec()) {
401: if (pISQLEngine != null) {
402: pISQLEngine.unLockTPlan();
403: }
404: }
405: isLocked = false;
406: } else {
407: throw new Exception(
408: "[Api->lockTestPlan] Database is not locked");
409: }
410: }
411:
412: public static void closeDB() {
413: if (!isSOAP()) {
414: Util
415: .log("[Api:closeDB] Try to close the database by SQLEngine");
416: if (pISQLEngine != null) {
417: try {
418: pISQLEngine.close();
419: Util.log("[Api:closeDB] Database is closed");
420: } catch (Exception e) {
421: Util
422: .log("[Api:closeDB] Error when close the database, database is not closed");
423: }
424: }
425: }
426: pISQLEngine = null;
427: deleteTMP();
428: }
429:
430: public static void setUrlBase(URL _urlBase) {
431: try {
432: String url_txt = _urlBase.toString();
433: url_txt = url_txt.substring(0, url_txt.lastIndexOf("/"));
434: urlBase = new java.net.URL(url_txt);
435: } catch (Exception e) {
436: e.printStackTrace();
437: }
438: }
439:
440: /* ISQLObjectFactory*/
441:
442: public static void regiterSQLObjectFactory(
443: ISQLObjectFactory pSQLObjectFactory) {
444: pISQLObjectFactory = pSQLObjectFactory;
445: }
446:
447: static public ISQLObjectFactory getISQLObjectFactory() {
448: return pISQLObjectFactory;
449: }
450:
451: static public void addWatchListener(java.util.Observer o,
452: String projet) {
453: changeDispatcher.addObserver(o);
454: if (listenerThread == null) {
455: if (pConfig.isNET_CHANGE_TRACK()) {
456: pChangeListener = pISQLObjectFactory
457: .getChangeListener(projet);
458: listenerThread = new Thread(pChangeListener);
459: listenerThread.start();
460: } else {
461: pISQLObjectFactory.changeListenerProject(projet);
462: }
463: }
464: }
465:
466: static public void runListenerThread(String projet) {
467: try {
468: if (listenerThread == null) {
469: pChangeListener = pISQLObjectFactory
470: .getChangeListener(projet);
471: listenerThread = new Thread(pChangeListener);
472: listenerThread.start();
473: } else {
474: pChangeListener.safe_restart();
475: listenerThread = new Thread(pChangeListener);
476: listenerThread.start();
477: }
478: } catch (Exception e) {
479: }
480: }
481:
482: static public void stopListenerThread(String projet) {
483: try {
484: if (listenerThread != null) {
485: pChangeListener.safe_stop();
486: }
487: } catch (Exception e) {
488:
489: }
490: }
491:
492: /* Config */
493: public static String getUsedLocale() {
494: return pConfig.getUsedLocale();
495: }
496:
497: public static void saveLocale(String locale) {
498: pConfig.saveLocale(locale.toLowerCase().trim());
499: }
500:
501: public static Vector getLocales() {
502: return pConfig.getLocales();
503: }
504:
505: public static boolean isDEBUG() {
506: return pConfig.isDEBUG();
507: }
508:
509: public static boolean isIDE_DEV() {
510: return pConfig.isIDE_DEV();
511: }
512:
513: public static boolean isALLOW_PLUGINS() {
514: return pConfig.isALLOW_PLUGINS();
515: }
516:
517: public static boolean isNET_CHANGE_TRACK() {
518: return pConfig.isNET_CHANGE_TRACK();
519: }
520:
521: public static boolean isWith_ICAL() {
522: return pConfig.isWITH_ICAL();
523: }
524:
525: public static boolean isLockExecutedTest() {
526: return pConfig.isLockExecutedTest();
527: }
528:
529: public static boolean isDYNAMIC_VALUE_DATASET() {
530: return pConfig.isDYNAMIC_VALUE_DATASET();
531: }
532:
533: /**
534: * @return -1 = NONE, 0 = mysql, 1 = salome
535: */
536: public static int getLockMeth() {
537: return pConfig.getUSE_DB_LOCK();
538: }
539:
540: /* Session */
541: public static void deleteConnection(int idConn) {
542: try {
543: pISQLObjectFactory.getISQLSession().deleteSession(idConn);
544: } catch (Exception e) {
545: Util.log("[Api->deleteConnection] Error on session "
546: + idConn);
547: e.printStackTrace();
548: }
549: }
550:
551: public static String getConnectionProject(int IdConnection) {
552: String project = null;
553: try {
554: ConnectionWrapper pConnectionWrapper = pISQLObjectFactory
555: .getISQLSession().getSession(IdConnection);
556: project = pConnectionWrapper.getProjectConnected();
557: String login = pConnectionWrapper.getLoginConnected();
558:
559: int permission = 0;
560: if (!project.equals("ALL")) {
561: int idProject = pISQLObjectFactory.getISQLProject()
562: .getProject(project).getIdBDD();
563: permission = pISQLObjectFactory.getISQLPersonne()
564: .getPermissionOfUser(idProject, login);
565:
566: } else {
567: permission |= Permission.ALLOW_CREATE_TEST;
568: permission |= Permission.ALLOW_CREATE_CAMP;
569: permission |= Permission.ALLOW_UPDATE_CAMP;
570: permission |= Permission.ALLOW_UPDATE_TEST;
571: permission |= Permission.ALLOW_EXECUT_CAMP;
572: permission |= Permission.ALLOW_DELETE_CAMP;
573: permission |= Permission.ALLOW_DELETE_TEST;
574: }
575: Permission.setPermission(permission);
576:
577: } catch (Exception ex) {
578: Util.log("[Api->getConnectionProject] Error on session "
579: + IdConnection);
580: ex.printStackTrace();
581: }
582: return project;
583: }
584:
585: public static void initConnectionUser(String project, String login) {
586: try {
587:
588: int permission = 0;
589: if (!project.equals("ALL")) {
590: int idProject = pISQLObjectFactory.getISQLProject()
591: .getProject(project).getIdBDD();
592: permission = pISQLObjectFactory.getISQLPersonne()
593: .getPermissionOfUser(idProject, login);
594:
595: } else {
596: permission |= Permission.ALLOW_CREATE_TEST;
597: permission |= Permission.ALLOW_CREATE_CAMP;
598: permission |= Permission.ALLOW_UPDATE_CAMP;
599: permission |= Permission.ALLOW_UPDATE_TEST;
600: permission |= Permission.ALLOW_EXECUT_CAMP;
601: permission |= Permission.ALLOW_DELETE_CAMP;
602: permission |= Permission.ALLOW_DELETE_TEST;
603: }
604: Permission.setPermission(permission);
605:
606: } catch (Exception ex) {
607: Util
608: .log("[Api->getConnectionProject] Error on set Permission ");
609: ex.printStackTrace();
610: }
611: }
612:
613: /**
614: * Selection du login pour une connection
615: *
616: * @param idConnection
617: */
618: public static String getConnectionUser(int IdConnection) {
619: String login = null;
620: try {
621: ConnectionWrapper pConnectionWrapper = pISQLObjectFactory
622: .getISQLSession().getSession(IdConnection);
623: login = pConnectionWrapper.getLoginConnected();
624: String project = pConnectionWrapper.getProjectConnected();
625: int permission = 0;
626: if (!project.equals("ALL")) {
627: int idProject = pISQLObjectFactory.getISQLProject()
628: .getProject(project).getIdBDD();
629: permission = pISQLObjectFactory.getISQLPersonne()
630: .getPermissionOfUser(idProject, login);
631:
632: } else {
633: permission |= Permission.ALLOW_CREATE_TEST;
634: permission |= Permission.ALLOW_CREATE_CAMP;
635: permission |= Permission.ALLOW_UPDATE_CAMP;
636: permission |= Permission.ALLOW_UPDATE_TEST;
637: permission |= Permission.ALLOW_EXECUT_CAMP;
638: permission |= Permission.ALLOW_DELETE_CAMP;
639: permission |= Permission.ALLOW_DELETE_TEST;
640: }
641: Permission.setPermission(permission);
642: } catch (Exception ex) {
643: Util.log("[Api->getConnectionUser] Error on session "
644: + IdConnection);
645: ex.printStackTrace();
646: }
647: return login;
648: }
649:
650: static void createTMP() {
651: try {
652: String tmpDir = System.getProperties().getProperty(
653: "java.io.tmpdir");
654: String fs = System.getProperties().getProperty(
655: "file.separator");
656: File file = new File(tmpDir + fs
657: + ApiConstants.PATH_TO_ADD_TO_TEMP);
658: if (!file.exists()) {
659: file.mkdirs();
660: } else {
661: clearDir(file);
662: }
663: } catch (Exception e) {
664: if (isDEBUG()) {
665: e.printStackTrace();
666: }
667: }
668: }
669:
670: static boolean deleteDir(File dir) {
671: if (dir.isDirectory()) {
672: String[] children = dir.list();
673: for (int i = 0; i < children.length; i++) {
674: boolean success = deleteDir(new File(dir, children[i]));
675: if (!success) {
676: return false;
677: }
678: }
679: }
680: // The directory is now empty so delete it
681: return dir.delete();
682: }
683:
684: static boolean clearDir(File dir) {
685: if (dir.isDirectory()) {
686: String[] children = dir.list();
687: for (int i = 0; i < children.length; i++) {
688: boolean success = deleteDir(new File(dir, children[i]));
689: if (!success) {
690: return false;
691: }
692: }
693: }
694: return true;
695: }
696:
697: static void deleteTMP() {
698: try {
699: String tmpDir = System.getProperties().getProperty(
700: "java.io.tmpdir");
701: String fs = System.getProperties().getProperty(
702: "file.separator");
703: File file = new File(tmpDir + fs
704: + ApiConstants.PATH_TO_ADD_TO_TEMP);
705: deleteDir(file);
706: } catch (Exception e) {
707: if (isDEBUG()) {
708: e.printStackTrace();
709: }
710: }
711: }
712: }
|