001    // Copyright 2007, 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.ioc.services;
016    
017    import org.apache.tapestry5.ioc.AnnotationProvider;
018    import org.apache.tapestry5.ioc.ObjectLocator;
019    import org.apache.tapestry5.ioc.ObjectProvider;
020    import org.apache.tapestry5.ioc.annotations.UsesOrderedConfiguration;
021    
022    /**
023     * A service that acts as a chain-of-command over a number of {@link org.apache.tapestry5.ioc.ObjectProvider}, but
024     * allows for the case where no object may be provided.
025     * <p/>
026     * This service is itself a key part of Tapestry's general injection mechanism; it is used when instantiating a service
027     * implementation instance, invoking module methods (service builder, decorator, or contribution methods), when
028     * {@linkplain ObjectLocator#autobuild(Class) autobuilding} objects of any type.
029     */
030    @UsesOrderedConfiguration(ObjectProvider.class)
031    public interface MasterObjectProvider
032    {
033        /**
034         * Provides an object based on an expression. The process of providing objects occurs within a particular
035         * <em>context</em>, which will typically be a service builder method, service contributor method, or service
036         * decorator method. The locator parameter provides access to the services visible <em>to that context</em>.
037         * <p/>
038         * When the value is required and no {@link ObjectProvider} provided a non-null value, then {@link
039         * ObjectLocator#getService(Class)} is invoked, to provide a uniquely matching service, or throw a failure exception
040         * if no <em>single</em> service can be found.
041         *
042         * @param objectType         the expected object type
043         * @param annotationProvider provides access to annotations (typically, the field or parameter to which an
044         *                           injection-related annotation is attached); annotations on the field or parameter may
045         *                           also be used when resolving the desired object
046         * @param locator            locator for the <em>context</em> in which the provider is being used
047         * @param required           if true (normal case) a value must be provided; if false then it is allowed for no
048         *                           ObjectProvider to provide a value, and this method may return null to indicate the
049         *                           failure
050         * @param <T>
051         * @return the requested object, or null if this object provider can not supply an object
052         * @throws RuntimeException if the expression can not be evaluated, or the type of object identified is not
053         *                          assignable to the type specified by the objectType parameter
054         */
055        <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator, boolean required);
056    }