Adding main for testing

How can I easily add a main to the following code so I can test it in my IDE when required?

package edge;
import java.io.*;
import java.net.UnknownHostException;
import java.security.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.*;

public class CFSSLTransportLayer
{
      final String host = "xxx.xxx.gov.au";
      final int port = 5608;
      private static SSLSocket sslServerSocket;
      static boolean debug = true;
      final String keyStoreLocation ="C:\\Documents and Settings\\Administrator\\.keystore";
      final String password = "xxx";
      final int socketTimeout = 60000; // Set timeout to one minute as per specs.
      SSLSocketFactory factory;
      PrintWriter pw;
      BufferedReader in = null;


      public CFSSLTransportLayer()
      {

            try
            {
                  KeyStore myKeyStore = KeyStore.getInstance( "JKS" );
                  myKeyStore.load( new FileInputStream( keyStoreLocation ), password.toCharArray() );
                  TrustManagerFactory myTrustFactory = TrustManagerFactory.getInstance( "SunX509", "SunJSSE" );
                  myTrustFactory.init( myKeyStore );
                  TrustManager[] myTMs = myTrustFactory.getTrustManagers();
                  SSLContext ctx = SSLContext.getInstance( "SSL" );
                  ctx.init( null, myTMs, null );
                  factory = ctx.getSocketFactory();
            }
            catch ( Exception e )
            {
                  System.err.println( "Error creating SSL objects." );
                  e.printStackTrace( System.out );
            }

            try
            {
                  sslServerSocket = ( SSLSocket ) factory.createSocket( host, port );
                  // Set the timeout
                  sslServerSocket.setSoTimeout( socketTimeout );
                  if ( debug )
                  {
                        System.out.println( "*************************************************" );
                        System.out.println( "***********Secure socket made********************" );
                        System.out.println( "*************************************************" );
                        System.out.println();
                  }
                  pw = new PrintWriter( sslServerSocket.getOutputStream() );
            }
            catch ( UnknownHostException e )
            {
              System.err.println( "Unknown host." );
            }
            catch ( java.net.SocketTimeoutException e )
            {
                  System.err.println( "The Socket waited " + socketTimeout / 1000 + " seconds for a response." );
            }
            catch  ( IOException e )
            {
              System.err.println( "I/O Exception." );
        }
        catch ( Exception e )
            {
              System.err.println( "An exception has occurred, error message: " + e.getMessage() );
        }
      }

      
      public String send ( String message )
      {
            String response;
            String messageToSend = "BDAT " + message.length() + " LAST\r\n" + message;
            // Sent the message to EDGE
            pw.println( messageToSend );
            pw.flush();
            if ( debug )
            {
                  System.out.println( "Message that was sent to EDGE " + messageToSend );
                  System.out.println( "Message sent to EDGE." );
            }
            return getResponse();
      }


      private String getResponse()
      {
            String messageString = "";
            try
            {
                  if ( debug )
                  {
                        System.out.println( "*************************************************" );
                        System.out.println( "************Server Output************************" );
                        System.out.print( "Outputting reply from server: " );
                  }

                  in = new BufferedReader( new InputStreamReader( sslServerSocket.getInputStream() ) );

                  String header = "";
                  char[] myChar = new char[ 1 ];
                  // Making sure that we do not enter an infinite loop
                  int i = 0;
                  final int MAX_LENGTH_TO_READ = 30;
                  // Loop until we find the newline character, this should give us the header
                  while ( !header.endsWith( "\n" ) && i < MAX_LENGTH_TO_READ )
                  {
                        in.read( myChar, 0, 1 );
                        String myStringchar = new String( myChar );
                        header += myStringchar;
                        i++;
                  }
                  if ( i == MAX_LENGTH_TO_READ )
                  {
                        System.err.println( "Could not read the header!" );
                  }

                  int byteToRead = 0;
                  // Parse out the message length
                  Pattern myPattern = Pattern.compile( "\\d+" );
                  Matcher myMatch = myPattern.matcher( header );
                  if ( myMatch.find() )
                  {
                        byteToRead = Integer.parseInt( header.substring( myMatch.start(), myMatch.end() ) );
                  }
                  else
                  {
                        System.err.println( "Message length could not be found in the header!" );
                        // take your action in case no integer found in yourString
                        throw new Exception("MessageLengthNotFound", new Throwable( "The message length could not be found in the header." ) );
                  }

                  char[] myMessage = new char[ byteToRead ];
            // Try and read the rest of the message
            try
                  {
                  int remaining = byteToRead;
                  // Loop untill all bytes are read
                  while ( remaining > 0 )
                  {
                        remaining -= in.read( myMessage, byteToRead - remaining, remaining );
                  }
                  }
            catch ( Exception e )
                  {
                  System.err.println( "Error reading the rest of the message, byteToRead: " + byteToRead + ". message: " + e.getMessage() + "\n" );
            }
            // Convert the message array to a string
            messageString = new String( myMessage );
            System.out.println( messageString );

                  if ( debug )
                  {
                        System.out.println( "***********Server output complete****************" );
                        System.out.println( "*************************************************" );
                        System.out.println();
                  }
            }
            catch ( IOException e )
            {
                  System.err.println( "There was an error reading the Input Stream." );
            }
            catch ( Exception e )
            {
                  e.printStackTrace( System.out );
            }
            return messageString;
      }


