Layer7 API Management

Expand all | Collapse all

Custom Assertion: ClassNotFoundException - JDBC Driver

Jump to Best Answer
  • 1.  Custom Assertion: ClassNotFoundException - JDBC Driver

    Posted 08-15-2018 10:46 AM

    Hi,

     

    I'm developing a Custom Assertion for the CA API Gateway that uses the jdbc Driver to connect to a database. The first version of the Custom Assertion did not has a custom UI. I just used the default one, that is created by the Gateway.

     

    The current version should have its' own custom UI. I want to collect and check the database parameters in this UI. When I try to establish the connection i get the following Exception:

     

    ClassNotFoundException:

    Message: com.mysql.jdbc.Driver

    Stacktrace:

    com.l7tech.console.util.CustomAssertionClassLoader.findClass(Unknown Source)

    java.lang.ClassLoader.loadClass(ClassLoader.java:424)

    java.lang.ClassLoader.loadClass(ClassLoader.java:357)

    java.lang.Class.forName()(Native Method)

    java.lang.Class.forName(Class.java:264)

    com.l7tech.custom.xmlassertion.Database.<init>(Unknown Source)

    com.l7tech.custom.xmlassertion.Database.getInstance(Unknown Source)

    com.l7tech.custom.xmlassertion.MainWindow$2.actionPerformed(Unknown Source)

    ...

    ...

    ...

     

    Obviously the jdbc Driver cannot be loaded. I tried to add the mysql-jdbc-connector to the ANT Buildfile of the project without success. Here is my code:

    MainWindow.java

    ...

    btn_test_connection.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    String ip = jT_db_ip.getText();
    String port = jT_db_port.getText();
    String user = jT_db_user.getText();
    char[] password = jP_password.getPassword();
    String name = jT_db_name.getText();

    if (
    ip.length() == 0 ||
    port.length() == 0 ||
    user.length() == 0 ||
    password.length == 0 ||
    name.length() == 0
    ) {
    JOptionPane.showMessageDialog(MainWindow.this, "All fields must be filled!", "Warning", HEIGHT);
    } else {
    try {
    Database.getInstance(ip, port, name, user, new String(password));
    jL_status.setText("Connection successfully established.");
    jL_status.setForeground(Color.GREEN);
    jB_ok.setEnabled(true);
    }
    catch (Exception ex) {
    jL_status.setText("Connection could not be established.");
    jL_status.setForeground(Color.RED);
    jB_ok.setEnabled(false);
    String stack = "";
    for (int i = 0; i < ex.getStackTrace().length; i++) {
    stack += ex.getStackTrace()[i] + "\n";
    }
    JOptionPane.showMessageDialog(MainWindow.this, "Error Message:\n" + ex.getMessage() + "\nStacktrace:\n" + stack, ex.getClass().toString(), HEIGHT);
    }
    }

    }
    });

    ...

    Database.java

    public class Database {

    private static Connection connection = null;

    private Database(String databaseIP, String databasePort, String databaseName, String databaseUser, String databasePassword) throws SQLException, ClassNotFoundException {
    XML_AssertionServiceInvocation.logger.config("CUSTOM ASSERTION [XML_Assertion]: Loading JDBC driver");
    Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber fuer JDBC Schnittstellen laden.
    Properties connectionProperties = new Properties();
    connectionProperties.put("autoReconnect", "true");

    // Verbindung zur JDBC-Datenbank herstellen.
    connection = DriverManager.getConnection("jdbc:mysql://" + databaseIP + ":" + databasePort + "/" + databaseName + "?" + "user="
    + databaseUser + "&" + "password=" + databasePassword, connectionProperties);
    }

    public static Connection getInstance(String databaseIP, String databasePort, String databaseName, String databaseUser, String databasePassword) throws SQLException, ClassNotFoundException {
    if (connection == null) {
    new Database(databaseIP, databasePort, databaseName, databaseUser, databasePassword);
    }
    return connection;
    }
    }

    }

     

    As i mentioned, before the UI was added everything worked just fine.

     

    I hope you guys have any ideas.

     

    Best Regards

    Lucas



  • 2.  Re: Custom Assertion: ClassNotFoundException - JDBC Driver
    Best Answer

    Broadcom Employee
    Posted 08-22-2018 01:58 AM

    Hello Lucas,

     

    I believe the jdbc Driver class shouldn't be used in the CustomAssertion bean side (custom UI side).
    It is instantiated on the Policy Manager, not on the API Gateway nodes.
    The DB connection parameters need to be stored in context variables or somewhere else on the Policy Manager and actual DB connection(s) should be established at the runtime.

     

    Best regards,
    Seiji



  • 3.  Re: Custom Assertion: ClassNotFoundException - JDBC Driver

    Posted 08-22-2018 03:36 AM

    Hello Seiji,

     

    i tried to insantiate the connection in the ServiceInvocation part and that worked. Thank You for that.

     

    The idea why i wanted to instantiate the connection in the UI part is that i want to check the connection before you can go on. Any ideas how i should do that in the UI?

     

    Best regards,

    Lucas



  • 4.  Re: Custom Assertion: ClassNotFoundException - JDBC Driver

    Broadcom Employee
    Posted 08-22-2018 05:04 AM

    I took a quick look at sample code (Salesforce Connector) and found the CustomExtensionInterfaceBinding class could be used for the purpose.

    It is described in the javadoc like this:

     

    CustomExtensionInterfaceBinding (Layer 7 External API)

    • public class CustomExtensionInterfaceBinding<T>extends Object
      Describes an available implementation of an custom extension interface. These are interfaces that the custom assertion can register on the Gateway, and the corresponding Policy Manager code can invoke server side methods of these interfaces via the Console Context.

      For example, a Custom Assertion connector may have its GUI classes invoke methods on an extension interface in order to test the configuration settings to an external system.

     

    Does it work for you?

     

    Best regards,

    Seiji



  • 5.  Re: Custom Assertion: ClassNotFoundException - JDBC Driver

    Posted 09-05-2018 03:47 AM

    This is working so far and i guess it is the solution.

    I'm now struggling with registering the CustomExtensionInterface. It always says that there is no ExtensionInterface registered although i did all the steps that are listed here: Using the CustomExtensionInterfaceFinder Object - CA API Gateway - 9.2 - CA Technologies Documentation 

    I also checked the SalesForce Sample Custom Assertion and did like that. But it does not work.