使用SOAP协议修改websphere数据源的密码,测试连接

package com.sortec.jmx.ds;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;


import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.ObjectName;
import javax.management.RuntimeMBeanException;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.Session;
import com.ibm.websphere.management.configservice.ConfigServiceHelper;
import com.ibm.websphere.management.configservice.ConfigServiceProxy;


/**
 * 使用JMX更改WAS的JDBC数据源,并实现不重启应用
 * @author wuxin 
 * @email wuxin@sortec.com.cn
 * @date 2012-5-9
 */
public class DataSourceHandler {


private Log log = LogFactory.getLog(DataSourceHandler.class);

private JdbcDsObj jdbcDsObj ;

public DataSourceHandler(JdbcDsObj jdbcDsObj){
this.jdbcDsObj = jdbcDsObj;
}


public boolean testConnectJdbcDs(){
boolean result = false;
log.info("Testing JDBC data source {}"+ jdbcDsObj.getDsName()+" password:"+jdbcDsObj.getDsPassword());         
AdminClient adminClient = null;
try{
adminClient = this.getAdminClient();
            if(adminClient == null) return result; 
            
            
            ObjectName handle = null;
            ObjectName queryName = new ObjectName("WebSphere:type=DataSourceCfgHelper,*");
            Set s = adminClient.queryNames(queryName, null);
            
            Iterator iter = s.iterator();
            while (iter.hasNext()) {
                // use the first MBean that is found
                handle = (ObjectName) iter.next();
                System.out.println("Found this ->" + handle);
            }
            
            
            String resURI = "cells/ooNode01Cell/nodes/ooNode01/servers/server1|resources.xml#DataSource_1336439714043";
            
            String[] signature = { "java.lang.String" };
            Object[] params = { resURI };  


            if (true) {
                System.out.println("\nTesting connection to the database using " + handle);
            }
            
            try {
                /*************************************************************************/
                /**  Start to test the connection to the database                        */
                /*************************************************************************/
                Object testResult = adminClient.invoke(handle, "testConnection", params, signature);
                System.out.println(testResult);
            } catch (MBeanException mbe) {
                // ****** all user exceptions come in here
                if (true) {
                    Exception ex = mbe.getTargetException(); // this is the real exception from the Mbean
                    System.out.println("\nNLS:Mbean Exception was received contains " + ex);
                    ex.printStackTrace();
                    System.exit(1);
                }
            } catch (InstanceNotFoundException infe) {
                System.out.println("Cannot find " + infe);
            } catch (RuntimeMBeanException rme) {
                Exception ex = rme.getTargetException();
                ex.printStackTrace(System.out);
                throw ex;
            } catch (Exception ex) {
                System.out.println("\nUnexpected Exception occurred: " + ex);
                ex.printStackTrace();
            }
          
}catch(Exception e){
e.printStackTrace();
}finally{
if(adminClient != null) adminClient = null;
return result;
}


}

public boolean updateJdbcDsPassword(){
boolean result = false;
log.info("Updating JDBC data source {}"+ jdbcDsObj.getDsName()+" password:"+jdbcDsObj.getDsPassword());         
AdminClient adminClient = null;
try{
adminClient = this.getAdminClient();
            if(adminClient == null) return result; 
            
            
            ConfigServiceProxy configService =  new ConfigServiceProxy(adminClient);
            Session session = new Session();
            ObjectName dataSource = ConfigServiceHelper.createObjectName(null, "DataSource", jdbcDsObj.getDsName());
            dataSource = configService.queryConfigObjects(session, null, dataSource, null)[0];
            System.out.println(dataSource.toString());
            ObjectName provider = (ObjectName) configService.getAttribute(session, dataSource, "provider");
            String currentProvider = (String) configService.getAttribute(session, provider, "name");
            log.info(jdbcDsObj.getDsName()+" currentProvider:"+currentProvider);
            
            
            AttributeList value = configService.getAttributes(session, dataSource, new String[]{"propertySet"}, false);
            ObjectName propertySet = (ObjectName) ConfigServiceHelper.getAttributeValue(value, "propertySet");
            AttributeList passowrdProperty = new AttributeList();
            passowrdProperty.add(new Attribute("name", "password"));
            passowrdProperty.add(new Attribute("type", "java.lang.String"));    
            passowrdProperty.add(new Attribute("value", jdbcDsObj.getDsPassword()));      
            
           // configService.removeElement(session, propertySet, "J2EEResourceProperty", passowrdProperty);
            configService.addElement(session, propertySet, "resourceProperties", passowrdProperty, -1);
            
             
         
            
            configService.save(session, true);
            result = true;
}catch(Exception e){
e.printStackTrace();
}finally{
if(adminClient != null) adminClient = null;
return result;
}
}

/**
* 查询所有的JDBC DataBase
* @return
*/
public List<String> queryJDBCDataSource(){
List list = new ArrayList();
log.info("Querying JDBC data source {}");
AdminClient adminClient = null;
try{
adminClient = this.getAdminClient();
            if(adminClient == null) return list; 
            
            ConfigServiceProxy configService =  new ConfigServiceProxy(adminClient);            
            Session session = new Session();
            
            ObjectName dsName = ConfigServiceHelper.createObjectName(null,"DataSource",null);
            
            ObjectName[] allDs = configService.queryConfigObjects(session, null,dsName, null);
            log.info("query all jdbc ds "+allDs.length);
            for(ObjectName ds : allDs){
                Object name = configService.getAttribute(session, ds, "name");
                log.info("jdbc ds name:"+name.toString());
                list.add(name.toString());
            }            
}catch(Exception e){
e.printStackTrace();
}finally{
if(adminClient != null) adminClient = null;
return list;
}
}


private AdminClient getAdminClient() {
AdminClient adminClient = null;
try {
String hostname = jdbcDsObj.getHostname();
int port = jdbcDsObj.getSoap_port();
String username = jdbcDsObj.getUsername();
String password = jdbcDsObj.getPassword();
String trustStore = jdbcDsObj.getTrustStore();
String keyStore = jdbcDsObj.getKeyStore();
String trustStorePassword = jdbcDsObj.getTrustStorePassword();
String keyStorePassword = jdbcDsObj.getKeyStorePassword();
log.info("\nWebsphere 开始连接");
// System.setProperty("javax.net.debug", "ssl");
Properties connectProps = new Properties();
connectProps.setProperty(AdminClient.CONNECTOR_TYPE,
AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST, hostname);
connectProps.setProperty(AdminClient.CONNECTOR_PORT, String
.valueOf(port));
connectProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED,
"false");


// WebSphere服务器是否已启用管理安全性
if (trustStore != null && !trustStore.trim().equals(""))
connectProps
.setProperty("javax.net.ssl.trustStore", trustStore);


if (keyStore != null && !keyStore.trim().equals(""))
connectProps.setProperty("javax.net.ssl.keyStore", keyStore);


if (trustStorePassword != null
&& !trustStorePassword.trim().equals(""))
connectProps.setProperty("javax.net.ssl.trustStorePassword",
trustStorePassword);


if (keyStorePassword != null && !keyStorePassword.trim().equals(""))
connectProps.setProperty("javax.net.ssl.keyStorePassword",
keyStorePassword);


if (username != null && !username.trim().equals(""))
connectProps.setProperty(AdminClient.USERNAME, username);


if (password != null && !password.trim().equals(""))
connectProps.setProperty(AdminClient.PASSWORD, password);


adminClient = AdminClientFactory.createAdminClient(connectProps);


log.info("\nWebsphere 连接成功");


} catch (Exception e) {
e.printStackTrace();
log.error("异常:getConnectWebsphere(),可能服务未启,或jar包资源不全 ");
}
return adminClient;
}  


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值