001: package com.sun.portal.util;
002:
003: import java.util.Hashtable;
004: import java.util.logging.Level;
005: import java.util.logging.Logger;
006:
007: import com.iplanet.log.LogException;
008: import com.iplanet.log.LogManager;
009: import com.iplanet.log.LogRecord;
010: import com.iplanet.sso.SSOException;
011: import com.iplanet.sso.SSOToken;
012: import com.iplanet.sso.SSOTokenManager;
013: import com.sun.portal.log.common.PortalLogger;
014: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
015:
016: public class NetletLogMgr {
017:
018: private static String logName = "srapNetlet";
019:
020: public static LogManager lm = null;
021:
022: public static boolean loggingEnabled = false;
023:
024: // public static SSOToken appSession = null;
025: private static int logId = 0;
026:
027: // Lihue # 7.4.4.1
028: public static Hashtable logIdMap = new Hashtable();
029:
030: // EOC :: Lihue # 7.4.4.1
031:
032: private static String actualLogName = null;
033:
034: private static boolean logError = false;
035:
036: private static String platformLogName = null;
037:
038: // static Logger logger = Logger.getLogger("com.sun.portal.sra.rproxy");
039: private static Logger logger = PortalLogger
040: .getLogger(NetletLogMgr.class);
041:
042: static {
043: platformLogName = SystemProperties.get("netlet.logname");
044: if (platformLogName != null) {
045: platformLogName = platformLogName.trim().replace('-', '_');
046: if (platformLogName.length() == 0) {
047: platformLogName = null;
048: }
049: }
050: /*
051: * try{ if (appSession == null) { String sessid =
052: * ApplicationLogin.login("srapGateway"); appSession =
053: * createSession(sessid); } }catch(Exception ex){
054: * System.err.println("Error while creating Application Session !");
055: * ex.printStackTrace(); }
056: */
057: /*
058: * if (appSession != null){ System.out.println("Got session ... "); }
059: * else{ System.out.println("NO SESSION !!!!"); }
060: */
061: }
062:
063: public static String getUserId(String sessionID) {
064: try {
065: // sessionID = URLDecoder.decode(sessionID);
066: // SSOTokenManager ssoTokenManager = SSOTokenManager.getInstance();
067: // SSOToken ssoToken = ssoTokenManager.createSSOToken(sessionID);
068: SSOToken ssoToken = SSOUtil.getSSOToken(sessionID);
069: String userDN = ssoToken.getPrincipal().getName();
070: return userDN.substring((userDN.indexOf("=") + 1), userDN
071: .indexOf(","));
072: } catch (Exception ex) {
073: // logger.severe("NetletLogMgr: Unable to get user ID -> " + ex);
074: logger.log(Level.SEVERE, "PSSR_CSPU078", ex);
075: }
076: return ""; // If null is returned, nothing will be logged
077: }
078:
079: public synchronized static int getLogId() {
080: return ++logId;
081: }
082:
083: public static void createDefault() {
084: createDefault(logName);
085: }
086:
087: private static SSOToken createSession(String sessid) {
088:
089: SSOToken token = null;
090: try {
091: // SSOTokenManager manager = SSOTokenManager.getInstance();
092: // token = manager.createSSOToken(sessid);
093: token = SSOUtil.getSSOTokenNoDecode(sessid);
094: } catch (SSOException ex) {
095: // logger.severe("Unable to create application session!");
096: logger.severe("PSSR_CSPU079");
097: ex.printStackTrace();
098: token = null;
099: } catch (Exception ex) {
100: // logger.severe("Unable to create application session!");
101: logger.severe("PSSR_CSPU080");
102: ex.printStackTrace();
103: token = null;
104: }
105: return token;
106: }
107:
108: static {
109: loggingEnabled = GatewayProfile.getBoolean(
110: "NetletLoggingEnabled", false);
111: }
112:
113: public static void createDefault(String name) {
114:
115: /*
116: * if (appSession == null) { String sessid =
117: * ApplicationLogin.login("srapGateway"); appSession =
118: * createSession(sessid); }
119: */
120:
121: if (loggingEnabled) {
122:
123: lm = new LogManager(GWLogManager.appSession);
124:
125: if (name != null) {
126: if (name.length() > 0) {
127: logName = name;
128: }
129: }
130:
131: if (platformLogName != null) {
132: // Override the default log name.
133: logName = platformLogName;
134: }
135:
136: try {
137: NetletLogMgr.lm.create(logName);
138: actualLogName = logName;
139: } catch (LogException e) {
140: actualLogName = null;
141: // logger.severe("NetletLogMgr: Unable to create log");
142: logger.severe("PSSR_CSPU081");
143: // logger.severe("NetletLogMgr: " + e.toString());
144: Object[] params4 = { e.toString() };
145: logger.log(Level.SEVERE, "PSSR_CSPU082", params4);
146: }
147: }
148: }
149:
150: public static void write(String type, String msg) {
151:
152: if (lm == null) {
153: return;
154: }
155: if (logError) {
156: return;
157: }
158:
159: if (!loggingEnabled) {
160: return;
161: }
162:
163: try {
164: // LogRecord lr = new LogRecord(type, msg);
165: LogRecord lr = new LogRecord("ACCESS", msg);
166: NetletLogMgr.lm.write(lr, logName);
167: } catch (Exception e) {
168: logError = true;
169: // logger.severe("NetletLogMgr: Unable to create log record");
170: logger.severe("PSSR_CSPU083");
171: String reason = e.toString();
172: // logger.severe("NetletLogMgr: " + reason);
173: Object[] params6 = { reason };
174: logger.log(Level.SEVERE, "PSSR_CSPU084", params6);
175: /*
176: * if ((reason.indexOf("invalidSession") != -1) &&
177: * logName.equals("srapGateway")) { String cmd =
178: * "/etc/init.d/ipsgateway start";
179: *
180: * try { // Need to change back to root. GW gw = new GW();
181: * gw.unrun_GW();
182: *
183: * Runtime.getRuntime().exec(cmd); } catch (Exception ex) { }
184: * System.exit(1); }
185: */
186: // What do we do ??
187: // Currently just getting another session.
188: synchronized (actualLogName) {
189: if (logError) {
190: if (!isAppSessionValid(GWLogManager.appSession)) {
191: createDefault(actualLogName);
192: logError = false;
193: }
194: }
195: }
196: }
197: }
198:
199: private static boolean isAppSessionValid(SSOToken token) {
200: boolean isvalid = false;
201: try {
202: SSOTokenManager manager = SSOTokenManager.getInstance();
203: isvalid = manager.isValidToken(token);
204: } catch (SSOException ssoex) {
205: }
206: return isvalid;
207: }
208:
209: }
|