无法通过JMXConnector连接并无法通过LDAP进行身份验证

时间:2019-08-29 04:58:17

标签: java ldap jmx

我们正尝试通过JMXConnector使用Java程序与服务器进行通信,并且为了进行身份验证,我们正在使用LDAP。但是我们在这样做时却遇到了这个异常:

D:\Development\Workspace\chatbot\test_script\BSNL_TCCHATBOTCLEARLOCKS_20190828.log
Connecting to jmx pool service:jmx:rmi:///jndi/rmi://bsnl-tc-dev.bsnltcdev:8088/jmxrmito delete s's sessionjava.rmi.UnmarshalException: Error unmarshaling return; nested exception is:
        java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.sun.jndi.ldap.LdapCtx
        at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
        at sun.rmi.server.UnicastRef.invoke(Unknown Source)
        at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
        at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
        at com.sun.proxy.$Proxy0.newClient(Unknown Source)
        at javax.management.remote.rmi.RMIConnector.getConnection(Unknown Source)
        at javax.management.remote.rmi.RMIConnector.connect(Unknown Source)
        at javax.management.remote.JMXConnectorFactory.connect(Unknown Source)
        at com.asml.listjmxdomains.BSNL_listJMXDomains.<init>(BSNL_listJMXDomains.java:77)
        at com.asml.listjmxdomains.BSNL_listJMXDomains.main(BSNL_listJMXDomains.java:118)
Caused by: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.sun.jndi.ldap.LdapCtx
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
        at java.io.ObjectInputStream.readSerialData(Unknown Source)
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)

我们正在使用的代码可以在下面找到:

/**
 * @copyright 2017 BSNL. All Rights Reserved.
 * @file BSNL_CommonHelper.java
 * @package com.bsnl.teamcenter.helper
 * @Description This class implements clear locks use case for chat bot
 * @author BSNL
 * @version 1.0
 * @date Jan 11, 2019
 * 
 *  <table border="1">
 * <caption align="left">Change History</caption>
 * <tr><th>Description</th>                                    <th>Who</th>      <th>Date</th></tr>
  * </table>
 */
