public static void modifyProperties(Properties properties)
{
InputStream inputStream = null;
OutputStream fos = null;
Properties tempProper = new Properties();
File file = new File(Constant.CipherKey_FILE_PATH);
try
{
inputStream = new FileInputStream(file);
tempProper.load(inputStream);
fos = new FileOutputStream(file);
tempProper.setProperty("userName", properties.getProperty("userName"));
tempProper.setProperty("passwd", properties.getProperty("passwd"));
tempProper.store(fos, null);
}
catch (Exception e)
{
log.error("Tools calss Setting PropertyValue is fail!"+e.getMessage());
}finally
{
try
{
if(null != fos)
{
fos.close();
}
if(null != inputStream)
{
inputStream.close();
}
}
catch (IOException e)
{
log.error("Tools calss close OutputStream fial!"+e.getMessage());
}
}
}
=================================================
package com.huawei.idesign.tablesetdatas.dataprocess.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.log4j.PropertyConfigurator;
import com.as.oms.log.OMSLog;
import com.as.oms.log.OMSLogFactory;
import com.huawei.authorization.HttpValidate;
import com.huawei.idesign.util.DataBaseException;
/**
* 工具类
*/
public class Tools
{
static OMSLog log = Tools.getLog(Tools.class);
/**
* 文件内容集合 key :实际文件名称 value :Map<实际文件中的sheet名称, Map<表字段名称,
* String[文件中列索引,文件中实际列名]>>
*/
private static Map<String, Map<String, Map<String, String[]>>> fileMap =
null;
/**
* 名称集合 <文件名称,<实际文件中的sheet名称,表名(常量)>
*/
private static Map<String, Map<String, String>> fileNameMap = null;
/**
* 表名称对应的列集合 <表名,<列名,String[文件中列索引,文件中实际列名]>
*/
private static Map<String, Map<String, String[]>> tableMap = null;
/**
* 数据表集合 key :表名(常量) value :map<列索引,列名>
*/
private static Map<String, Map<Integer, String>> dataBaseMap = null;
/**
* 文件下的sheet名称集合(按读入顺序),导出
*/
private static Map<String, List<String>> fileSheetNameMap = null;
/**
* 支持导出文件名称集合
*/
private static List<String> exportFileNameList = null;
/**
* 插入
*/
public final static int insert = 1;
/**
* 修改
*/
public final static int update = 2;
/**
* 删除
*/
public final static int delete = 3;
/**
* 文件语言版本
*/
private static Map<String, String> fileLanguageVersion = null;
/**
* 取得日志
*
* @param clas
* @return
*/
public static OMSLog getLog(Class<?> clas)
{
String log4jName = getStringNoNull(System.getProperty("log4jname"));
if (!"".equals(log4jName))
{
PropertyConfigurator.configure(Constant.PROJECT_DIR + "/config/"
+ log4jName + ".properties");
}
return OMSLogFactory.getLog(clas);
}
/**
* 得到对应资源的资源实例
*
* @param resourceFilePath 资源文件路径
* @return ResourceBundle 资源实例
*/
public static ResourceBundle getResourceBundle(String resourceFilePath)
{
ResourceBundle resourceBundle =
ResourceBundle.getBundle(resourceFilePath);
return resourceBundle;
}
/**
* 得到对应资源的资源实例
*
* @param resourceFilePath 资源文件路径
* @return ResourceBundle 资源实例
*/
public static ResourceBundle getResourceBundle(String resourceFilePath,
Locale locale)
{
ResourceBundle resourceBundle =
ResourceBundle.getBundle(resourceFilePath, locale);
return resourceBundle;
}
/**
* 得到属性文件实例
*/
public static Properties getPropertiesByFile(String filePath)
{
InputStream inputStream = null;
Properties properties = new Properties();
try
{
File file = new File(filePath);
inputStream = new FileInputStream(file);
properties.load(inputStream);
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
finally
{
try
{
inputStream.close();
}
catch (IOException e)
{
log.error(e.getMessage(), e);
}
}
return properties;
}
/**
* 初始化导入文件功能的配置文件
*/
private static void initConfig()
{
XMLReader reader = new XMLReader();
fileMap = reader.readConfigXMLFile();
dataBaseMap = reader.readDatabaseXMLFile();
fileNameMap = reader.getSheetNameMap();
exportFileNameList = reader.getExportFileNameList();
tableMap = reader.getTableMap();
fileSheetNameMap = reader.getFileSheetNameMap();
fileLanguageVersion = reader.getFileLanguageVersion();
}
/**
* 支持导出文件名称集合
*/
public static List<String> getExportFileNameList()
{
if (exportFileNameList == null)
{
initConfig();
}
return exportFileNameList;
}
/**
* 根据表名称从database.xml文件中得到表字段集合
*
* @param tableName 表名称
* @return Map<Integer,String> 表字段集合<字段索引,字段名称>
*/
public static Map<Integer, String> getColumnMapByTableName(String tableName)
{
if (dataBaseMap == null)
{
initConfig();
}
return dataBaseMap.get(tableName.toLowerCase(Locale.getDefault()));
}
/**
* 得到对应的EXCEL文件内容集合
*
* @param fileName 文件名称
* @return Map<String, Map<String, String[]>> EXCEL文件内容集合
*/
public static Map<String, Map<String, String[]>> getSheetsByFile(
String fileName)
{
if (fileMap == null)
{
initConfig();
}
return fileMap.get(fileName.toLowerCase(Locale.getDefault()));
}
/**
* 得到对应的EXCEL文件内一个sheet页的内容集合
*
* @param fileName 文件名称
* @param sheetName sheet页名称
* @return Map<String, String[]> 一个sheet页的内容集合<表列字段名称,[文件列索引,文件列名]>
*/
public static Map<String, String[]> getColumnBySheet(String fileName,
String sheetName)
{
Map<String, Map<String, String[]>> map =
getSheetsByFile(fileName.toLowerCase(Locale.getDefault()));
return map.get(sheetName.toLowerCase(Locale.getDefault()));
}
/**
* 得到表名对应配置的列属性集合
*
* @param fileName 表名称
* @return Map<String, String[]> map<表列字段名称,[文件列索引,文件列名]>
*/
public static Map<String, String[]> getColumnByTableName(String tableName)
{
if (tableMap == null)
{
initConfig();
}
return tableMap.get(tableName.toLowerCase(Locale.getDefault()));
}
/**
* 得到EXCEL中sheet页对应的表名
*
* @param sheetName sheet页名
* @return String 表名
*/
public static String getTableName(String fileName, String sheetName)
{
if (fileNameMap == null)
{
initConfig();
}
return fileNameMap.get(fileName.toLowerCase(Locale.getDefault())).get(
sheetName.toLowerCase(Locale.getDefault()));
}
/**
* 得到EXCEL文件中sheet页的名称集合
*
* @return List<String> sheet页名集合,按读入顺序
*/
public static List<String> getSheetNameByFileName(String fileName)
{
if (fileSheetNameMap == null)
{
initConfig();
}
return fileSheetNameMap.get(fileName.toLowerCase(Locale.getDefault()));
}
/**
* 配置文件中的文件集合
*
* @return Map<实际文件中的sheet名称, Map<表字段名称, String[文件中列索引,文件中实际列名]>>
*/
public static Map<String, Map<String, Map<String, String[]>>> getFileMap()
{
if (fileMap == null)
{
initConfig();
}
return fileMap;
}
/**
* 取当前数据库中数据版本
*
* @return String 当前数据版本
*/
public static int getDataBaseVersion(int languageId)
{
int version = 0;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try
{
connection = JdbcUtil.getConnection();
statement = connection.createStatement();
rs =
statement
.executeQuery("select Version,languageId from version where languageId="
+ languageId);
while (rs.next())
{
String s = rs.getString("version");
version = Integer.parseInt(s);
}
}
catch (DataBaseException e)
{
log.error(e.getMessage(), e);
}
catch (SQLException e)
{
log.error(e.getMessage(), e);
}
finally
{
JdbcUtil.release(rs, statement);
}
return version;
}
/**
* 取出当前表中最大ID值
*
* @return 表中ID值
*/
public static long getTableMaxID(String tableName, String idName)
{
long id = 0;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try
{
connection = JdbcUtil.getConnection();
statement = connection.createStatement();
rs =
statement.executeQuery("select max(" + idName + ") from "
+ tableName);
while (rs.next())
{
id = rs.getLong(1);
}
}
catch (DataBaseException e)
{
log.error(e.getMessage(), e);
}
catch (SQLException e)
{
log.error(e.getMessage(), e);
}
finally
{
JdbcUtil.release(rs, statement);
}
id++;
return id;
}
/**
* 取出当前表中最大ID值(默认ID列名称为“id”)
*
* @return 表中ID值
*/
public static long getTableMaxID(String tableName)
{
return getTableMaxID(tableName, "id");
}
/**
* 请空表数据
*
* @param histableName 表名
* @throws DataBaseException
*/
public static void clearTableData(String tableName)
throws DataBaseException
{
String sql = "delete from " + tableName;
PreparedStatement preparedStatement = null;
try
{
preparedStatement = JdbcUtil.getConnection().prepareStatement(sql);
preparedStatement.execute();
}
catch (SQLException e)
{
throw new DataBaseException(e.getMessage(), e);
}
finally
{
JdbcUtil.release(null, preparedStatement);
}
}
/**
* 从文件路径中取出文件名称,不包含文件类型后缀
*
* @param filePath
* @return
*/
public static String getFileNameFromFilePath(String filePath)
{
String fileName =
filePath.substring(filePath.lastIndexOf("\\") + 1,
filePath.lastIndexOf("."));
// 处理有些文件配置路径为test/file.xml格式
if (fileName.indexOf("/") != -1)
{
fileName =
fileName.substring(fileName.lastIndexOf("/") + 1,
fileName.length());
}
return fileName;
}
/**
* 获取操作类型
*
* @param action
* @return
*/
public static String getOperationByAction(int action)
{
String op = "";
switch (action)
{
case insert:
op = Constant.OPERATION_CREATE;
break;
case update:
op = Constant.OPERATION_UPDATE;
break;
case delete:
op = Constant.OPERATION_DELETE;
break;
default:
break;
}
return op;
}
/**
* 获取操作类型对应值
*
* @param action
* @return
*/
public static int getActionByOperation(String operation)
{
int action = 0;
if (Constant.OPERATION_CREATE.equalsIgnoreCase(operation))
{
action = insert;
}
else if (Constant.OPERATION_UPDATE.equalsIgnoreCase(operation))
{
action = update;
}
else if (Constant.OPERATION_DELETE.equalsIgnoreCase(operation))
{
action = delete;
}
return action;
}
/**
* 得到文件中配置版本号子,如果配置不正确返-1。
*
* @return int 文件中配置版本号
*/
public static int getFileVersion(int languageId, ExcelReader excelReader)
{
int result = 0;
try
{
Locale locale = Locale.getDefault();
if (languageId == 1)
{
locale = Locale.CHINA;
}
else if (languageId == 2)
{
locale = Locale.US;
}
String fileVersion =
getResourceBundle(
"com.huawei.idesign.tablesetdatas.dataprocess.properties.tool",
locale).getString("file_version");
for (int i = 0; i < excelReader.getSheetNum(); i++)
{
String execlSheetName = excelReader.getSheetNameByIndex(i);
if (fileVersion.equalsIgnoreCase(execlSheetName))
{
List<List<String>> list = excelReader.getAllData(i);
String version = list.get(0).get(0);
result = (int) Double.parseDouble(version);
}
}
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
return result;
}
/**
* 文件语言版本
*
* @return map<文件名,语言版本>
*/
public static Map<String, String> getFileLanguageVersion()
{
if (fileLanguageVersion == null)
{
initConfig();
}
return fileLanguageVersion;
}
/**
* 将excel中的列索引转换为下标
*
* @param op
* @return
*/
public static int getIndex(String op)
{
String alphabet = "abcdefghijklmnopqrstuvwxyz";
try
{
// 如果是数字,直接返回,是字母再转换
int index = Integer.parseInt(op) - 1;
return index;
}
catch (NumberFormatException e)
{
String s = op.toLowerCase(Locale.getDefault());
if (s.length() == 1)
{
return alphabet.indexOf(s);
}
else if (s.length() == 2)
{
String fir = s.substring(0, 1);
String sed = s.substring(1, 2);
int f =
(alphabet.indexOf(fir) + 1) * 26 + alphabet.indexOf(sed);
return f;
}
else
{
return -1;
}
}
}
public static String getStringNoNull(Object str)
{
if (str == null)
{
return "";
}
else
{
return str.toString().trim();
}
}
public static String getPropertyByString(String key, String defaultValue)
{
return getStringNoNull(Tools.getPropertiesByFile(
Constant.CipherKey_FILE_PATH).getProperty(key, defaultValue));
}
/**
* 需要修改的properties键值对集合
* @param listproperties
* @see [类、类#方法、类#成员]
*/
public static void modifyProperties(Properties properties)
{
InputStream inputStream = null;
OutputStream fos = null;
Properties tempProper = new Properties();
File file = new File(Constant.CipherKey_FILE_PATH);
try
{
inputStream = new FileInputStream(file);
tempProper.load(inputStream);
fos = new FileOutputStream(file);
tempProper.setProperty("userName", properties.getProperty("userName"));
tempProper.setProperty("passwd", properties.getProperty("passwd"));
tempProper.store(fos, null);
}
catch (Exception e)
{
log.error("Tools calss Setting PropertyValue is fail!"+e.getMessage());
}finally
{
try
{
if(null != fos)
{
fos.close();
}
if(null != inputStream)
{
inputStream.close();
}
}
catch (IOException e)
{
log.error("Tools calss close OutputStream fial!"+e.getMessage());
}
}
}
public static int getPropertyByInt(String key, int defaultValue)
{
int result = defaultValue;
String strValue = getStringNoNull(Tools.getPropertiesByFile(
Constant.CipherKey_FILE_PATH).getProperty(key));
try {
result = Integer.parseInt(strValue);
} catch (NumberFormatException e) {
log.error(e.getMessage());
}
return result;
}
public static boolean authorise()
{
/**
* 增加用户鉴权
*/
boolean isLogin = false;
try
{
//得到解密后的passwd值
String tempPasswd = CipherUtil.decryptPwd(getPropertyByString("passwd",""));
//得到配置文件中的user值
String tempUser = getPropertyByString("userName","");
//得到配置的URL
String tempUrl = getPropertyByString("url","");
isLogin = HttpValidate.validConnector(tempUser, tempPasswd, tempUrl);
// if("admin".equals(tempUser) && "admin".equals(tempPasswd))
// {
// isLogin = true;
// }
}
catch (Exception e1)
{
log.error("CFGSelectDialog calss Getting PropertyValue is fail!"+e1.getMessage());
}
return isLogin;
}
}