<commit_before>package gov.nih.nci.cagrid.gts.client.gtssync;

import gov.nih.nci.cagrid.gridca.common.CertUtil;
import gov.nih.nci.cagrid.gts.bean.TrustedAuthority;
import gov.nih.nci.cagrid.gts.bean.TrustedAuthorityFilter;
import gov.nih.nci.cagrid.gts.client.GTSSearchClient;

import java.io.File;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.globus.common.CoGProperties;
import org.projectmobius.common.MobiusDate;


/**
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @version $Id: ArgumentManagerTable.java,v 1.2 2004/10/15 16:35:16 langella
 *          Exp $
 */
public class GTSSync {

	private GTSSyncProperties prop;
	private Map caListings;
	private Map listingsById;
	private Logger logger;
	private int nextFileId = 0;


	public GTSSync(GTSSyncProperties prop) {
		this.prop = prop;
		logger = Logger.getLogger(this.getClass().getName());
	}


	private void reset() {
		this.caListings = null;
		this.listingsById = null;
		this.nextFileId = 0;
	}


	private int getNextFileId() {
		nextFileId = nextFileId + 1;
		boolean found = false;
		while (!found) {
			if (listingsById.containsKey(new Integer(nextFileId))) {
				found = true;
			} else {
				nextFileId = nextFileId + 1;
			}
		}
		return nextFileId;
	}


	public void sync() throws Exception {
		try {
			// Need to make it so multiple certs of the same CA cannot be
			// written
			// Need to make a priority for this.
			// Hash by service, prioritize by service order
			reset();
			this.readInCurrentCADirectory();
			Map master = new HashMap();
			int taCount = 1;
			String error = null;
			String dt = MobiusDate.getCurrentDateTimeAsString();
			for (int i = 0; i < prop.getSyncDescriptorCount(); i++) {
				SyncDescriptor des = prop.getSyncDescriptor(i);
				String uri = des.getGTSServiceURI();
				this.logger.info("Syncing with the GTS " + uri);
				GTSSearchClient client = new GTSSearchClient(uri);
				Map taMap = new HashMap();
				for (int j = 0; j < des.getFilterCount(); j++) {
					TrustedAuthorityFilter f = des.getFilter(j);
					int filter = j + 1;
					try {
						TrustedAuthority[] tas = client.findTrustedAuthorities(f);
						int length = 0;
						if (tas != null) {
							length = tas.length;
						}
						this.logger.debug("Successfully synced with " + uri + " using filter " + filter
							+ " the search found " + length + " Trusted Authority(s)!!!");

						for (int x = 0; x < length; x++) {
							taMap.put(tas[x].getTrustedAuthorityName(), tas[x]);
						}

					} catch (Exception e) {
						logger.error("An error occurred syncing with " + uri + " using filter " + filter + "!!!", e);
					}
				}

				// Write all to the master list;

				Iterator itr = taMap.values().iterator();
				while (itr.hasNext()) {
					TrustedAuthority ta = (TrustedAuthority) itr.next();

					if (master.containsValue(ta.getTrustedAuthorityName())) {
						TrustedCAListing gta = (TrustedCAListing) master.get(ta.getTrustedAuthorityName());
						String msg = "Conflict Detected: The Trusted Authority " + ta.getTrustedAuthorityName()
							+ " was determined to be trusted by both " + gta.getService() + " and " + uri + ".";
						if (prop.errorOnConflicts()) {
							error = msg;
							logger.error(error);
							break;
						} else {
							logger.warn(msg);
						}
					} else {
						master.put(ta.getTrustedAuthorityName(), new TrustedCAListing(uri, ta));
					}
				}
				this.logger.debug("Done syncing with the GTS " + uri + " " + taMap.size()
					+ " Trusted Authority(s) found!!!");
				
				if (error != null) {
					break;
				}
			}
			
			// TODO: Write out tas.
			Iterator itr = master.values().iterator();
			while (itr.hasNext()) {
				int fid = this.getNextFileId();
				String filePrefix = CoGProperties.getDefault().getCaCertLocations() + File.separator + prop.getFilePrefix() + "-"
					+ dt + "-" + taCount;
				File caFile = new File(filePrefix + "." + fid);
				File crlFile = new File(filePrefix + ".r" + fid);
				try {
					TrustedCAListing listing = (TrustedCAListing) itr.next();
					TrustedAuthority ta = listing.getTrustedAuthority();
					X509Certificate cert = CertUtil.loadCertificate(ta.getCertificate()
						.getCertificateEncodedString());
					CertUtil.writeCertificate(cert, caFile);
					logger.debug("Wrote out the certificate for the Trusted Authority "
						+ ta.getTrustedAuthorityName() + " to the file " + caFile.getAbsolutePath());
					if (ta.getCRL() != null) {
						if (ta.getCRL().getCrlEncodedString() != null) {
							X509CRL crl = CertUtil.loadCRL(ta.getCRL().getCrlEncodedString());
							CertUtil.writeCRL(crl, crlFile);
							logger.debug("Wrote out the CRL for the Trusted Authority "
								+ ta.getTrustedAuthorityName() + " to the file " + crlFile.getAbsolutePath());
						}
					}

				} catch (Exception e) {
					logger.error("An unexpected error occurred writing out the Trusted Authorities!!!", e);
					caFile.delete();
					crlFile.delete();
				}
				taCount = taCount + 1;
			}
			// TODO: Delete Other TAs.
			

			if (error != null) {
				throw new Exception(error);
			}


		} catch (Exception e) {
			// TODO: Handle Exception
			throw e;
		}
	}


	private void readInCurrentCADirectory() throws Exception {
		caListings = new HashMap();
		this.listingsById = new HashMap();
		String caDir = CoGProperties.getDefault().getCaCertLocations();
		logger.info("Taking Snapshot of Trusted CA Directory (" + caDir + ")....");<commit_msg>*** empty log message ***<commit_after>package gov.nih.nci.cagrid.gts.client.gtssync;

import gov.nih.nci.cagrid.gridca.common.CertUtil;
import gov.nih.nci.cagrid.gts.bean.TrustedAuthority;
import gov.nih.nci.cagrid.gts.bean.TrustedAuthorityFilter;
import gov.nih.nci.cagrid.gts.client.GTSSearchClient;

import java.io.File;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.globus.common.CoGProperties;
import org.projectmobius.common.MobiusDate;


/**
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @version $Id: ArgumentManagerTable.java,v 1.2 2004/10/15 16:35:16 langella
 *          Exp $
 */
public class GTSSync {

	private GTSSyncProperties prop;
	private Map caListings;
	private Map listingsById;
	private Logger logger;
	private int nextFileId = 0;


	public GTSSync(GTSSyncProperties prop) {
		this.prop = prop;
		logger = Logger.getLogger(this.getClass().getName());
	}


	private void reset() {
		this.caListings = null;
		this.listingsById = null;
		this.nextFileId = 0;
	}


	private int getNextFileId() {
		nextFileId = nextFileId + 1;
		boolean found = false;
		while (!found) {
			if (listingsById.containsKey(new Integer(nextFileId))) {
				found = true;
			} else {
				nextFileId = nextFileId + 1;
			}
		}
		return nextFileId;
	}


	public void sync() throws Exception {
		try {
			// Need to make it so multiple certs of the same CA cannot be
			// written
			// Need to make a priority for this.
			// Hash by service, prioritize by service order
			reset();
			this.readInCurrentCADirectory();
			Map master = new HashMap();

			String error = null;
			String dt = MobiusDate.getCurrentDateTimeAsString();
			for (int i = 0; i < prop.getSyncDescriptorCount(); i++) {
				SyncDescriptor des = prop.getSyncDescriptor(i);
				String uri = des.getGTSServiceURI();
				this.logger.info("Syncing with the GTS " + uri);
				GTSSearchClient client = new GTSSearchClient(uri);
				Map taMap = new HashMap();
				for (int j = 0; j < des.getFilterCount(); j++) {
					TrustedAuthorityFilter f = des.getFilter(j);
					int filter = j + 1;
					try {
						TrustedAuthority[] tas = client.findTrustedAuthorities(f);
						int length = 0;
						if (tas != null) {
							length = tas.length;
						}
						this.logger.debug("Successfully synced with " + uri + " using filter " + filter
							+ " the search found " + length + " Trusted Authority(s)!!!");

						for (int x = 0; x < length; x++) {
							taMap.put(tas[x].getTrustedAuthorityName(), tas[x]);
						}

					} catch (Exception e) {
						logger.error("An error occurred syncing with " + uri + " using filter " + filter + "!!!", e);
					}
				}

				// Write all to the master list;

				Iterator itr = taMap.values().iterator();
				while (itr.hasNext()) {
					TrustedAuthority ta = (TrustedAuthority) itr.next();

					if (master.containsValue(ta.getTrustedAuthorityName())) {
						TrustedCAListing gta = (TrustedCAListing) master.get(ta.getTrustedAuthorityName());
						String msg = "Conflict Detected: The Trusted Authority " + ta.getTrustedAuthorityName()
							+ " was determined to be trusted by both " + gta.getService() + " and " + uri + ".";
						if (prop.errorOnConflicts()) {
							error = msg;
							logger.error(error);
							break;
						} else {
							logger.warn(msg);
						}
					} else {
						master.put(ta.getTrustedAuthorityName(), new TrustedCAListing(uri, ta));
					}
				}
				this.logger.debug("Done syncing with the GTS " + uri + " " + taMap.size()
					+ " Trusted Authority(s) found!!!");

				if (error != null) {
					break;
				}
			}

			// TODO: Write out tas.
			int taCount = 0;
			Iterator itr = master.values().iterator();
			while (itr.hasNext()) {
				taCount = taCount + 1;
				int fid = this.getNextFileId();
				String filePrefix = CoGProperties.getDefault().getCaCertLocations() + File.separator
					+ prop.getFilePrefix() + "-" + dt + "-" + taCount;
				File caFile = new File(filePrefix + "." + fid);
				File crlFile = new File(filePrefix + ".r" + fid);
				try {
					TrustedCAListing listing = (TrustedCAListing) itr.next();
					TrustedAuthority ta = listing.getTrustedAuthority();
					X509Certificate cert = CertUtil.loadCertificate(ta.getCertificate().getCertificateEncodedString());
					CertUtil.writeCertificate(cert, caFile);
					logger.debug("Wrote out the certificate for the Trusted Authority " + ta.getTrustedAuthorityName()
						+ " to the file " + caFile.getAbsolutePath());
					if (ta.getCRL() != null) {
						if (ta.getCRL().getCrlEncodedString() != null) {
							X509CRL crl = CertUtil.loadCRL(ta.getCRL().getCrlEncodedString());
							CertUtil.writeCRL(crl, crlFile);
							logger.debug("Wrote out the CRL for the Trusted Authority " + ta.getTrustedAuthorityName()
								+ " to the file " + crlFile.getAbsolutePath());
						}
					}

				} catch (Exception e) {
					logger.error("An unexpected error occurred writing out the Trusted Authorities!!!", e);
					caFile.delete();
					crlFile.delete();
				}

			}
			logger.info("Successfully wrote out " + taCount + " Trusted Authority(s) to "
				+ CoGProperties.getDefault().getCaCertLocations());
			// TODO: Delete Other TAs.
			int removeCount = 0;
			Iterator del = caListings.values().iterator();
			while (del.hasNext()) {
				TrustedCAFileListing fl = (TrustedCAFileListing) del.next();
				if (fl.getName().indexOf(prop.getFilePrefix()) >= 0) {
					removeCount = removeCount + 1;
					if (fl.getCertificate() != null) {
						fl.getCertificate().delete();
						logger.debug("Removed File "+fl.getCertificate().getAbsolutePath());
					}
					
					if (fl.getCRL() != null) {
						fl.getCRL().delete();
						logger.debug("Removed File "+fl.getCRL().getAbsolutePath());
					}
					
					if (fl.getSigningPolicy() != null) {
						fl.getSigningPolicy() .delete();
						logger.debug("Removed File "+fl.getSigningPolicy() .getAbsolutePath());
					}
				}
			}
			logger.info("Successfully removed " + taCount + " Trusted Authority(s) from "
				+ CoGProperties.getDefault().getCaCertLocations());

			if (error != null) {
				throw new Exception(error);
			}

		} catch (Exception e) {
			// TODO: Handle Exception
			throw e;
		}
	}


	private void readInCurrentCADirectory() throws Exception {
		caListings = new HashMap();
		this.listingsById = new HashMap();
		String caDir = CoGProperties.getDefault().getCaCertLocations();
		logger.info("Taking Snapshot of Trusted CA Directory (" + caDir + ")....");
{'fraction_non_alphanumeric': 0.13825220701384952, 'fraction_numerical': 0.006722280715963392, 'mean_word_length': 3.12012012012012, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144781', 'n_tokens_mistral': 4983, 'n_tokens_neox': 4008, 'n_words': 951}