Package com.trulytech.mantis.system

Source Code of com.trulytech.mantis.system.CacheManager

package com.trulytech.mantis.system;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.LinkedHashMap;
import com.trulytech.mantis.result.DBResult;
import java.util.Enumeration;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.DatagramPacket;

/**
*
* <p>
* Title: Mantis
* </p>
*
* <p>
* Description: Cache管理
* </p>
*
* <p>
* Copyright: Copyright (c) 2002
* </p>
*
* <p>
* Company:
* </p>
*
* @author Wang Xian
* @version 1.0
*/
public class CacheManager {

  // 是否初始化
  public static boolean isInited = false;

  /**
   * 设置Cache到application中的CacheTable对象中
   *
   * @throws Exception
   */
  public static void init() throws Exception {
    if (isInited)
      return;
    Statement stmt = null;
    Connection conn = null;
    logWriter.Info("reading cache table...");
    try {
      conn = ConnManager.getConnection();
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      stmt.setFetchSize(Properties.Fetch_Size);
      stmt.setMaxRows(Properties.MaxRows);
      stmt.setQueryTimeout(Properties.QueryTimeout);

      SQLParser Parser = new SQLParser(stmt, null);

      for (Enumeration enumeration = com.trulytech.mantis.system.Properties.Cachekeys
          .keys(); enumeration.hasMoreElements();) {
        String key = (String) enumeration.nextElement();

        DBResult Result = Parser
            .QueryExecute(com.trulytech.mantis.system.Properties.Cachekeys
                .getProperty(key));
        logWriter.Debug("read " + key + " object successfully ( total "
            + Result.getResultBuffer().size() + " records )");
        com.trulytech.mantis.system.Properties.cacheTable.put(key
            .toLowerCase(), Result);

      }
      Parser = null;
      logWriter.Info("read cache table successfully");
      isInited = true;
    } catch (Exception e) {
      logWriter.Error("read cache table failure " + e.toString());
      isInited = true;
    } finally {
      try {
        if (stmt != null) {
          stmt.close();
          stmt = null;
        }
      } catch (Exception ex) {
        logWriter.Error("read cache table failure " + ex.toString());
      }
      try {
        ConnManager.closeConnection(conn);
      } catch (Exception ex) {
        logWriter.Error("read cache table failure " + ex.toString());
      }

    }

  }

  /**
   * 根据对象名获得对象
   *
   * @param Key
   *            String 对象名
   * @return DBResult
   */
  public static DBResult getCache(String Key) {

    logWriter.Debug("get Cache Table " + Key);

    if (isInited) {
      LinkedHashMap map = com.trulytech.mantis.system.Properties.cacheTable;
      if (map == null)
        return new DBResult();
      else {
        DBResult Result = (DBResult) map.get(Key.toLowerCase());
        if (Result == null)
          return new DBResult();
        else
          return Result;
      }
    } else
      return new DBResult();
  }

  /**
   * 刷新缓存表
   *
   * @param Key
   *            String 对象名
   * @param conn
   *            数据库连接
   * @throws Exception
   */
  public synchronized static void refresh(String Key, Connection conn)
      throws Exception {
    if (Key == null)
      return;
    else if (com.trulytech.mantis.system.Properties.Cachekeys == null)
      return;
    else if (com.trulytech.mantis.system.Properties.Cachekeys.get(Key) == null)
      return;
    // 如果数据合法
    Statement stmt = null;

    logWriter.Info("refresh cache table..." + Key);
    try {

      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      stmt.setFetchSize(Properties.Fetch_Size);
      stmt.setMaxRows(Properties.MaxRows);
      stmt.setQueryTimeout(Properties.QueryTimeout);

      SQLParser Parser = new SQLParser(stmt, null);
      DBResult Result = Parser
          .QueryExecute(com.trulytech.mantis.system.Properties.Cachekeys
              .getProperty(Key));

      com.trulytech.mantis.system.Properties.cacheTable.put(Key
          .toLowerCase(), Result);

      logWriter.Info("refresh cache table successfully (" + Key + ", total "
          + Result.getResultBuffer().size() + " records) ");
    } catch (Exception e) {
      logWriter.Error("refresh cache table failure (" + Key + ") " + e.toString());

    } finally {
      try {
        if (stmt != null) {
          stmt.close();
          stmt = null;
        }
      } catch (Exception ex) {
        logWriter.Error("refresh cache table failure (" + Key + ") " + ex.toString());
      }

    }

  }

