Author: drobiazko
Date: Mon Jan 12 15:26:42 2009
New Revision: 733964
URL: http://svn.apache.org/viewvc?rev=733964&view=rev
Log:
TAP5-439: Add annotation, @ServiceId, that can be placed on a builder method or
service class to identify the service id to use
Added:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/ServiceId.java
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationModule.java
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationServiceImpl.java
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaMethodAnnotationServiceImpl.java
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/ServiceBinderImpl.java
tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
Added:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/ServiceId.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/ServiceId.java?rev=733964&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/ServiceId.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/annotations/ServiceId.java
Mon Jan 12 15:26:42 2009
@@ -0,0 +1,39 @@
+// Copyright 2006, 2007 2008 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 java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+
+/**
+ * An optional annotation that may be placed on a service building method of a
module, or on the implementation class
+ * (when using service binding via the {...@link
org.apache.tapestry5.ioc.ServiceBinder}). The annotation overrides the default
+ * id for services (the default service id is the simple name of the service
interface).
+ *
+ */
+...@target({ TYPE, METHOD })
+...@retention(RUNTIME)
+...@documented
+public @interface ServiceId
+{
+ /**
+ * An identifier of a service.
+ */
+ String value();
+}
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=733964&r1=733963&r2=733964&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
Mon Jan 12 15:26:42 2009
@@ -277,7 +277,18 @@
*/
private void addServiceDef(final Method method, boolean
modulePreventsServiceDecoration)
{
- String serviceId = stripMethodPrefix(method, BUILD_METHOD_NAME_PREFIX);
+ ServiceId serviceIdAnnotation = method.getAnnotation(ServiceId.class);
+
+ String serviceId;
+
+ if(serviceIdAnnotation != null)
+ {
+ serviceId = serviceIdAnnotation.value();
+ }
+ else
+ {
+ serviceId = stripMethodPrefix(method, BUILD_METHOD_NAME_PREFIX);
+ }
// If the method name was just "build()", then work from the return
type.
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java?rev=733964&r1=733963&r2=733964&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
Mon Jan 12 15:26:42 2009
@@ -19,6 +19,7 @@
import org.apache.tapestry5.ioc.annotations.Marker;
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.def.ServiceDef;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.ioc.internal.util.Defense;
@@ -212,9 +213,20 @@
this.serviceImplementation = serviceImplementation;
// Set defaults for the other properties.
-
+
+
eagerLoad = serviceImplementation.getAnnotation(EagerLoad.class) !=
null;
- serviceId = serviceInterface.getSimpleName();
+
+ ServiceId serviceIdAnnotation =
serviceImplementation.getAnnotation(ServiceId.class);
+
+ if(serviceIdAnnotation != null)
+ {
+ serviceId = serviceIdAnnotation.value();
+ }
+ else
+ {
+ serviceId = serviceInterface.getSimpleName();
+ }
Scope scope = serviceImplementation.getAnnotation(Scope.class);
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt?rev=733964&r1=733963&r2=733964&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt Mon Jan 12
15:26:42 2009
@@ -133,7 +133,17 @@
When using a service builder method, the service id is the <simple name> of
the service interface.
- This can be overridden by adding the service id to the method name, after
"build", for example:
+ This can be overridden by adding the
{{{../apidocs/org/apache/tapestry5/ioc/annotations/ServiceId.html}ServiceId}}
annotation to the service builder method:
+
++---+
+ @ServiceId("FileSystemIndexer")
+ public static Indexer buildIndexer(@InjectService("FileSystem") FileSystem
fileSystem)
+ {
+ . . .
+ }
++----+
+
+ Another option is to add the service id to the method name, after "build",
for example:
+---+
public static Indexer buildFileSystemIndexer(@InjectService("FileSystem")
FileSystem fileSystem)
@@ -144,7 +154,25 @@
Here, the service id is "FileSystemIndexer" not "Indexer".
- For autobuilt services, the service id can be specified when the service is
bound:
+ For autobuilt services, the service id can be specified by placing the
+
{{{../apidocs/org/apache/tapestry5/ioc/annotations/ServiceId.html}ServiceId}}
annotation directly
+ on a service implementation class.
+
++---+
+ @ServiceId("FileSystemIndexer")
+ public class IndexerImpl implements Indexer
+ {
+ ...
+ }
++---+
+
+ When the service is bound, the value of the annotation is used as id:
+
++---+
+ binder.bind(Indexer.class, IndexerImpl.class);
++---+
+
+ This id can be overriden again by calling the method
{{{../apidocs/org/apache/tapestry5/ioc/ServiceBindingOptions.html#withId(java.lang.String)}withId}}
+---+
binder.bind(Indexer.class, IndexerImpl.class).withId("FileSystemIndexer");
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java?rev=733964&r1=733963&r2=733964&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
Mon Jan 12 15:26:42 2009
@@ -102,6 +102,42 @@
}
@Test
+ public void default_service_id_from_method_annotation()
+ {
+ Logger logger = mockLogger();
+
+ replay();
+
+ ModuleDef def = new
DefaultModuleDefImpl(ServiceIdViaAnnotationModule.class, logger, null);
+
+ assertEquals(def.getServiceIds().size(), 2);
+
+ ServiceDef sd = def.getServiceDef("FooService");
+
+ assertEquals(sd.getServiceId(), "FooService");
+
+ verify();
+ }
+
+ @Test
+ public void default_service_id_from_annotation()
+ {
+ Logger logger = mockLogger();
+
+ replay();
+
+ ModuleDef def = new
DefaultModuleDefImpl(ServiceIdViaAnnotationModule.class, logger, null);
+
+ assertEquals(def.getServiceIds().size(), 2);
+
+ ServiceDef sd = def.getServiceDef("BarneyService");
+
+ assertEquals(sd.getServiceId(), "BarneyService");
+
+ verify();
+ }
+
+ @Test
public void default_service_id_from_return_type()
{
Logger logger = mockLogger();
Added:
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationModule.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationModule.java?rev=733964&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationModule.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationModule.java
Mon Jan 12 15:26:42 2009
@@ -0,0 +1,33 @@
+// Copyright 2007 2008 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 org.apache.tapestry5.ioc.ServiceBinder;
+import org.apache.tapestry5.ioc.annotations.ServiceId;
+
+public class ServiceIdViaAnnotationModule
+{
+ @ServiceId("FooService")
+ public static Runnable buildSomething()
+ {
+ return new ServiceIdViaMethodAnnotationServiceImpl();
+ }
+
+ public static void bind(ServiceBinder binder)
+ {
+ binder.bind(Runnable.class, ServiceIdViaAnnotationServiceImpl.class);
+ }
+
+}
Added:
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationServiceImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationServiceImpl.java?rev=733964&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationServiceImpl.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaAnnotationServiceImpl.java
Mon Jan 12 15:26:42 2009
@@ -0,0 +1,27 @@
+// Copyright 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.tapestry5.ioc.internal;
+
+import org.apache.tapestry5.ioc.annotations.ServiceId;
+
+...@serviceid("BarneyService")
+public class ServiceIdViaAnnotationServiceImpl implements Runnable
+{
+
+ public void run()
+ {
+ }
+
+}
Added:
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaMethodAnnotationServiceImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaMethodAnnotationServiceImpl.java?rev=733964&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaMethodAnnotationServiceImpl.java
(added)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceIdViaMethodAnnotationServiceImpl.java
Mon Jan 12 15:26:42 2009
@@ -0,0 +1,25 @@
+// Copyright 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.tapestry5.ioc.internal;
+
+
+public class ServiceIdViaMethodAnnotationServiceImpl implements Runnable
+{
+
+ public void run()
+ {
+ }
+
+}