package os2200gencode;

import com.unisys.os2200.connector.*;

import javax.resource.*;
import javax.resource.cci.*;
import java.io.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
//
//  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 String  conFactory = "%factory";
private Connection _connection;
private boolean _connected = false;
OS2200ConnectionSpec _connSpec;
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, String ConFactory){
		_iPAdd = IpIn;
		_port = portIn;
		conFactory = ConFactory;
		_connected = false;
		if (conFactory != null){
			 if (conFactory.trim().length()==0){
				conFactory = null;
			} 		
		}
           
	}
	/**
	* 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;
	  }
	/**
	* make a connection to the 2200 using 
	* a connection factory which we look up. 
	* @return boolean true if successful
	*  throws ResourceException if it fails
	*/
	public boolean Connect()throws ResourceException{
	  try{
		ConnectionFactory cf;
		// get the connection factory from the lookup
		InitialContext initCtx = new InitialContext();
		cf = (ConnectionFactory)initCtx.lookup(conFactory);
		// now get the connection
		_connSpec = new OS2200ConnectionSpec(_iPAdd,_port);
		_connection = cf.getConnection(_connSpec) ;
		}
		 catch (NamingException ne ){
		     ne.printStackTrace();
			 logit("Naming exception connection",ne);	
			 throw new ResourceException("Error Connecting to 2200",ne);

		 }
		 catch (ResourceException re ){
		 	 re.printStackTrace();
			 logit("Resource exception connection",re);
			 throw new ResourceException("Error Connecting to 2200",re);
		 }
		 _connected = true;
        return true;
	}
		/**
	* make a connection to the 2200 using 
	* a connection factory which we look up, and specifying
	* the userid and password
	* @param userid  String userid for connecting in TIP session control
	* @param password String password for connecting in TIP session control
	* @return boolean true if successful
	*  throws ResourceException if it fails
	*/
	public boolean Connect(String userid, String password)
	      throws ResourceException{
	  try{
		ConnectionFactory cf;
		// get the connection factory from the lookup
		InitialContext initCtx = new InitialContext();
		cf = (ConnectionFactory)initCtx.lookup(conFactory);
		// now get the connection
		_connSpec = new OS2200ConnectionSpec(_iPAdd,_port);
	    _connSpec.setUser(userid);
	    _connSpec.setPassword(password);
		_connection = cf.getConnection(_connSpec) ;
		}
		 catch (NamingException ne ){
		     ne.printStackTrace();
			 logit("Naming exception connection",ne);	
			 throw new ResourceException("Error Connecting to 2200",ne);

		 }
		 catch (ResourceException re ){
		 	 re.printStackTrace();
			 logit("Resource exception connection",re);
			 throw new ResourceException("Error Connecting to 2200",re);
		 }
		 _connected = true;
        return true;
	}
	
	//Make a new interaction
	public Interaction getNewInteraction(){
		try{
		return _connection.createInteraction();
		}
		catch (ResourceException re){
			logit("error creating interaction",re);
			return null;
		}
	}
	
   //Close the connection
   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();
	}
}
	
