2 Replies Latest reply on May 8, 2009 5:22 AM by chetanchatwani

    How to change datasource properties using java client code ?

      Hi ,

      We have server/profile called ejbServer which contain datasource as BizLogicDS, we want to change the properties(such as URL, username, password, driver class etc.) for same using java client code and persist new values.

      Can someone help for same with required pointers to documents sample code if possible?


      Thanks and Regards,
      Chetan

        • 1. Re: How to change datasource properties using java client co
          vickyk

           

          "chetanchatwani" wrote:
          Hi ,
          Can someone help for same with required pointers to documents sample code if possible?

          Check this
          http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4129581#4129581

          • 2. Re: How to change datasource properties using java client co

            Hi Vicky,

            Thanks for you help, however I am getting following excprion :

            javax.management.InstanceNotFoundException: jboss.jca:service=ManagedConnectionF
            actory,name=BizLogicDB
            at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknow
            n Source)
            at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(Unknown
            Source)
            at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(Unknown Source)
            at TestJBoss.main(TestJBoss.java:61)


            *************************************Source code*********

            import javax.management.MBeanServer;
            import javax.management.MBeanServerInvocationHandler;
            import javax.management.ObjectName;
            import javax.management.MBeanServerFactory;

            import org.jboss.mx.util.MBeanServerLocator;
            import java.util.Properties;
            import javax.management.AttributeList;
            import javax.management.Attribute;

            import javax.naming.InitialContext;
            import javax.naming.Context;

            public class TestJBoss {

            public static void main(String args[]) {

            final String jndiURL = "jdbc/BizLogicDB";
            final String serverUrl="jnp://sys181.tdiinc.com:18026";
            final StringBuilder properties = new StringBuilder(512);
            javax.naming.InitialContext ic = null;

            try{

            ic = getInitialContext(serverUrl);

            if(null!=ic){
            System.out.println("Got InitialContext object");
            } else {
            System.out.println("Failed to get InitialContext object");
            }

            MBeanServer server = MBeanServerFactory.createMBeanServer("ejbServer");

            if(null!=server){
            System.out.println("Got server object");

            ObjectName mcfName = new ObjectName("jboss.jca:service=ManagedConnectionFactory,name=" + "BizLogicDB");
            ObjectName mcpName = new ObjectName("jboss.jca:service=ManagedConnectionPool,name=" + jndiURL);
            ObjectName dsName = new ObjectName("jboss.jca:service=DataSourceBinding,name=" + jndiURL);
            Object dsBean = server.createMBean("org.jboss.resource.adapter.jdbc.remote.WrapperDataSourceService", dsName);
            if(null!=mcfName){
            System.out.println("Got mcfName object");
            } else {
            System.out.println("Failed to get mcfName object");
            }
            if(null!=dsName){
            System.out.println("Got dsName object");

            AttributeList al = new AttributeList();
            al.add(new Attribute("UseJavaContext", Boolean.TRUE));
            server.setAttributes(dsName, al);

            properties.append("\nUser=");
            properties.append("chetan1");
            properties.append("\nPassword=");
            properties.append("chetan1");
            properties.append('\n');
            server.invoke(mcfName, "setManagedConnectionFactoryAttribute", new Object[] { "XADataSourceProperties",
            java.lang.String.class, properties.toString() }, new String[] { "java.lang.String", "java.lang.Class",
            "java.lang.Object" });
            } else {
            System.out.println("Failed to get dsName object");
            }

            } else {
            System.out.println("Failed to get server object");
            }

            }catch(Throwable th){
            th.printStackTrace();
            }


            }

            public static InitialContext getInitialContext(String serverUrl) throws Exception{

            Properties props = new Properties();
            props.put(Context.PROVIDER_URL, serverUrl);
            props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            props.setProperty(Context.SECURITY_PRINCIPAL, "guest");
            props.setProperty(Context.SECURITY_CREDENTIALS, "guest");

            return new InitialContext(props);
            }

            }


            ************************************************************

            Any help for same
            Thanks and Regards,
            Chetan