Listing 1: A partial listing of EJBSkeleton

package com.tricomgroup.tools.ejb;

import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.ejb.deployment.*;

public class EJBSkeleton {

 public static void main(String args[]) {
  new EJBSkeletonFrame();
 }
}

class EJBSkeletonFrame extends JFrame
 implements ActionListener, ItemListener, EJBSkeletonConstants {
 JMenuBar mb = new JMenuBar();
 ButtonGroup ejbVersion = new ButtonGroup();
 ButtonGroup ejbType = new ButtonGroup();
 ButtonGroup sessionType = new ButtonGroup();
 ButtonGroup entityType = new ButtonGroup();
 JRadioButton versionOneZero = new JRadioButton("1.0",true);
 JRadioButton versionOneOne = new JRadioButton("1.1",false);
 JRadioButton session = new JRadioButton("Session",true);
 JRadioButton entity = new JRadioButton("Entity",false);
 JRadioButton stateless = new JRadioButton("Stateless",true);
 JRadioButton stateful = new JRadioButton("Stateful",false);
 JRadioButton containerManaged = 
  new JRadioButton("Container Managed",true);
 JRadioButton beanManaged = new JRadioButton("Bean Managed",false);
 JCheckBox compile = new JCheckBox("Compile EJB Source Files");
 JCheckBox jar = new JCheckBox("Package Bean");
 JCheckBox client = new JCheckBox("Generate Client Source");
 JButton generate = new JButton("Generate");
 JButton source = new JButton("Add EJB Source Code...");
 JButton close = new JButton("Close");
 JButton about = new JButton("About...");
 JTextField pkg = new JTextField(20);
 JTextField base = new JTextField(20);
 JTextField root = new JTextField(20);
 JPanel northPanel = new JPanel();
 JPanel northPanelA = new JPanel();
 JPanel northPanelB = new JPanel();
 JPanel cardPanelA = new JPanel();
 JPanel cardPanelB = new JPanel();
 JPanel northPanelC = new JPanel();
 JPanel southPanel = new JPanel();
 JPanel centerPanel = new JPanel();
 JPanel eastPanel = new JPanel();
 CardLayout cl = new CardLayout();
 String newLine = System.getProperty("line.separator");
 String baseName;
 String pkgName;
 String rootName;
 String destPrefix;
 ArrayList ejbSource;
 ArrayList cmpFieldSource;
 ArrayList variableFieldSource;
 ArrayList pkFieldSource;
 ProgressDialog pd;
 Process p;

 EJBSkeletonFrame() {
  super("EJBSkeleton");

  /* Components should be added to the container's content pane */
  Container cp = getContentPane();

  /* Add radio buttons to corresponding group */
  ejbVersion.add(versionOneZero);
  ejbVersion.add(versionOneOne);

  ejbType.add(session);
  ejbType.add(entity);

  sessionType.add(stateless);
  sessionType.add(stateful);

  entityType.add(containerManaged);
  entityType.add(beanManaged);

  /* Set the north panel layout */
  northPanel.setLayout(new BorderLayout());

  /* Set the north sub-panel layout */
  northPanelA.setLayout(new GridLayout(3,2));

  /* Add components to the north sub-panels */
  northPanelA.add(new JLabel("Root Package Directory:"));
  northPanelA.add(root);
  northPanelA.add(new JLabel("Package Name:"));
  northPanelA.add(pkg);
  northPanelA.add(new JLabel("Base Name:"));
  northPanelA.add(base);

  northPanelB.setLayout(new GridLayout(2,3));
  northPanelB.add(new JLabel("EJB Version:"));
  northPanelB.add(versionOneZero);
  northPanelB.add(versionOneOne);
  northPanelB.add(new JLabel("EJB Type:"));
  northPanelB.add(session);
  northPanelB.add(entity);

  northPanelC.setLayout(cl);
  northPanelC.add(cardPanelA,"cardA");
  northPanelC.add(cardPanelB,"cardB");

  /* Set the card panel layouts */
  cardPanelA.setLayout(new GridLayout(1,3));
  cardPanelB.setLayout(new GridLayout(1,3));

  /* Add the components the cards */
  cardPanelA.add(new JLabel("EJB Sub-type:"));
  cardPanelA.add(stateless);
  cardPanelA.add(stateful);

  cardPanelB.add(new JLabel("EJB Sub-type:"));
  cardPanelB.add(beanManaged);
  cardPanelB.add(containerManaged);

  northPanel.add(BorderLayout.NORTH,northPanelA);
  northPanel.add(BorderLayout.CENTER,northPanelB);
  northPanel.add(BorderLayout.SOUTH,northPanelC);

  /* Add components to the south panel */
  southPanel.add(generate);
  southPanel.add(source);
  southPanel.add(close);
  southPanel.add(about);

  centerPanel.setBorder(BorderFactory.createCompoundBorder(
   BorderFactory.createTitledBorder("Miscellaneous Options"),
   BorderFactory.createEmptyBorder(10,10,10,10)));
  centerPanel.add(client);
  centerPanel.add(compile);
  centerPanel.add(jar);

  /* Add panels to frame */
  cp.add(BorderLayout.NORTH,northPanel);
  cp.add(BorderLayout.CENTER,centerPanel);
  cp.add(BorderLayout.SOUTH,southPanel);

  /* Set new documents */
  base.setDocument(new EnableGenerateDocument());
  pkg.setDocument(new EnableGenerateDocument());

  /* Add the item listeners */
  session.addItemListener(this);
  entity.addItemListener(this);
  compile.addItemListener(this);

  /* Add the action listeners */
  generate.addActionListener(this);
  source.addActionListener(this);
  close.addActionListener(this);
  about.addActionListener(this);

  /* Add the window listener */
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
    dispose(); deleteTempFiles(); System.exit(0);}});

  /* Disable Generate button, Add EJB Source Code button, 
     and Package Bean checkbox */
  generate.setEnabled(false);
  source.setEnabled(false);
  jar.setEnabled(false);

  /* Size the frame */
  pack();

  /* Set the frame un-sizable */
  setResizable(false);

  /* Center the frame */
  Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
  Rectangle frameDim = getBounds();
  setLocation((screenDim.width - frameDim.width) / 2,
              (screenDim.height - frameDim.height) / 2);

  /* Show the frame */
  setVisible(true);
 }

 class EnableGenerateDocument extends PlainDocument
 {
  ...
 }

 public void actionPerformed(ActionEvent evt)
 {
  ...
 }

 private void deleteTempFiles()
 {
  ...
 }

 public void itemStateChanged(ItemEvent evt)
 {
  ...
 }

 private boolean generateEJBHome()
 {
  ...
 }

 private boolean generateEJBRemote()
 {
  ...
 }

 private boolean generateEJB()
 {
  ...
 }

 private void generateEJBRemoteMethodDeclarations(PrintWriter pw)
 {
  ...
 }

 private boolean generatePrimaryKeyClass()
 {
  ...
 }

 private boolean generateEJB10DeploymentDescriptor()
 {
  ...
 }

 private boolean generateEJB10DeploymentDescriptorManifest()
 {
  ...
 }

 private boolean generateEJB11DeploymentDescriptor()
 {
  ...
 }

 private boolean generateClientSource()
 {
  ...
 }

 private void writeContents(ArrayList al, PrintWriter pw)
 {
  ...
 }

 private ArrayList readTempFile(String name)
 {
  ...
 }

 private void add11CmpFields(ArrayList al, PrintWriter pw)
 {
  ...
 }

 private void writePackageStatement(PrintWriter pw)
 {
  ...
 }

 private void writeImportStatements(PrintWriter pw, boolean isEJB)
 {
  ...
 }
}

