package os2200gencode;
import com.unisys.os2200.connector.*;

import javax.resource.*;
import java.io.*;
import javax.resource.cci.*;

//
//  This class is generated from the annotations at build time
//  This code cannot be edited.
//
public class %connClassName {

private String _iPAdd = "%ipAdd";
private String    _port = "%port";
private boolean _connected;
OS2200ConnectionSpec _connSpec;
private Connection _connection;
private String ServerConfig = "serverconfig.properties";


	/**
	* This constructor uses a specified ip address, port and factory name
	*  to build the connection
	* It is used when these configuration items are handled in the 
	*   users main code.
	*/
	public %connClassName(String IpIn, String portIn){
		_iPAdd = IpIn;
		_port = portIn;
		_connected = false;
		}
           
		/**
	* This constructor reads the IP address and port number from
	*  a file generated in the code.  
	* It is a default way of allowing for dynamically configurable code.
	*   users main code.
	*/ 
	public %connClassName(){
       try {
		InputStream ff =this.getClass().getResourceAsStream(ServerConfig);
		if (ff != null) {
			int count = ff.available();
			byte[] buffin = new byte[count+100];
			int countin = ff.read(buffin);
		    String strBuff = new String(buffin,0,countin);
		    ff.close();
		    String[] ov =  strBuff.split("\n");
		    _iPAdd = ov[0].substring(ov[0].indexOf("=")+1);
		    _port = ov[1].substring(ov[1].indexOf("=")+1);
	    }
	    _connected = false;
		}
		catch (IOException ioe){
			System.out.println("error reading resource "+ ioe.getLocalizedMessage());		 
		}
		catch (Throwable tt){
			System.out.println("error reading resource "+ tt.getLocalizedMessage());		 
		}
	}
	
	public String getIpAdd(){
		return _iPAdd;
	}
	public String getPort(){
		return _port;
	}
	
	public boolean connected(){
	  return _connected;
	  }
	/**
	* Create a connection using the host and port 
	*   that are class members (passed in to the constructor or embedded)
    * @return boolean true if connection succeeded
    *   throws ResourceException
    */
	public boolean Connect() throws ResourceException{
	  try{
	    OS2200ManagedConnectionFactory mcf;
		ConnectionFactory cf;
			boolean nonmanaged = true;
			mcf = new OS2200ManagedConnectionFactory(nonmanaged);
			  cf = (OS2200ConnectionFactory)mcf.createConnectionFactory();
			_connSpec = new OS2200ConnectionSpec(_iPAdd,new Integer(_port).toString());
			_connection = cf.getConnection(_connSpec) ;
		}
		 catch (ResourceException re ){
			 logit("Resource exception connection",re);
			 throw new ResourceException("Error Connecting to 2200",re);
		 }
		 catch (FileNotFoundException ne ){
			 logit("File Not Found exception connection",ne);
			 throw new ResourceException("Error Connecting to 2200",ne);
		 }
		 _connected = true;
        return true;
	}
	
   /**
   * create a connection given a user id and password 
   *   and using the host and port that are class members
   *  (passed in to the constructor or embedded)
   * @param String userid 
   * @param String password
   * @return boolean true if connection succeeded
   *   throws ResourceException
   */
	public boolean Connect(String userid, String password) throws ResourceException{
	  try{
	    OS2200ManagedConnectionFactory mcf;
		ConnectionFactory cf;
			boolean nonmanaged = true;
			mcf = new OS2200ManagedConnectionFactory(nonmanaged);
			  cf = (OS2200ConnectionFactory)mcf.createConnectionFactory();
			_connSpec = new OS2200ConnectionSpec(_iPAdd,new Integer(_port).toString());
			_connSpec.setUser(userid);
			_connSpec.setPassword(password);
			_connection = cf.getConnection(_connSpec) ;
		}
		 catch (ResourceException re ){
			 logit("Resource exception connection",re);
			 throw new ResourceException("Error Connecting to 2200",re);
		 }
		 catch (FileNotFoundException ne ){
			 logit("File Not Found exception connection",ne);
			 throw new ResourceException("Error Connecting to 2200",ne);
		 }
		 _connected = true;
        return true;
	}	
	public Interaction getNewInteraction(){
		try{
		return _connection.createInteraction();
		}
		catch (ResourceException re){
			logit("error creating interaction",re);
			return null;
		}
	}
	
    public void closeConnection(){
    	try{
       _connection.close();
       _connected = false;
    	}
    	catch (ResourceException re){
    		logit("error closing the connection",re);
    	}
       }
	private void logit(String msgIn, Throwable tt){
		System.out.println(msgIn + "\n" + tt.getLocalizedMessage());
		tt.printStackTrace();
	}
}
	