<commit_before>/**
 * ALMA - Atacama Large Millimiter Array (c) UNSPECIFIED - FILL IN, 2005
 * 
 * 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
 */

/**
 * @author dfugate
 * @version $Id: ChannelProperties.java,v 1.11 2009/04/27 13:46:05 hsommer Exp $
 * @since
 */

/**
 * ChannelProperties is a class which discovers the the quality of service and
 * administrative properties that should be used with any particular channel
 * name.
 */

package alma.acs.nc;

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

import org.omg.CORBA.Any;
import org.omg.CosNotification.AnyOrder;
import org.omg.CosNotification.BestEffort;
import org.omg.CosNotification.ConnectionReliability;
import org.omg.CosNotification.DeadlineOrder;
import org.omg.CosNotification.DiscardPolicy;
import org.omg.CosNotification.EventReliability;
import org.omg.CosNotification.FifoOrder;
import org.omg.CosNotification.LifoOrder;
import org.omg.CosNotification.MaxConsumers;
import org.omg.CosNotification.MaxEventsPerConsumer;
import org.omg.CosNotification.MaxQueueLength;
import org.omg.CosNotification.MaxSuppliers;
import org.omg.CosNotification.OrderPolicy;
import org.omg.CosNotification.Persistent;
import org.omg.CosNotification.Priority;
import org.omg.CosNotification.PriorityOrder;
import org.omg.CosNotification.Property;
import org.omg.CosNotification.RejectNewEvents;
import org.omg.CosNotification.StartTimeSupported;
import org.omg.CosNotification.StopTimeSupported;
import org.omg.CosNotification.Timeout;

import com.cosylab.CDB.DAO;

import alma.JavaContainerError.wrappers.AcsJContainerServicesEx;
import alma.acs.container.ContainerServicesBase;
import alma.acs.exceptions.AcsJException;
import alma.cdbErrType.CDBFieldDoesNotExistEx;
import alma.cdbErrType.WrongCDBDataTypeEx;

/**
 * ChanncelProperties is a class designed to retrieve the various quality of
 * service and administrative properties for a given channel. It is intended to
 * be used primarily by the Helper class within this package.
 *  * <p>
 * @TODO think about joining this class with {@link ChannelInfo}.
 * 
 * @author dfugate
 */
public class ChannelProperties {
	/**
	 * Creates a new instance of ChannelProperties.
	 * 
	 * @param services
	 *           A reference to the ContainerServices. Used to retrieve other
	 *           CORBA references and to access the logger.
	 */
	public ChannelProperties(ContainerServicesBase services) {
		// save a local reference to the container services
		m_services = services;

		// immediately grab a logger
		m_logger = m_services.getLogger();
	}

	// //////////////////////////////////////////////////////////////////////////
	/**
	 * Simple function which returns true if the given channel has an entry in
	 * $ACS_CDB/CDB/MACI/EventChannels/ section of the ACS configuration
	 * database.
	 * 
	 * @param channelName
	 *           is a string referring to the name of a Notification Channel
	 *           registered in the CORBA Naming Service
	 * @return true if
	 *         $ACS_CDB/CDB/MACI/EventChannels/channel_name/channel_name.xml
	 *         exists and is a valid XML. false otherwise.
	 */
	public boolean cdbChannelConfigExists(String channelName) {
		try {
			m_services.getCDB().get_DAO("MACI/Channels/" + channelName);
			return true;
		} catch (Exception e) {
			m_logger.finer("No CDB entry found for '" + channelName + "' channel");
			return false;
		}
	}

	// -----------------------------------------------------------
	
