ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/datatestlang.htm

299 lines
11 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Example: Test a connection using country and language (properties)</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h6><a name="datatestlang"></a>Example: Test a connection using country and language (properties)</h6>
<p>It is possible to invoke the same test connection operation on the DataSourceCfgHelper MBean from a Java program that wsadmin uses by passing in the properties you wish to test. The following interfaces are tested:</p>
<ul>
<li>getPropertiesForDataSource()</li>
<li>reload()</li>
<li>testConnectionToDataSource()</li>
</ul>
<p>See the <a href="codex.htm">Code example disclaimer</a> for legal information about these code examples.</p>
<pre>
//
// This program may be used, executed, copied, modified and distributed without royalty for the
// purpose of developing, using, marketing, or distributing.
//
// Product 5630-A36, (C) COPYRIGHT International Business Machines Corp., 2001, 2002
// All Rights Reserved * Licensed Materials - Property of IBM
//
import java.util.*;
import javax.sql.DataSource;
import javax.transaction.*;
import javax.management.*;
import com.ibm.websphere.management.*;
import com.ibm.websphere.management.configservice.*;
import com.ibm.ws.exception.WsException;
import com.ibm.websphere.rsadapter.DSPropertyEntry;
/**
* Resource adapter test program to make sure that the MBean interfaces work.
* Following interfaces are tested
*
* -getPropertiesForDataSource()
* -reload()
* -testConnectionToDataSource()
*
*
* We need following to run .....
*
* From an iSeries command line:
*
* -> QSHELL
* (Start the QSHELL environment)
*
* -> cd (yourDirectory)
* (change current directory to the directory that holds the java source)
*
* -> . /QIBM/ProdData/WebASE/ASE5/base/bin/setupClient
* (produce the $JAVA_FLAGS_EXT environment variable)
*
* -> javac -extdirs /QIBM/ProdData/WebASE/ASE5/base/lib -d . testDS.java
* (compile the testDS class)
*
* -> java $JAVA_FLAGS_EXT testDS
* (call the program)
*
*/
public class testDS {
String port = &quot;8880&quot;;
String host = &quot;localhost&quot;;
final static boolean verbose = true;
/**
* Main method.
*
* @param args - Not used
*/
public static void main(String[] args) {
testDS cds = new testDS();
try {
cds.run(args);
} catch (com.ibm.ws.exception.WsException ex) {
System.out.println(&quot;Caught this &quot; + ex );
ex.printStackTrace();
//ex.getCause().printStackTrace();
} catch (Exception ex) {
System.out.println(&quot;Caught this &quot; + ex );
ex.printStackTrace();
}
}
/**
* This method tests the DataSourceCfgHelper Mbean.
*
* @param args - Not used
* @exception Exception
*/
public void run(String[] args) throws Exception {
try {
System.out.println(&quot;Connecting to the application server.......&quot;);
/*************************************************************************/
/** Initialize the AdminClient */
/*************************************************************************/
Properties adminProps = new Properties();
adminProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
adminProps.setProperty(AdminClient.CONNECTOR_HOST, host);
adminProps.setProperty(AdminClient.CONNECTOR_PORT, port);
AdminClient adminClient = null;
try {
/* Port 8880 must be listening when this program is called or the SOAP connection will fail */
adminClient = AdminClientFactory.createAdminClient(adminProps);
} catch (com.ibm.websphere.management.exception.ConnectorException ce) {
System.out.println(&quot;Cannot make a connection to the application server\n&quot;+ce);
System.exit(1);
}
/*************************************************************************/
/** Locate the Mbean */
/*************************************************************************/
ObjectName handle = null;
try {
ObjectName queryName = new ObjectName(&quot;WebSphere:type=DataSourceCfgHelper,*&quot;);
Set s = adminClient.queryNames(queryName, null);
Iterator iter = s.iterator();
if (iter.hasNext()) handle = (ObjectName)iter.next();
} catch (MalformedObjectNameException mone) {
System.out.println(&quot;Check the program variable queryName&quot; + mone);
} catch (com.ibm.websphere.management.exception.ConnectorException ce) {
System.out.println(&quot;Cannot connect to the application server&quot; + ce);
}
//System.out.println(&quot;Connected to the application server&quot; + handle);
/*************************************************************************/
/** Call the Mbean to get the data source properties */
/*************************************************************************/
String dsClassName = &quot;com.ibm.db2.jdbc.app.DB2StdXADataSource&quot;;
String providerLibPath = &quot;/QIBM/ProdData/Java400/ext/db2_classes.jar&quot;;
String[] signature = { &quot;java.lang.String&quot;, &quot;java.lang.String&quot;};
Object[] params = { dsClassName, providerLibPath};
Object result = null;
if (verbose) {
System.out.println(&quot;Calling getPropertiesForDataSource() for &quot; + dsClassName + &quot;\n&quot;);
}
try {
// get the properties
result = adminClient.invoke(handle, &quot;getPropertiesForDataSource&quot;, params, signature);
} catch (MBeanException mbe) {
if (verbose) {
System.out.println(&quot;\tMbean Exception &quot; + dsClassName);
}
} catch (InstanceNotFoundException infe) {
System.out.println(&quot;Cannot find &quot; + dsClassName);
} catch (Exception ex) {
System.out.println(&quot;Exception occurred calling getPropertiesForDataSource() for &quot; + dsClassName + ex);
}
// Pretty print what we found
Iterator propIterator = ((List)result).iterator();
System.out.println(format(&quot;Name&quot;,21)+ &quot;|&quot; + format(&quot;Default Value&quot;,34) + &quot;|&quot; +
format(&quot;Type&quot;,17) +&quot;|Reqd&quot;);
String line = &quot;_______________________________________________________________________________&quot;;
System.out.println(line);
while (propIterator.hasNext()) {
DSPropertyEntry dspe = (DSPropertyEntry)propIterator.next();
System.out.print(format(dspe.getPropertyName(),21)+&quot;|&quot;+ format(dspe.getDefaultValue(),34) + &quot;|&quot;);
System.out.println(format(dspe.getPropertyType(),17) +&quot;|&quot;+ ((dspe.isRequired())? &quot; Y&quot; : &quot; N&quot;));
}
System.out.println(line);
/*************************************************************************/
/** Invoke the reload function from the AdminClient to pickup the */
/* data source from the naming space. */
/*************************************************************************/
if (verbose) {
System.out.println(&quot;Calling reload()&quot;);
}
try {
result = adminClient.invoke(handle, &quot;reload&quot;, new Object[] {}, new String[] {});
} catch (MBeanException mbe) {
if (verbose) {
System.out.println(&quot;\tMbean Exception calling reload&quot; + mbe);
}
} catch (InstanceNotFoundException infe) {
System.out.println(&quot;Cannot find reload &quot;);
} catch (Exception ex) {
System.out.println(&quot;Exception occurred calling reload()&quot; + ex);
}
if (result==null &amp;&amp; verbose) {
System.out.println(&quot;OK reload()&quot;);
}
/*************************************************************************/
/** Start to test the connection to the database */
/*************************************************************************/
if (verbose) {
System.out.println(&quot;\nTesting connection to the database using &quot; + dsClassName);
}
String user = &quot;db2admin&quot;;
String password = &quot;db2admin&quot;;
Properties props = new Properties();
props.setProperty(&quot;databaseName&quot;, &quot;section&quot;);
// There are two ways to pass the locale: In WS 5.0, you can only pass in the
// language and the country in a String format. In WS 5.0.1 release, you can also pass
// in a Locale object.
// String[] signature2 = { &quot;java.lang.String&quot;, &quot;java.lang.String&quot;, &quot;java.lang.String&quot;,
// &quot;java.util.Properties&quot;, &quot;java.lang.String&quot;,&quot;java.util.Locale&quot;};
// Object[] params2 = { dsClassName, user, password,props ,providerLibPath, Locale.US};
Object result2 = null;
String[] signature2 = { &quot;java.lang.String&quot;, &quot;java.lang.String&quot;, &quot;java.lang.String&quot;,
&quot;java.util.Properties&quot;,
&quot;java.lang.String &quot;java.lang.String&quot;,&quot;java.lang.String&quot;);
Object[] params2 = { dsClassName, user, password,props ,providerLibPath, &quot;EN&quot;, &quot;US&quot;};
try {
result2 = adminClient.invoke(handle, &quot;testConnectionToDataSource&quot;, params2, signature2);
} catch (MBeanException mbe) {
if (verbose) {
System.out.println(&quot;\tMbean Exception &quot; + dsClassName);
}
} catch (InstanceNotFoundException infe) {
System.out.println(&quot;Cannot find &quot; + dsClassName);
} catch (RuntimeMBeanException rme) {
Exception ex = rme.getTargetException();
ex.printStackTrace(System.out);
throw ex;
} catch (Exception ex) {
System.out.println(&quot;Exception occurred calling testConnectionToDataSource() for &quot; + dsClassName + ex);
ex.printStackTrace();
}
if (result2 != null) {
System.out.println(&quot;ERROR Result= &quot; + result2);
} else if (verbose) {
System.out.println(&quot;OK testConnectionToDataSource()&quot;);
}
} catch (RuntimeOperationsException roe) {
Exception ex = roe.getTargetException();
ex.printStackTrace(System.out);
throw ex;
} catch (Exception ex) {
ex.printStackTrace(System.out);
throw ex;
}
}
/**
* Format the string right justified in the space provided,
* or truncate the string.
*
* @param in
* @param length
* @return
*/
public String format(Object in, int length) {
if (in ==null) {
in = &quot;-null-&quot;;
}
String ins = in.toString();
int insLength = ins.length();
if ( insLength > length) {
return ins.substring(0,length);
} else {
StringBuffer sb = new StringBuffer(length);
while (length - insLength > 0) {
sb.append(&quot; &quot;);
length--;
}
sb.append(ins);
return sb.toString();
}
}
}
</pre>
<p><strong>Note: </strong>This example may be wrapped for display purposes.</p>
</body>
</html>