Java Portability by Design
bu John J. Rofrano


Listing One
public class CategoryFactory
{
  /** Default Constructor */
  private CategoryFactory()
  {
  }
  /** Modifier to return the appropriate Category object
    * @param Catalog the catalog this category is in
    * @return Category
    */
  public static final Category createCategory( Catalog catalog )
  {
    Category category = null;
    StringBuffer className = new StringBuffer("com.ibm.catalog.");
    try
    {
       DataStore dataStore = catalog.getDataStore();
       className.append(dataStore.getDBPrefix());
       className.append("Category");
       category = (Category)Class.forName(className.toString()).newInstance();
       category.setCatalog(catalog);
    }
    catch ( Exception e )
    {
        System.err.println("*** ERROR: CategoryFactory.createCategory() - 
                  instantiating " + className.toString() + " from factory");
     category = null;
    }
    return category;
  }
}

Listing Two
public final static StringLengthCalculator 
                         createStringLengthCalculator(DataStore dataStore)
{
    StringLengthCalculator slc = null;      // the object to be returned
    String packageName  = "util.";      // package name of class
    String className    = "StringLengthCalculator"; // base class name
    
    /*  the generic class name is used when there is no specific one */
    StringBuffer genericClassName = new StringBuffer(packageName);  
                                                           // package name
    genericClassName.append(dataStore.getByteMode());      // byte mode
    genericClassName.append(className);                    // base class name
    /* the specific class name is used in special cases where the 
       generic isn't enough */
    StringBuffer specificClassName = new StringBuffer(packageName); 
                                                          // package name
    specificClassName.append(dataStore.getDbPrefix());    // database type
    specificClassName.append(dataStore.getByteMode());    // byte mode
    specificClassName.append(className);                  // base class name
    /* Try to instantiate a specific object first */
    try
    {
    slc = (StringLengthCalculator)Class.
                       forName(specificClassName.toString()).newInstance();
    }
    catch (Exception e)
    {
    /* If that fails, try to instantiate a generic object */
    try
    {
        slc = (StringLengthCalculator)Class.forName(genericClassName.
                                                 toString()).newInstance();
    }
    catch (Exception e1)
    {
       slc = null;
        System.err.println("*** ERROR: StringLengthCalculatorFactory.
                 createStringLengthCalculator() - instantiating " + 
                 genericClassName.toString() + " from factory");
    }
    }
    return slc;
}

Listing Three
/** Method to return the appropriate URLCommandLink object based on
 * command syntax version
 */
public static final URLCommandLink createURLCommandLink(MerchantServer ms)
{
    URLCommandLink tmpLink = null;
    StringBuffer className = new StringBuffer("com.ibm.catalog.");
    try
    {
        className.append(ms.getURLCommandVersion());
        className.append("URLCommandLink");
        tmpLink = (URLCommandLink)Class.
                           forName(className.toString()).newInstance();
    }
    catch ( Exception e )
    {
        System.err.println("URLCommandLinkFactory.createURLCommandLink() - 
                             could not instantiate class for " + className);
    }
    return tmpLink;
}


1