package com.bsnl.listjmxdomains;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class BSNL_listJMXDomains {
    private JMXServiceURL url;
    private JMXConnector jmxc;
    private MBeanServerConnection mbsc;
    private static Properties props = null;
    static String hostname = null;
    static String BSNL_CLEARLOCKSLOGGER = null;
    static String logFilePath = null;;
    static String user_name=null;
    static final String DATE_FORMAT_yyyyMMdd = 
            new SimpleDateFormat("yyyyMMdd").format(new Date()).toString();
    private static Logger logger =null;
    String store = null;
    String idle33 = null;
    String idle44 = null;
    String test1 = null;
    String test2 = null;
    String test79 = null;
    static boolean idle = false;
    static boolean value7979 = false;
    boolean sample =false;
    boolean sample1 = false;

    public BSNL_listJMXDomains(String url)
    {
        try
        {
            this.url = new JMXServiceURL(url);
            this.jmxc = JMXConnectorFactory.connect(this.url);
            this.mbsc = this.jmxc.getMBeanServerConnection();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

    }
    public void go()
    {       
        listJMXDomains();
        listManager();
        listTCServers();
        listTCManagerMonitors();
        listTCServerMonitors();

    }

    public static void main(String[] args)
    {
        /* construct jmx pool server url of corporate servers to connect */
        String url = "service:jmx:rmi:///jndi/rmi://" + args[1] + ":8088/jmxrmi" ;

        /* read server details from properties file*/
        props = readPropertiesFromFile(args[2]);

        /* create log file */
        BSNL_CLEARLOCKSLOGGER="BSNL_TCCHATBOTCLEARLOCKS_"+DATE_FORMAT_yyyyMMdd + ".log";
        logFilePath =  System.getProperty("user.dir") + File.separator + BSNL_CLEARLOCKSLOGGER;

        createLoggerFile(logFilePath);

        user_name = args[0]; /* user name whose session should be removed from server */
        hostname=args[1];    /* server */

        System.out.print("Connecting to jmx pool " + url + "to delete " + user_name + "'s session");

        new BSNL_listJMXDomains(url).go();

    }
    public static void test(String inpStr)
    {
        System.out.println(inpStr);
    }
    void listJMXDomains()
    {
        try
        {
            String[] domains = this.mbsc.getDomains();

            System.out.println("Domains: ");
            String[] arrayOfString1;
            int j = (arrayOfString1 = domains).length;
            for(int i = 0; i<j; i++)
            {
                String name = arrayOfString1[i];
                System.out.println("\t"+name);
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    void listManager()
    {
        try
        {
            ObjectName oNamePattern = new ObjectName("AdministerPoolA manager :id=PoolA");
            Set<ObjectInstance> objs = this.mbsc.queryMBeans(oNamePattern, null);
            for(ObjectInstance obj : objs)
            {
                ObjectName oName = obj.getObjectName();
                String oClass = obj.getClassName();
                String serverId = oName.getKeyProperty("id");
                System.out.println("Class: "+oClass+", Id: "+serverId);

                printAttributes(oName);
            }
        }
        catch(MalformedObjectNameException|IOException|AttributeNotFoundException|InstanceNotFoundException|MBeanException|ReflectionException|IntrospectionException e)
        {
            e.printStackTrace();
        }
    }
    void listTCServers()
    {
        try 
        {
            ObjectName oNamePattern = new ObjectName("Administer PoolA servers :id=*") ;
            Set<ObjectInstance> objs = this.mbsc.queryMBeans(oNamePattern, null);
            for(ObjectInstance obj : objs)
            {
                ObjectName oName = obj.getObjectName();
                String oClass = obj.getClassName();
                if(oClass.equals("com.teamcenter.jeti.serversubpoolmanager.ServerInfo"))
                {
                    String serverId = oName.getKeyProperty("id");
                    System.out.println(serverId);
                    if(idle==true)
                    {
                        printAttributes2(oName);

                    }
                    printAttributes1(oName);
                }

            }
        }
        catch(MalformedObjectNameException|IOException|AttributeNotFoundException|InstanceNotFoundException|MBeanException|ReflectionException|IntrospectionException e)
        {
            e.printStackTrace();
        }
    }
    void listTCManagerMonitors()
    {
        try
        {
            ObjectName oNamePattern = new ObjectName("Administer PoolA manager monitoring:id=*");
            Set<ObjectInstance> objs = this.mbsc.queryMBeans(oNamePattern,null);
            for(ObjectInstance obj : objs){
                ObjectName oName = obj.getObjectName();
                String oClass = obj.getClassName();
                String serverId = oName.getKeyProperty("id");
                System.out.println("Class : " + oClass + ", Id: " + serverId);
                printAttributes(oName);
            }
        }
        catch(MalformedObjectNameException|IOException|AttributeNotFoundException|InstanceNotFoundException|MBeanException|ReflectionException|IntrospectionException e)
        {
            e.printStackTrace();
        }
    }
    void listTCServerMonitors()
    {
        try
        {
            ObjectName oNamePattern = new ObjectName("AdministerPoolA serversmonitoring:type=*,id=*");
            Set<ObjectInstance> objs = this.mbsc.queryMBeans(oNamePattern,null);
            for(ObjectInstance obj : objs){
                ObjectName oName = obj.getObjectName();
                String oClass = obj.getClassName();
                String type = oName.getKeyProperty("type");
                String id = oName.getKeyProperty("id");
                System.out.println("Class : " + oClass + ", Type: "+ type + ", Id: " + id);
                printAttributes(oName);

            }
        }
        catch(MalformedObjectNameException|IOException|AttributeNotFoundException|InstanceNotFoundException|MBeanException|ReflectionException|IntrospectionException e)
        {
            e.printStackTrace();
        }
    }
    private void printAttributes(ObjectName oName) throws InstanceNotFoundException,IntrospectionException,AttributeNotFoundException,ReflectionException,MBeanException,IOException
    {
        MBeanAttributeInfo[] arrayOfMBeanAttributeInfo;
        int j = (arrayOfMBeanAttributeInfo = this.mbsc.getMBeanInfo(oName).getAttributes()).length;
        for(int i=0; i<j; i++)
        {
            MBeanAttributeInfo attr = arrayOfMBeanAttributeInfo[i];
            System.out.println("\t" + attr.getName() + ": " + this.mbsc.getAttribute(oName, attr.getName().toString()) );

        }
    }
    private void printAttributes2(ObjectName oName) throws InstanceNotFoundException,IntrospectionException,AttributeNotFoundException,ReflectionException,MBeanException,IOException
    {
        MBeanAttributeInfo[] arrayOfMBeanAttributeInfo;
        int j = (arrayOfMBeanAttributeInfo = this.mbsc.getMBeanInfo(oName).getAttributes()).length;
        for(int i=0; i<j; i++)
        {
            MBeanAttributeInfo attr = arrayOfMBeanAttributeInfo[i];
            String idle22 = null;
            String idle11 = null;
            System.out.println("\t" + attr.getName() + ": " + this.mbsc.getAttribute(oName, attr.getName().toString()) );
            try
            {
                if(attr.getName().toString().compareToIgnoreCase("Process ID")== 0)
                {
                    idle22 = this.mbsc.getAttribute(oName, attr.getName().toString()).toString();
                    System.out.println("process id: " + idle22 );
                    idle33 = idle22;
                }
                if(attr.getName().toString().compareToIgnoreCase("Active")==0)
                {
                    idle11 = this.mbsc.getAttribute(oName, attr.getName().toString()).toString();
                    System.out.println("idle11 "+ idle11);
                    if(idle11.equalsIgnoreCase("false"))
                    {
                        System.out.println("entered");
                        idle44 = "true";

                    }
                    else
                    {
                        System.out.println("Not Entered");
                        idle44 = "false";
                    }

                }
                if(attr.getName().toString().compareToIgnoreCase("Health Monitoring")==0)
                {
                    if(idle44.compareToIgnoreCase("true")==0)
                    {
                        logger.info("\nDeleting "+ user_name + " session on corporate server " + hostname + "\n" );
                        String cmd = "taskkill /s " + hostname + " /u " + props.getProperty("username") + " /p " + props.getProperty("pwd") + " /pid " + idle33 + " /f";
                        System.out.println("cmd"+cmd);
                        Runtime.getRuntime().exec(cmd);

                    }
                }

            }
            catch(Exception localException){}
        }
    }
    private void printAttributes1(ObjectName oName) throws InstanceNotFoundException, IntrospectionException, AttributeNotFoundException, ReflectionException, MBeanException, IOException
    {
        boolean sample = false;
        MBeanAttributeInfo[] arrayOfMBeanAttributeInfo;
        int j = (arrayOfMBeanAttributeInfo = this.mbsc.getMBeanInfo(oName).getAttributes()).length;
        for( int i =0; i<j; i++)
        {
            MBeanAttributeInfo attr =  arrayOfMBeanAttributeInfo[i];
            System.out.println("\t" + attr.getName() + ":" + this.mbsc.getAttribute(oName,  attr.getName().toString()));
            try
            {
                String test1 = null;
                test1 = this.mbsc.getAttribute(oName,  attr.getName().toString()).toString();
                String test79 = user_name;
                if(test1.equalsIgnoreCase(test79))
                {
                    sample = true;
                }
                if(attr.getName().toString().compareToIgnoreCase("Process ID")==0)
                {
                    if(sample)
                    {
                        sample = false;
                        logger.info("\nDeleting "+ user_name + " session on corporate server " + hostname + "\n" );
                        String cmd = "taskkill /s " + hostname + " /u " + props.getProperty("username") + " /p " + props.getProperty("pwd") + " /pid " + test1 + " /f";
                        System.out.println("cmd"+cmd);
                        Runtime.getRuntime().exec(cmd);

                    }
                }
            }
            catch(Exception localException){}
        }
    }

    private static Properties readPropertiesFromFile(String paramString)
    {

      Properties localProperties = null;
      if (paramString != null)
      {
        try
        {
          localProperties = new Properties();
          FileInputStream localFileInputStream = new FileInputStream(paramString);
          localProperties.load(localFileInputStream);
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
      }
      return localProperties;
    }

    private static Logger createLoggerFile(String paramString)
      {
        try
        {
            System.out.println(paramString);
          FileHandler localFileHandler = new FileHandler(paramString, true);
          SimpleFormatter localSimpleFormatter = new SimpleFormatter();
          localFileHandler.setFormatter(localSimpleFormatter);
          if (logger == null)
              logger = Logger.getLogger("BSNL_listJMXDomains" );
          logger.addHandler(localFileHandler);
          logger.setLevel(Level.INFO);
        }
        catch (SecurityException localSecurityException)
        {
          localSecurityException.printStackTrace();
        }
        catch (IOException localIOException)
        {
          localIOException.printStackTrace();
        }
        return logger;
      } 
}

您能帮我解释一下此消息吗?我确实知道这可能是因为该类不可序列化,并且Transient关键字可以解决此问题,但问题是它抱怨的类-LDAPCtx不归我们所有。因此,我试图查看是否还有其他方法可以解决此问题。我也不是LDAP专家,无法朝着这个方向射击。

任何指针都会有所帮助。

谢谢, 帕万。

0 个答案:

没有答案
相关问题