private static String getOrCreateSession(int connectorId) { String sessionId = getConnectorSession(connectorId); if (null != sessionId) { return sessionId; // we're using the key to the session table rec as the session ID } String sql = "insert into " + TABLE_SESSION + "(" + "SESSION_PKEY," + COL_DATE_CREATE + ",CONNECTOR_KEY," + COL_DATE_EXPIRE + ") values (?,?,?,?)"; sessionId = Util.getGuid(); Timestamp now = new Timestamp(Gmt.getGmtDate().getTime()); ResourceFactory rf = Context.rf.get(); Sql.sqlExe( sql, new Object[] {sessionId, now, connectorId, getExpireDt(now)}, new int[] {Types.VARCHAR, Types.TIMESTAMP, Types.INTEGER, Types.TIMESTAMP}, rf); System.out.println(new Date() + ": Created new connector session -- " + sessionId); return sessionId; // we're using the key to the session table rec as the session ID }
/** * We're here because for some reason, we can't write to the logging database. So instead, we're * just going to write directly to a file. The filename will be the name of the service + ".log" * and in the current directory. * * @param level int severity level * @param serviceName String name of the service making the call * @param methodClass String class of the caller * @param method String method name of the caller * @param message String message to be logged */ private static synchronized void handleNonLoggableError( LogLevel level, String serviceName, Class methodClass, String method, String message) { final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss.SSS"; FileOutputStream stream = null; try { String logFilename = getLogFilename(serviceName); StringBuffer buf = new StringBuffer(); buf.append(new SimpleDateFormat(DATE_FORMAT).format(Gmt.getGmtTimestamp())); buf.append(" "); buf.append(level); if (methodClass != null) { buf.append(" "); String className = methodClass.getName(); int idx = className.lastIndexOf('.'); buf.append(idx == -1 ? className : className.substring(idx + 1)); buf.append("."); buf.append(method); } buf.append(" "); buf.append(message); buf.append(System.getProperty("line.separator")); stream = new FileOutputStream(logFilename, true); // Because this is a synchronized method, and no other app instance should be writing to this // file, we // should have excluse access to the file. So we don't need any file locking. String msg = buf.toString(); stream.write(msg.getBytes()); //noinspection UseOfSystemOutOrSystemErr System.out.println(msg); // this is the fallback for when the 'more-robust-logging' fails } catch (Exception e) { // Give up on doing anything with an exception here. There's simply nothing more we can do // to attempt to log something. } finally { if (stream != null) { //noinspection EmptyCatchBlock try { stream.close(); } catch (Throwable t) { } } } }
private static String getConnectorSession(int conKey) { final String sql = "select SESSION_PKEY," + COL_DATE_EXPIRE + " from " + TABLE_SESSION + " with(nolock) where CONNECTOR_KEY=?"; Date now = Gmt.getGmtDate(); ResourceFactory rf = Context.rf.get(); ResultSet rs = Sql.sqlQuery(sql, new Object[] {conKey}, new int[] {Types.INTEGER}, rf); try { while (rs.next()) { String sessionId = rs.getString(1); Date expireDate = rs.getTimestamp(2); if (expireDate.getTime() >= now.getTime()) { System.out.println(new Date() + ": Extending existing session -- " + conKey); final Date expireDt = getExpireDt(now); Sql.sqlExe( "update " + TABLE_SESSION + " set " + COL_DATE_EXPIRE + "=? where CONNECTOR_KEY=?", new Object[] {expireDt, conKey}, new int[] {Types.TIMESTAMP, Types.INTEGER}, rf); return sessionId; } else { System.out.println(new Date() + ": Removing expired session -- " + sessionId); Sql.sqlExe( "delete " + TABLE_SESSION + " where SESSION_PKEY=?", new Object[] {sessionId}, new int[] {Types.VARCHAR}, rf); } } } catch (RuntimeException rte) { throw rte; } catch (Throwable t) { throw new IllegalStateException(t); } finally { Sql.closeAll(rs); } return null; }
private static Map<String, Object> findSession(String sessionId, ResourceFactory rf) { String sql = "select CONNECTOR_KEY," + COL_DATE_EXPIRE + " from " + TABLE_SESSION + " with(nolock) where SESSION_PKEY=?"; ResultSet rs = Sql.sqlQuery(sql, new Object[] {sessionId}, new int[] {Types.VARCHAR}, rf); try { if (rs.next()) { Date now = Gmt.getGmtDate(); Date expireDate = rs.getTimestamp(2); if (expireDate.getTime() >= now.getTime()) { // we've received a valid transaction; move the session expiration date out Date newExpireDate = getExpireDt(now); extendSessionExpiration(sessionId, newExpireDate, rf); Map<String, Object> ret = new HashMap<String, Object>(); ret.put(COL_DATE_EXPIRE, newExpireDate); ret.put(COL_CONNECTOR_ID, rs.getInt(1)); return ret; } } } catch (RuntimeException rte) { throw rte; } catch (Throwable t) { throw new IllegalStateException(t); } finally { Sql.closeAll(rs); } throw new ReportableException(ReportableException.STALE_TRANSACTION, sessionId); }