Advertisement
tko_pb

SessionInfo

Nov 9th, 2018
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.83 KB | None | 0 0
  1. /*
  2.  *************************************************************************
  3.  * The contents of this file are subject to the Openbravo  Public  License
  4.  * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
  5.  * Version 1.1  with a permitted attribution clause; you may not  use this
  6.  * file except in compliance with the License. You  may  obtain  a copy of
  7.  * the License at http://www.openbravo.com/legal/license.html
  8.  * Software distributed under the License  is  distributed  on  an "AS IS"
  9.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  10.  * License for the specific  language  governing  rights  and  limitations
  11.  * under the License.
  12.  * The Original Code is Openbravo ERP.
  13.  * The Initial Developer of the Original Code is Openbravo SLU
  14.  * All portions are Copyright (C) 2009-2017 Openbravo SLU
  15.  * All Rights Reserved.
  16.  * Contributor(s):  ______________________________________.
  17.  ************************************************************************
  18.  */
  19.  
  20. package org.openbravo.database;
  21.  
  22. import java.sql.Connection;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26.  
  27. import org.apache.log4j.Logger;
  28.  
  29. /**
  30.  * This class is used to maintain session information which will be used for audit purposes.
  31.  *
  32.  */
  33. public class SessionInfo {
  34.   private static final Logger log4j = Logger.getLogger(SessionInfo.class);
  35.  
  36.   public static final String IMPORT_ENTRY_PROCESS = "IE";
  37.  
  38.   /**
  39.    * updated on context start and via SL_AuditTable. used to switch on/off the audit trail system
  40.    */
  41.   private static boolean isAuditActive = false;
  42.   private static boolean usageAuditActive = false;
  43.  
  44.   /*
  45.    * The following variables track per thread the information about the current 'user' of the thread
  46.    * (this info is later at getConnection() time passed into a temporary AD_CONTEXT_INFO table to be
  47.    * available to the generated audit triggers.
  48.    */
  49.   private static ThreadLocal<String> sessionId = new ThreadLocal<String>();
  50.   private static ThreadLocal<String> userId = new ThreadLocal<String>();
  51.   private static ThreadLocal<String> processType = new ThreadLocal<String>();
  52.   private static ThreadLocal<String> processId = new ThreadLocal<String>();
  53.   private static ThreadLocal<String> command = new ThreadLocal<String>();
  54.   private static ThreadLocal<String> queryProfile = new ThreadLocal<String>();
  55.  
  56.   /*
  57.    * To optimize updating of the AD_CONTEXT_INFO information, getConnection() is changed to return
  58.    * the same connection on all getConnection() calls done inside the same request when possible.
  59.    * Then the ad_context_info does not need to be updated so often (as the data doesn't change so
  60.    * often for a specific connection).
  61.    */
  62.   private static ThreadLocal<Connection> sessionConnection = new ThreadLocal<Connection>();
  63.   private static ThreadLocal<Boolean> changedInfo = new ThreadLocal<Boolean>();
  64.  
  65.   /*
  66.    * Maintain artifact's module id. This element is not persisted in auxiliary session table, it is
  67.    * intended to be used in the usage audit.
  68.    */
  69.   private static ThreadLocal<String> moduleId = new ThreadLocal<String>();
  70.  
  71.   private static ThreadLocal<Boolean> auditThisThread = new ThreadLocal<Boolean>();
  72.  
  73.   /**
  74.    * Sets all session information to null. Called at the end of http-request handling, to reset the
  75.    * audit information for that thread.
  76.    */
  77.   public static void init() {
  78.     sessionId.set(null);
  79.     userId.set(null);
  80.     processType.set(null);
  81.     processId.set(null);
  82.     changedInfo.set(null);
  83.     moduleId.set(null);
  84.     command.set(null);
  85.     queryProfile.set(null);
  86.     auditThisThread.set(true);
  87.     // if there is an open connection associated to get current request, close it
  88.     Connection conn = sessionConnection.get();
  89.     try {
  90.       if (conn != null && !conn.isClosed()) {
  91.         log4j.debug("Close session's connection");
  92.         conn.setAutoCommit(true);
  93.         conn.close();
  94.       }
  95.     } catch (SQLException e) {
  96.       log4j.error("Error closing sessionConnection", e);
  97.     }
  98.     sessionConnection.set(null);
  99.   }
  100.  
  101.   /**
  102.    * Creates the needed infrastructure for audit. Which is temporary session table for PostgreSQL
  103.    * connections.
  104.    *
  105.    * Called whenever a new physical db-connection is created.
  106.    *
  107.    * @param conn
  108.    *          Connection to database
  109.    * @param rdbms
  110.    *          Database, only action is take for POSTGRESQL
  111.    */
  112.   public static void initDB(Connection conn, String rdbms) {
  113.  
  114.     if (rdbms != null && rdbms.equals("POSTGRE")) {
  115.       // Create temporary table
  116.       PreparedStatement psQuery = null;
  117.       PreparedStatement psCreate = null;
  118.       try {
  119.         if (conn.isReadOnly()) {
  120.           return;
  121.         }
  122.  
  123.         psQuery = getPreparedStatement(
  124.             conn,
  125.             "select count(*) from information_schema.tables where table_name='ad_context_info' and table_type = 'LOCAL TEMPORARY'");
  126.         ResultSet rs = psQuery.executeQuery();
  127.  
  128.         if (rs.next() && rs.getString(1).equals("0")) {
  129.           StringBuffer sql = new StringBuffer();
  130.           sql.append("CREATE TEMPORARY TABLE AD_CONTEXT_INFO");
  131.           sql.append("(AD_USER_ID VARCHAR(32), ");
  132.           sql.append("  AD_SESSION_ID VARCHAR(32),");
  133.           sql.append("  PROCESSTYPE VARCHAR(60), ");
  134.           sql.append("  PROCESSID VARCHAR(32)) on commit preserve rows");
  135.           psCreate = getPreparedStatement(conn, sql.toString());
  136.           psCreate.execute();
  137.         }
  138.       } catch (Exception e) {
  139.         log4j.error("Error initializating audit infrastructure", e);
  140.       } finally {
  141.         releasePreparedStatement(psQuery);
  142.         releasePreparedStatement(psCreate);
  143.       }
  144.     }
  145.   }
  146.  
  147.   /**
  148.    * @deprecated In most of the cases, this method is no longer required to be invoked: it was used
  149.    *             to manually set in database audit info. Now, this is in an smarter manner only if
  150.    *             needed from DAL/SQLC. When this method is still in use, it should be reviewed
  151.    *             whether it is no longer needed because of this automatic mechanism or if it is
  152.    *             required because new mechanism doesn't detect it (ie. DB modifications directly
  153.    *             with jdbc), in which case saveContextInfoIntoDB method is recommended to make
  154.    *             explicit in the code this need
  155.    * @see #saveContextInfoIntoDB(Connection)
  156.    */
  157.   static void setDBSessionInfo(Connection conn, boolean onlyIfChanged) {
  158.     if (!isAuditActive || (onlyIfChanged && (changedInfo.get() == null || !changedInfo.get()))) {
  159.       if (log4j.isDebugEnabled()) {
  160.         log4j.debug("No session info set isAuditActive: " + isAuditActive + " - changes in info: "
  161.             + changedInfo.get());
  162.       }
  163.       return;
  164.     }
  165.     saveContextInfoIntoDB(conn);
  166.   }
  167.  
  168.   /**
  169.    * @deprecated In most of the cases this method is no longer required to be invoked
  170.    * @see #saveContextInfoIntoDB(Connection)
  171.    */
  172.   public static void setDBSessionInfo(Connection conn) {
  173.     saveContextInfoIntoDB(conn);
  174.   }
  175.  
  176.   /**
  177.    * Saves currently stored context information into DB. Generally, this method shouldn't be
  178.    * directly invoked, as the Platform already does it when flushing changes to DB. Only in case
  179.    * Openbravo platform is bypassed (ie. DB operations performed on a manually obtained connection),
  180.    * this method must be manually invoked.
  181.    *
  182.    * @param conn
  183.    *          The connection where the session information will be stored in, note it must be the
  184.    *          same one performing DB modifications so audit trail triggers can retrieve the session
  185.    *          information.
  186.    */
  187.   public static void saveContextInfoIntoDB(Connection conn) {
  188.     if (!isAuditActive) {
  189.       return;
  190.     }
  191.  
  192.     PreparedStatement psCleanUp = null;
  193.     PreparedStatement psInsert = null;
  194.     try {
  195.       // When working with DAL sessionConnection is not set. This allows to have in the same thread
  196.       // a connection for DAL within its session with autocommit false and another one for sqlc with
  197.       // autocommit true.
  198.       boolean infoModified = Boolean.TRUE.equals(changedInfo.get())
  199.           || sessionConnection.get() == null || !conn.equals(sessionConnection.get());
  200.       if (!infoModified || Boolean.FALSE.equals(auditThisThread.get()) || conn.isReadOnly()) {
  201.         return;
  202.       }
  203.  
  204.       if (log4j.isDebugEnabled()) {
  205.         log4j.debug("saving DB context info " + SessionInfo.getUserId() + " - "
  206.             + SessionInfo.getSessionId() + " - " + SessionInfo.getProcessType() + " - "
  207.             + SessionInfo.getProcessId());
  208.       }
  209.  
  210.       psCleanUp = getPreparedStatement(conn, "delete from ad_context_info");
  211.       psCleanUp.executeUpdate();
  212.  
  213.       psInsert = getPreparedStatement(
  214.           conn,
  215.           "insert into ad_context_info (ad_user_id, ad_session_id, processType, processId) values (?, ?, ?, ?)");
  216.       psInsert.setString(1, SessionInfo.getUserId());
  217.       psInsert.setString(2, SessionInfo.getSessionId());
  218.       psInsert.setString(3, SessionInfo.getProcessType());
  219.       psInsert.setString(4, SessionInfo.getProcessId());
  220.       psInsert.executeUpdate();
  221.  
  222.       if (conn == sessionConnection.get()) {
  223.         // Handling only for the sqlc connection, as DAL should be automatically handled so that
  224.         // this method is invoked only once.
  225.         changedInfo.set(false);
  226.       }
  227.     } catch (Exception e) {
  228.       log4j.error("Error setting audit info", e);
  229.     } finally {
  230.       releasePreparedStatement(psCleanUp);
  231.       releasePreparedStatement(psInsert);
  232.     }
  233.   }
  234.  
  235.   /**
  236.    * Initialized DB with temporary tab/*
  237.  *************************************************************************
  238.  * The contents of this file are subject to the Openbravo  Public  License
  239.  * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
  240.  * Version 1.1  with a permitted attribution clause; you may not  use this
  241.  * file except in compliance with the License. You  may  obtain  a copy of
  242.  * the License at http://www.openbravo.com/legal/license.html
  243.  * Software distributed under the License  is  distributed  on  an "AS IS"
  244.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  245.  * License for the specific  language  governing  rights  and  limitations
  246.  * under the License.
  247.  * The Original Code is Openbravo ERP.
  248.  * The Initial Developer of the Original Code is Openbravo SLU
  249.  * All portions are Copyright (C) 2009-2017 Openbravo SLU
  250.  * All Rights Reserved.
  251.  * Contributor(s):  ______________________________________.
  252.  ************************************************************************
  253.  */
  254.  
  255. package org.openbravo.database;
  256.  
  257. import java.sql.Connection;
  258. import java.sql.PreparedStatement;
  259. import java.sql.ResultSet;
  260. import java.sql.SQLException;
  261.  
  262. import org.apache.log4j.Logger;
  263.  
  264. /**
  265.  * This class is used to maintain session information which will be used for audit purposes.
  266.  *
  267.  */
  268. public class SessionInfo {
  269.   private static final Logger log4j = Logger.getLogger(SessionInfo.class);
  270.  
  271.   public static final String IMPORT_ENTRY_PROCESS = "IE";
  272.  
  273.   /**
  274.    * updated on context start and via SL_AuditTable. used to switch on/off the audit trail system
  275.    */
  276.   private static boolean isAuditActive = false;
  277.   private static boolean usageAuditActive = false;
  278.  
  279.   /*
  280.    * The following variables track per thread the information about the current 'user' of the thread
  281.    * (this info is later at getConnection() time passed into a temporary AD_CONTEXT_INFO table to be
  282.    * available to the generated audit triggers.
  283.    */
  284.   private static ThreadLocal<String> sessionId = new ThreadLocal<String>();
  285.   private static ThreadLocal<String> userId = new ThreadLocal<String>();
  286.   private static ThreadLocal<String> processType = new ThreadLocal<String>();
  287.   private static ThreadLocal<String> processId = new ThreadLocal<String>();
  288.   private static ThreadLocal<String> command = new ThreadLocal<String>();
  289.   private static ThreadLocal<String> queryProfile = new ThreadLocal<String>();
  290.  
  291.   /*
  292.    * To optimize updating of the AD_CONTEXT_INFO information, getConnection() is changed to return
  293.    * the same connection on all getConnection() calls done inside the same request when possible.
  294.    * Then the ad_context_info does not need to be updated so often (as the data doesn't change so
  295.    * often for a specific connection).
  296.    */
  297.   private static ThreadLocal<Connection> sessionConnection = new ThreadLocal<Connection>();
  298.   private static ThreadLocal<Boolean> changedInfo = new ThreadLocal<Boolean>();
  299.  
  300.   /*
  301.    * Maintain artifact's module id. This element is not persisted in auxiliary session table, it is
  302.    * intended to be used in the usage audit.
  303.    */
  304.   private static ThreadLocal<String> moduleId = new ThreadLocal<String>();
  305.  
  306.   private static ThreadLocal<Boolean> auditThisThread = new ThreadLocal<Boolean>();
  307.  
  308.   /**
  309.    * Sets all session information to null. Called at the end of http-request handling, to reset the
  310.    * audit information for that thread.
  311.    */
  312.   public static void init() {
  313.     sessionId.set(null);
  314.     userId.set(null);
  315.     processType.set(null);
  316.     processId.set(null);
  317.     changedInfo.set(null);
  318.     moduleId.set(null);
  319.     command.set(null);
  320.     queryProfile.set(null);
  321.     auditThisThread.set(true);
  322.     // if there is an open connection associated to get current request, close it
  323.     Connection conn = sessionConnection.get();
  324.     try {
  325.       if (conn != null && !conn.isClosed()) {
  326.         log4j.debug("Close session's connection");
  327.         conn.setAutoCommit(true);
  328.         conn.close();
  329.       }
  330.     } catch (SQLException e) {
  331.       log4j.error("Error closing sessionConnection", e);
  332.     }
  333.     sessionConnection.set(null);
  334.   }
  335.  
  336.   /**
  337.    * Creates the needed infrastructure for audit. Which is temporary session table for PostgreSQL
  338.    * connections.
  339.    *
  340.    * Called whenever a new physical db-connection is created.
  341.    *
  342.    * @param conn
  343.    *          Connection to database
  344.    * @param rdbms
  345.    *          Database, only action is take for POSTGRESQL
  346.    */
  347.   public static void initDB(Connection conn, String rdbms) {
  348.  
  349.     if (rdbms != null && rdbms.equals("POSTGRE")) {
  350.       // Create temporary table
  351.       PreparedStatement psQuery = null;
  352.       PreparedStatement psCreate = null;
  353.       try {
  354.         if (conn.isReadOnly()) {
  355.           return;
  356.         }
  357.  
  358.         psQuery = getPreparedStatement(
  359.             conn,
  360.             "select count(*) from information_schema.tables where table_name='ad_context_info' and table_type = 'LOCAL TEMPORARY'");
  361.         ResultSet rs = psQuery.executeQuery();
  362.  
  363.         if (rs.next() && rs.getString(1).equals("0")) {
  364.           StringBuffer sql = new StringBuffer();
  365.           sql.append("CREATE TEMPORARY TABLE AD_CONTEXT_INFO");
  366.           sql.append("(AD_USER_ID VARCHAR(32), ");
  367.           sql.append("  AD_SESSION_ID VARCHAR(32),");
  368.           sql.append("  PROCESSTYPE VARCHAR(60), ");
  369.           sql.append("  PROCESSID VARCHAR(32)) on commit preserve rows");
  370.           psCreate = getPreparedStatement(conn, sql.toString());
  371.           psCreate.execute();
  372.         }
  373.       } catch (Exception e) {
  374.         log4j.error("Error initializating audit infrastructure", e);
  375.       } finally {
  376.         releasePreparedStatement(psQuery);
  377.         releasePreparedStatement(psCreate);
  378.       }
  379.     }
  380.   }
  381.  
  382.   /**
  383.    * @deprecated In most of the cases, this method is no longer required to be invoked: it was used
  384.    *             to manually set in database audit info. Now, this is in an smarter manner only if
  385.    *             needed from DAL/SQLC. When this method is still in use, it should be reviewed
  386.    *             whether it is no longer needed because of this automatic mechanism or if it is
  387.    *             required because new mechanism doesn't detect it (ie. DB modifications directly
  388.    *             with jdbc), in which case saveContextInfoIntoDB method is recommended to make
  389.    *             explicit in the code this need
  390.    * @see #saveContextInfoIntoDB(Connection)
  391.    */
  392.   static void setDBSessionInfo(Connection conn, boolean onlyIfChanged) {
  393.     if (!isAuditActive || (onlyIfChanged && (changedInfo.get() == null || !changedInfo.get()))) {
  394.       if (log4j.isDebugEnabled()) {
  395.         log4j.debug("No session info set isAuditActive: " + isAuditActive + " - changes in info: "
  396.             + changedInfo.get());
  397.       }
  398.       return;
  399.     }
  400.     saveContextInfoIntoDB(conn);
  401.   }
  402.  
  403.   /**
  404.    * @deprecated In most of the cases this method is no longer required to be invoked
  405.    * @see #saveContextInfoIntoDB(Connection)
  406.    */
  407.   public static void setDBSessionInfo(Connection conn) {
  408.     saveContextInfoIntoDB(conn);
  409.   }
  410.  
  411.   /**
  412.    * Saves currently stored context information into DB. Generally, this method shouldn't be
  413.    * directly invoked, as the Platform already does it when flushing changes to DB. Only in case
  414.    * Openbravo platform is bypassed (ie. DB operations performed on a manually obtained connection),
  415.    * this method must be manually invoked.
  416.    *
  417.    * @param conn
  418.    *          The connection where the session information will be stored in, note it must be the
  419.    *          same one performing DB modifications so audit trail triggers can retrieve the session
  420.    *          information.
  421.    */
  422.   public static void saveContextInfoIntoDB(Connection conn) {
  423.     if (!isAuditActive) {
  424.       return;
  425.     }
  426.  
  427.     PreparedStatement psCleanUp = null;
  428.     PreparedStatement psInsert = null;
  429.     try {
  430.       // When working with DAL sessionConnection is not set. This allows to have in the same thread
  431.       // a connection for DAL within its session with autocommit false and another one for sqlc with
  432.       // autocommit true.
  433.       boolean infoModified = Boolean.TRUE.equals(changedInfo.get())
  434.           || sessionConnection.get() == null || !conn.equals(sessionConnection.get());
  435.       if (!infoModified || Boolean.FALSE.equals(auditThisThread.get()) || conn.isReadOnly()) {
  436.         return;
  437.       }
  438.  
  439.       if (log4j.isDebugEnabled()) {
  440.         log4j.debug("saving DB context info " + SessionInfo.getUserId() + " - "
  441.             + SessionInfo.getSessionId() + " - " + SessionInfo.getProcessType() + " - "
  442.             + SessionInfo.getProcessId());
  443.       }
  444.  
  445.       psCleanUp = getPreparedStatement(conn, "delete from ad_context_info");
  446.       psCleanUp.executeUpdate();
  447.  
  448.       psInsert = getPreparedStatement(
  449.           conn,
  450.           "insert into ad_context_info (ad_user_id, ad_session_id, processType, processId) values (?, ?, ?, ?)");
  451.       psInsert.setString(1, SessionInfo.getUserId());
  452.       psInsert.setString(2, SessionInfo.getSessionId());
  453.       psInsert.setString(3, SessionInfo.getProcessType());
  454.       psInsert.setString(4, SessionInfo.getProcessId());
  455.       psInsert.executeUpdate();
  456.  
  457.       if (conn == sessionConnection.get()) {
  458.         // Handling only for the sqlc connection, as DAL should be automatically handled so that
  459.         // this method is invoked only once.
  460.         changedInfo.set(false);
  461.       }
  462.     } catch (Exception e) {
  463.       log4j.error("Error setting audit info", e);
  464.     } finally {
  465.       releasePreparedStatement(psCleanUp);
  466.       releasePreparedStatement(psInsert);
  467.     }
  468.   }
  469.  
  470.   /**
  471.    * Initialized DB with temporary table and sets session information on it.
  472.    *
  473.    * @param conn
  474.    *          Connection where the session information will be stored in
  475.    * @param rdbms
  476.    *          Database type
  477.    * @deprecated
  478.    */
  479.   @Deprecated
  480.   public static void setDBSessionInfo(Connection conn, String rdbms) {
  481.     if (!isAuditActive) {
  482.       return;
  483.     }
  484.     initDB(conn, rdbms);
  485.     setDBSessionInfo(conn);
  486.   }
  487.  
  488.   /**
  489.    * Return the connection associated with the current session, if there is one.
  490.    */
  491.   static Connection getSessionConnection() {
  492.     Connection conn = sessionConnection.get();
  493.     try {
  494.       if (conn == null || conn.isClosed()) {
  495.         return null;
  496.       }
  497.     } catch (SQLException e) {
  498.       log4j.error("Error checking connection", e);
  499.       return null;
  500.     }
  501.     log4j.debug("Reuse session's connection");
  502.     return conn;
  503.   }
  504.  
  505.   private static PreparedStatement getPreparedStatement(Connection conn, String SQLPreparedStatement)
  506.       throws SQLException {
  507.     if (conn == null || SQLPreparedStatement == null || SQLPreparedStatement.equals(""))
  508.       return null;
  509.     PreparedStatement ps = null;
  510.  
  511.     try {
  512.       if (log4j.isDebugEnabled())
  513.         log4j.debug("preparedStatement requested");
  514.       ps = conn.prepareStatement(SQLPreparedStatement, ResultSet.TYPE_SCROLL_INSENSITIVE,
  515.           ResultSet.CONCUR_READ_ONLY);
  516.       if (log4j.isDebugEnabled())
  517.         log4j.debug("preparedStatement received");
  518.     } catch (SQLException e) {
  519.       log4j.error("getPreparedStatement: " + SQLPreparedStatement + "\n" + e);
  520.       if (conn != null) {
  521.         try {
  522.           conn.setAutoCommit(true);
  523.           conn.close();
  524.         } catch (Exception ex) {
  525.           ex.printStackTrace();
  526.         }
  527.       }
  528.     }
  529.     return (ps);
  530.   }
  531.  
  532.   private static void releasePreparedStatement(PreparedStatement ps) {
  533.     if (ps != null) {
  534.       try {
  535.         ps.close();
  536.       } catch (Exception e) {
  537.         log4j.error("Error closing PreparedStatement", e);
  538.       }
  539.     }
  540.   }
  541.  
  542.   public static void setUserId(String user) {
  543.     if (user == null || !user.equals(getUserId())) {
  544.       userId.set(user);
  545.       changedInfo.set(true);
  546.     }
  547.   }
  548.  
  549.   public static String getUserId() {
  550.     return userId.get();
  551.   }
  552.  
  553.   public static void setProcessId(String processId) {
  554.     if (processId == null || !processId.equals(getProcessId())) {
  555.       SessionInfo.processId.set(processId);
  556.       changedInfo.set(true);
  557.     }
  558.   }
  559.  
  560.   public static String getProcessId() {
  561.     return processId.get();
  562.   }
  563.  
  564.   public static void setProcessType(String processType) {
  565.     if (processType == null || !processType.equals(getProcessType())) {
  566.       SessionInfo.processType.set(processType);
  567.       changedInfo.set(true);
  568.     }
  569.   }
  570.  
  571.   public static String getProcessType() {
  572.     return processType.get();
  573.   }
  574.  
  575.   public static void setSessionId(String session) {
  576.     if (session == null || !session.equals(getSessionId())) {
  577.       sessionId.set(session);
  578.       changedInfo.set(true);
  579.     }
  580.   }
  581.  
  582.   /**
  583.    * Forces changed info flag so next time it is checked it session info will be set in DB
  584.    */
  585.   public static void infoChanged() {
  586.     changedInfo.set(true);
  587.   }
  588.  
  589.   public static String getCommand() {
  590.     return command.get();
  591.   }
  592.  
  593.   public static void setCommand(String comm) {
  594.     command.set(comm);
  595.   }
  596.  
  597.   public static String getQueryProfile() {
  598.     return queryProfile.get();
  599.   }
  600.  
  601.   public static void setQueryProfile(String profile) {
  602.     queryProfile.set(profile);
  603.   }
  604.  
  605.   public static String getSessionId() {
  606.     return sessionId.get();
  607.   }
  608.  
  609.   public static void setAuditActive(boolean isAuditActive) {
  610.     SessionInfo.isAuditActive = isAuditActive;
  611.   }
  612.  
  613.   static void setSessionConnection(Connection conn) {
  614.     sessionConnection.set(conn);
  615.   }
  616.  
  617.   public static String getModuleId() {
  618.     return moduleId.get();
  619.   }
  620.  
  621.   public static void setModuleId(String moduleId) {
  622.     SessionInfo.moduleId.set(moduleId);
  623.   }
  624.  
  625.   public static boolean isUsageAuditActive() {
  626.     return usageAuditActive;
  627.   }
  628.  
  629.   public static void setUsageAuditActive(boolean usageAuditActive) {
  630.     SessionInfo.usageAuditActive = usageAuditActive;
  631.   }
  632.  
  633.   /** Set this value to {@code false} to prevent context info to be set in DB */
  634.   public static void auditThisThread(boolean shouldAudit) {
  635.     auditThisThread.set(shouldAudit);
  636.   }
  637. }
  638. le and sets session information on it.
  639.    *
  640.    * @param conn
  641.    *          Connection where the session information will be stored in
  642.    * @param rdbms
  643.    *          Database type
  644.    * @deprecated
  645.    */
  646.   @Deprecated
  647.   public static void setDBSessionInfo(Connection conn, String rdbms) {
  648.     if (!isAuditActive) {
  649.       return;
  650.     }
  651.     initDB(conn, rdbms);
  652.     setDBSessionInfo(conn);
  653.   }
  654.  
  655.   /**
  656.    * Return the connection associated with the current session, if there is one.
  657.    */
  658.   static Connection getSessionConnection() {
  659.     Connection conn = sessionConnection.get();
  660.     try {
  661.       if (conn == null || conn.isClosed()) {
  662.         return null;
  663.       }
  664.     } catch (SQLException e) {
  665.       log4j.error("Error checking connection", e);
  666.       return null;
  667.     }
  668.     log4j.debug("Reuse session's connection");
  669.     return conn;
  670.   }
  671.  
  672.   private static PreparedStatement getPreparedStatement(Connection conn, String SQLPreparedStatement)
  673.       throws SQLException {
  674.     if (conn == null || SQLPreparedStatement == null || SQLPreparedStatement.equals(""))
  675.       return null;
  676.     PreparedStatement ps = null;
  677.  
  678.     try {
  679.       if (log4j.isDebugEnabled())
  680.         log4j.debug("preparedStatement requested");
  681.       ps = conn.prepareStatement(SQLPreparedStatement, ResultSet.TYPE_SCROLL_INSENSITIVE,
  682.           ResultSet.CONCUR_READ_ONLY);
  683.       if (log4j.isDebugEnabled())
  684.         log4j.debug("preparedStatement received");
  685.     } catch (SQLException e) {
  686.       log4j.error("getPreparedStatement: " + SQLPreparedStatement + "\n" + e);
  687.       if (conn != null) {
  688.         try {
  689.           conn.setAutoCommit(true);
  690.           conn.close();
  691.         } catch (Exception ex) {
  692.           ex.printStackTrace();
  693.         }
  694.       }
  695.     }
  696.     return (ps);
  697.   }
  698.  
  699.   private static void releasePreparedStatement(PreparedStatement ps) {
  700.     if (ps != null) {
  701.       try {
  702.         ps.close();
  703.       } catch (Exception e) {
  704.         log4j.error("Error closing PreparedStatement", e);
  705.       }
  706.     }
  707.   }
  708.  
  709.   public static void setUserId(String user) {
  710.     if (user == null || !user.equals(getUserId())) {
  711.       userId.set(user);
  712.       changedInfo.set(true);
  713.     }
  714.   }
  715.  
  716.   public static String getUserId() {
  717.     return userId.get();
  718.   }
  719.  
  720.   public static void setProcessId(String processId) {
  721.     if (processId == null || !processId.equals(getProcessId())) {
  722.       SessionInfo.processId.set(processId);
  723.       changedInfo.set(true);
  724.     }
  725.   }
  726.  
  727.   public static String getProcessId() {
  728.     return processId.get();
  729.   }
  730.  
  731.   public static void setProcessType(String processType) {
  732.     if (processType == null || !processType.equals(getProcessType())) {
  733.       SessionInfo.processType.set(processType);
  734.       changedInfo.set(true);
  735.     }
  736.   }
  737.  
  738.   public static String getProcessType() {
  739.     return processType.get();
  740.   }
  741.  
  742.   public static void setSessionId(String session) {
  743.     if (session == null || !session.equals(getSessionId())) {
  744.       sessionId.set(session);
  745.       changedInfo.set(true);
  746.     }
  747.   }
  748.  
  749.   /**
  750.    * Forces changed info flag so next time it is checked it session info will be set in DB
  751.    */
  752.   public static void infoChanged() {
  753.     changedInfo.set(true);
  754.   }
  755.  
  756.   public static String getCommand() {
  757.     return command.get();
  758.   }
  759.  
  760.   public static void setCommand(String comm) {
  761.     command.set(comm);
  762.   }
  763.  
  764.   public static String getQueryProfile() {
  765.     return queryProfile.get();
  766.   }
  767.  
  768.   public static void setQueryProfile(String profile) {
  769.     queryProfile.set(profile);
  770.   }
  771.  
  772.   public static String getSessionId() {
  773.     return sessionId.get();
  774.   }
  775.  
  776.   public static void setAuditActive(boolean isAuditActive) {
  777.     SessionInfo.isAuditActive = isAuditActive;
  778.   }
  779.  
  780.   static void setSessionConnection(Connection conn) {
  781.     sessionConnection.set(conn);
  782.   }
  783.  
  784.   public static String getModuleId() {
  785.     return moduleId.get();
  786.   }
  787.  
  788.   public static void setModuleId(String moduleId) {
  789.     SessionInfo.moduleId.set(moduleId);
  790.   }
  791.  
  792.   public static boolean isUsageAuditActive() {
  793.     return usageAuditActive;
  794.   }
  795.  
  796.   public static void setUsageAuditActive(boolean usageAuditActive) {
  797.     SessionInfo.usageAuditActive = usageAuditActive;
  798.   }
  799.  
  800.   /** Set this value to {@code false} to prevent context info to be set in DB */
  801.   public static void auditThisThread(boolean shouldAudit) {
  802.     auditThisThread.set(shouldAudit);
  803.   }
  804. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement