Author: drobiazko Date: Sat Jun 12 19:42:58 2010 New Revision: 954122 URL: http://svn.apache.org/viewvc?rev=954122&view=rev Log: TAP5-104: Add support for startup() methods in modules, as an easy way to add startup logic
Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/Startup.java (with props) tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/ModuleDef3.java (with props) tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/StartupDef.java (with props) tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/StartupDefImpl.java (with props) tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/StartupModule2.java (with props) Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/Module.java tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ModuleImpl.java tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/startup.apt tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/Startup.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/Startup.java?rev=954122&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/Startup.java (added) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/Startup.java Sat Jun 12 19:42:58 2010 @@ -0,0 +1,37 @@ +// Copyright 2010 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.tapestry5.ioc.annotations; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * An annotation that may be placed on a startup method of a module. A startup method is an simple way + * to provide extra logic to be executed at {...@link org.apache.tapestry5.ioc.Registry#performRegistryStartup()}. + * Instead of making contributions to the <i>RegistryStartup</i> service configuration you can provide startup + * methods inside your modules. + * + * @since 5.2.0 + * + */ +...@target(METHOD) +...@retention(RUNTIME) +...@documented +public @interface Startup +{ +} Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/Startup.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/Startup.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/ModuleDef3.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/ModuleDef3.java?rev=954122&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/ModuleDef3.java (added) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/ModuleDef3.java Sat Jun 12 19:42:58 2010 @@ -0,0 +1,30 @@ +// Copyright 2010 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.tapestry5.ioc.def; + +import java.util.Set; + +/** + * Extended version of {...@link org.apache.tapestry5.ioc.def.ModuleDef2} introduced in Tapestry 5.2 to allow + * the definition of {...@link org.apache.tapestry5.ioc.def.StartupDef}s. + * + * @since 5.2.0 + */ +public interface ModuleDef3 extends ModuleDef2 +{ + /** + * Returns all the {...@link org.apache.tapestry5.ioc.def.StartupDef}s. + */ + Set<StartupDef> getStartupDefs(); +} Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/ModuleDef3.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/ModuleDef3.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/StartupDef.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/StartupDef.java?rev=954122&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/StartupDef.java (added) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/StartupDef.java Sat Jun 12 19:42:58 2010 @@ -0,0 +1,39 @@ +// Copyright 2010 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.tapestry5.ioc.def; + +import org.apache.tapestry5.ioc.ModuleBuilderSource; +import org.apache.tapestry5.ioc.ObjectLocator; +import org.apache.tapestry5.ioc.OperationTracker; +import org.apache.tapestry5.ioc.Registry; +import org.slf4j.Logger; + +/** + * Defines an execution of a series of operations, automatically invoked at {...@linkplain Registry#performRegistryStartup() registry + * startup} + * + * @since 5.2.0 + */ +public interface StartupDef +{ + /** + * Executes a series of operations at registry startup. + * + * @param moduleSource provides access to the the module instance associated with the module containing the startup + * @param locator provides access to services defined within a {...@link org.apache.tapestry5.ioc.Registry} + * @param tracker used to track some set of operations + * @param logger logger + */ + void startup(ModuleBuilderSource moduleSource, ObjectLocator locator, OperationTracker tracker, Logger logger); +} Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/StartupDef.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/def/StartupDef.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java?rev=954122&r1=954121&r2=954122&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java Sat Jun 12 19:42:58 2010 @@ -14,29 +14,52 @@ package org.apache.tapestry5.ioc.internal; -import org.apache.tapestry5.ioc.*; -import org.apache.tapestry5.ioc.annotations.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.tapestry5.ioc.AdvisorDef; +import org.apache.tapestry5.ioc.Configuration; +import org.apache.tapestry5.ioc.MappedConfiguration; +import org.apache.tapestry5.ioc.MethodAdviceReceiver; +import org.apache.tapestry5.ioc.ObjectCreator; +import org.apache.tapestry5.ioc.OrderedConfiguration; +import org.apache.tapestry5.ioc.ScopeConstants; +import org.apache.tapestry5.ioc.ServiceBinder; +import org.apache.tapestry5.ioc.ServiceBuilderResources; +import org.apache.tapestry5.ioc.annotations.Contribute; +import org.apache.tapestry5.ioc.annotations.EagerLoad; +import org.apache.tapestry5.ioc.annotations.Marker; +import org.apache.tapestry5.ioc.annotations.Match; +import org.apache.tapestry5.ioc.annotations.Order; +import org.apache.tapestry5.ioc.annotations.PreventServiceDecoration; +import org.apache.tapestry5.ioc.annotations.Scope; +import org.apache.tapestry5.ioc.annotations.ServiceId; +import org.apache.tapestry5.ioc.annotations.Startup; import org.apache.tapestry5.ioc.def.ContributionDef; import org.apache.tapestry5.ioc.def.ContributionDef2; import org.apache.tapestry5.ioc.def.DecoratorDef; -import org.apache.tapestry5.ioc.def.ModuleDef2; +import org.apache.tapestry5.ioc.def.ModuleDef3; import org.apache.tapestry5.ioc.def.ServiceDef; +import org.apache.tapestry5.ioc.def.StartupDef; import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.ioc.internal.util.InternalUtils; import org.apache.tapestry5.ioc.services.ClassFactory; import org.slf4j.Logger; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.*; - /** * Starting from the Class for a module, identifies all the services (service builder methods), * decorators (service * decorator methods) and (not yet implemented) contributions (service contributor methods). */ -public class DefaultModuleDefImpl implements ModuleDef2, ServiceDefAccumulator +public class DefaultModuleDefImpl implements ModuleDef3, ServiceDefAccumulator { /** * The prefix used to identify service builder methods. @@ -78,6 +101,8 @@ public class DefaultModuleDefImpl implem private final Map<String, AdvisorDef> advisorDefs = CollectionFactory.newCaseInsensitiveMap(); private final Set<ContributionDef> contributionDefs = CollectionFactory.newSet(); + + private final Set<StartupDef> startupDefs = CollectionFactory.newSet(); private final Set<Class> defaultMarkers = CollectionFactory.newSet(); @@ -232,9 +257,21 @@ public class DefaultModuleDefImpl implem remainingMethods.remove(m); continue; } + + if (m.isAnnotationPresent(Startup.class)) + { + addStartupDef(m); + remainingMethods.remove(m); + continue; + } } } + private void addStartupDef(Method method) + { + startupDefs.add(new StartupDefImpl(method)); + } + private void addContributionDef(Method method) { Contribute annotation = method.getAnnotation(Contribute.class); @@ -477,6 +514,11 @@ public class DefaultModuleDefImpl implem { return contributionDefs; } + + public Set<StartupDef> getStartupDefs() + { + return startupDefs; + } public String getLoggerName() { Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/Module.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/Module.java?rev=954122&r1=954121&r2=954122&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/Module.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/Module.java Sat Jun 12 19:42:58 2010 @@ -23,6 +23,7 @@ import org.apache.tapestry5.ioc.def.Cont import org.apache.tapestry5.ioc.def.DecoratorDef; import org.apache.tapestry5.ioc.def.ServiceDef; import org.apache.tapestry5.ioc.def.ServiceDef2; +import org.apache.tapestry5.ioc.def.StartupDef; /** * A module within the Tapestry IoC registry. Each Module is constructed around a corresponding module builder instance; @@ -97,4 +98,9 @@ public interface Module extends ModuleBu * @return module logger name */ String getLoggerName(); + + /** + * Returns all the {...@link StartupDef}s defined in the module. + */ + Set<StartupDef> getStartupDefs(); } Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ModuleImpl.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ModuleImpl.java?rev=954122&r1=954121&r2=954122&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ModuleImpl.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ModuleImpl.java Sat Jun 12 19:42:58 2010 @@ -40,9 +40,10 @@ import org.apache.tapestry5.ioc.def.Cont import org.apache.tapestry5.ioc.def.ContributionDef2; import org.apache.tapestry5.ioc.def.DecoratorDef; import org.apache.tapestry5.ioc.def.ModuleDef; -import org.apache.tapestry5.ioc.def.ModuleDef2; +import org.apache.tapestry5.ioc.def.ModuleDef3; import org.apache.tapestry5.ioc.def.ServiceDef; import org.apache.tapestry5.ioc.def.ServiceDef2; +import org.apache.tapestry5.ioc.def.StartupDef; import org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator; import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.ioc.internal.util.ConcurrentBarrier; @@ -63,7 +64,7 @@ public class ModuleImpl implements Modul private final ServiceActivityTracker tracker; - private final ModuleDef2 moduleDef; + private final ModuleDef3 moduleDef; private final ClassFactory classFactory; @@ -97,7 +98,7 @@ public class ModuleImpl implements Modul { this.registry = registry; this.tracker = tracker; - this.moduleDef = InternalUtils.toModuleDef2(moduleDef); + this.moduleDef = InternalUtils.toModuleDef3(moduleDef); this.classFactory = classFactory; this.logger = logger; @@ -570,6 +571,11 @@ public class ModuleImpl implements Modul { return moduleDef.getLoggerName(); } + + public Set<StartupDef> getStartupDefs() + { + return moduleDef.getStartupDefs(); + } @Override public String toString() Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java?rev=954122&r1=954121&r2=954122&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java Sat Jun 12 19:42:58 2010 @@ -30,8 +30,10 @@ import org.apache.tapestry5.ioc.def.Deco import org.apache.tapestry5.ioc.def.ModuleDef; import org.apache.tapestry5.ioc.def.ServiceDef; import org.apache.tapestry5.ioc.def.ServiceDef2; +import org.apache.tapestry5.ioc.def.StartupDef; import org.apache.tapestry5.ioc.internal.services.PerthreadManagerImpl; import org.apache.tapestry5.ioc.internal.services.RegistryShutdownHubImpl; +import org.apache.tapestry5.ioc.internal.services.ServiceMessages; import org.apache.tapestry5.ioc.internal.util.CollectionFactory; import org.apache.tapestry5.ioc.internal.util.Defense; import org.apache.tapestry5.ioc.internal.util.InjectionResources; @@ -281,9 +283,32 @@ public class RegistryImpl implements Reg proxy.eagerLoadService(); getService("RegistryStartup", Runnable.class).run(); + + invokeStartups(); cleanupThread(); } + + private void invokeStartups() + { + for (Module m : moduleToServiceDefs.keySet()) + { + Logger logger = this.loggerSource.getLogger(m.getLoggerName()); + + for (StartupDef sd : m.getStartupDefs()) + { + try + { + sd.startup(m, this, this, logger); + } + catch(RuntimeException e) + { + logger.error(ServiceMessages.startupFailure(e)); + } + } + + } + } public Logger getServiceLogger(String serviceId) { Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/StartupDefImpl.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/StartupDefImpl.java?rev=954122&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/StartupDefImpl.java (added) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/StartupDefImpl.java Sat Jun 12 19:42:58 2010 @@ -0,0 +1,81 @@ +// Copyright 2010 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.tapestry5.ioc.internal; + +import static java.lang.String.format; +import static org.apache.tapestry5.ioc.internal.util.InternalUtils.asString; +import static org.apache.tapestry5.ioc.internal.util.InternalUtils.toMessage; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + +import org.apache.tapestry5.ioc.ModuleBuilderSource; +import org.apache.tapestry5.ioc.ObjectLocator; +import org.apache.tapestry5.ioc.OperationTracker; +import org.apache.tapestry5.ioc.def.StartupDef; +import org.apache.tapestry5.ioc.internal.util.CollectionFactory; +import org.apache.tapestry5.ioc.internal.util.InjectionResources; +import org.apache.tapestry5.ioc.internal.util.InternalUtils; +import org.apache.tapestry5.ioc.internal.util.MapInjectionResources; +import org.slf4j.Logger; + + +public class StartupDefImpl implements StartupDef +{ + private final Method startupMethod; + + public StartupDefImpl(Method startupMethod) + { + this.startupMethod = startupMethod; + } + + public void startup(ModuleBuilderSource moduleSource, ObjectLocator locator, OperationTracker tracker, Logger logger) + { + Map<Class, Object> resourceMap = CollectionFactory.newMap(); + + resourceMap.put(ObjectLocator.class, locator); + resourceMap.put(Logger.class, logger); + + InjectionResources injectionResources = new MapInjectionResources(resourceMap); + + Throwable fail = null; + + Object moduleInstance = InternalUtils.isStatic(startupMethod) ? null : moduleSource + .getModuleBuilder(); + + try + { + Object[] parameters = InternalUtils.calculateParametersForMethod( + startupMethod, + locator, + injectionResources, tracker); + + startupMethod.invoke(moduleInstance, parameters); + } + catch (InvocationTargetException ex) + { + fail = ex.getTargetException(); + } + catch (Exception ex) + { + fail = ex; + } + + if (fail != null) + throw new RuntimeException( + format("Error invoking startup method %s: %s", asString(startupMethod), toMessage(fail)), fail); + } + +} Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/StartupDefImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/StartupDefImpl.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java?rev=954122&r1=954121&r2=954122&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java Sat Jun 12 19:42:58 2010 @@ -29,13 +29,37 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.net.URL; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.tapestry5.func.Mapper; import org.apache.tapestry5.func.Predicate; -import org.apache.tapestry5.ioc.*; +import org.apache.tapestry5.ioc.AdvisorDef; +import org.apache.tapestry5.ioc.AnnotationProvider; +import org.apache.tapestry5.ioc.Configuration; +import org.apache.tapestry5.ioc.Invokable; +import org.apache.tapestry5.ioc.Locatable; +import org.apache.tapestry5.ioc.Location; +import org.apache.tapestry5.ioc.MappedConfiguration; +import org.apache.tapestry5.ioc.ModuleBuilderSource; +import org.apache.tapestry5.ioc.ObjectCreator; +import org.apache.tapestry5.ioc.ObjectLocator; +import org.apache.tapestry5.ioc.OperationTracker; +import org.apache.tapestry5.ioc.OrderedConfiguration; +import org.apache.tapestry5.ioc.ServiceBuilderResources; +import org.apache.tapestry5.ioc.ServiceLifecycle; +import org.apache.tapestry5.ioc.ServiceLifecycle2; +import org.apache.tapestry5.ioc.ServiceResources; import org.apache.tapestry5.ioc.annotations.Inject; import org.apache.tapestry5.ioc.annotations.InjectResource; import org.apache.tapestry5.ioc.annotations.InjectService; @@ -44,9 +68,10 @@ import org.apache.tapestry5.ioc.def.Cont import org.apache.tapestry5.ioc.def.ContributionDef2; import org.apache.tapestry5.ioc.def.DecoratorDef; import org.apache.tapestry5.ioc.def.ModuleDef; -import org.apache.tapestry5.ioc.def.ModuleDef2; +import org.apache.tapestry5.ioc.def.ModuleDef3; import org.apache.tapestry5.ioc.def.ServiceDef; import org.apache.tapestry5.ioc.def.ServiceDef2; +import org.apache.tapestry5.ioc.def.StartupDef; import org.apache.tapestry5.ioc.services.ClassFabUtils; import org.apache.tapestry5.ioc.services.ClassFactory; import org.apache.tapestry5.ioc.services.Coercion; @@ -846,12 +871,12 @@ public class InternalUtils }; } - public static ModuleDef2 toModuleDef2(final ModuleDef md) + public static ModuleDef3 toModuleDef3(final ModuleDef md) { - if (md instanceof ModuleDef2) - return (ModuleDef2) md; + if (md instanceof ModuleDef3) + return (ModuleDef3) md; - return new ModuleDef2() + return new ModuleDef3() { public Set<AdvisorDef> getAdvisorDefs() { @@ -887,6 +912,11 @@ public class InternalUtils { return md.getServiceIds(); } + + public Set<StartupDef> getStartupDefs() + { + return Collections.emptySet(); + } }; } Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/startup.apt URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/startup.apt?rev=954122&r1=954121&r2=954122&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/startup.apt (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/startup.apt Sat Jun 12 19:42:58 2010 @@ -25,4 +25,25 @@ public class MyModule } ---- - Generally, these contributions are in the form of inner classes; if they were services, they could just be eagerly loaded. \ No newline at end of file + Generally, these contributions are in the form of inner classes; if they were services, they could just be eagerly loaded. + +Startup Methods + + In Tapestry 5.2 the way of providing extra logic to be executed at Registry startup has been simplified. + Instead of making contributions to the RegistryStartup service configuration you can provide startup methods inside your modules. + A startup method is a static or instance method of a module annotated with {{{../apidocs/org/apache/tapestry5/ioc/annotations/Startup.html}Startup}} + annotation. Each module is allowed to contain several startup methods. + +---- +public class MyModule +{ + + @Startup + public static void initMyApplication(Logger logger, MyService service) + { + logger.info("Starting up..."); + + service.init(); + } +} +---- \ No newline at end of file Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java?rev=954122&r1=954121&r2=954122&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java (original) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java Sat Jun 12 19:42:58 2010 @@ -31,6 +31,7 @@ import org.apache.tapestry5.ioc.internal import org.apache.tapestry5.ioc.internal.IOCInternalTestCase; import org.apache.tapestry5.ioc.internal.PrivateConstructorModule; import org.apache.tapestry5.ioc.internal.UpcaseService; +import org.apache.tapestry5.ioc.internal.services.StartupModule2; import org.apache.tapestry5.ioc.services.Builtin; import org.apache.tapestry5.ioc.services.ServiceActivity; import org.apache.tapestry5.ioc.services.ServiceActivityScoreboard; @@ -1433,6 +1434,8 @@ public class IntegrationTest extends IOC NameListHolder2 holder = r.getService("ServiceWithEmptyConfiguration", NameListHolder2.class); assertEquals(holder.getNames(), Arrays.asList()); + + r.shutdown(); } @@ -1489,4 +1492,23 @@ public class IntegrationTest extends IOC r.shutdown(); } + + @Test + public void startup_inside_module() + { + Registry r = buildRegistry(StartupModule2.class); + + assertFalse(StartupModule2.staticStartupInvoked); + + assertFalse(StartupModule2.instanceStartupInvoked); + + r.performRegistryStartup(); + + assertTrue(StartupModule2.staticStartupInvoked); + + assertTrue(StartupModule2.instanceStartupInvoked); + + r.shutdown(); + + } } Added: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/StartupModule2.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/StartupModule2.java?rev=954122&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/StartupModule2.java (added) +++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/StartupModule2.java Sat Jun 12 19:42:58 2010 @@ -0,0 +1,39 @@ +// Copyright 2010 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.tapestry5.ioc.internal.services; + +import org.apache.tapestry5.ioc.ObjectLocator; +import org.apache.tapestry5.ioc.annotations.Startup; +import org.slf4j.Logger; + +public class StartupModule2 +{ + public static boolean staticStartupInvoked; + public static boolean instanceStartupInvoked; + + @Startup + public static void foo(ObjectLocator locator) + { + staticStartupInvoked = true; + } + + @Startup + public void bar(ObjectLocator locator, Logger logger) + { + instanceStartupInvoked = true; + + logger.info("StartupModule2.bar invoked"); + } +} Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/StartupModule2.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/StartupModule2.java ------------------------------------------------------------------------------ svn:mime-type = text/plain