[Jrisk-cvs] SF.net SVN: domination-code:[2520] Domination/src/net/yura
Brought to you by:
yuranet
|
From: <yu...@us...> - 2024-04-21 12:09:40
|
Revision: 2520
http://sourceforge.net/p/domination/code/2520
Author: yuranet
Date: 2024-04-21 12:09:37 +0000 (Sun, 21 Apr 2024)
Log Message:
-----------
load AIs through ServiceLoader also
Modified Paths:
--------------
Domination/src/net/yura/domination/engine/ai/AIManager.java
Domination/src/net/yura/util/Service.java
Modified: Domination/src/net/yura/domination/engine/ai/AIManager.java
===================================================================
--- Domination/src/net/yura/domination/engine/ai/AIManager.java 2024-04-19 10:05:40 UTC (rev 2519)
+++ Domination/src/net/yura/domination/engine/ai/AIManager.java 2024-04-21 12:09:37 UTC (rev 2520)
@@ -28,16 +28,21 @@
try {
// each AIManager has its own instances of AI players so the state does not leak
AI ai = providers.next().newInstance();
- int type = ai.getType();
- if ( ais.get( type ) !=null ) {
- throw new RuntimeException("more then 1 ai with same type");
- }
- ais.put( type , ai );
+ registerAI(ai);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
+
+ try {
+ for (AI ai : java.util.ServiceLoader.load(AI.class)) {
+ registerAI(ai);
+ }
+ }
+ catch (Throwable ex) {
+ Logger.getLogger(AIManager.class.getName()).info("ServiceLoader AIs not loaded " + ex);
+ }
if (ais.isEmpty()) {
Logger.getLogger(AIManager.class.getName()).info("NO AIs FOUND!!!!");
@@ -44,6 +49,14 @@
}
}
+ private void registerAI(AI ai) {
+ int type = ai.getType();
+ if (ais.get(type) != null) {
+ throw new RuntimeException("more then 1 ai with same type: " + type);
+ }
+ ais.put(type, ai);
+ }
+
public void play(Risk risk) {
RiskGame game = risk.getGame();
String output = getOutput(game, game.getCurrentPlayer().getType() );
Modified: Domination/src/net/yura/util/Service.java
===================================================================
--- Domination/src/net/yura/util/Service.java 2024-04-19 10:05:40 UTC (rev 2519)
+++ Domination/src/net/yura/util/Service.java 2024-04-21 12:09:37 UTC (rev 2520)
@@ -10,6 +10,7 @@
* After the Service class from Sun and the Apache project.
* With help from Fr\xE9d\xE9ric Miserey.
*
+ * @see java.util.ServiceLoader
* @credits Fr\xE9d\xE9ric Miserey, Joseph Oettinger
* @author Matthias L. Jugel
* @version $id$
|