	/**
	 * When some attribute is set within the CDB (currently <code>IntegrationLogs</code> 
	 * defined in <code>EventChannel.xsd</code>, see NC document), a log is published 
	 * each time an event is sent or received. 
	 * For performance reasons, this should be used very carefully to say the least.
	 * 
	 * @param channelName
	 *           Name of the channel.
	 * @return True if event publishing/receiving should be traced by log messages.
	 * @throws AcsJException
	 * @since ACS 6.0.1 (formerly called "getIntegrationLogs")
	 */
	public boolean isTraceEventsEnabled(String channelName) throws AcsJException {
		// sanity check
		// @todo avoid this unnecessary CDB call; instead base this check on getCDB().get_DAO_Servant below
		if (this.cdbChannelConfigExists(channelName) == false) {
			return false;
		}

		// use this object to get at channel information from the CDB
		DAO tempDAO = null;
		try {
			tempDAO = m_services.getCDB().get_DAO_Servant("MACI/Channels/" + channelName);
		} catch (alma.cdbErrType.CDBXMLErrorEx e) {
			m_logger.log(Level.SEVERE, "Bad CDB entry found for '" + channelName + "' channel", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJUnknownEx(e);
		} catch (alma.cdbErrType.CDBRecordDoesNotExistEx e) {
			m_logger.log(Level.SEVERE, "No CDB entry found for '" + channelName + "' channel", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJFileNotFoundEx(e);
		} catch (AcsJContainerServicesEx e) {
			m_logger.log(Level.SEVERE, "CDB unavailable", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJNoResourcesEx(e);
		}

		try {
			if (tempDAO.get_string("IntegrationLogs").equals("false")) {
				return false;
			}
			return true;
		} catch (WrongCDBDataTypeEx e) {
			m_logger.log(Level.SEVERE, "Wrong type of data for IntegrationLogs", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJNoResourcesEx(e);
		} catch (CDBFieldDoesNotExistEx e) {
			m_logger.log(Level.SEVERE, "Field does not exist for IntegrationLogs", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJNoResourcesEx(e);
		}
	}

	// //////////////////////////////////////////////////////////////////////////
	/**
	 * Given a channel name that exists in the ACS CDB
	 * ($ACS_CDB/CDB/MACI/Channels/channelName/channelName.xml), this function
	 * returns the channels administrative properties in their CORBA format.
	 * 
	 * @param channelName<commit_msg>Method isTraceEventsEnabled() no longer calls the CDB twice. Still should be optimized further, to share CDB data with other pieces of NC code.<commit_after>/**
 * ALMA - Atacama Large Millimiter Array (c) UNSPECIFIED - FILL IN, 2005
 * 
 * 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
 */

/**
 * @author dfugate
 * @version $Id: ChannelProperties.java,v 1.12 2010/09/02 08:43:39 hsommer Exp $
 * @since
 */

/**
 * ChannelProperties is a class which discovers the the quality of service and
 * administrative properties that should be used with any particular channel
 * name.
 */

package alma.acs.nc;

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

import org.omg.CORBA.Any;
import org.omg.CosNotification.AnyOrder;
import org.omg.CosNotification.BestEffort;
import org.omg.CosNotification.ConnectionReliability;
import org.omg.CosNotification.DeadlineOrder;
import org.omg.CosNotification.DiscardPolicy;
import org.omg.CosNotification.EventReliability;
import org.omg.CosNotification.FifoOrder;
import org.omg.CosNotification.LifoOrder;
import org.omg.CosNotification.MaxConsumers;
import org.omg.CosNotification.MaxEventsPerConsumer;
import org.omg.CosNotification.MaxQueueLength;
import org.omg.CosNotification.MaxSuppliers;
import org.omg.CosNotification.OrderPolicy;
import org.omg.CosNotification.Persistent;
import org.omg.CosNotification.Priority;
import org.omg.CosNotification.PriorityOrder;
import org.omg.CosNotification.Property;
import org.omg.CosNotification.RejectNewEvents;
import org.omg.CosNotification.StartTimeSupported;
import org.omg.CosNotification.StopTimeSupported;
import org.omg.CosNotification.Timeout;

import com.cosylab.CDB.DAO;

import alma.JavaContainerError.wrappers.AcsJContainerServicesEx;
import alma.acs.container.ContainerServicesBase;
import alma.acs.exceptions.AcsJException;
import alma.cdbErrType.CDBFieldDoesNotExistEx;
import alma.cdbErrType.WrongCDBDataTypeEx;

/**
 * ChanncelProperties is a class designed to retrieve the various quality of
 * service and administrative properties for a given channel. It is intended to
 * be used primarily by the Helper class within this package.
 *  * <p>
 * @TODO think about joining this class with {@link ChannelInfo}.
 * 
 * @author dfugate
 */
public class ChannelProperties {
	/**
	 * Creates a new instance of ChannelProperties.
	 * 
	 * @param services
	 *           A reference to the ContainerServices. Used to retrieve other
	 *           CORBA references and to access the logger.
	 */
	public ChannelProperties(ContainerServicesBase services) {
		// save a local reference to the container services
		m_services = services;

		// immediately grab a logger
		m_logger = m_services.getLogger();
	}

	// //////////////////////////////////////////////////////////////////////////
	/**
	 * Simple function which returns true if the given channel has an entry in
	 * $ACS_CDB/CDB/MACI/EventChannels/ section of the ACS configuration
	 * database.
	 * 
	 * @param channelName
	 *           is a string referring to the name of a Notification Channel
	 *           registered in the CORBA Naming Service
	 * @return true if
	 *         $ACS_CDB/CDB/MACI/EventChannels/channel_name/channel_name.xml
	 *         exists and is a valid XML. false otherwise.
	 */
	public boolean cdbChannelConfigExists(String channelName) {
		try {
			m_services.getCDB().get_DAO("MACI/Channels/" + channelName);
			return true;
		} catch (Exception e) {
			m_logger.finer("No CDB entry found for '" + channelName + "' channel");
			return false;
		}
	}

	// -----------------------------------------------------------
	
	/**
	 * When some attribute is set within the CDB (currently <code>IntegrationLogs</code> 
	 * defined in <code>EventChannel.xsd</code>, see NC document), a log is published 
	 * each time an event is sent or received. 
	 * For performance reasons, this should be used very carefully to say the least.
	 * 
	 * @param channelName
	 *           Name of the channel.
	 * @return True if event publishing/receiving should be traced by log messages.
	 * @throws AcsJException
	 * @since ACS 6.0.1 (formerly called "getIntegrationLogs")
	 */
	public boolean isTraceEventsEnabled(String channelName) throws AcsJException {
		// use this object to get at channel information from the CDB
		DAO tempDAO = null;
		try {
			tempDAO = m_services.getCDB().get_DAO_Servant("MACI/Channels/" + channelName);
		} catch (alma.cdbErrType.CDBRecordDoesNotExistEx e) {
			return false;
		} catch (alma.cdbErrType.CDBXMLErrorEx e) {
			m_logger.log(Level.SEVERE, "Bad CDB entry found for '" + channelName + "' channel", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJUnknownEx(e);
		} catch (AcsJContainerServicesEx e) {
			m_logger.log(Level.SEVERE, "CDB unavailable", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJNoResourcesEx(e);
		}

		try {
			if (tempDAO.get_string("IntegrationLogs").equals("false")) {
				return false;
			}
			return true;
		} catch (WrongCDBDataTypeEx e) {
			m_logger.log(Level.SEVERE, "Wrong type of data for IntegrationLogs", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJNoResourcesEx(e);
		} catch (CDBFieldDoesNotExistEx e) {
			m_logger.log(Level.SEVERE, "Field does not exist for IntegrationLogs", e);
			throw new alma.ACSErrTypeCommon.wrappers.AcsJNoResourcesEx(e);
		}
	}

	// //////////////////////////////////////////////////////////////////////////
	/**
	 * Given a channel name that exists in the ACS CDB
	 * ($ACS_CDB/CDB/MACI/Channels/channelName/channelName.xml), this function
	 * returns the channels administrative properties in their CORBA format.
	 * 
	 * @param channelName
{'fraction_non_alphanumeric': 0.1172901921132457, 'fraction_numerical': 0.006222291358792875, 'mean_word_length': 3.464583333333333, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6125803', 'n_tokens_mistral': 4055, 'n_tokens_neox': 3706, 'n_words': 1181}