  /**
   * 刷新Cache (具备同步功能)
   *
   * @param Key
   *            String
   * @throws Exception
   */
  public synchronized static void refresh(String Key) throws Exception {
    if (Properties.HasCluster) {
      SynchronizeCache(Key);
    } else {
      Connection conn = ConnManager.getConnection();
      try {
        refresh(Key, conn);

      } finally {
        try {
          ConnManager.closeConnection(conn);
        } catch (Exception ex) {
          logWriter.Error("refresh cache table failure (" + Key + ") " + ex.toString());
        }

      }
    }
  }

  /**
   * 同步Cache Cluster环境下Multicast
   *
   * @param Key
   *            String
   */
  private static void SynchronizeCache(String Key) {
    if (Properties.HasCluster) {
      try {
        InetAddress group = InetAddress
            .getByName(Properties.MultiCastGroupIP);
        MulticastSocket s = new MulticastSocket(
            Properties.MultiCastPort);
        s.joinGroup(group);
        DatagramPacket hi = new DatagramPacket(Key.getBytes(), Key
            .length(), group, Properties.MultiCastPort);
        s.send(hi);
        s.leaveGroup(group);
        s.close();
        logWriter.Info("sync cache information successfully (" + Key + ") ");
      } catch (Exception ex) {
        logWriter.Error("sync cache information failure (" + Key + ") " + ex.toString());
      }
    }
  }

  /**
   * 刷新所有缓存表
   *
   * @throws Exception
   */
  public synchronized static void refreshAll() throws Exception {
    if (com.trulytech.mantis.system.Properties.Cachekeys == null)
      return;
    // 如果数据合法
    Statement stmt = null;
    Connection conn = null;
    logWriter.Info("refresh all cache tables...");
    try {
      conn = ConnManager.getConnection();

      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      stmt.setFetchSize(Properties.Fetch_Size);
      stmt.setMaxRows(Properties.MaxRows);
      stmt.setQueryTimeout(Properties.QueryTimeout);

      SQLParser Parser = new SQLParser(stmt, null);
      for (Enumeration enumeration = com.trulytech.mantis.system.Properties.Cachekeys
          .keys(); enumeration.hasMoreElements();) {
        String key = (String) enumeration.nextElement();
        try
        {
        DBResult Result = Parser
            .QueryExecute(com.trulytech.mantis.system.Properties.Cachekeys
                .getProperty(key));

        com.trulytech.mantis.system.Properties.cacheTable.put(key
            .toLowerCase(), Result);
        logWriter.Debug("read " + key + " object successfully (total "
            + Result.getResultBuffer().size() + " records)");
        }
        catch (Exception ex)
        {
          logWriter.Error("refresh key failure " + ex.toString());
        }
      }
      Parser = null;
      logWriter.Info("refresh all cache tables successfully");
    } catch (Exception e) {
      logWriter.Error("refresh all cache tables failure" + e.toString());

    } finally {
      try {
        if (stmt != null) {
          stmt.close();
          stmt = null;
        }
      } catch (Exception ex) {
        logWriter.Error("refresh all cache tables failure" + ex.toString());
      }
      try {
        ConnManager.closeConnection(conn);
      } catch (Exception ex) {
        logWriter.Error("refresh all cache tables failure" + ex.toString());
      }

    }

  }

}
TOP

Related Classes of com.trulytech.mantis.system.CacheManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.