001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.kernel.standard;
018    
019    import java.util.concurrent.Executor;
020    import java.util.concurrent.TimeUnit;
021    import org.apache.xbean.kernel.Kernel;
022    import org.apache.xbean.kernel.ServiceFactory;
023    import org.apache.xbean.kernel.ServiceName;
024    
025    /**
026     * The ServiceManagerFactory handles the construction ServiceManagers.
027     *
028     * @author Dain Sundstrom
029     * @version $Id$
030     * @since 2.0
031     */
032    public class ServiceManagerFactory {
033        /**
034         * The kernel in which the service will be bound.
035         */
036        private final Kernel kernel;
037    
038        /**
039         * This monitor broadcasts events to the listeners registered for service.
040         */
041        private final ServiceMonitorBroadcaster serviceMonitor;
042    
043        /**
044         * Events service events are sent asynchronously using this executor.
045         */
046        private final Executor serviceExecutor;
047    
048        /**
049         * The maximum duration to wait for a service event to complete.
050         */
051        private final long timeoutDuration;
052    
053        /**
054         * The unit of measure for the {@link #timeoutDuration}.
055         */
056        private final TimeUnit timeoutUnits;
057    
058        /**
059         * Creates a ServiceManagerFactory.
060         *
061         * @param kernel the kernel in which the service will be registered
062         * @param serviceMonitor the service monitor used for all services created by this factory
063         * @param serviceExecutor the executor available to the service manager
064         * @param timeoutDuration the maximum duration to wait for a service event to complete
065         * @param timeoutUnits the unit of measure for the timeoutDuration
066         */
067        public ServiceManagerFactory(Kernel kernel, ServiceMonitorBroadcaster serviceMonitor, Executor serviceExecutor, long timeoutDuration, TimeUnit timeoutUnits) {
068            this.kernel = kernel;
069            this.serviceMonitor = serviceMonitor;
070            this.serviceExecutor = serviceExecutor;
071            this.timeoutDuration = timeoutDuration;
072            this.timeoutUnits = timeoutUnits;
073        }
074    
075        /**
076         * Creates a ServiceManager.
077         *
078         * @param serviceId the id of the service
079         * @param serviceName the name of the service
080         * @param serviceFactory the factory for the service
081         * @param classLoader the classloader for the service
082         * @return a new service manager
083         */
084        public ServiceManager createServiceManager(long serviceId, ServiceName serviceName, ServiceFactory serviceFactory) {
085            return new ServiceManager(kernel,
086                    serviceId,
087                    serviceName,
088                    serviceFactory,
089                    new AsyncServiceMonitor(serviceMonitor, serviceExecutor),
090                    timeoutDuration,
091                    timeoutUnits);
092        }
093    }