Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a non proxied object is beeing eager loaded registry has to inst… #35

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ private static boolean callIsSealed(final java.lang.invoke.MethodHandle isSealed
*/
private final Map<String, Object> services = CollectionFactory.newCaseInsensitiveMap();


/**
* EagerLoadProxies collection into which proxies for eager loaded services are added. Guarded by BARRIER
*/
private final Collection<EagerLoadServiceProxy> eagerLoadProxies = CollectionFactory.newList();

private final Map<String, ServiceDef3> serviceDefs = CollectionFactory.newCaseInsensitiveMap();

/**
Expand Down Expand Up @@ -161,7 +167,7 @@ public <T> T getService(String serviceId, Class<T> serviceInterface)
// RegistryImpl should already have checked that the service exists.
assert def != null;

Object service = findOrCreate(def, null);
Object service = findOrCreate(def);

try
{
Expand Down Expand Up @@ -225,10 +231,9 @@ public Collection<String> findServiceIdsForInterface(Class serviceInterface)
* Locates the service proxy for a particular service (from the service definition).
*
* @param def defines the service
* @param eagerLoadProxies collection into which proxies for eager loaded services are added (or null)
* @return the service proxy
*/
private Object findOrCreate(final ServiceDef3 def, final Collection<EagerLoadServiceProxy> eagerLoadProxies)
private Object findOrCreate(final ServiceDef3 def)
{
final String key = def.getServiceId();

Expand All @@ -247,7 +252,7 @@ public Object invoke()

if (result == null)
{
result = create(def, eagerLoadProxies);
result = create(def);

services.put(key, result);
}
Expand Down Expand Up @@ -284,8 +289,16 @@ public void run()
for (ServiceDef3 def : serviceDefs.values())
{
if (def.isEagerLoad())
findOrCreate(def, proxies);
findOrCreate(def);
}

BARRIER.withWrite(new Runnable() {
@Override
public void run() {
proxies.addAll(eagerLoadProxies);
eagerLoadProxies.clear();
}
});
}
};

Expand All @@ -294,10 +307,8 @@ public void run()

/**
* Creates the service and updates the cache of created services.
*
* @param eagerLoadProxies a list into which any eager loaded proxies should be added
*/
private Object create(final ServiceDef3 def, final Collection<EagerLoadServiceProxy> eagerLoadProxies)
private Object create(final ServiceDef3 def)
{
final String serviceId = def.getServiceId();

Expand Down Expand Up @@ -376,11 +387,7 @@ public Object invoke()

registry.addRegistryShutdownListener(delegate);

// Occasionally eager load service A may invoke service B from its service builder method; if
// service B is eager loaded, we'll hit this method but eagerLoadProxies will be null. That's OK
// ... service B is being realized anyway.

if (def.isEagerLoad() && eagerLoadProxies != null)
if (def.isEagerLoad())
eagerLoadProxies.add(delegate);

tracker.setStatus(serviceId, Status.VIRTUAL);
Expand Down
8 changes: 6 additions & 2 deletions tapestry-ioc/src/test/groovy/ioc/specs/EagerLoadSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class EagerLoadSpec extends AbstractRegistrySpecification {
def "proxied service does eager load"() {
expect:

EagerProxyReloadModule.eagerLoadServiceDidLoad == false
EagerProxyReloadModule.eagerLoadService1DidLoad == false
EagerProxyReloadModule.nonProxyEagerLoadServiceDidLoad == false
EagerProxyReloadModule.eagerLoadService2DidLoad == false

when:

Expand All @@ -17,6 +19,8 @@ class EagerLoadSpec extends AbstractRegistrySpecification {

then:

EagerProxyReloadModule.eagerLoadServiceDidLoad == true
EagerProxyReloadModule.eagerLoadService1DidLoad == true
EagerProxyReloadModule.nonProxyEagerLoadServiceDidLoad == true
EagerProxyReloadModule.eagerLoadService2DidLoad == true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package org.apache.tapestry5.ioc.test;

public interface EagerLoadService
public interface EagerLoadService1
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import org.apache.tapestry5.ioc.annotations.EagerLoad;

@EagerLoad
public class EagerLoadServiceImpl implements EagerLoadService
public class EagerLoadService1Impl implements EagerLoadService1
{
public EagerLoadServiceImpl()
public EagerLoadService1Impl()
{
EagerProxyReloadModule.eagerLoadServiceDidLoad = true;
EagerProxyReloadModule.eagerLoadService1DidLoad = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.test;

public interface EagerLoadService2
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2010, 2012 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.test;


public class EagerLoadService2Impl implements EagerLoadService2
{
public EagerLoadService2Impl()
{
EagerProxyReloadModule.eagerLoadService2DidLoad = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

import org.apache.tapestry5.ioc.ServiceBinder;

//@ImportModule(EagerProxy2ReloadModule.class)
public class EagerProxyReloadModule
{
public static boolean eagerLoadServiceDidLoad;
public static boolean eagerLoadService1DidLoad;
public static boolean nonProxyEagerLoadServiceDidLoad;
public static boolean eagerLoadService2DidLoad;

public static void bind(ServiceBinder binder)
{
binder.bind(EagerLoadService.class, EagerLoadServiceImpl.class);
binder.bind(EagerLoadService1.class, EagerLoadService1Impl.class);
binder.bind(NonProxiedEagerLoadService.class).eagerLoad();
binder.bind(EagerLoadService2.class, EagerLoadService2Impl.class).eagerLoad();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.apache.tapestry5.ioc.test;

public class NonProxiedEagerLoadService {
public NonProxiedEagerLoadService(EagerLoadService2 service2) {
EagerProxyReloadModule.nonProxyEagerLoadServiceDidLoad = true;
}
}