public static final String ADAPTOR_CONFIG_PATH = "com/qam/idm/dataexchange/app/adaptor/config/";
public static String xmlPath = AdaptorFTPDaoImpl.class.getClass().getResource("/").getPath();
private Logger logger = Logger.getLogger(this.getClass());
//创建适配器配置文件
public void insertAdaptor(BaseAdaptor adaptor) throws IOException {
String fileName = adaptor.getAdaptorName();
boolean rtn = false;
// 使用DocumentHelper.createDocument方法建立一个文档实例
Document document = DocumentHelper.createDocument();
// 使用addElement方法方法创建根元素
Element beansElement = document.addElement("beans", "http://www.springframework.org/schema/beans");
// 使用addAttribute方法向beansElement元素添加xmlns和xmlns:xsi和"xsi:schemaLocation"属性
beansElement.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
beansElement.addAttribute("xsi:schemaLocation", "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd");
// 使用addComment方法方法向catalog元素添加注释
beansElement.addComment("An XML beans");
// 使用addElement方法向beans元素添加descriptionElement子元素
Element descriptionElement = beansElement.addElement("description");
// 使用addAttribute方法向journal元素添加title和publisher属性
descriptionElement.setText("this is my test description!");
//使用addElement方法向beans元素添加readerElement子元素
Element readerElement = beansElement.addElement("bean");
readerElement.addAttribute("id", "Reader");
readerElement.addAttribute("class", "org.openadaptor.auxil.connector.jdbc.reader.JDBCReadConnector");
//向readerElement元素添加子元素descriptionElement
Element readerDescElement = readerElement.addElement("description");
readerDescElement.setText("Reader which polls database using configured SQL.");
//向readerElement元素添加子元素propertyElement
Element readerPropertyJDBCElement = readerElement.addElement("property");
readerPropertyJDBCElement.addAttribute("name", "jdbcConnection");
readerPropertyJDBCElement.addAttribute("ref", "JdbcConnection");
//向readerElement元素添加子元素propertyElement
Element readerPropertyBitchSizeElement = readerElement.addElement("property");
readerPropertyBitchSizeElement.addAttribute("name", "batchSize");
readerPropertyBitchSizeElement.addAttribute("value", "${bathsize}");
//向readerElement元素添加子元素propertyElement
Element readerPropertyResultSetConverterElement = readerElement.addElement("property");
readerPropertyResultSetConverterElement.addAttribute("name", "resultSetConverter");
//向readerPropertyResultSetConverterElement元素添加子元素beanElement
Element readerPropertyResultSetConverterBeanElement = readerPropertyResultSetConverterElement.addElement("bean");
readerPropertyResultSetConverterBeanElement.addAttribute("class", "org.openadaptor.auxil.connector.jdbc.reader.xml.ResultSetToXMLConverter");
//向readerElement元素添加子元素propertyElement
Element readerPropertySQLElement = readerElement.addElement("property");
readerPropertySQLElement.addAttribute("name", "sql");
//向readerPropertySQLElement元素添加子元素propertyValueElement
Element readerPropertySQLValueElement = readerPropertySQLElement.addElement("value");
readerPropertySQLValueElement.setText("${readersql}");
if("".equals("")) {
//使用addElement方法向beans元素添加jdbcConnectionElement子元素
Element jdbcConnElement = beansElement.addElement("bean");
jdbcConnElement.addAttribute("id", "JdbcConnection");
jdbcConnElement.addAttribute("class", "org.openadaptor.auxil.connector.jdbc.JDBCConnection");
//向jdbcConnElement元素添加子元素propertyDriverElement
Element jdbcConnPropertyDriverElement = jdbcConnElement.addElement("property");
jdbcConnPropertyDriverElement.addAttribute("name", "driver");
jdbcConnPropertyDriverElement.addAttribute("value", "${driver}");
//向jdbcConnElement元素添加子元素propertyURLElement
Element jdbcConnPropertyURLElement = jdbcConnElement.addElement("property");
jdbcConnPropertyURLElement.addAttribute("name", "url");
jdbcConnPropertyURLElement.addAttribute("value", "${jdbcurl}");
//向jdbcConnElement元素添加子元素propertyUsernameElement
Element jdbcConnPropertyUsernameElement = jdbcConnElement.addElement("property");
jdbcConnPropertyUsernameElement.addAttribute("name", "username");
jdbcConnPropertyUsernameElement.addAttribute("value", "${uname}");
//向jdbcConnElement元素添加子元素propertyPasswordElement
Element jdbcConnPropertyPasswordElement = jdbcConnElement.addElement("property");
jdbcConnPropertyPasswordElement.addAttribute("name", "password");
jdbcConnPropertyPasswordElement.addAttribute("value", "${pword}");
}
if("".equals("")) {
// <bean id="JndiConnection" class="org.openadaptor.auxil.connector.jndi.JNDIConnection">
// <property name="initialContextFactory" value="${jndi.initialContextFactory}" />
// <property name="providerUrl" value="${jndi.providerUrl}" />
// </bean>
//使用addElement方法向beans元素添加jndiConnectionElement子元素
Element jndiConnectionElement = beansElement.addElement("bean");
jndiConnectionElement.addAttribute("id", "JndiConnection");
jndiConnectionElement.addAttribute("class", "org.openadaptor.auxil.connector.jndi.JNDIConnection");
//向jndiConnectionElement元素添加子元素propertyDriverElement
Element jndiInitialContextFactoryElement = jndiConnectionElement.addElement("property");
jndiInitialContextFactoryElement.addAttribute("name", "initialContextFactory");
jndiInitialContextFactoryElement.addAttribute("value", "${jndi.initialContextFactory}");
//向jdbcConnElement元素添加子元素propertyURLElement
Element jndiProviderUrlElement = jndiConnectionElement.addElement("property");
jndiProviderUrlElement.addAttribute("name", "providerUrl");
jndiProviderUrlElement.addAttribute("value", "${jndi.providerUrl}");
}
// id,adaptorName, adaptorType,status,createTime
// 使用DocumentHelper.createDocument方法建立一个文档实例,来记录所有的adaptor信息
Document adaptorManageDocument = DocumentHelper.createDocument();
// 使用addElement方法方法创建根元素
Element beansAdaptorElement = adaptorManageDocument.addElement("beans", "http://www.springframework.org/schema/beans");
// 使用addAttribute方法向beansAdaptorElement元素添加xmlns和xmlns:xsi和"xsi:schemaLocation"属性
beansAdaptorElement.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
beansAdaptorElement.addAttribute("xsi:schemaLocation", "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd");
// 使用addComment方法方法向catalog元素添加注释
beansAdaptorElement.addComment("An XML beans");
// 使用addElement方法向beans元素添加descriptionElement子元素
Element adaptorBeanElement = beansAdaptorElement.addElement("bean");
adaptorBeanElement.addAttribute("id", "id");
adaptorBeanElement.addAttribute("adaptorName", "testadaptorName");
adaptorBeanElement.addAttribute("adaptorType", "adaptorName");
adaptorBeanElement.addAttribute("status", "true");
adaptorBeanElement.addAttribute("createTime", "2010-08-24");
XMLWriter output;
//输出格式化
OutputFormat format = OutputFormat.createPrettyPrint();
try {
output = new XMLWriter(new FileWriter(xmlPath + ADAPTOR_CONFIG_PATH + "test.xml"), format);
output.write(document);
output.println();
output.close();
rtn = true;
} catch (IOException e) {
e.printStackTrace();
}
try {
output = new XMLWriter(new FileWriter(xmlPath + ADAPTOR_CONFIG_PATH + "adaptormanage.xml"), format);
output.write(adaptorManageDocument);
output.println();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public int deleteAdaptor(BaseAdaptor adaptor) {
//DOM4J使用
try {
//删除指定Adaptor配置文件
File f = new File(xmlPath + this.ADAPTOR_CONFIG_PATH + "test.xml"); //定义文件路径
if(f.exists()) {
f.delete();
}
//从Adaptor配置管理文件中删除指定的Adaptor适配器记录
Document document = new SAXReader().read(Thread.currentThread().getContextClassLoader()
.getResourceAsStream(ADAPTOR_CONFIG_PATH + "adaptormanage.xml"));
Element root = document.getRootElement();
for(Iterator iter = root.elementIterator(); iter.hasNext();) {
Element beanElement = (Element) iter.next();
System.out.println(beanElement.attributeValue("adaptorName"));
String adaptorName = beanElement.attributeValue("adaptorName");
if(null != adaptorName && adaptorName.equals("testadaptorName")) {
root.remove(beanElement);
break;
}
}
XMLWriter output;
//输出格式化
OutputFormat format = OutputFormat.createPrettyPrint();
try {
output = new XMLWriter(new FileWriter(xmlPath + ADAPTOR_CONFIG_PATH + "adaptormanage.xml"), format);
output.write(document);
output.println();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public static void main(String[] args) throws IOException {
BaseAdaptor adaptor = new BaseAdaptor();
adaptor.setAdaptorName("E:/test.xml");
AdaptorFTPDaoImpl ftpAdaptorImpl = new AdaptorFTPDaoImpl();
// ftpAdaptorImpl.insertAdaptor(adaptor);
// ftpAdaptorImpl.deleteAdaptor(adaptor);
}