/*
	PLJavaCheckEnvButtonActionListener
*/

package com.gradwell.PLJavaCheckEnv;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class PLJavaCheckEnvButtonActionListener implements ActionListener
{

    /* data */
    
    javax.swing.JTextArea myMessages = null;
    Runtime myRuntime = null;

    /* constructor */
    
    PLJavaCheckEnvButtonActionListener(javax.swing.JTextArea theMessageArea)
    {
        this.myMessages = theMessageArea;
        myRuntime = Runtime.getRuntime();
    }
        
    public void actionPerformed(ActionEvent ae)
	{
                int a = 1;
                
                boolean OK = true;
                this.myMessages.setText("\nStarting ...\n");
                
                String myPath = System.getenv("PATH");
                this.myMessages.append("\nPATH is: \n" + myPath + "\n\n");
                String[] splitPath = myPath.split(";", 0);
                int numPaths = splitPath.length;
                this.myMessages.append("Number of components is: " + numPaths + "\n");
                
                Runtime.getRuntime();
                // make sure that the java vm is accessible
                boolean javaVMOK = tryLoading("jvm");
                
                // postgres.exe needs to accessible - needed to load pljava - but the following doesn't work - probably because it is a .exe
                //boolean postgresOK = tryLoading("postgres");

                boolean pljavaOKJ = tryLoading("pljava");
               
                if(javaVMOK &&  pljavaOKJ)
                {
                   this.myMessages.append("\njvm.dll and pljava.dll both loaded successfully.  \nNow you should be able to deploy pljava.\n"); 
                }
                else
                {
                   this.myMessages.append("\n\njvm.dll and pljava.dll must be on the Windows PATH.\n" +
                           "jvm.dll is to be found in the Java .../client directory, for example, 'C:\\Program Files\\Java\\jre1.6.0_04\\bin\\client'.\n\n" +
                           "pljava.dll is to be found in the postgreSQL .../lib directory, for example 'C:\\Program Files\\PostgreSQL\\8.3RC2\\lib'.\n\n" +
                           "Please add these directories to your PATH (Start/Control Panel/System/Advanced/Environment Variables) and run this program again.  " +
                           "\n\nYou will need to restart the program and possibly the machine to make the new PATH active." +
                           "\n\nDependency Walker - see http://www.dependencywalker.com/ is a good tool to check that dlls can be loaded along with all the other dlls they reference."); 
                }
                a = 2;
 
            
        }
    
    public boolean tryLoading(String dllName)
    {
        try
                {
                    // no .dll subscript and no directory names allowed !!
                    myRuntime.loadLibrary(dllName);
                    return true;
                }
                catch (java.lang.UnsatisfiedLinkError ule)
                {
                    String uleMessage = ule.getMessage();
                    boolean notOnClassPath = uleMessage.equalsIgnoreCase("no " + dllName + " in java.library.path");
                    String thrownMessage = "\nError whilst loading " + dllName + ".\nError Messages are:\n\n" + uleMessage;
                    if (notOnClassPath)
                    {
                        this.myMessages.append("\n" + dllName +" is not on the classpath");
                    }
                    else
                    {
                        this.myMessages.append("\n" + dllName +" references other dlls that are not on the classpath");
                    }
                    this.myMessages.append(thrownMessage);
                    return false;
                }
                catch (Throwable t)
                {
                    String throwableClass = t.getClass().getName();
                    String thrownMessage = "\n\nError whilst loading postgres.exe.\nException class is: '" + throwableClass + "'\nError Messages are:\n\n" + t.getMessage();
                    this.myMessages.append(thrownMessage);
                    return false;
               }
        
    }
}