001: /*
002: * Copyright (c) %G% Sun Microsystems, Inc. All Rights Reserved.
003: *
004: *
005: * $Id: GWLogManager.java,v 1.27 2005/09/21 11:07:59 dg154973 Exp $
006: * $Source: /m/portal/ps/srap/src/com/sun/portal/util/GWLogManager.java,v $
007: * $Log: GWLogManager.java,v $
008: * Revision 1.27 2005/09/21 11:07:59 dg154973
009: * CR 6280402 - log system results in excessive lock contention
010: *
011: * Revision 1.26 2005/08/30 08:59:13 ss150821
012: * 6317346 GW fails after PS restart. when polling is disabled
013: *
014: * Revision 1.25 2005/03/01 10:21:36 np145014
015: * CR 6224556
016: *
017: * Revision 1.24 2005/02/25 09:44:55 ss150821
018: * RFE 6223490 - SRA Should use JDK based logging, changed to start throwing the full stacktrace for the exception in the logs
019: *
020: * Revision 1.23 2005/02/23 11:39:12 ss150821
021: * RFE 6223490 - SRA Should use JDK based logging
022: *
023: * Revision 1.22 2005/02/21 08:03:15 ss150821
024: * 6223490 - JDK Logging move for SRA
025: *
026: * Revision 1.21 2005/02/17 08:27:38 ss150821
027: * RFE 6223490 - Add JDK Logging Support to SRA
028: *
029: * Revision 1.20 2004/11/18 09:53:16 ss150821
030: * Bug #6195816 - Significant performance degradation when Gateway Logging (Identity) Enabled on the SRA
031: *
032: * Revision 1.19 2004/07/29 05:03:09 vt126379
033: * RFE#5075809, CRT#102
034: *
035: * Revision 1.18 2004/05/10 04:58:58 ss150821
036: * Codefix for 5043753 - StackOverflowError fix
037: *
038: * Revision 1.17 2003/09/01 12:11:08 mm132998
039: * Bug ID : 4911706 , 4801456
040: *
041: * Revision 1.16 2003/08/13 13:57:00 mm132998
042: * Remove console logging
043: *
044: * Revision 1.15 2003/07/21 11:42:07 mm132998
045: * Bug 4889079
046: *
047: * Revision 1.14 2003/07/21 11:21:16 mm132998
048: * Bug 4889079
049: *
050: * Revision 1.13 2003/07/15 13:04:40 mm132998
051: * Beta showstopper - unresolved issues
052: *
053: * Revision 1.12 2003/06/16 13:43:59 mm132998
054: * loggingEnabled wrongly compared in createDefault()
055: *
056: * Revision 1.11 2003/06/04 14:13:30 mm132998
057: * Bug ID : 4726157 - changes to SessionWrapper to handle Application Login SSOToken
058: *
059: * Revision 1.10 2003/05/30 12:54:32 mm132998
060: * Session Validation at server side
061: *
062: * Revision 1.9 2003/04/24 11:42:56 mm132998
063: * Log name should be customisable : Bugid : 4780721
064: *
065: * Revision 1.8 2003/04/24 11:37:02 mm132998
066: * Log name should be customisable : Bugid : 4780721
067: *
068: * Revision 1.7 2003/01/23 07:00:53 bv131302
069: * id60 changes
070: *
071: * Revision 1.6.4.1 2002/12/30 11:13:49 bv131302
072: * id60 related changes
073: *
074: * Revision 1.6 2002/10/17 12:38:31 bv131302
075: * bringing some early changes of appserver_branch into the tip
076: *
077: * Revision 1.5 2002/08/21 07:36:19 bv131302
078: * Fixes for 4681457, 4703696, 4713296, 4718232, 4726004, 4726008
079: *
080: * Revision 1.4 2002/08/16 15:13:07 bv131302
081: * Hana CRT#1888 - Check log settings before logging
082: *
083: * Revision 1.3 2002/08/16 12:21:22 bv131302
084: * Hana CRT#1884 - RProxy perf issues
085: *
086: * Revision 1.2 2002/06/21 13:04:15 bv131302
087: * LDAP Attribute name changes
088: *
089: * Revision 1.1 2002/06/14 09:04:22 rt130506
090: * SRAP rebranding
091: *
092: * Revision 1.8 2002/06/11 16:02:12 bv131302
093: * new branded
094: *
095: * Revision 1.7 2002/04/17 07:02:09 mm132998
096: * Bug id : # 4669834 , CRT : # 841 , Desc : Error messages
097: *
098: * Revision 1.6 2002/03/18 10:19:28 mm132998
099: * Bug ID : # 4653988 CRT : # 574 Desc : Lihue PRD 7.4.4.1 and requirements for Bug 4195483
100: *
101: * Revision 1.5 2002/03/13 13:43:33 mm132998
102: * Gateway log migration to iDSAME , Bug ID : # 4651299 , CRT : # 543
103: *
104: * Revision 1.4 2002/03/04 07:27:36 bv131302
105: * Hana CRT#441 - The debug logs have the instance name as the extension
106: *
107: * Revision 1.3 2002/02/22 10:15:42 mm132998
108: * Fix for Bug id #4641656
109: *
110: * Revision 1.2 2002/02/18 07:16:00 mm132998
111: * Logs to local file
112: *
113: *
114: */
115:
116: package com.sun.portal.util;
117:
118: import java.util.ArrayList;
119: import java.util.Hashtable;
120: import java.util.Iterator;
121: import java.util.logging.Level;
122: import java.util.logging.Logger;
123:
124: import com.iplanet.log.LogException;
125: import com.iplanet.log.LogManager;
126: import com.iplanet.log.LogRecord;
127: import com.iplanet.sso.SSOException;
128: import com.iplanet.sso.SSOToken;
129: import com.iplanet.sso.SSOTokenManager;
130: import com.sun.portal.log.common.PortalLogger;
131: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
132: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
133: import com.sun.portal.util.SSOUtil;
134:
135: public class GWLogManager {
136:
137: private static String logName = "srapGateway";
138:
139: public static LogManager lm = null;
140:
141: public static boolean loggingEnabled = false;
142:
143: public static SSOToken appSession = null;
144:
145: private static int logId = 0;
146:
147: // Lihue # 7.4.4.1
148: public static Hashtable logIdMap = new Hashtable();
149:
150: // EOC :: Lihue # 7.4.4.1
151:
152: static String actualLogName = null;
153:
154: static boolean logError = false;
155:
156: private static String platformLogName = null;
157:
158: private static LogCreateThread createThread = null;
159:
160: private static RemoteWriteLogThread remoteLogThread = null;
161:
162: // static Logger logger = Logger.getLogger("com.sun.portal.sra.rproxy");
163: private static Logger logger = PortalLogger
164: .getLogger(GWLogManager.class);
165:
166: static {
167: platformLogName = SystemProperties.get("gateway.logname");
168: if (platformLogName != null) {
169: platformLogName = platformLogName.trim().replace('-', '_');
170: if (platformLogName.length() == 0) {
171: platformLogName = null;
172: }
173: }
174: createNewAppSession(true);
175: createThread = new LogCreateThread();
176: Thread thr = new Thread(createThread);
177: thr.start();
178:
179: remoteLogThread = new RemoteWriteLogThread();
180: new Thread(remoteLogThread).start();
181:
182: /*
183: * if (appSession != null){ System.out.println("Got session ... "); }
184: * else{ System.out.println("NO SESSION !!!!"); }
185: */
186: }
187:
188: public static synchronized void createNewAppSession() {
189: createNewAppSession(false);
190: }
191:
192: public static synchronized void createNewAppSession(boolean internal) {
193:
194: // logger.info("createNewAppSession ... ");
195: logger.info("PSSR_CSPU026");
196: // Thread.currentThread().dumpStack();
197:
198: try {
199: // System.out.println("createNewAppSession : app session created
200: // !");
201: String sessid = ApplicationLogin.login("srapGateway");
202: appSession = createSession(sessid);
203: // System.out.println("createNewAppSession : app session created : "
204: // + appSession);
205: // logger.info("createNewAppSession : app session created : " +
206: // appSession);
207: Object[] params1 = { appSession };
208: logger.log(Level.INFO, "PSSR_CSPU027", params1);
209: if (!logCreateInvoked && loggingEnabled && !internal) {
210: // logger.info("createNewAppSession : recreating log ... ");
211: logger.info("PSSR_CSPU028");
212: recreateLog();
213: }
214: } catch (Exception ex) {
215: // System.err.println("Error while creating Application Session !");
216: // logger.log(Level.SEVERE, "Error while creating Application
217: // Session ", ex);
218: logger.log(Level.SEVERE, "PSSR_CSPU029", ex);
219: }
220: // System.out.println("appSession : "+appSession);
221: }
222:
223: public static String getUserId(String sessionID) {
224: if (sessionID == null)
225: return "";
226: try {
227:
228: SSOToken ssoToken = SSOUtil
229: .getSSOTokenThrowExceptionToClient(sessionID);
230: String userDN = ssoToken.getPrincipal().getName();
231: return userDN.substring((userDN.indexOf("=") + 1), userDN
232: .indexOf(","));
233: } catch (SSOException ssoexp) {
234: if (ssoexp.toString().indexOf("Session state is invalid") != -1) {
235: if (GWDebug.debug.errorEnabled())
236: GWDebug.debug
237: .error("GWLogManager: Unable to get user ID, did you not login!!");
238: }
239:
240: } catch (Exception ex) {
241: if (GWDebug.debug.errorEnabled())
242: GWDebug.debug
243: .error("GWLogManager: Unable to get user ID -> "
244: + ex);
245: }
246: return ""; // If null is returned, nothing will be logged
247:
248: }
249:
250: public synchronized static int getLogId() {
251: return ++logId;
252: }
253:
254: public static void createDefault() {
255: createDefault(logName);
256: }
257:
258: private static SSOToken createSession(String sessid) {
259:
260: SSOToken token = null;
261: try {
262: // SSOTokenManager manager = SSOTokenManager.getInstance();
263: // token = manager.createSSOToken(sessid);
264: token = SSOUtil.getSSOTokenNoDecode(sessid);
265: } catch (SSOException ex) {
266: // logger.log(Level.SEVERE, "Unable to create application session",
267: // ex);
268: logger.log(Level.SEVERE, "PSSR_CSPU030", ex);
269: token = null;
270: } catch (Exception ex) {
271: // logger.log(Level.SEVERE, "Unable to create application session",
272: // ex);
273: logger.log(Level.SEVERE, "PSSR_CSPU031", ex);
274: token = null;
275: }
276: return token;
277: }
278:
279: public static void initialise() {
280: loggingEnabled = GatewayProfile.getBoolean("LoggingEnabled",
281: false);
282: }
283:
284: public static void createDefault(String name) {
285:
286: if (appSession == null) {
287: createNewAppSession(true);
288: }
289:
290: if (loggingEnabled) {
291:
292: lm = new LogManager(appSession);
293:
294: if (name != null) {
295: if (name.length() > 0) {
296: logName = name;
297: }
298: }
299:
300: if (platformLogName != null) {
301: // Override the default log name.
302: logName = platformLogName;
303: }
304:
305: try {
306: System.out.println("Creating " + logName);
307: GWLogManager.lm.create(logName);
308: actualLogName = logName;
309: } catch (LogException e) {
310: actualLogName = null;
311: // logger.severe("GWLogManager: Unable to create log");
312: logger.severe("PSSR_CSPU032");
313: // logger.severe("GWLogManager: " + e.toString());
314: Object[] params7 = { e.toString() };
315: logger.log(Level.SEVERE, "PSSR_CSPU033", params7);
316: }
317: }
318: }
319:
320: public static void write(String type, String msg) {
321:
322: // System.out.println("write ... "+type + " , " + msg);
323: if (lm == null) {
324: return;
325: }
326: if (logError) {
327: return;
328: }
329:
330: if (!loggingEnabled) {
331: return;
332: }
333:
334: // System.out.println("Before try ... ");
335:
336: // DSAME does not throw an exception is SSOToken is invalid !
337: // We have to verify it ourself :(
338: // System.out.println("Validating session ... "+appSession);
339: // logger.info("Validating session ... " + appSession);
340: Object[] params8 = { appSession };
341: logger.log(Level.INFO, "PSSR_CSPU034", params8);
342: if (!isAppSessionValid(appSession)) {
343: // logger.severe("Invalid Application session ... " + appSession);
344: Object[] params9 = { appSession };
345: logger.log(Level.SEVERE, "PSSR_CSPU035", params9);
346: // logger.severe("Attempting to recreate logs ... ");
347: logger.severe("PSSR_CSPU036");
348: recreateLog();
349: return;
350: }
351:
352: try {
353: remoteLogThread.addEntryForLogging("ACCESS", msg);
354:
355: } catch (Exception e) {
356: logError = true;
357: // logger.log(Level.SEVERE, "GWLogManager: Unable to create log
358: // record", e);
359: Object[] params = { e };
360: logger.log(Level.SEVERE, "PSSR_CSPU037", e);
361:
362: String reason = e.toString();
363: // logger.severe("GWLogManager: " + reason);
364: Object[] params12 = { reason };
365: logger.log(Level.SEVERE, "PSSR_CSPU038", params12);
366: /*
367: * if ((reason.indexOf("invalidSession") != -1) &&
368: * logName.equals("srapGateway")) { String cmd =
369: * "/etc/init.d/ipsgateway start";
370: *
371: * try { // Need to change back to root. GW gw = new GW();
372: * gw.unrun_GW();
373: *
374: * Runtime.getRuntime().exec(cmd); } catch (Exception ex) { }
375: * System.exit(1); }
376: */
377: // What do we do ??
378: // Currently just getting another session.
379: recreateLog();
380: }
381: }
382:
383: static boolean logCreateInvoked = false;
384:
385: private static synchronized void recreateLog() {
386: if (logCreateInvoked) {
387: // System.out.println("logCreateInvoked : true");
388: // logger.info("logCreateInvoked : true");
389: logger.info("PSSR_CSPU039");
390: return;
391: }
392: logError = true;
393:
394: // logger.info("Recreate log !!");
395: logger.info("PSSR_CSPU040");
396: if (!isAppSessionValid(appSession)) {
397: appSession = null;
398: createNewAppSession(true);
399: }
400: logCreateInvoked = true;
401: // logger.info("Notifying ... ");
402: logger.info("PSSR_CSPU041");
403: createThread.createLogsInThread(actualLogName);
404: return;
405: }
406:
407: static boolean isAppSessionValid(SSOToken token) {
408: boolean isvalid = false;
409: if (token == null) {
410: return false;
411: }
412: try {
413: // String tokenId = token.getTokenID().toString();
414: SSOTokenManager manager = SSOTokenManager.getInstance();
415: SSOToken tok = token;
416: isvalid = manager.isValidToken(tok);
417: } catch (SSOException ssoex) {
418: isvalid = false;
419: // logger.log(Level.SEVERE, "Invalid Application session ", ssoex);
420: logger.log(Level.SEVERE, "PSSR_CSPU042", ssoex);
421: } catch (Exception ex) {
422: isvalid = false;
423: // logger.log(Level.SEVERE, "Invalid Application session ", ex);
424: logger.log(Level.SEVERE, "PSSR_CSPU043", ex);
425: }
426: return isvalid;
427: }
428:
429: public static synchronized void createAppSessionIfInvalid() {
430: try {
431:
432: // Begin - CR 6317346
433: boolean survivedRefresh = false;
434:
435: try {
436: SSOTokenManager manager = SSOTokenManager.getInstance();
437: manager.refreshSession(appSession);
438: survivedRefresh = true;
439: } catch (Exception e) {
440: logger.warning("PSSR_CSPU0431");
441: }
442:
443: if (!survivedRefresh || !isAppSessionValid(appSession)) {
444: // End - CR 6317346
445: System.out
446: .println("\n\n\nAppsession was invalid creating a new appSession...\n\n\n");
447: // logger.info("Appsession was invalid creating a new
448: // appSession...");
449: logger.info("PSSR_CSPU044");
450:
451: createNewAppSession();
452:
453: System.out
454: .println("\n\n\nAppsession created sucessfully...\n\n\n");
455: System.out.flush();
456: // logger.info("Appsession created sucessfully....");
457: logger.info("PSSR_CSPU045");
458: }
459: } catch (Exception ex) {
460: // logger.log(Level.SEVERE, "Invalid Application session ", ex);
461: logger.log(Level.SEVERE, "PSSR_CSPU046", ex);
462: }
463: }
464:
465: /**
466: *
467: */
468: public static LogManager getLogManager() {
469:
470: return lm;
471:
472: }
473:
474: /**
475: * @return
476: */
477: public static String getLogName() {
478: return logName;
479: }
480:
481: }
482:
483: class LogCreateThread implements Runnable {
484:
485: private String createLogName = null;
486:
487: // static Logger logger = Logger.getLogger("com.sun.portal.sra.rproxy");
488: private static Logger logger = PortalLogger
489: .getLogger(LogCreateThread.class);
490:
491: synchronized void createLogsInThread(String logName) {
492: createLogName = logName;
493: this .notify();
494: // logger.info("Notifying actual ... ");
495: logger.info("PSSR_CSPU047");
496: }
497:
498: public void run() {
499: synchronized (this ) {
500: while (true) {
501: try {
502: wait();
503: if (!GWLogManager.logError) {
504: // logger.info("!logError !!");
505: logger.info("PSSR_CSPU048");
506: }
507:
508: while (GWLogManager.logError) {
509: try {
510: if (!GWLogManager
511: .isAppSessionValid(GWLogManager.appSession)) {
512: // logger.info("LogCreateThread : Invalid
513: // session ! recreating .... ");
514: logger.info("PSSR_CSPU049");
515: // System.out.println("LogCreateThread : Invalid
516: // session ! recreating .... ");
517: GWLogManager.appSession = null;
518: GWLogManager.createNewAppSession(true);
519: }
520: GWLogManager.lm = new LogManager(
521: GWLogManager.appSession);
522: try {
523: GWLogManager.lm.create(createLogName);
524: // logger.info("Log creation suceeded !!");
525: logger.info("PSSR_CSPU050");
526: GWLogManager.logError = false;
527: GWLogManager.logCreateInvoked = false;
528: } catch (LogException e) {
529: // e.printStackTrace();
530: GWLogManager.actualLogName = null;
531: // logger.severe("GWLogManager: Unable to create
532: // log : " + e.toString());
533: Object[] params25 = { e.toString() };
534: logger.log(Level.SEVERE,
535: "PSSR_CSPU051", params25);
536: // logger.log(Level.SEVERE, "GWLogManager: ",
537: // e);
538: logger.log(Level.SEVERE,
539: "PSSR_CSPU052", e);
540: }
541: } catch (Exception ex) {
542: // ex.printStackTrace();
543: // logger.severe("GWLogManager: Unable to create log
544: // : " + ex.toString());
545: Object[] params27 = { ex.toString() };
546: logger.log(Level.SEVERE, "PSSR_CSPU053",
547: params27);
548: // logger.log(Level.SEVERE, "GWLogManager: ", ex);
549: logger
550: .log(Level.SEVERE, "PSSR_CSPU054",
551: ex);
552: }
553: // Wait for 0.5 secs before retrying ...
554: Thread.currentThread().sleep(500);
555: }
556: } catch (Exception ex) {
557: }
558: }
559: }
560: }
561: }
562:
563: /*
564: * This is a separate thread that takes uses Identity Server Logging API's to
565: * log information. The settings to control this are on the amconsole under the
566: * Gateway Profile, under the Logging tab
567: *
568: */
569:
570: class RemoteWriteLogThread implements Runnable {
571:
572: private ArrayList _list = new ArrayList();
573:
574: private LogManager _lm = null;
575:
576: public void run() {
577: while (true) {
578: ArrayList al = getEntriesForLogging();
579:
580: for (Iterator iterator = al.iterator(); iterator.hasNext();) {
581: LogRecord element = (LogRecord) iterator.next();
582: try {
583: MonitoringSubsystem.handleEvent(
584: SRAEvent.IS_LOGGING_START, new Integer(
585: element.getRecMsg().length()));
586: GWLogManager.getLogManager().write(element,
587: GWLogManager.getLogName());
588: MonitoringSubsystem
589: .handleEvent(SRAEvent.IS_LOGGING_END);
590: } catch (LogException e) {
591: e.printStackTrace();
592: }
593: }
594: try {
595: Thread.sleep(5000);
596: } catch (InterruptedException e) {
597: e.printStackTrace();
598: }
599: }
600:
601: }
602:
603: /**
604: * @return
605: */
606: private ArrayList getEntriesForLogging() {
607: ArrayList alist = new ArrayList();
608:
609: synchronized (_list) {
610: if (_list.isEmpty())
611: return alist;
612: else {
613: ArrayList arlist = (ArrayList) _list.clone();
614: _list.clear();
615: return arlist;
616: }
617: }
618: }
619:
620: public void addEntryForLogging(String type, String msg) {
621: LogRecord lr = new LogRecord("ACCESS", msg);
622: synchronized (_list) {
623: _list.add(lr);
624: }
625: }
626:
627: }
|