<commit_before>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2002
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *    MA 02111-1307  USA
 */
package alma.acs.container.archive;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import alma.acs.container.ContainerServices;
import alma.xmlstore.Identifier;
import alma.xmlstore.IdentifierHelper;


/**
 * Encapsulates access to the archive, currently only to the Identifier component
 * of the archive.
 * <p>
 * Also provides two failover mechanisms to be used during development. 
 * <ol>
 * <li>If connection to the archive fails, will try to instantiate and use a user-supplied
 * {@link UniqueIdDepot}
 * <li>if that also fails, will use random numbers instead if real IDs.
 * </ol>
 * <p>
 * This class should not be instantiated before the container has become operational, 
 * having access to the Manager and central logger etc.
 * 
 * @author hsommer
 */
public class ArchiveProxy 
{
	private static ArchiveProxy s_instance;
	
	private Logger m_logger;

	private ContainerServices m_contServ;
	
	private Uid m_uid;

	// if true, use random ids instead of really unique ids from the archive
	private boolean m_substituteRandomIds = true;  		

	

	/**
	 * Singleton accessor
	 * @param managerProxy
	 * @return ArchiveProxy
	 */
	public static synchronized ArchiveProxy getArchiveProxy(ContainerServices contServ, Logger logger)
	{ 
		if (s_instance == null)
		{
			s_instance = new ArchiveProxy(contServ, logger);
		}
		return s_instance;
	}
	
	
	/**
	 * private singleton ctor
	 */
	private ArchiveProxy(ContainerServices contServ, Logger logger)
	{
		if (contServ == null)
		{
			throw new IllegalArgumentException(
				"containerServices must not be null.");
		}
		m_contServ = contServ;
		m_logger = logger;
	}
	

	/**
	 * Gets a Uid, which actually manages a range of unique entity ids rather 
	 * than a single one. 
	 * First tries to get it from the archive, and then, depending on 
	 * <code>m_substituteRandomIds</code>, either uses a random <code>long</code> number 
	 * for the global part of the Uid, or throws an <code>ArchiveNotAvailableException</code>.
	 * 
	 * To get access to the archive, the ACS manager is queried for instances.
	 * The first such instance will be used. The connection to the archive component
	 * will be released right after the Uid string has been retrieved, since
	 * a Uid object will last us for some 2^32 unique ids. 
	 * 
	 * @return Uid<commit_msg>Enforces memory synchronization between threads.<commit_after>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2002
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *    MA 02111-1307  USA
 */
package alma.acs.container.archive;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import alma.acs.container.ContainerServices;
import alma.xmlstore.Identifier;
import alma.xmlstore.IdentifierHelper;


/**
 * Encapsulates access to the archive, currently only to the Identifier component
 * of the archive.
 * <p>
 * Also provides two failover mechanisms to be used during development. 
 * <ol>
 * <li>If connection to the archive fails, will try to instantiate and use a user-supplied
 * {@link UniqueIdDepot}
 * <li>if that also fails, will use random numbers instead if real IDs.
 * </ol>
 * <p>
 * This class should not be instantiated before the container has become operational, 
 * having access to the Manager and central logger etc.
 * 
 * @author hsommer
 */
public class ArchiveProxy 
{
	private static ArchiveProxy s_instance;
	
	private Logger m_logger;

	private ContainerServices m_contServ;
	
	private volatile Uid m_uid;

	// if true, use random ids instead of really unique ids from the archive
	private boolean m_substituteRandomIds = true;  		

	

	/**
	 * Singleton accessor
	 * @param managerProxy
	 * @return ArchiveProxy
	 */
	public static synchronized ArchiveProxy getArchiveProxy(ContainerServices contServ, Logger logger)
	{ 
		if (s_instance == null)
		{
			s_instance = new ArchiveProxy(contServ, logger);
		}
		return s_instance;
	}
	
	
	/**
	 * private singleton ctor
	 */
	private ArchiveProxy(ContainerServices contServ, Logger logger)
	{
		if (contServ == null)
		{
			throw new IllegalArgumentException(
				"containerServices must not be null.");
		}
		// multithreading: ensure values are copied to main memory 
		synchronized (this) {
			m_contServ = contServ;
			m_logger = logger;
		}
	}
	

	/**
	 * Gets a Uid, which actually manages a range of unique entity ids rather 
	 * than a single one. 
	 * First tries to get it from the archive, and then, depending on 
	 * <code>m_substituteRandomIds</code>, either uses a random <code>long</code> number 
	 * for the global part of the Uid, or throws an <code>ArchiveNotAvailableException</code>.
	 * 
	 * To get access to the archive, the ACS manager is queried for instances.
	 * The first such instance will be used. The connection to the archive component
	 * will be released right after the Uid string has been retrieved, since
	 * a Uid object will last us for some 2^32 unique ids. 
	 * 
	 * @return Uid
{'fraction_non_alphanumeric': 0.06926915174640447, 'fraction_numerical': 0.0067508071617258586, 'mean_word_length': 3.528239202657807, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6204733', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1810, 'n_words': 834}