如何从Weblogic Server获取JNDI名称列表?

时间:2014-03-20 05:40:01

标签: java websphere weblogic

我使用以下方法获取Tomcat服务器的context.xml中配置的数据源列表:

public static List<String> getDataSourcesList() {

  List<String> dataSourceList = new ArrayList<String>();
  try {
     if( initialContext == null ) {
        initialContext = new InitialContext();
     }
     NamingEnumeration<NameClassPair> list = ( ( Context )initialContext.lookup( DATASOURCE_CONTEXT ) ).list( "" );
     while( list.hasMore() ) {
        dataSourceList.add( list.next().getName() );
     }
  }
  catch( NamingException ex ) {
     Logger.getLogger( JDBCUtil.class.getName() ).log( Level.SEVERE, null, ex );
  }
  return dataSourceList;

}

但是这种方法不适用于Weblogic和Websphere服务器。

如何获取在Weblogic / WebSphere Servers上配置的数据源名称列表?

是否有任何方法可以获取数据源名称列表?

2 个答案:

答案 0 :(得分:0)

对于WebSphere Application Server,只需使用PROFILE_ROOT\bin

中的dumpNameSpace命令行工具

你的代码经过小修改在WAS 8.5.5中运行良好:

      try {
         InitialContext initialContext = new InitialContext();
         NamingEnumeration<NameClassPair> list = ((Context)initialContext.lookup( "jdbc" ) ).list( "" );
         while( list.hasMore() ) {
            System.out.println(list.next().getName() );
         }
      }
      catch( NamingException ex ) {
         ex.printStackTrace();
      }

答案 1 :(得分:-1)

package jms.queue;

import java.io.PrintWriter;
import java.io.StringWriter;

import java.sql.Connection;

import java.sql.Statement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Hashtable;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import javax.sql.DataSource;

public class CheckDB {

    public CheckDB() {
        super();
    }

    public static void main(String[] args) {
        CheckDB checkDB = new CheckDB();
        System.out.println(checkDB.listJDBCContextTable());
    }

    private Context getContext() throws NamingException {
        Hashtable myCtx = new Hashtable();
        myCtx.put(Context.INITIAL_CONTEXT_FACTORY,
                  "weblogic.jndi.WLInitialContextFactory");
//        myCtx.put(Context.PROVIDER_URL, "t3://172.30.60.76:7001"); //Admin Server
        myCtx.put(Context.PROVIDER_URL, "t3://astpdsoam03.auca.corp:8001"); // SOA Cluster
        Context ctx = new InitialContext(myCtx);
        return ctx;
    }

    private String checkDataSource(DataSource ds) {
        try {
            Connection conn = ds.getConnection();
            Statement st = conn.createStatement();
            st.execute("select sysdate mydate from dual");
            st.getResultSet().next();
            Date mydate = st.getResultSet().getDate("mydate");
            conn.close();
            String date = mydate.toString();
            if (date.length() == 10 && date.indexOf("-") == 4 &&
                date.lastIndexOf("-") == 7) {
                return "OK";
            } else {
                return "NOK";
            }
        } catch (Exception e) {
            return "NOK"; //getStackTrace(e);
        }
    }

    private static String getStackTrace(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }

    private String listJDBCContextTable() {
        String output = "<table>";
        ArrayList<String> tab = new ArrayList<String>();
        String line = "";
        try {
            tab = listContext((Context)getContext().lookup("jdbc"), "", tab);
            Collections.sort(tab);
            for (int i = 0; i < tab.size(); i++) {
                output += tab.get(i);
            }
            output += "</table>";
            return output;
        } catch (NamingException e) {
            return getStackTrace(e);
        }
    }

    private ArrayList<String> listContext(Context ctx, String indent,
                                          ArrayList<String> output) throws NamingException {
        String name = "";
        try {
            NamingEnumeration list = ctx.listBindings("");
            while (list.hasMore()) {
                Binding item = (Binding)list.next();
                String className = item.getClassName();
                name = item.getName();
//                System.out.println("Name : " + name);
                if (!(item.getObject() instanceof DataSource)) {
                    //output = output+indent + className + " " + name+"\n";
                } else {
                    output.add("<tr><td>" + name + "</td><td>" +
                               checkDataSource((DataSource)item.getObject()) +
                               "</td></tr>\n");
                }
                Object o = item.getObject();
                if (o instanceof javax.naming.Context) {
                    listContext((Context)o, indent + " ", output);
                }
            }
        } catch (NamingException ex) {
            output.add("<tr><td>" + name + "</td><td>" + getStackTrace(ex) +
                       "</td></tr>\n");
        }
        return output;
    }
}