Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added smart lazy actor implementation discovery.
No need to depend on code generation or application hints for fast load times.
- Loading branch information
1 parent
089c740
commit 08eb390
Showing
5 changed files
with
211 additions
and
120 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
actors/core/src/main/java/com/ea/orbit/actors/providers/IActorClassFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ea.orbit.actors.providers; | ||
|
||
import com.ea.orbit.actors.IActor; | ||
import com.ea.orbit.actors.providers.IOrbitProvider; | ||
|
||
public interface IActorClassFinder extends IOrbitProvider | ||
{ | ||
<T extends IActor> Class<? extends T> findActorImplementation(Class<T> iActorInterface); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
actors/stage/src/main/java/com/ea/orbit/actors/runtime/ActorClassFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.ea.orbit.actors.runtime; | ||
|
||
import com.ea.orbit.actors.IActor; | ||
import com.ea.orbit.actors.providers.IActorClassFinder; | ||
import com.ea.orbit.concurrent.Task; | ||
import com.ea.orbit.exception.UncheckedException; | ||
import com.ea.orbit.util.ClassPath; | ||
import com.ea.orbit.util.IOUtils; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
public class ActorClassFinder implements IActorClassFinder | ||
{ | ||
private static final ClassPathSearch search = new ClassPathSearch(IActor.class); | ||
|
||
private ConcurrentHashMap<Class<?>, Class<?>> cache = new ConcurrentHashMap<>(); | ||
private Execution execution; | ||
|
||
public ActorClassFinder(Execution execution) | ||
{ | ||
this.execution = execution; | ||
} | ||
|
||
public ActorClassFinder() | ||
{ | ||
|
||
} | ||
|
||
@Override | ||
public <T extends IActor> Class<? extends T> findActorImplementation(Class<T> iActorInterface) | ||
{ | ||
Class<?> r = cache.get(iActorInterface); | ||
return r != null ? (Class<? extends T>) r : search.findImplementation(iActorInterface); | ||
} | ||
|
||
@Override | ||
public Task<?> start() | ||
{ | ||
if (execution == null) | ||
{ | ||
return Task.done(); | ||
} | ||
try | ||
{ | ||
if (execution.getAutoDiscovery()) | ||
{ | ||
final List<ClassPath.ResourceInfo> actorInterfacesRes = ClassPath.get().getAllResources().stream() | ||
.filter(r -> r.getResourceName().startsWith("META-INF/orbit/actors/interfaces")).collect(Collectors.toList()); | ||
final List<ClassPath.ResourceInfo> actorClassesRes = ClassPath.get().getAllResources().stream() | ||
.filter(r -> r.getResourceName().startsWith("META-INF/orbit/actors/classes")).collect(Collectors.toList()); | ||
|
||
for (ClassPath.ResourceInfo irs : actorInterfacesRes) | ||
{ | ||
// pre register factories | ||
String nameFactoryName = IOUtils.toString(irs.url().openStream()); | ||
ActorFactory<?> factory = (ActorFactory<?>) classForName(nameFactoryName).newInstance(); | ||
execution.registerFactory(factory); | ||
} | ||
|
||
for (ClassPath.ResourceInfo irs : actorClassesRes) | ||
{ | ||
String className = irs.getResourceName().substring("META-INF/orbit/actors/classes".length() + 1); | ||
Class<?> actorClass = classForName(className); | ||
cacheBestInterface(actorClass); | ||
} | ||
} | ||
List<Class<?>> actorClasses = execution.getActorClasses(); | ||
if (actorClasses != null && !actorClasses.isEmpty()) | ||
{ | ||
for (Class<?> actorClass : actorClasses) | ||
{ | ||
cacheBestInterface(actorClass); | ||
} | ||
} | ||
} | ||
|
||
catch (Throwable e) | ||
{ | ||
throw new UncheckedException(e); | ||
} | ||
return Task.done(); | ||
} | ||
|
||
private void cacheBestInterface(Class<?> actorClass) | ||
{ | ||
Class<?> bestInterface = null; | ||
for (Class<?> interfaceClass : actorClass.getInterfaces()) | ||
{ | ||
if (IActor.class.isAssignableFrom(interfaceClass) && interfaceClass != IActor.class) | ||
{ | ||
bestInterface = (bestInterface == null) ? interfaceClass | ||
: bestInterface.isAssignableFrom(interfaceClass) ? interfaceClass | ||
: bestInterface; | ||
} | ||
} | ||
if (bestInterface != null) | ||
{ | ||
cache.put(actorClass, bestInterface); | ||
} | ||
} | ||
|
||
private Class<?> classForName(String className) | ||
{ | ||
try | ||
{ | ||
return Class.forName(className); | ||
} | ||
catch (ClassNotFoundException e) | ||
{ | ||
throw new UncheckedException(e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.