WebSphere Application Server for z/OS, Version 6.1   
             オペレーティング・システム: z/OS

             目次と検索結果のパーソナライズ化

例: Java Management Extensions API を使用した、BMP Bean、セ ッション Bean、またはサーブレット用の JDBC ドライバーと データ・ソースの作成

このコード例は、JDBC プロバイダーとデータ・ソースの構成 、データ・ソースの許可別名の設定、および Mbean を再ロードしての構成変更の 方法を示しています。

// "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.*;
import javax.transaction.*;
import javax.management.*;

import com.ibm.websphere.management.*;
import com.ibm.websphere.management.configservice.*;
import com.ibm.ws.exception.WsException;

/**
 * Creates a node-scoped resource.xml entry for a
 * DB2 for zOS Local JDBC Provider (RRS) DataSource
 * when WebSphere security is not enabled
 *
 * To run this example, the following must be done:
 *
 * 1)  Set the WAS_HOME environment variable to the location of
 *     your WebSphere Application Server for z/OS Configuration
 *     directory
 *
 *     Example:  export WAS_HOME=/WebSphereV6R1M0/AppServer
 *
 * 2)  Set the following environment variables:
 *
 *     export WAS_LIB=$WAS_HOME/lib
 *     export WAS_CLASSPATH=[DIRECTORY_CONTAINING_THIS_FILE]
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/jmxc.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/admin.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_LIB/ibmjlog.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_HOME/runtime/admin.client_6.1.0.jar
 *     export WAS_CLASSPATH=$WAS_CLASSPATH:$WAS_HOME/java/jre/lib/ext/mail.jar

 *
 * 3)  Execute the following commands:
 *
 *     javac -classpath $WAS_CLASSPATH CreateDataSourceBMP.java
 *     java -classpath $WAS_CLASSPATH CreateDataSourceBMP
 */
 public class CreateDataSourceBMP {

    String dsName = "MyDataSourceBMP";  // ds display name , also jndi name and CF name
    String dbName           = "LOC1";           // database name
    String authDataAlias    = "IBMUSER";        // an authentication data alias
    String uid              = "IBMUSER";        // userid
    String pw               = "IBMUSER";        // password
    String dbclasspath      = "/db2beta/db2710/classes/db2j2classes.zip"; // path to the db driver
    String dblibpath = "/db2beta/db2710/lib";                         // path to the db native library directory

    /**
     * Main method.
     */
    public static void main(String[] args) {
        CreateDataSourceBMP cds = new CreateDataSourceBMP();
        try {
            cds.run(args);
        } catch (com.ibm.ws.exception.WsException ex) {
            System.out.println("Caught this " + ex );
            ex.printStackTrace();
        } catch (Exception ex) {
            System.out.println("Caught this " + ex );
            ex.printStackTrace();
        }
    }

    /**
     * This method creates the datasource using JMX.
     *
     * The datasource created here is only written into resources.xml.
     * It is not bound into namespace until the server is restarted, or an application started
     */
    public void run(String[] args) throws Exception {

        try {
            // Initialize the AdminClient.
            Properties adminProps = new Properties();
            adminProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
            adminProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
            adminProps.setProperty(AdminClient.CONNECTOR_PORT, "8880");
            AdminClient adminClient = AdminClientFactory.createAdminClient(adminProps);

            // Get the ConfigService implementation.			
            com.ibm.websphere.management.configservice.ConfigServiceProxy configService =
            new com.ibm.websphere.management.configservice.ConfigServiceProxy(adminClient);

            Session session = new Session();

            // Use this group to add to the node scoped resource.xml.
            ObjectName node1 = ConfigServiceHelper.createObjectName(null, "Node", null);
            ObjectName[] matches = configService.queryConfigObjects(session, null, node1, null);
            node1 = matches[0];     // use the first node found

            // Use this group to add to the server1 scoped resource.xml.
            ObjectName server1 = ConfigServiceHelper.createObjectName(null, "Server", "server1");
            matches = configService.queryConfigObjects(session, null, server1, null);
            server1 = matches[0];   // use the first server found

            // Create the JDBCProvider
            String providerName = "My DB2 for zOS Local JDBC Provider (RRS) for BMP";
            System.out.println("Creating JDBCProvider " + providerName );

            // Prepare the attribute list
            AttributeList provAttrs = new AttributeList();
            provAttrs.add(new Attribute("name", providerName));
            provAttrs.add(new Attribute("implementationClassName", "com.ibm.db2.jcc.DB2ConnectionPoolDataSource"));
            provAttrs.add(new Attribute("description","Legacy DB2 for z/OS driver using RRS"));

            //create it
            ObjectName jdbcProv = configService.createConfigData(session,node1,"JDBCProvider", "resources.jdbc:JDBCProvider",provAttrs);
// now plug in the classpath
            configService.addElement(session,jdbcProv,"classpath",dbclasspath,-1);
            configService.addElement(session,jdbcProv,"nativepath",dblibpath,-1);

            // Search for RRA so we can link it to the datasource
            ObjectName rra = ConfigServiceHelper.createObjectName(null, "J2CResourceAdapter", null);
            matches = configService.queryConfigObjects(session, node1, rra, null);
            rra = matches[0]; // use the first J2CResourceAdapter segment for builtin_rra

            // Prepare the attribute list
            AttributeList dsAttrs = new AttributeList();
            dsAttrs.add(new Attribute("name", dsName));
            dsAttrs.add(new Attribute("jndiName", "jdbc/" + dsName));
            dsAttrs.add(new Attribute("datasourceHelperClassname","com.ibm.websphere.rsadapter.DB2DataStoreHelper"));
            dsAttrs.add(new Attribute("statementCacheSize", new Integer(10)));
            dsAttrs.add(new Attribute("relationalResourceAdapter", rra)); // this is where we make the
                                                                          // link to "builtin_rra"
            dsAttrs.add(new Attribute("description", "JDBC Datasource for BMP usage"));
            dsAttrs.add(new Attribute("authDataAlias",authDataAlias));

            // Create the datasource
            System.out.println("  **  Creating datasource");
            ObjectName dataSource = configService.createConfigData(session,jdbcProv,"DataSource", "resources.jdbc:DataSource",dsAttrs);
// Add a propertySet.
            AttributeList propSetAttrs = new AttributeList();
            ObjectName resourcePropertySet =configService.createConfigData(session,dataSource,
               				"propertySet","",propSetAttrs);

            // Add resourceProperty databaseName
            AttributeList propAttrs1 = new AttributeList();
            propAttrs1.add(new Attribute("name", "databaseName"));
            propAttrs1.add(new Attribute("type", "java.lang.String"));
            propAttrs1.add(new Attribute("value", dbName));

            configService.addElement(session,resourcePropertySet,"resourceProperties",propAttrs1,-1);


            // ===== start Security section
            System.out.println("Creating an authorization data alias " + authDataAlias);

            // Find the parent security object
            ObjectName security = ConfigServiceHelper.createObjectName(null, "Security", null);
            ObjectName[] securityName = configService.queryConfigObjects(session, null, security, null);
            security=securityName[0];

            // Prepare the attribute list
            AttributeList authDataAttrs = new AttributeList();
            authDataAttrs.add(new Attribute("alias", authDataAlias));
            authDataAttrs.add(new Attribute("userId", uid));
            authDataAttrs.add(new Attribute("password", pw));
            authDataAttrs.add(new Attribute("description","Auto created alias for datasource"));

            //create it
            ObjectName authDataEntry = configService.createConfigData(session,security,"authDataEntries", "JAASAuthData",authDataAttrs);
// ===== end Security section

            // Save the session
            System.out.println("Saving session" );
            configService.save(session, false);

            // reload resources.xml
            reload(adminClient,true);

        } catch (Exception ex) {
            ex.printStackTrace(System.out);
            throw ex;
        }
    }

    /**
     * Get the DataSourceConfigHelperMbean and call reload() on it
     *
     * @param adminClient
     * @param verbose true - print messages to stdout
     */
    public void reload(AdminClient adminClient,boolean verbose) {
        if (verbose) {
            System.out.println("Finding the Mbean to call reload()");
        }

        // First get the  Mbean
        ObjectName handle = null;
        try {
            ObjectName queryName = new ObjectName("WebSphere:type=DataSourceCfgHelper,*");
            Set s = adminClient.queryNames(queryName, null);
            Iterator iter = s.iterator();
            if (iter.hasNext()) handle = (ObjectName)iter.next();
        } catch (MalformedObjectNameException mone) {
            System.out.println("Check the program variable queryName" + mone);
        } catch (com.ibm.websphere.management.exception.ConnectorException ce) {
            System.out.println("Cannot connect to the application server" + ce);
        }

        if (verbose) {
            System.out.println("Calling reload()");
        }
        Object result = null;
        try {
            result = adminClient.invoke(handle, "reload", new Object[] {}, new String[] {});
        } catch (MBeanException mbe) {
            if (verbose) {
                System.out.println("¥tMbean Exception calling reload" + mbe);
            }
        } catch (InstanceNotFoundException infe) {
            System.out.println("Cannot find reload ");
        } catch (Exception ex) {
            System.out.println("Exception occurred calling reload()"  + ex);
        }

        if (result==null && verbose) {
            System.out.println("OK reload()");
        }
    }
}



関連概念
JDBC プロバイダー
データ・ソース
参照トピック    

ご利用条件 | フィードバック

最終更新: Jan 21, 2008 9:12:22 PM EST
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.zseries.doc/info/zseries/ae/rdat_crtdsbmp.html