package com.finet.bochk.common.distributed.service;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import com.finet.bochk.common.log.JLogger;
import com.finet.bochk.common.log.LoggerFactory;
/**
* 将保存失败的数据保存到服务器物理文件中
* @author lancelot
* @date 2008-11-05
*/
public class WriteErrorDataFile
{
private static JLogger log = LoggerFactory.getLogger(WriteErrorDataFile.class);
private static String fileField; //写入文件的目录
private static String fileNameInsert; //插入动作失败写入的文件
private static String fileNameUpdate; //更新动作失败写入的文件
private static String fileNameDefault; //其他动作失败写入的文件
private static String systemSeparator=System.getProperties().getProperty("file.separator");
private static String dbAndTableSeparator; //数据库名和表名之间的分隔符
private static String tableAndColsValueSeparator; //表名和列值之间的分隔符
private static String colsValueSeparator; //列值之间的分隔符
private static String colsValueAndNameSeparator; //列名称与值之间的分隔符
private static final String PROPERTIES_FILE_NAME="bochk-db-error.properties";
static
{
try
{
PropertiesConfiguration pc=new PropertiesConfiguration(PROPERTIES_FILE_NAME);
fileField=(String) (pc.getProperty("bochk.db.error.write.filefield"));
fileNameInsert=(String) (pc.getProperty("bochk.db.error.insert.file"));
fileNameUpdate=(String) (pc.getProperty("bochk.db.error.update.file"));
fileNameDefault=(String) (pc.getProperty("bochk.db.error.default.file"));
dbAndTableSeparator=(String) (pc.getProperty("bochk.db.table.separator"));
tableAndColsValueSeparator=(String) (pc.getProperty("bochk.table.cols.separator"));
colsValueSeparator=(String) (pc.getProperty("bochk.db.cols.separator"));
colsValueAndNameSeparator=(String) (pc.getProperty("bochk.db.cols.value.name.separator"));
}
catch (ConfigurationException e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
}
/**
* 将数据写入文件
* @param databaseName 数据库名
* @param operactionName 动作类型
* @param data 数据对象
*/
public synchronized void writeFile(String databaseName,String operactionName,Object data)
{
log.warn("Write File Start.........");
File file=new File(assemblyFileName(operactionName));
log.warn("Write File Name="+file.getPath()+file.getName());
FileWriter fw=null;
try
{
fw=new FileWriter(file,true);
fw.write(assemblyDataInfo(databaseName,operactionName,data));
fw.write(System.getProperty("line.separator"));
log.warn("Write File Success.........");
fw.flush();
}
catch (IOException e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
finally
{
if(fw!=null)
{
try
{
fw.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
}
}
log.warn("Write File End.........");
}
/**
* 获取要写入的字符串
* @param databaseName
* @param operactionName
* @param data
* @return
*/
protected String assemblyDataInfo(String databaseName,String operactionName,Object data)
{
String objectName=getClassName(operactionName);
StringBuffer info=new StringBuffer();
info.append(databaseName);
info.append(dbAndTableSeparator);
info.append(objectName);
info.append(tableAndColsValueSeparator);
info.append(analysisObject(objectName,data));
return info.toString();
}
/**
* 获取要写入的文件完整路径
* @param operactionName
* @return
*/
protected String assemblyFileName(String operactionName)
{
StringBuffer fileName=new StringBuffer();
fileName.append(fileField);
fileName.append(systemSeparator);
if(operactionName.indexOf("insert")>-1)
{
fileName.append(fileNameInsert);
}
else if(operactionName.indexOf("update")>-1)
{
fileName.append(fileNameUpdate);
}
else
{
fileName.append(fileNameDefault);
}
return fileName.toString();
}
/**
* 获取要解析的对象名
* @param operactionName
* @return
*/
protected String getClassName(String operactionName)
{
return operactionName.replaceAll("insert|update", "");
}
/**
* 动态获取要解析对象的属性值组装成字符串
* @param objectName
* @param object
* @return
*/
protected String analysisObject(String objectName,Object data)
{
StringBuffer colsValue=new StringBuffer();
//这里将用JAVA反射机制获取传入对象的所有get方法的值,排除getClass方法。
//由于大部分属性是private的,所有不用Fields.
try
{
Class cls=data.getClass();
Method[] methods=cls.getMethods();
for(int i=0;i<methods.length;i++)
{
if((methods[i].getName().indexOf("get")==0||methods[i].getName().indexOf("is")==0)&&!methods[i].getName().equals("getClass"))
{
String value=String.valueOf(methods[i].invoke(data, new Object[]{}));
String colName=methods[i].getName().replaceAll("get|is","");
colsValue.append(colName);
colsValue.append(colsValueAndNameSeparator);
if(value!=null&&!value.equals("null"))
{
colsValue.append(value);
colsValue.append(colsValueSeparator);
}
else
{
colsValue.append("");
colsValue.append(colsValueSeparator);
}
}
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
return colsValue.toString();
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import com.finet.bochk.common.log.JLogger;
import com.finet.bochk.common.log.LoggerFactory;
/**
* 将保存失败的数据保存到服务器物理文件中
* @author lancelot
* @date 2008-11-05
*/
public class WriteErrorDataFile
{
private static JLogger log = LoggerFactory.getLogger(WriteErrorDataFile.class);
private static String fileField; //写入文件的目录
private static String fileNameInsert; //插入动作失败写入的文件
private static String fileNameUpdate; //更新动作失败写入的文件
private static String fileNameDefault; //其他动作失败写入的文件
private static String systemSeparator=System.getProperties().getProperty("file.separator");
private static String dbAndTableSeparator; //数据库名和表名之间的分隔符
private static String tableAndColsValueSeparator; //表名和列值之间的分隔符
private static String colsValueSeparator; //列值之间的分隔符
private static String colsValueAndNameSeparator; //列名称与值之间的分隔符
private static final String PROPERTIES_FILE_NAME="bochk-db-error.properties";
static
{
try
{
PropertiesConfiguration pc=new PropertiesConfiguration(PROPERTIES_FILE_NAME);
fileField=(String) (pc.getProperty("bochk.db.error.write.filefield"));
fileNameInsert=(String) (pc.getProperty("bochk.db.error.insert.file"));
fileNameUpdate=(String) (pc.getProperty("bochk.db.error.update.file"));
fileNameDefault=(String) (pc.getProperty("bochk.db.error.default.file"));
dbAndTableSeparator=(String) (pc.getProperty("bochk.db.table.separator"));
tableAndColsValueSeparator=(String) (pc.getProperty("bochk.table.cols.separator"));
colsValueSeparator=(String) (pc.getProperty("bochk.db.cols.separator"));
colsValueAndNameSeparator=(String) (pc.getProperty("bochk.db.cols.value.name.separator"));
}
catch (ConfigurationException e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
}
/**
* 将数据写入文件
* @param databaseName 数据库名
* @param operactionName 动作类型
* @param data 数据对象
*/
public synchronized void writeFile(String databaseName,String operactionName,Object data)
{
log.warn("Write File Start.........");
File file=new File(assemblyFileName(operactionName));
log.warn("Write File Name="+file.getPath()+file.getName());
FileWriter fw=null;
try
{
fw=new FileWriter(file,true);
fw.write(assemblyDataInfo(databaseName,operactionName,data));
fw.write(System.getProperty("line.separator"));
log.warn("Write File Success.........");
fw.flush();
}
catch (IOException e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
finally
{
if(fw!=null)
{
try
{
fw.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
}
}
log.warn("Write File End.........");
}
/**
* 获取要写入的字符串
* @param databaseName
* @param operactionName
* @param data
* @return
*/
protected String assemblyDataInfo(String databaseName,String operactionName,Object data)
{
String objectName=getClassName(operactionName);
StringBuffer info=new StringBuffer();
info.append(databaseName);
info.append(dbAndTableSeparator);
info.append(objectName);
info.append(tableAndColsValueSeparator);
info.append(analysisObject(objectName,data));
return info.toString();
}
/**
* 获取要写入的文件完整路径
* @param operactionName
* @return
*/
protected String assemblyFileName(String operactionName)
{
StringBuffer fileName=new StringBuffer();
fileName.append(fileField);
fileName.append(systemSeparator);
if(operactionName.indexOf("insert")>-1)
{
fileName.append(fileNameInsert);
}
else if(operactionName.indexOf("update")>-1)
{
fileName.append(fileNameUpdate);
}
else
{
fileName.append(fileNameDefault);
}
return fileName.toString();
}
/**
* 获取要解析的对象名
* @param operactionName
* @return
*/
protected String getClassName(String operactionName)
{
return operactionName.replaceAll("insert|update", "");
}
/**
* 动态获取要解析对象的属性值组装成字符串
* @param objectName
* @param object
* @return
*/
protected String analysisObject(String objectName,Object data)
{
StringBuffer colsValue=new StringBuffer();
//这里将用JAVA反射机制获取传入对象的所有get方法的值,排除getClass方法。
//由于大部分属性是private的,所有不用Fields.
try
{
Class cls=data.getClass();
Method[] methods=cls.getMethods();
for(int i=0;i<methods.length;i++)
{
if((methods[i].getName().indexOf("get")==0||methods[i].getName().indexOf("is")==0)&&!methods[i].getName().equals("getClass"))
{
String value=String.valueOf(methods[i].invoke(data, new Object[]{}));
String colName=methods[i].getName().replaceAll("get|is","");
colsValue.append(colName);
colsValue.append(colsValueAndNameSeparator);
if(value!=null&&!value.equals("null"))
{
colsValue.append(value);
colsValue.append(colsValueSeparator);
}
else
{
colsValue.append("");
colsValue.append(colsValueSeparator);
}
}
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
log.error(e.getMessage(),e);
}
return colsValue.toString();
}
}