package edu.usc.glidein.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.globus.gsi.GlobusCredential;
import org.globus.gsi.GlobusCredentialException;

public class CredentialUtil
{
	public static void save(GlobusCredential credential, File file) 
	throws IOException
	{
		// Create the file if it doesn't exist
		file.createNewFile();
		
		// Change file permissions
		CommandLine chmod = new CommandLine();
		chmod.setExecutable(new File("/bin/chmod"));
		chmod.addArgument("600");
		chmod.addArgument(file.getAbsolutePath());
		chmod.execute();
		int exitCode = chmod.getExitCode();
		if(exitCode != 0){
			throw new IOException(
					"Unable to change proxy permissions\n\n"+
					"Stdout:\n"+chmod.getOutput()+
					"Stderr:\n"+chmod.getError());
		}
		
		// Save the credential to the file
		FileOutputStream pstream = 
			new FileOutputStream(file);
		credential.save(pstream);
		pstream.close();
	}
	
	public static GlobusCredential load(File file) 
	throws IOException
	{
		try {
			FileInputStream fis = new FileInputStream(file);
			GlobusCredential cred = new GlobusCredential(fis);
			fis.close();
			return cred;
		} catch (GlobusCredentialException gce) {
			IOException ioe = new IOException("Invalid credential");
			ioe.initCause(gce);
			throw ioe;
		}
	}
}

Add credential util to store and load GlobusCredentials from files. Automatically changes permissions of file.
Add credential util to store and load GlobusCredentials from files. Automatically changes permissions of file.

git-svn-id: bdb5f82b9b83a1400e05d69d262344b821646179@1380 e217846f-e12e-0410-a4e5-89ccaea66ff7
