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    public void invoke(final ModuleBuilderSource moduleBuilderSource,
042                       final OperationTracker tracker,
043                       final ObjectLocator locator,
044                       final Logger logger)
045    {
046
047        tracker.run(String.format("Invoking startup method %s.", InternalUtils.asString(startupMethod)),
048                new Runnable()
049                {
050                    public void run()
051                    {
052                        Map<Class, Object> resourceMap = CollectionFactory.newMap();
053
054                        resourceMap.put(ObjectLocator.class, locator);
055                        resourceMap.put(Logger.class, logger);
056
057                        InjectionResources injectionResources = new MapInjectionResources(resourceMap);
058
059                        Throwable fail = null;
060
061                        Object moduleInstance = InternalUtils.isStatic(startupMethod) ? null : moduleBuilderSource.getModuleBuilder();
062
063                        try
064                        {
065                            ObjectCreator[] parameters = InternalUtils.calculateParametersForMethod(startupMethod, locator,
066                                    injectionResources, tracker);
067
068                            startupMethod.invoke(moduleInstance, InternalUtils.realizeObjects(parameters));
069                        } catch (InvocationTargetException ex)
070                        {
071                            fail = ex.getTargetException();
072                        } catch (RuntimeException ex)
073                        {
074                            throw ex;
075                        } catch (Exception ex)
076                        {
077                            fail = ex;
078                        }
079
080                        if (fail != null)
081                        {
082                            throw new RuntimeException(fail);
083                        }
084
085                    }
086                });
087    }
088}