class SourceCodeDialog extends JDialog implements ActionListener, 
  EJBSkeletonConstants
{
  JTextArea sourceTextArea = new JTextArea();
  JScrollPane sourceScrollPane = new JScrollPane(
   sourceTextArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  JTextArea variableTextArea = new JTextArea();
  JScrollPane variableScrollPane = new JScrollPane(variableTextArea,
   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  JTextArea primaryKeyTextArea = new JTextArea();
  JScrollPane primaryKeyScrollPane = 
   new JScrollPane(primaryKeyTextArea,
   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  JTextArea cmpTextArea = new JTextArea();
  JScrollPane cmpScrollPane = new JScrollPane(cmpTextArea,
   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  JButton save = new JButton("Save");
  JButton cancel = new JButton("Cancel");
  JPanel center = new JPanel();
  JPanel centerA = new JPanel();
  JPanel centerB = new JPanel();
  JPanel centerC = new JPanel();
  JPanel centerD = new JPanel();
  JPanel south = new JPanel();
  EJBSkeletonFrame owner;

  public SourceCodeDialog(Frame owner)
  {
   ...
  }

  private void layoutCCenterPanel()
  {
   ...
  }

  public void actionPerformed(ActionEvent evt)
  {
   ...
  }

  private boolean accessTempFiles(boolean readOnly)
  {
   ...
  }

  class EnableSaveDocument extends PlainDocument
  {
   ...
  }
}

class ProgressDialog extends JDialog implements ActionListener
{
  JTextArea progressTextArea = new JTextArea();
  JScrollPane progressScrollPane = new JScrollPane(progressTextArea,
   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  JButton ok = new JButton("OK");
  JPanel center = new JPanel();
  JPanel south = new JPanel();

  public ProgressDialog(Frame owner)
  {
    super(owner,"Generate",true);

    /* Components should be added to the container's content pane */
    Container cp = getContentPane();

    /* Add action listeners */
    ok.addActionListener(this);

    /* Add the window listener */
    addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent evt) {
      dispose();}});

    /* Disable the text area */
    progressTextArea.setEnabled(false);

    /* Set center panel layout */
    center.setLayout(new GridLayout(1,1));

    /* Add components to the center panel */
    center.add(progressScrollPane);

    /* Add components to the south panel */
    south.add(ok);

    /* Add panels to the dialog */
    cp.add(BorderLayout.CENTER,center);
    cp.add(BorderLayout.SOUTH,south);

    /* Size the dialog */
    setSize(430,170);

    /* Center the dialog */
    Dimension screenDim = 
        Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle frameDim = getBounds();
    setLocation((screenDim.width - frameDim.width) / 2,
                (screenDim.height - frameDim.height) / 2);
  }

  public void actionPerformed(ActionEvent evt)
  {
    Object obj = evt.getSource();

    if (obj == ok)
     dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));
  }

  public void addMessage(String msg)
  {
    progressTextArea.append(msg);
  }
}

