9 Working with an ADF Swing Login Dialog

This chapter describes how to create a login dialog and run the ADF Swing application using the login dialog. The ADF Swing login dialog is optimized to work with the ADF Business Components project.

This chapter includes the following sections:

9.1 About the ADF Swing Login Dialog

You can add a customizable login dialog to your user interface project that will require a user name and password to run your ADF Swing application that you have created for an ADF Business Components data model. Currently, the login dialog is not supported for other business services.

By default the generated login dialog works with Oracle WebLogic Server container-enforced authentication and uses SECURITY_PRINCIPAL and SECURITY_CREDENTIALS properties. However, you can customize the generated file when you prefer to bypass these security provider properties and work instead with a JDBC connection that requires DB_USERNAME_PROPERTY and DB_PASSWORD_PROPERTY properties.

You add the login dialog class to your user interface project when you generate a frame to run your ADF Swing application. You can use two wizards in ADF Swing to generate a JFrame with the appropriate ADF Swing bootstrap code:

  • Create ADF Swing Form wizard - click Generate a Login Dialog in the File Names page

  • Create ADF Swing Empty Form dialog - click Generate a Login Dialog

When you run either of these with the generate login dialog option selected, JDeveloper:

  • Modifies the application object constructor in the application bootstrap code to create an instance of JCLoginDialog:

    BindingContext ctx = new BindingContext(); ctx.put(DataControlFactory.APP_PARAM_ENV_INFO, new JCLoginDialog());

  • Adds the JCLoginDialog.java file to your user interface project, which implements the EnvInfoProvider interface to provide the runtime login dialog. The wizard will not overwrite an existing JCLoginDialog.java file of the same name.

Because the generated login dialog (JCLoginDialog.java) implements the methods of the interface, it gives you the starting code that you can modify. Using the JCLoginDialog.java file, you can customize any aspect of the login dialog:

  • Its visual appearance by adding images and changing the layout

  • Configuration parameters to connect to the ADF Business Components application module

  • Connection parameters to connect to the database

  • Specification of the number of times to retry connecting after failing to connect

9.2 How to Create a Login Dialog

You can use the Create ADF Swing Form wizard or the Create ADF Swing Empty Form dialog to add a generated login dialog to your ADF Swing application. The generated login dialog relies on the Oracle ADF security framework to authenticate principal and credentials and therefore does not work with a JDBC connection.

To add the ADF Swing login dialog to your project:

  1. In the Applications window, select the user interface project and launch the Create ADF Swing Form wizard or the Create ADF Swing Empty Form dialog.

  2. On the last page of the form wizard, select Generate a Login Dialog.

  3. Click Finish to complete the wizard.

9.3 How to Run the Application Using the Login Dialog

To use the login dialog with Oracle WebLogic Server and ADF Security authentication, you must run the Configure ADF Security wizard to configure the ADF Swing application to use ADF security.

To configure the ADF Swing client to use ADF authentication:

  1. In the Applications window, select the user interface project for which ADF authentication is needed and choose Application > Secure > Configure ADF Security.

  2. In the ADF Security wizard, select ADF Authentication and Authorization.

  3. Click Finish.

To enable the login provider for ADF Swing forms:

  1. In the Applications window, right-click the user interface project and choose Project Properties.

  2. In the Project Properties dialog, click Libraries and Classpath to display the current list of libraries associated with your user interface project.

  3. Click Add Library.

  4. In the list of libraries, locate BC4J Security and add it to the list.

    Otherwise an error message will display when using the logon dialog. The BC4J Security library contains the JAR files required to use ADF authentication within the project.

  5. Click OK to save the project settings.

  6. If your user interface project requires access to ADF Business Components deployed as an EJB session bean, you must grant read/write access to the users in your group.

User accounts are stored in the jazn-data.xml file located in ./src/META-INF directory relative to the ADF Swing application. One of the default names is admin/welcome. To test your logon dialog, use one of the default names. Alternatively, you can add your own user to jazn-data.xml.

The jazn-data.xml file encrypts the password the first time the server instance gets started after an account is added. To make sure a password is encrypted, add an '!' in front of the password. For example, to encrypt the password WELCOME, define it as!WELCOME.

To work with the username/credential pair for your users, use the code in Example 9-1 in your application.

Example 9-1 Code for Username/Credential Pairs

Hashtable h = panelBinding.getBindingContext().getDefaultDataControl(). getApplicationModule().getSession().getEnvironment(); 
 
String username = h.get(JboContext.SECURITY_PRINCIPAL);
String credential = h.get(JboContext.SECURITY_CREDENTIALS);
…

9.4 How to Run the Application Without the Login Dialog

When you run one of the ADF Swing wizards to generate an ADF Swing frame, but do not generate a login dialog in your ADF Swing user interface project (by leaving Generate a Login Dialog in the wizard unselected), you can still run the application by selecting Save Password in the Edit Database Connection dialog. In this case, when you run the application, JDeveloper displays the frame without prompting you for login.

9.5 What You May Need to Know About Customizing the Login Dialog Code

You generate the JCLoginDialog.java file in your user interface project when you want to modify the starter code that implements the required methods of the EnvInfoProvider interface. The interface is used in the ADF Business Components connection strategy to provide the hooks to change login parameters at runtime.

The EnvInfoProvider interface expects the following methods to be implemented:

public void getInfo(String info, Object connEnvironment)

This method is called before connecting to the database. It allows you to update (and return) the hashtable with all the connection parameters.

Public int getNumOfRetries()

This method determines how many times the business components will attempt to connect after failing. Each time it will obtain connection information from the EnvInfoProvider.

Note:

The method public void modifyInitialContext(Object initialContext) has been used in previous releases, but is now deprecated. You may implement it as an empty method.

9.6 How to Modify the Login Dialog to Work with a JDBC Connection

You can modify the generated ADF Swing login dialog to work with a JDBC connection instead of Oracle ADF authentication.

To modify the generated ADF Swing login dialog for JDBC connections:

  1. Add the login dialog to your user interface project using the Create ADF Swing Form wizard or the Create ADF Swing Empty Form dialog.

    For more information, see Section 9.2, "How to Create a Login Dialog.".

  2. In the user interface project, double-click the JCLoginDialog.java file.

  3. In the Java source editor, comment out the code as shown in Example 9-2.

    Example 9-2 Commented Code in JCLoginDialog.java

    /*
      String securityEnforceStr =
          ((String)((Hashtable)connEnvironment).get(PropertyMetadata.ENV_SECURITY_ENFORCE .pName));
          if (securityEnforceStr == null
            (securityEnforceStr != null && PropertyConstants.SECURITY_ENFORCE_NONE.equals(securityEnforceStr)))
          {
            return null;
          }
    */ 
    
  4. Press Ctrl+F to display the Find dialog in the source editor.

  5. Perform this search and replace operation:

    Change JboContext.SECURITY_PRINCIPAL to
    Configuration.DB_USERNAME_PROPERTY

  6. Display the Find dialog again and perform another search and replace operation:

    Change JboContext.SECURITY_CREDENTIALS to
    Configuration.DB_PASSWORD_PROPERTY

    Note:

    These changes appear twice within the file: once when reading from the hash table and once when setting user name and password from the fields in the login dialog.

  7. Save the changes to JCLoginDialog.java.

  8. If you saved the password when you created your data model project (the default), you need to change these settings. In the Databases window, right-click the connection and choose Properties.

  9. In the Edit Database Connection dialog, delete the password and deselect Save Password.

You are now ready to run your ADF Swing application. If you have a JDBC connection that the bc4j.xcfg file names, the application will invoke the dialog automatically.