Package org.eweb4j.cache

Source Code of org.eweb4j.cache.Props

package org.eweb4j.cache;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;

import org.eweb4j.config.ConfigConstant;
import org.eweb4j.config.LogFactory;
import org.eweb4j.config.bean.ConfigBean;
import org.eweb4j.config.bean.Prop;
import org.eweb4j.util.FileUtil;
import org.eweb4j.util.RegexList;
import org.eweb4j.util.StringUtil;

public class Props {
  /**
   * Global properties
   */
  private static Hashtable<String, String> globalMap = new Hashtable<String, String>();
  /**
   *
   */
  private static Hashtable<String, Hashtable<String, String>> props = new Hashtable<String, Hashtable<String, String>>();

  private Props() {
  }

  public static String get(String key) {
    return globalMap.get(key);
  }

  public static Hashtable<String, String> getMap(String id) {
    return props.get(id);
  }

  // 读取properties的全部信息
  public static synchronized String readProperties(Prop f, boolean isCreate)
      throws IOException {
    if (f == null || f.getPath().length() == 0)
      return null;

    String error = null;
    String filePath = ConfigConstant.CONFIG_BASE_PATH + f.getPath();
    String global = f.getGlobal();
    String id = f.getId();
    Properties properties = new Properties();
    InputStream in = null;
    Hashtable<String, String> tmpHt = new Hashtable<String, String>();
    try {
      in = new BufferedInputStream(new FileInputStream(filePath));
      properties.load(in);
      Enumeration<?> en = properties.propertyNames();
      while (en.hasMoreElements()) {
        String key = (String) en.nextElement();
        String property = properties.getProperty(key);
        if (!property.matches(RegexList.chinese_regexp)) {
          property = new String(property.getBytes("ISO-8859-1"),
              "UTF-8");
        }

        key = StringUtil.parseSingleProp(key, tmpHt);
        property = StringUtil.parseSingleProp(property, tmpHt);

        tmpHt.put(key, property);
        LogFactory.getIOCLogger("INFO").write(
            "key->" + key + "|prop->" + property);
      }
     
      if ("true".equalsIgnoreCase(global) || "1".equalsIgnoreCase(global))
        globalMap.putAll(tmpHt);
      else if (id != null && id.length() > 0) {
        props.put(id, tmpHt);
        LogFactory.getIOCLogger("INFO").write(
            "id->" + id + "|map->" + tmpHt);
      }

    } catch (FileNotFoundException e) {
      LogFactory.getIOCLogger("err").write(
          StringUtil.getExceptionString(e));
      if (isCreate) {
        boolean flag = FileUtil.createFile(filePath);
        if (flag) {
          error = filePath + " 创建成功";
          Props.writeProperties(filePath, "framework", "eweb4j");
          LogFactory.getIOCLogger("warring").write(error);
        } else {
          LogFactory.getIOCLogger("warring")
              .write(filePath + " 创建失败");
        }
      }
    } finally {
      if (in != null)
        in.close();
    }

    return error;

  }

  public synchronized static void writeProp(String propId, String key,
      Object value) throws IOException {
    if (propId == null)
      return;

    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class);
    List<Prop> files = cb.getProperties().getFile();
    for (Prop f : files) {
      if (propId.equals(f.getId())) {

        Hashtable<String, String> map = props.get(propId);
        if (map == null) {
          map = new Hashtable<String, String>();
          props.put(propId, map);
        }
        String val = String.valueOf(value);
        map.put(key, val);
        String filePath = ConfigConstant.CONFIG_BASE_PATH + f.getPath();

        writeProperties(filePath, key, val);

        break;
      }
    }

  }

  // 写入properties信息
  public synchronized static void writeProperties(String filePath,
      String parameterName, String parameterValue) throws IOException {
    Properties prop = new Properties();
    InputStream fis = null;
    OutputStream fos = null;

    fis = new FileInputStream(filePath);
    // 从输入流中读取属性列表(键和元素对)
    prop.load(fis);
    // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
    // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
    fos = new FileOutputStream(filePath);
    prop.setProperty(parameterName, parameterValue);
    // 以适合使用 load 方法加载到 Properties 表中的格式,
    // 将此 Properties 表中的属性列表(键和元素对)写入输出流
    prop.store(fos, "eweb4j last update '" + parameterName + "' value");

    fos.flush();

    if (fis != null)
      fis.close();
    if (fos != null)
      fos.close();

  }

  public static Hashtable<String, String> getGlobalMap() {
    return globalMap;
  }

  public static void setGlobalMap(Hashtable<String, String> globalMap) {
    Props.globalMap = globalMap;
  }

  public static Hashtable<String, Hashtable<String, String>> getProps() {
    return props;
  }

  public static void setProps(
      Hashtable<String, Hashtable<String, String>> props) {
    Props.props = props;
  }

}
TOP

Related Classes of org.eweb4j.cache.Props

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.