class FileClassLoader extends ClassLoader
{
 private String path;

 public Class loadClass(String name, boolean resolve) 
   throws ClassNotFoundException
 {
  /* Determine if class has already been loaded */
  Class c = findLoadedClass(name);

  try
  {
   /* Determine if the class can be loaded from the CLASSPATH */
   if (c == null)
    c = findSystemClass(name);
  }
  catch(ClassNotFoundException cnfe)
  {
   //class is not present in the CLASSPATH
  }

  if (c == null)
   c = loadClassBytes(new File(path + "\\" + name + ".class"));

  return c;
 }

 public Class loadClassBytes(File f) throws ClassNotFoundException
 {
  FileInputStream fis = null;
  byte b[] = null;

  try
  {
   fis = new FileInputStream(f);
   b = new byte[(int) f.length()];

   fis.read(b,0,b.length);
  }
  catch(IOException e)
  {
   throw new ClassNotFoundException("Class could not be loaded!");
  }
  finally
  {
    try
    {
      if (fis != null)
       fis.close();
    }
    catch(IOException ioe)
    {
      //no op
    }
  }

  /* Save the file's path name */
  path = f.getParent();

  return defineClass(null,b,0,b.length);
 }
}

interface EJBSkeletonConstants
{
 public static final String TEMP_FILE_PREFIX = "_tmp";
 public static final String TEMP_SOURCE_SUFFIX = 
  "EJBSource." + TEMP_FILE_PREFIX;
 public static final String TEMP_FIELDS_SUFFIX = 
  "EJBFields." + TEMP_FILE_PREFIX;
 public static final String TEMP_PK_FIELDS_SUFFIX = 
  "EJBPkFields." + TEMP_FILE_PREFIX;
 public static final String TEMP_CMP_FIELDS_SUFFIX = 
  "EJBCmpFields." + TEMP_FILE_PREFIX;
}
— End of Listing —