You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2006/07/07 19:13:11 UTC
svn commit: r419928 - in /tapestry/tapestry5/tapestry-core/trunk/src:
main/java/org/apache/tapestry/internal/ioc/
main/java/org/apache/tapestry/ioc/
main/java/org/apache/tapestry/ioc/annotations/
main/java/org/apache/tapestry/test/ test/java/org/apache...
Author: hlship
Date: Fri Jul 7 10:13:10 2006
New Revision: 419928
URL: http://svn.apache.org/viewvc?rev=419928&view=rev
Log:
Make ServiceCreator part of the public API for Tapestry IoC.
Refact ServiceLifecycle around ServiceCreator.
Add tests for SingletonServiceLifecycle.
Added:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceCreator.java
- copied, changed from r419581, tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceCreator.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycleTest.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/ToStringService.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/UpcaseService.java
Removed:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceCreator.java
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/BasicServiceCreator.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycle.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLifecycle.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Private.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/BasicServiceCreatorTest.java
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/BasicServiceCreator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/BasicServiceCreator.java?rev=419928&r1=419927&r2=419928&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/BasicServiceCreator.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/BasicServiceCreator.java Fri Jul 7 10:13:10 2006
@@ -20,6 +20,7 @@
import org.apache.commons.logging.Log;
import org.apache.tapestry.ioc.ErrorLog;
+import org.apache.tapestry.ioc.ServiceCreator;
import org.apache.tapestry.ioc.ServiceResources;
import org.apache.tapestry.ioc.annotations.InjectService;
import org.apache.tapestry.ioc.def.ServiceDef;
@@ -27,7 +28,7 @@
import static org.apache.tapestry.util.CollectionFactory.newMap;
/**
- * Basic implementation of {@link org.apache.tapestry.internal.ioc.ServiceCreator} that handles
+ * Basic implementation of {@link org.apache.tapestry.ioc.ServiceCreator} that handles
* invoking a method on the module builder, and figures out the correct parameters to pass into the
* annotated method.
*
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycle.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycle.java?rev=419928&r1=419927&r2=419928&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycle.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycle.java Fri Jul 7 10:13:10 2006
@@ -23,9 +23,9 @@
import org.apache.hivemind.service.MethodIterator;
import org.apache.hivemind.service.MethodSignature;
import org.apache.tapestry.ioc.IOCConstants;
+import org.apache.tapestry.ioc.ServiceCreator;
import org.apache.tapestry.ioc.ServiceLifecycle;
import org.apache.tapestry.ioc.ServiceResources;
-import org.apache.tapestry.ioc.def.ServiceDef;
/**
* The basic implementation of a service lifecycle, which provides for a proxy that forces the
@@ -35,16 +35,9 @@
*/
public class SingletonServiceLifecycle implements ServiceLifecycle
{
- public Object createService(ServiceDef serviceDef, Object moduleBuilder,
- ServiceResources resources)
+ public Object createService(ServiceResources resources, ServiceCreator creator)
{
- ClassFactory factory = resources.getService(
- IOCConstants.CLASS_FACTORY_SERVICE_ID,
- ClassFactory.class);
-
- Class proxyClass = createProxyClass(serviceDef, factory);
-
- ServiceCreator creator = new BasicServiceCreator(serviceDef, moduleBuilder, resources);
+ Class proxyClass = createProxyClass(resources);
try
{
@@ -58,12 +51,17 @@
}
}
- private Class createProxyClass(ServiceDef serviceDef, ClassFactory factory)
+ private Class createProxyClass(ServiceResources resources)
{
- String serviceId = serviceDef.getServiceId();
- Class serviceInterface = serviceDef.getServiceInterface();
+ String serviceId = resources.getServiceId();
+ Class serviceInterface = resources.getServiceInterface();
String name = ClassFabUtils.generateClassName(serviceInterface);
+
+ ClassFactory factory = resources.getService(
+ IOCConstants.CLASS_FACTORY_SERVICE_ID,
+ ClassFactory.class);
+
ClassFab cf = factory.newClass(name, Object.class);
addInfrastructure(cf, serviceInterface);
@@ -106,15 +104,15 @@
{
BodyBuilder builder = new BodyBuilder();
builder.begin();
-
+
// We can release the creator after invoking it, we only create the service once.
-
+
builder.addln("if (_delegate == null)");
builder.begin();
builder.addln("_delegate = ({0}) _creator.createService();", serviceInterface.getName());
builder.addln("_creator = null;");
builder.end();
-
+
builder.addln("return _delegate;");
builder.end();
Copied: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceCreator.java (from r419581, tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceCreator.java)
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceCreator.java?p2=tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceCreator.java&p1=tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceCreator.java&r1=419581&r2=419928&rev=419928&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceCreator.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceCreator.java Fri Jul 7 10:13:10 2006
@@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package org.apache.tapestry.internal.ioc;
+package org.apache.tapestry.ioc;
+
+import org.apache.tapestry.internal.ioc.SingletonServiceLifecycle;
/**
* Interface used to encapsulate any strategy used to obtain a service implementation (the real
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLifecycle.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLifecycle.java?rev=419928&r1=419927&r2=419928&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLifecycle.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLifecycle.java Fri Jul 7 10:13:10 2006
@@ -14,8 +14,6 @@
package org.apache.tapestry.ioc;
-import org.apache.tapestry.ioc.def.ServiceDef;
-
/**
* A strategy for creating a service when first referenced. Most implementations create a proxy to
* the service, and defer the creation of the actual service implementation until needed.
@@ -27,16 +25,14 @@
/**
* Creates the service implementation or, more commonly, creates a proxy that will cause the
* service itself to be created as needed.
- * @param serviceDef
- * additional definition of the service
- * @param resources
- * source of additional services or other resources that may be needed when
- * constructing the core service implementation
- * @param builder
- * module builder instance, against which methods may be invoked to create the
- * service
*
+ * @param resources
+ * source of information about the service to be created, and source of additional
+ * services or other resources that may be needed when constructing the core service
+ * implementation
+ * @param creator
+ * object capable of creating the service implementation on demand
* @return the service or equivalent service proxy
*/
- Object createService(ServiceDef serviceDef, Object moduleBuilder, ServiceResources resources);
+ Object createService(ServiceResources resources, ServiceCreator creator);
}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Private.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Private.java?rev=419928&r1=419927&r2=419928&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Private.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Private.java Fri Jul 7 10:13:10 2006
@@ -1,3 +1,17 @@
+// Copyright 2006 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.tapestry.ioc.annotations;
import java.lang.annotation.Documented;
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java?rev=419928&r1=419927&r2=419928&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java Fri Jul 7 10:13:10 2006
@@ -29,6 +29,7 @@
import org.apache.hivemind.Resource;
import org.apache.tapestry.internal.annotations.SuppressNullCheck;
import org.apache.tapestry.ioc.ErrorLog;
+import org.apache.tapestry.ioc.ServiceCreator;
import org.apache.tapestry.ioc.ServiceResources;
import org.apache.tapestry.ioc.def.ServiceDef;
import org.apache.tapestry.model.MutableComponentModel;
@@ -204,6 +205,29 @@
{
resources.getService(serviceInterface);
setReturnValue(service);
+ }
+
+ protected final void trainCreateService(ServiceCreator creator, Object service)
+ {
+ creator.createService();
+ setReturnValue(service);
+ }
+
+ protected final ServiceCreator newServiceCreator()
+ {
+ return newMock(ServiceCreator.class);
+ }
+
+ protected final void trainGetServiceInterface(ServiceResources resources, Class serviceInterface)
+ {
+ resources.getServiceInterface();
+ setReturnValue(serviceInterface);
+ }
+
+ protected final void trainGetServiceId(ServiceResources resources, String serviceId)
+ {
+ resources.getServiceId();
+ setReturnValue(serviceId);
}
}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/BasicServiceCreatorTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/BasicServiceCreatorTest.java?rev=419928&r1=419927&r2=419928&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/BasicServiceCreatorTest.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/BasicServiceCreatorTest.java Fri Jul 7 10:13:10 2006
@@ -20,6 +20,7 @@
import org.apache.commons.logging.Log;
import org.apache.tapestry.internal.test.InternalBaseTestCase;
import org.apache.tapestry.ioc.ErrorLog;
+import org.apache.tapestry.ioc.ServiceCreator;
import org.apache.tapestry.ioc.ServiceResources;
import org.apache.tapestry.ioc.annotations.InjectService;
import org.apache.tapestry.ioc.def.ServiceDef;
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycleTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycleTest.java?rev=419928&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycleTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/SingletonServiceLifecycleTest.java Fri Jul 7 10:13:10 2006
@@ -0,0 +1,131 @@
+// Copyright 2006 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.tapestry.internal.ioc;
+
+import org.apache.hivemind.service.ClassFactory;
+import org.apache.hivemind.service.impl.ClassFactoryImpl;
+import org.apache.tapestry.internal.test.InternalBaseTestCase;
+import org.apache.tapestry.ioc.IOCConstants;
+import org.apache.tapestry.ioc.ServiceCreator;
+import org.apache.tapestry.ioc.ServiceLifecycle;
+import org.apache.tapestry.ioc.ServiceResources;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class SingletonServiceLifecycleTest extends InternalBaseTestCase
+{
+ private static final String UPCASE_SERVICE_ID = "ioc.Upcase";
+
+ private static final String TO_STRING_SERVICE_ID = "ioc.ToString";
+
+ @Test
+ public void basic()
+ {
+ ServiceResources resources = newServiceResources();
+ ServiceCreator creator = newServiceCreator();
+ UpcaseService service = newMock(UpcaseService.class);
+
+ ClassFactory cf = new ClassFactoryImpl();
+
+ trainGetServiceId(resources, UPCASE_SERVICE_ID);
+
+ trainGetServiceInterface(resources, UpcaseService.class);
+
+ trainGetService(resources, IOCConstants.CLASS_FACTORY_SERVICE_ID, ClassFactory.class, cf);
+
+ replay();
+
+ ServiceLifecycle lf = new SingletonServiceLifecycle();
+
+ UpcaseService proxy = (UpcaseService) lf.createService(resources, creator);
+
+ verify();
+
+ assertEquals(
+ proxy.toString(),
+ "<Singleton proxy for ioc.Upcase(org.apache.tapestry.internal.ioc.UpcaseService)>");
+
+ // Now to test when the creator gets invoked
+
+ trainCreateService(creator, service);
+
+ service.upcase("hello");
+ setReturnValue("HELLO");
+
+ replay();
+
+ assertEquals(proxy.upcase("hello"), "HELLO");
+
+ verify();
+
+ // Now test that the creator is not used again
+
+ service.upcase("bye");
+ setReturnValue("BYE");
+
+ replay();
+
+ assertEquals(proxy.upcase("bye"), "BYE");
+
+ verify();
+ }
+
+ @Test
+ public void toStringService()
+ {
+ ServiceResources resources = newServiceResources();
+ ServiceCreator creator = newServiceCreator();
+ ToStringService service = new ToStringService()
+ {
+ @Override
+ public String toString()
+ {
+ return "ToStringService";
+ }
+ };
+
+ ClassFactory cf = new ClassFactoryImpl();
+
+ trainGetServiceId(resources, TO_STRING_SERVICE_ID);
+
+ trainGetServiceInterface(resources, ToStringService.class);
+
+ trainGetService(resources, IOCConstants.CLASS_FACTORY_SERVICE_ID, ClassFactory.class, cf);
+
+ replay();
+
+ ServiceLifecycle lf = new SingletonServiceLifecycle();
+
+ ToStringService proxy = (ToStringService) lf.createService(resources, creator);
+
+ verify();
+
+ trainCreateService(creator, service);
+
+ replay();
+
+ assertEquals(proxy.toString(), "ToStringService");
+
+ // Now test that the creator is not used again
+
+ assertEquals(proxy.toString(), "ToStringService");
+
+ verify();
+ }
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/ToStringService.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/ToStringService.java?rev=419928&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/ToStringService.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/ToStringService.java Fri Jul 7 10:13:10 2006
@@ -0,0 +1,25 @@
+// Copyright 2006 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.tapestry.internal.ioc;
+
+/**
+ * Used by {@link org.apache.tapestry.internal.ioc.SingletonServiceLifecycleTest}.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public interface ToStringService
+{
+ String toString();
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/UpcaseService.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/UpcaseService.java?rev=419928&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/UpcaseService.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/UpcaseService.java Fri Jul 7 10:13:10 2006
@@ -0,0 +1,26 @@
+// Copyright 2006 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.tapestry.internal.ioc;
+
+/**
+ * Used by {@link org.apache.tapestry.internal.ioc.SingletonServiceLifecycleTest}.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public interface UpcaseService
+{
+ /** Returns the uppercase version of an input string. */
+ String upcase(String input);
+}