Reusable Code


Project: Cliffhanger Adventure Gear - CORBA Reference Application
Author: Kevin Minard

Description:
This document will highlight the areas of the Cliffhanger CORBA Reference Application that might be useful for your own JBuilder projects: 



 

timedConnect method:

The following method attempts to connect to the specified database every retrySeconds for a total of totalSeconds.  This is useful when database connections are short lived and a limited number of connections need to be shared by many users. Here is the code fragment of the timedConnect method taken from the TimedConncet class:  

public timedConnect(Database connectDb, int retrySeconds, int totalSeconds )
       throws DataSetException {

    long current;
    long nextRetry;
    long end = System.currentTimeMillis() + totalSeconds * 1000l;

    do {
      try {
        connectDb.openConnection();
        break;
      } catch (DataSetException e) {
        System.err.println(e);

        // Pause for the specified number of seconds before attempting
        // to connect again
        nextRetry = System.currentTimeMillis() + retrySeconds * 1000l;

        do {
          current = System.currentTimeMillis();

          // Re-throw the exception if the maximum amount of
          // time specified to attempt a connection has expired.
          if (current >= end) throw e;

        } while (current < nextRetry );
      }
    } while (current < end);
  }

findColumnTextField method:

The following method finds the Component within a specified container that is associated with a specified column name.  This methods is used by the applet to set focus to a required field that does not contain a value.  Here is the code fragment of the findColumnTextField method taken from the FindMissingRequired class:

  static Component findColumnTextField( Container container, String columnName) {
    int controlCount = container.getComponentCount();
    String associatedColumnName = null;
    Component component;

    for (int loop = 0; loop < controlCount; loop++) {

      component = container.getComponent( loop );

      // Recurse Containers

      if (component instanceof Container && !(component instanceof FieldView) ) {
        Component nested = FindMissingRequired.findColumnTextField( (Container ) component, columnName );
        if (nested != null ) return nested;
        else continue;
      }
      // If the control is capable of DB Access,
      // compare its column name to the search columnName
      else if (component instanceof TextFieldControl)
        associatedColumnName = ((TextFieldControl) component).getColumnName();
      else if (component instanceof TextAreaControl)
        associatedColumnName = ((TextAreaControl) component).getColumnName();
      else if (component instanceof ListControl)
        associatedColumnName = ((ListControl) component).getColumnName();
      else if (component instanceof FieldControl)
        associatedColumnName = ((FieldControl) component).getColumnName();
      else if (component instanceof ChoiceControl)
        associatedColumnName = ((ChoiceControl) component).getColumnName();
      else continue;

      if( associatedColumnName.equalsIgnoreCase(columnName)) return component;
    }
    return null;
  }