    001// Copyright 2014 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
    015package org.apache.tapestry5.ioc.internal;
    016
    017import org.apache.tapestry5.ioc.ModuleBuilderSource;
    018import org.apache.tapestry5.ioc.ObjectCreator;
    019import org.apache.tapestry5.ioc.ObjectLocator;
    020import org.apache.tapestry5.ioc.OperationTracker;
    021import org.apache.tapestry5.ioc.def.StartupDef;
    022import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    023import org.apache.tapestry5.ioc.internal.util.InjectionResources;
    024import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    025import org.apache.tapestry5.ioc.internal.util.MapInjectionResources;
    026import org.slf4j.Logger;
    027
    028import java.lang.reflect.InvocationTargetException;
    029import java.lang.reflect.Method;
    030import java.util.Map;
    031
    032public class StartupDefImpl implements StartupDef
    033{
    034    private final Method startupMethod;
    035
    036    public StartupDefImpl(Method contributorMethod)
    037    {
    038        this.startupMethod = contributorMethod;
    039    }
    040
    041    @Override
    042    public void invoke(final ModuleBuilderSource moduleBuilderSource,
    043                       final OperationTracker tracker,
    044                       final ObjectLocator locator,
    045                       final Logger logger)
    046    {
    047
    048        tracker.run(String.format("Invoking startup method %s.", InternalUtils.asString(startupMethod)),
    049                new Runnable()
    050                {
    051                    @Override
    052                    public void run()
    053                    {
    054                        Map<Class, Object> resourceMap = CollectionFactory.newMap();
    055
    056                        resourceMap.put(ObjectLocator.class, locator);
    057                        resourceMap.put(Logger.class, logger);
    058
    059                        InjectionResources injectionResources = new MapInjectionResources(resourceMap);
    060
    061                        Throwable fail = null;
    062
    063                        Object moduleInstance = InternalUtils.isStatic(startupMethod) ? null : moduleBuilderSource.getModuleBuilder();
    064
    065                        try
    066                        {
    067                            ObjectCreator[] parameters = InternalUtils.calculateParametersForMethod(startupMethod, locator,
    068                                    injectionResources, tracker);
    069
    070                            startupMethod.invoke(moduleInstance, InternalUtils.realizeObjects(parameters));
    071                        } catch (InvocationTargetException ex)
    072                        {
    073                            fail = ex.getTargetException();
    074                        } catch (RuntimeException ex)
    075                        {
    076                            throw ex;
    077                        } catch (Exception ex)
    078                        {
    079                            fail = ex;
    080                        }
    081
    082                        if (fail != null)
    083                        {
    084                            throw new RuntimeException(fail);
    085                        }
    086
    087                    }
    088                });
    089    }
    090}