/**
 * Copyright 2008-2009 DRIVER PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.common.ws;

import java.security.InvalidParameterException;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.log4j.Logger;

import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
import eu.dnetlib.enabling.is.sn.rmi.ISSNService;
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;

/**
 * Information Services beans factory. Used in spring configuration.
 * 
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 * @verion 0.0.1
 */
public class ISBeansFactoryCnrApi {

	/** The Constant log. */
	protected static final Logger log = Logger.getLogger(ISBeansFactoryCnrApi.class);

	/** The look up location. */
	private String lookUpLocation = null;
	
	/** The registry location. */
	private String registryLocation = null;
	
	/** The resultset location. */
	private String resultSetLocation = null;
	
	/** The sn location. */
	private String snLocation = null;

	/** The look up service. */
	ISLookUpService lookUpService = null;
	
	/** The registry service. */
	ISRegistryService registryService = null;
	
	/** The resultset service. */
	ResultSetService resultSetService = null;
	
	/** The sn service. */
	ISSNService snService = null;

	/**
	 * ISLookUp factory method. Returns ISLookUp service bean.
	 * 
	 * @return ISLookUp service bean
	 */
	public ISLookUpService getISLookUp() {

		if (lookUpService != null)
			return lookUpService;
		else {
			if (lookUpLocation != null) {

				JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
				factory.setServiceClass(ISLookUpService.class);
				factory.setAddress(lookUpLocation);

				//factory.getInInterceptors().add(new LoggingInInterceptor());
				//factory.getOutInterceptors().add(new LoggingOutInterceptor());

				return lookUpService = (ISLookUpService) factory.create();
				
			} else {
				throw new InvalidParameterException(
						"No lookUpLocation defined!");
			}
		}
	}

	/**
	 * ISRegistry factory method. Returns ISRegistry service bean.
	 * 
	 * @return ISRegistry service bean
	 */
	public ISRegistryService getISRegistry() {
		if (registryService != null)
			return registryService;
		else {
			if (registryLocation != null) {
				JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
				factory.setServiceClass(ISRegistryService.class);
				factory.setAddress(registryLocation);

				//factory.getInInterceptors().add(new LoggingInInterceptor());
				//factory.getOutInterceptors().add(new LoggingOutInterceptor());
				
				return registryService = (ISRegistryService) factory.create();
			
			} else {
				throw new InvalidParameterException(
						"No registryLocation defined!");
			}
		}
	}

	/**
	 * ResultSet factory method. Returns ResultSet service bean.
	 * 
	 * @return ResultSet service bean
	 */
	public ResultSetService getResultSet() {
		if (resultSetService != null)
			return resultSetService;
		else {
			if (resultSetLocation != null) {
				JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
				factory.setServiceClass(ResultSetService.class);
				factory.setAddress(resultSetLocation);

				//factory.getInInterceptors().add(new LoggingInInterceptor());
				//factory.getOutInterceptors().add(new LoggingOutInterceptor());

				return resultSetService = (ResultSetService) factory.create();

			} else {
				throw new InvalidParameterException(
						"No resultSetLocation defined!");
			}
		}
	}
	
	/**
	 * ISSN factory method. Returns ISSN service bean.
	 * 
	 * @return ISSN service bean
	 */
	public ISSNService getISSN() {

		if (snService != null)
			return snService;
		else {
			if (snLocation != null) {
				JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
				factory.setServiceClass(ISSNService.class);
				factory.setAddress(snLocation);

				//factory.getInInterceptors().add(new LoggingInInterceptor());
				//factory.getOutInterceptors().add(new LoggingOutInterceptor());

				return snService = (ISSNService) factory.create();

			} else {
				throw new InvalidParameterException("No snLocation defined!");
			}
		}
	}

	/**
	 * Gets the lookup location.
	 * 
	 * @return the lookup location
	 */
	public String getLookUpLocation() {
		return lookUpLocation;
	}

	/**
	 * Sets the lookup location.
	 * 
	 * @param lookUpLocation
	 *            the new look uplocation
	 */
	public void setLookUpLocation(String lookUpLocation) {
		this.lookUpLocation = lookUpLocation;
	}

	/**
	 * Gets the registry location.
	 * 
	 * @return the registry location
	 */
	public String getRegistryLocation() {
		return registryLocation;
	}

	/**
	 * Sets the registry location.
	 * 
	 * @param registryLocation
	 *            the new registry location
	 */
	public void setRegistryLocation(String registryLocation) {
		this.registryLocation = registryLocation;
	}

	/**
	 * Gets the resultset location.
	 * 
	 * @return the resultset location
	 */
	public String getResultSetLocation() {
		return resultSetLocation;
	}

	/**
	 * Sets the result setlocation.
	 * 
	 * @param resultSetLocation
	 *            the new resultset location
	 */
	public void setResultSetLocation(String resultSetLocation) {
		this.resultSetLocation = resultSetLocation;
	}

	/**
	 * Gets the sn location.
	 * 
	 * @return the sn location
	 */
	public String getSnLocation() {
		return snLocation;
	}

	/**
	 * Sets the sn location.
	 * 
	 * @param snLocation
	 *            the new sn location
	 */
	public void setSnLocation(String snLocation) {
		this.snLocation = snLocation;
	}
}
