/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 * $Header:$
 */
package service;

import java.beans.beancontext.BeanContextServiceProvider;
import java.beans.beancontext.BeanContextServices;
import java.io.Serializable;
import java.util.Iterator;

/**
 * Custom controls service provider.  Once registered with a ControlContainerContext (CCC), this
 * service provider produces EventRecorderService service instances for controls within the CCC.
 * <p/>
 * Controls may get a service instance using the <code>@org.apache.beehive.controls.api.context.Context</code>
 * annotation in their implementations.  See InnerControlImpl or OuterControlImpl for sample of usage.
 */
public class EventRecorderServiceProvider implements
        BeanContextServiceProvider, Serializable {

    private static final long serialVersionUID = -2688547397733439091L;
    private EventRecorderService _recorderService;

    /**
     * Service selectors are not used by this service provide.  Always returns null.
     *
     * @param bcs
     * @param serviceClass
     * @return <code>null</null>
     */
    public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) {
        return null;
    }

    /**
     * Requests an instance of a service from this <code>BeanContextServiceProvider</code>.
     *
     * @param bcs             The <code>BeanContextServices</code> associated with this
     *                        particular request. This parameter enables the
     *                        <code>BeanContextServiceProvider</code> to distinguish service
     *                        requests from multiple sources.
     * @param requestor       The object requesting the service
     * @param serviceClass    For this service provider value should always be <code>EventRecorderService</code>
     * @param serviceSelector Not applicable for this service provider, should always be <code>null</code>
     * @return a reference to the requested service
     */
    public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) {
        if (EventRecorderService.class.equals(serviceClass)) {
            if (_recorderService == null) {
                _recorderService = new EventRecorderService();
            }
            return _recorderService;
        }
        return null;
    }

    /**
     * Noop for this service provider.
     *
     * @param bcs
     * @param requestor
     * @param service
     */
    public void releaseService(BeanContextServices bcs, Object requestor, Object service) {
		// noop
	}
}
