001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.atteo.moonshine.services;
015
016
017import java.util.List;
018
019import org.atteo.moonshine.ConfigurationException;
020
021import com.google.inject.Injector;
022import com.google.inject.Module;
023
024/**
025 * Configures and starts {@link Service services}.
026 *
027 * <p>
028 * Each service should implement {@link Service}. This interface defines
029 * {@link Service#configure() configure()} method which allows to register Guice {@link Module},
030 * {@link Service#start() start()} and {@link Service#stop() stop()} methods to execute some logic
031 * upon start and stop of the application. </p>
032 */
033public interface Services extends AutoCloseable {
034
035    public static interface Builder {
036        /**
037         * Adds custom Guice module.
038         */
039        Builder addModule(Module module);
040
041        /**
042         * Sets services configuration.
043         */
044        Builder configuration(Service config);
045
046        /**
047         * Builds {@link Services} based on this builder parameters.
048         */
049        Services build() throws ConfigurationException;
050    }
051
052    public static class Factory {
053        public static Builder builder() {
054            return new ServicesImplementation();
055        }
056    }
057
058    /**
059     * Returns global injector.
060     * @return global injector
061     */
062    Injector getGlobalInjector();
063
064    /**
065     * Returns a mapping from {@link Service} to the list of bindings it registers.
066     *
067     * <p>
068     * This should be used only for debug purposes.
069     * </p>
070     * @return map from service to the list of bindings
071     */
072    List<? extends ServiceInfo> getServiceElements();
073
074    /**
075     * Starts all services.
076     */
077    void start();
078
079    /**
080     * Stops all services.
081     */
082    void stop();
083
084    /**
085     * Stops all services and destroys the injector.
086     */
087    @Override
088    void close();
089}
090