
org.apache.tapestry.ioc.services.TapestryIOCModule Maven / Gradle / Ivy
// Copyright 2006, 2007 The Apache Software Foundation
//
// Licensed 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.
package org.apache.tapestry.ioc.services;
import org.apache.tapestry.ioc.*;
import static org.apache.tapestry.ioc.IOCConstants.PERTHREAD_SCOPE;
import org.apache.tapestry.ioc.annotations.Marker;
import org.apache.tapestry.ioc.annotations.Value;
import org.apache.tapestry.ioc.internal.services.*;
import org.apache.tapestry.ioc.util.TimePeriod;
import org.apache.tapestry.services.MasterObjectProvider;
import java.io.File;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
* Defines the base set of services for the Tapestry IOC container.
*/
@Marker(Builtin.class)
public final class TapestryIOCModule
{
public static void bind(ServiceBinder binder)
{
binder.bind(LoggingDecorator.class, LoggingDecoratorImpl.class);
binder.bind(ChainBuilder.class, ChainBuilderImpl.class);
binder.bind(PropertyAccess.class, PropertyAccessImpl.class);
binder.bind(StrategyBuilder.class, StrategyBuilderImpl.class);
binder.bind(PropertyShadowBuilder.class, PropertyShadowBuilderImpl.class);
binder.bind(PipelineBuilder.class, PipelineBuilderImpl.class);
binder.bind(DefaultImplementationBuilder.class, DefaultImplementationBuilderImpl.class);
binder.bind(ExceptionTracker.class, ExceptionTrackerImpl.class);
binder.bind(ExceptionAnalyzer.class, ExceptionAnalyzerImpl.class);
binder.bind(TypeCoercer.class, TypeCoercerImpl.class);
binder.bind(ThreadLocale.class, ThreadLocaleImpl.class);
binder.bind(SymbolSource.class, SymbolSourceImpl.class);
binder.bind(SymbolProvider.class, MapSymbolProvider.class).withId("ApplicationDefaults")
.withMarker(ApplicationDefaults.class);
binder.bind(SymbolProvider.class, MapSymbolProvider.class).withId("FactoryDefaults")
.withMarker(FactoryDefaults.class);
binder.bind(Runnable.class, RegistryStartup.class).withId("RegistryStartup");
binder.bind(MasterObjectProvider.class, MasterObjectProviderImpl.class);
}
/**
* Provides access to additional service lifecycles. One lifecycles is built in ("singleton")
* but additional ones are accessed via this service (and its mapped configuration). Only
* proxiable services (those with explicit service interfaces) can be managed in terms of a
* lifecycle.
*/
public static ServiceLifecycleSource build(final Map configuration)
{
return new ServiceLifecycleSource()
{
public ServiceLifecycle get(String lifecycleName)
{
return configuration.get(lifecycleName);
}
};
}
/**
* Contributes the "perthread" scope.
*/
public void contributeServiceLifecycleSource(MappedConfiguration configuration,
ObjectLocator locator)
{
configuration.add(PERTHREAD_SCOPE, locator.autobuild(PerThreadServiceLifecycle.class));
}
/**
* Contributes "DefaultProvider", ordered last, that delegates to
* {@link ObjectLocator#getService(Class)}.
*
* Contributes "Value", which injects values (not services) triggered by the {@link Value}
* annotation.
*/
public static void contributeMasterObjectProvider(OrderedConfiguration configuration,
ObjectLocator locator)
{
configuration.add("Value", locator.autobuild(ValueObjectProvider.class));
configuration.add("Symbol", locator.autobuild(SymbolObjectProvider.class));
}
/**
* Contributes a set of standard type coercions to the {@link TypeCoercer} service:
*
* - Object to String
* - String to Double
* - String to BigDecimal
* - BigDecimal to Double
* - Double to BigDecimal
* - String to BigInteger
* - BigInteger to Long
* - String to Long
* - Long to Byte
* - Long to Short
* - Long to Integer
* - Double to Long
* - Double to Float
* - Float to Double
* - Long to Double
* - String to Boolean ("false" is always false, other non-blank strings are true)
* - Long to Boolean (true if long value is non zero)
* - Null to Boolean (always false)
* - Null to String (still null)
* - Collection to Boolean (false if empty)
* - Object[] to List
* - primitive[] to List
* - Object to List (by wrapping as a singleton list)
* - Null to List (still null)
* - Null to Long (zero)
* - Null to BigDecimal (zero)
* - Null to BigInteger (zero)
* - String to File
* - String to {@link org.apache.tapestry.ioc.util.TimePeriod}
* - {@link org.apache.tapestry.ioc.util.TimePeriod} to Long
*
*
* The coercion of String to Long, BigInteger, Double and BigDecimal causes some minor headaches
* when attempting to add coercions from null to various numeric types: we end up having to have
* many more coercions for the null case to prevent null --> String --> BigInteger. This may
* indicate a weakness in the algorithm, in that coercions through String should be considered
* "weaker" than other coercions. Alternately, coercions from null may need to be handled
* specially. We'll see if we tweak the algorithm in the future.
*/
@SuppressWarnings("unchecked")
public static void contributeTypeCoercer(Configuration configuration)
{
add(configuration, Object.class, String.class, new Coercion
© 2015 - 2025 Weber Informatics LLC | Privacy Policy