Search in sources :

Example 1 with TapestryIOCModule

use of org.apache.tapestry5.ioc.modules.TapestryIOCModule in project tapestry-5 by apache.

the class ModuleImpl method create.

/**
 * Creates the service and updates the cache of created services.
 *
 * @param eagerLoadProxies a list into which any eager loaded proxies should be added
 */
private Object create(final ServiceDef3 def, final Collection<EagerLoadServiceProxy> eagerLoadProxies) {
    final String serviceId = def.getServiceId();
    final Logger logger = registry.getServiceLogger(serviceId);
    final Class serviceInterface = def.getServiceInterface();
    final boolean canBeProxied = canBeProxiedPredicate.test(serviceInterface);
    String description = String.format("Creating %s service %s", canBeProxied ? "proxy for" : "non-proxied instance of", serviceId);
    if (logger.isDebugEnabled())
        logger.debug(description);
    final Module module = this;
    Invokable operation = new Invokable() {

        @Override
        public Object invoke() {
            try {
                ServiceBuilderResources resources = new ServiceResourcesImpl(registry, module, def, proxyFactory, logger);
                // Build up a stack of operations that will be needed to realize the service
                // (by the proxy, at a later date).
                ObjectCreator creator = def.createServiceCreator(resources);
                // For non-proxyable services, we immediately create the service implementation
                // and return it. There's no interface to proxy, which throws out the possibility of
                // deferred instantiation, service lifecycles, and decorators.
                ServiceLifecycle2 lifecycle = registry.getServiceLifecycle(def.getServiceScope());
                if (!canBeProxied) {
                    if (lifecycle.requiresProxy())
                        throw new IllegalArgumentException(String.format("Service scope '%s' requires a proxy, but the service does not have a service interface (necessary to create a proxy). Provide a service interface or select a different service scope.", def.getServiceScope()));
                    return creator.createObject();
                }
                creator = new OperationTrackingObjectCreator(registry, String.format("Instantiating service %s implementation via %s", serviceId, creator), creator);
                creator = new LifecycleWrappedServiceCreator(lifecycle, resources, creator);
                // Marked services (or services inside marked modules) are not decorated.
                // TapestryIOCModule prevents decoration of its services. Note that all decorators will decorate
                // around the aspect interceptor, which wraps around the core service implementation.
                boolean allowDecoration = !def.isPreventDecoration();
                if (allowDecoration) {
                    creator = new AdvisorStackBuilder(def, creator, getAspectDecorator(), registry);
                    creator = new InterceptorStackBuilder(def, creator, registry);
                }
                // Add a wrapper that checks for recursion.
                creator = new RecursiveServiceCreationCheckWrapper(def, creator, logger);
                creator = new OperationTrackingObjectCreator(registry, "Realizing service " + serviceId, creator);
                JustInTimeObjectCreator delegate = new JustInTimeObjectCreator(tracker, creator, serviceId);
                Object proxy = createProxy(resources, delegate, def.isPreventDecoration());
                registry.addRegistryShutdownListener(delegate);
                if (def.isEagerLoad() && eagerLoadProxies != null)
                    eagerLoadProxies.add(delegate);
                tracker.setStatus(serviceId, Status.VIRTUAL);
                return proxy;
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new RuntimeException(IOCMessages.errorBuildingService(serviceId, def, ex), ex);
            }
        }
    };
    return registry.invoke(description, operation);
}
Also used : JustInTimeObjectCreator(org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator) Logger(org.slf4j.Logger) ServiceBuilderResources(org.apache.tapestry5.ioc.ServiceBuilderResources) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectStreamException(java.io.ObjectStreamException) JustInTimeObjectCreator(org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator) ServiceLifecycle2(org.apache.tapestry5.ioc.ServiceLifecycle2) Invokable(org.apache.tapestry5.ioc.Invokable)

Aggregations

ObjectStreamException (java.io.ObjectStreamException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Invokable (org.apache.tapestry5.ioc.Invokable)1 ServiceBuilderResources (org.apache.tapestry5.ioc.ServiceBuilderResources)1 ServiceLifecycle2 (org.apache.tapestry5.ioc.ServiceLifecycle2)1 JustInTimeObjectCreator (org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator)1 Logger (org.slf4j.Logger)1