      public void closeConnection()
      {
            try
            {
              in.close();
              pw.close();
              // close socket
              sslServerSocket.close();
            }
        catch ( IOException e )
            {
              System.out.println( "Error trying to close the socket" + e.getMessage() + "\n" );
            }
      }
}
LVL 17
Tacobell777Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
just add this to Your class:

public static void main(String[] args) {
    CFSSLTransportLayer tl = new CFSSLTransportLayer();
    System.out.println(tl.send("message"));
    tl.closeConnection();
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tacobell777Author Commented:
Brilliant thanks for that.
Tacobell777Author Commented:
any idea why I would get the error:
"The constructor EDGESignature() is undefined"

with the following code?

package edge;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Signature;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

public class EDGESignature
{
      final String password = "changeit";
      final String keyStoreLocation ="C:\\Documents and Settings\\Administrator\\.keystore";
      final boolean debug = false;
      String message;
      Signature mySignature;
      KeyStore myKeyStore;


      public static void main(String[] args) {
            EDGESignature tl = new EDGESignature();
          System.out.println( tl.getSignature( "test message" ) );
      }


      public EDGESignature( String message )
      {
            this.message = message;
            try
            {
                  myKeyStore = KeyStore.getInstance( "JKS" );
                  myKeyStore.load( new FileInputStream( keyStoreLocation ), password.toCharArray() );
                  TrustManagerFactory trustFact = TrustManagerFactory.getInstance( "SunX509", "SunJSSE" );
                  trustFact.init( myKeyStore );
                  TrustManager[] myTMs = trustFact.getTrustManagers();
                  PrivateKey privKey = ( PrivateKey ) myKeyStore.getKey( "shelco", password.toCharArray() );
                  byte[] key = privKey.getEncoded();
                  mySignature = Signature.getInstance( "MD5withRSA" );
                  mySignature.initSign( privKey );
                  
            }
            catch ( Exception e )
            {
                  e.printStackTrace( System.out );
            }
      }

      public String getSignature()
      {
            String signatureAsString = "";
            byte[] messageAsByte = this.message.getBytes();
            try
            {
                  mySignature.update( messageAsByte );
                  byte[] signature = mySignature.sign();
                  mySignature.initVerify( myKeyStore.getCertificate( "shelco" ) );
                  mySignature.update( messageAsByte );
                  signatureAsString = Base64Utils.base64Encode( signature );
                  if ( debug )
                  {
                        System.out.println( "Clear Text: " + new String( messageAsByte ) );
                        System.out.println( "Signature: " + mySignature );
                        System.out.println( mySignature.verify( signature ) ? "Verification Succeeded" : "Verification failed" );
                  }
            }
            catch ( Exception e )
            {
                  System.err.println( "There was an error getting the signature!" );
            }
            return signatureAsString;
      }
}
Tacobell777Author Commented:
Never mind, teh error just wasn't very descriptive, it was teh argument that was missing ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.