001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.modules;
014
015import org.apache.tapestry5.beanmodel.internal.services.*;
016import org.apache.tapestry5.beanmodel.services.*;
017import org.apache.tapestry5.commons.MappedConfiguration;
018import org.apache.tapestry5.commons.OrderedConfiguration;
019import org.apache.tapestry5.http.internal.services.ResponseCompressionAnalyzerImpl;
020import org.apache.tapestry5.http.services.RequestGlobals;
021import org.apache.tapestry5.http.services.ResponseCompressionAnalyzer;
022import org.apache.tapestry5.internal.pageload.PageLoaderImpl;
023import org.apache.tapestry5.internal.services.AjaxPartialResponseRenderer;
024import org.apache.tapestry5.internal.services.AjaxPartialResponseRendererImpl;
025import org.apache.tapestry5.internal.services.ClientPersistentFieldStorage;
026import org.apache.tapestry5.internal.services.ClientPersistentFieldStorageImpl;
027import org.apache.tapestry5.internal.services.ComponentInstantiatorSource;
028import org.apache.tapestry5.internal.services.ComponentModelSource;
029import org.apache.tapestry5.internal.services.CookieSink;
030import org.apache.tapestry5.internal.services.CookieSource;
031import org.apache.tapestry5.internal.services.EndOfRequestEventHub;
032import org.apache.tapestry5.internal.services.FormControlNameManager;
033import org.apache.tapestry5.internal.services.FormControlNameManagerImpl;
034import org.apache.tapestry5.internal.services.InternalComponentInvalidationEventHub;
035import org.apache.tapestry5.internal.services.InternalRequestGlobals;
036import org.apache.tapestry5.internal.services.InternalRequestGlobalsImpl;
037import org.apache.tapestry5.internal.services.LinkDecorationListener;
038import org.apache.tapestry5.internal.services.LinkSource;
039import org.apache.tapestry5.internal.services.LinkSourceImpl;
040import org.apache.tapestry5.internal.services.LocalizationSetterImpl;
041import org.apache.tapestry5.internal.services.PageContentTypeAnalyzer;
042import org.apache.tapestry5.internal.services.PageContentTypeAnalyzerImpl;
043import org.apache.tapestry5.internal.services.PageElementFactory;
044import org.apache.tapestry5.internal.services.PageElementFactoryImpl;
045import org.apache.tapestry5.internal.services.PageLoader;
046import org.apache.tapestry5.internal.services.PageMarkupRenderer;
047import org.apache.tapestry5.internal.services.PageMarkupRendererImpl;
048import org.apache.tapestry5.internal.services.PageRenderQueue;
049import org.apache.tapestry5.internal.services.PageRenderQueueImpl;
050import org.apache.tapestry5.internal.services.PageResponseRenderer;
051import org.apache.tapestry5.internal.services.PageResponseRendererImpl;
052import org.apache.tapestry5.internal.services.PageSource;
053import org.apache.tapestry5.internal.services.PageSourceImpl;
054import org.apache.tapestry5.internal.services.PersistentFieldManager;
055import org.apache.tapestry5.internal.services.PersistentFieldManagerImpl;
056import org.apache.tapestry5.internal.services.ReloadHelper;
057import org.apache.tapestry5.internal.services.ReloadHelperImpl;
058import org.apache.tapestry5.internal.services.RequestPageCache;
059import org.apache.tapestry5.internal.services.RequestPageCacheImpl;
060import org.apache.tapestry5.internal.services.RequestSecurityManager;
061import org.apache.tapestry5.internal.services.RequestSecurityManagerImpl;
062import org.apache.tapestry5.internal.services.ResourceDigestManager;
063import org.apache.tapestry5.internal.services.ResourceDigestManagerImpl;
064import org.apache.tapestry5.internal.services.ResourceStreamer;
065import org.apache.tapestry5.internal.services.ResourceStreamerImpl;
066import org.apache.tapestry5.internal.services.TemplateParser;
067import org.apache.tapestry5.internal.services.TemplateParserImpl;
068import org.apache.tapestry5.internal.services.UnknownActivationContextHandler;
069import org.apache.tapestry5.internal.services.UnknownActivationContextHandlerImpl;
070import org.apache.tapestry5.internal.services.ajax.AjaxFormUpdateController;
071import org.apache.tapestry5.internal.services.javascript.JavaScriptStackPathConstructor;
072import org.apache.tapestry5.internal.structure.ComponentPageElementResourcesSource;
073import org.apache.tapestry5.internal.structure.ComponentPageElementResourcesSourceImpl;
074import org.apache.tapestry5.ioc.ServiceBinder;
075import org.apache.tapestry5.ioc.annotations.Contribute;
076import org.apache.tapestry5.ioc.annotations.Marker;
077import org.apache.tapestry5.services.ComponentClassResolver;
078import org.apache.tapestry5.services.Core;
079import org.apache.tapestry5.services.LinkCreationListener2;
080import org.apache.tapestry5.services.LocalizationSetter;
081import org.apache.tapestry5.services.transform.ControlledPackageType;
082
083import javax.servlet.http.Cookie;
084import java.util.Map;
085
086/**
087 * {@link org.apache.tapestry5.modules.TapestryModule} has gotten too complicated and it is nice to demarkate public
088 * (and stable) from internal (and volatile).
089 */
090@Marker(Core.class)
091public class InternalModule
092{
093    /**
094     * Bind all the private/internal services of Tapestry.
095     */
096    public static void bind(ServiceBinder binder)
097    {
098        binder.bind(PersistentFieldManager.class, PersistentFieldManagerImpl.class);
099        binder.bind(TemplateParser.class, TemplateParserImpl.class);
100        binder.bind(PageResponseRenderer.class, PageResponseRendererImpl.class);
101        binder.bind(PageMarkupRenderer.class, PageMarkupRendererImpl.class);
102        binder.bind(LinkSource.class, LinkSourceImpl.class);
103        binder.bind(LocalizationSetter.class, LocalizationSetterImpl.class);
104        binder.bind(PageElementFactory.class, PageElementFactoryImpl.class);
105        binder.bind(ResourceStreamer.class, ResourceStreamerImpl.class);
106        binder.bind(ClientPersistentFieldStorage.class, ClientPersistentFieldStorageImpl.class);
107        binder.bind(PageRenderQueue.class, PageRenderQueueImpl.class);
108        binder.bind(AjaxPartialResponseRenderer.class, AjaxPartialResponseRendererImpl.class);
109        binder.bind(PageContentTypeAnalyzer.class, PageContentTypeAnalyzerImpl.class);
110        binder.bind(ComponentPageElementResourcesSource.class, ComponentPageElementResourcesSourceImpl.class);
111        binder.bind(RequestSecurityManager.class, RequestSecurityManagerImpl.class);
112        binder.bind(InternalRequestGlobals.class, InternalRequestGlobalsImpl.class);
113        binder.bind(EndOfRequestEventHub.class);
114        binder.bind(ComponentModelSource.class);
115        binder.bind(JavaScriptStackPathConstructor.class);
116        binder.bind(AjaxFormUpdateController.class);
117        binder.bind(ResourceDigestManager.class, ResourceDigestManagerImpl.class);  // Remove in Tapestry 5.5
118        binder.bind(RequestPageCache.class, RequestPageCacheImpl.class);
119        binder.bind(ComponentInstantiatorSource.class).eagerLoad();
120        binder.bind(InternalComponentInvalidationEventHub.class);
121        binder.bind(PageSource.class, PageSourceImpl.class);
122        binder.bind(PageLoader.class, PageLoaderImpl.class).preventReloading();
123        binder.bind(UnknownActivationContextHandler.class, UnknownActivationContextHandlerImpl.class);
124        binder.bind(ReloadHelper.class, ReloadHelperImpl.class);
125        binder.bind(FormControlNameManager.class, FormControlNameManagerImpl.class);
126
127    }
128
129    public static CookieSource buildCookieSource(final RequestGlobals requestGlobals)
130    {
131        return new CookieSource()
132        {
133
134            public Cookie[] getCookies()
135            {
136                return requestGlobals.getHTTPServletRequest().getCookies();
137            }
138        };
139    }
140
141    public static CookieSink buildCookieSink(final RequestGlobals requestGlobals)
142    {
143        return new CookieSink()
144        {
145
146            public void addCookie(Cookie cookie)
147            {
148                requestGlobals.getHTTPServletResponse().addCookie(cookie);
149            }
150        };
151    }
152
153    /**
154     * Contributes:
155     * <dl>
156     * <dt>LinkDecoration (instance of {@link LinkDecorationListener})</dt>
157     * <dd>Triggers events for notifications about links</dd>
158     * </dl>
159     *
160     * @since 5.2.0
161     */
162    public static void contributeLinkSource(OrderedConfiguration<LinkCreationListener2> configuration)
163    {
164        configuration.addInstance("LinkDecoration", LinkDecorationListener.class);
165    }
166
167    /**
168     * Contributes packages identified by {@link ComponentClassResolver#getControlledPackageMapping()}.
169     *
170     * @since 5.3
171     */
172    @Contribute(ComponentInstantiatorSource.class)
173    public static void configureControlledPackagesFromComponentClassResolver(
174            MappedConfiguration<String, ControlledPackageType> configuration, ComponentClassResolver resolver)
175    {
176        for (Map.Entry<String, ControlledPackageType> entry : resolver.getControlledPackageMapping().entrySet())
177        {
178            configuration.add(entry.getKey(), entry.getValue());
179        }
180    }
181}