Skip to content

Commit

Permalink
refactor AudioFactory creation, add option to override via env var
Browse files Browse the repository at this point in the history
  • Loading branch information
bobjacobsen committed Nov 8, 2019
1 parent 4220c66 commit 1e6a034
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 24 deletions.
6 changes: 6 additions & 0 deletions help/en/html/doc/Technical/JUnit.shtml
Expand Up @@ -189,6 +189,12 @@
<dd>When set true, run some extra tests; usually used during code migration,
where not everything is right yet but you want to be able to include
tests for individual running. </dd>
<dt>jmri.jmrit.audio.DefaultAudioManager.implementation</dt>
<dd>When set, use the specified class for the AudioFactory implementation.
Setting it to <code>jmri.jmrit.audio.NullAudioFactory</code> will
recreate the audio environment on the CI machines, which have no
audio hardware available.
</dd>
<dt>jmri.shutdownmanager</dt>
<dd>When set, use the specified class as the default ShutDownManager. The specified class
must implement the jmri.ShutDownManager interface and have a public default constructor.
Expand Down
88 changes: 64 additions & 24 deletions java/src/jmri/jmrit/audio/DefaultAudioManager.java
Expand Up @@ -126,36 +126,76 @@ public List<String> getSystemNameList(char subType) {
}

/**
* Method used to initialise the manager
* Attempt to create and initialise an AudioFactory, working
* down a preference heirarchy. Result is in activeAudioFactory.
* Uses null implementation to always succeed
*/
private void createFactory() {
// was a specific implementation requested?
// define as jmri.jmrit.audio.NullAudioFactory to get headless CI form in testing
String className = System.getProperty("jmri.jmrit.audio.DefaultAudioManager.implementation");
// if present, determines the active factory class
if (className != null) {
log.debug("Try to initialise {} from property", className);
try {
Class<?> c = Class.forName(className);
if (AudioFactory.class.isAssignableFrom(c)) {
activeAudioFactory = (AudioFactory) c.getConstructor().newInstance();
if (activeAudioFactory.init()) {
// all OK
return;
} else {
log.error("Specified jmri.jmrit.audio.DefaultAudioManager.implementation value {} did not initialize, continuing", className);
}
} else {
log.error("Specified jmri.jmrit.audio.DefaultAudioManager.implementation value {} is not a jmri.AudioFactory subclass, continuing", className);
}
} catch (
ClassNotFoundException |
InstantiationException |
IllegalAccessException |
java.lang.reflect.InvocationTargetException |
NoSuchMethodException |
SecurityException e) {
log.error("Unable to instantiate AudioFactory class {} with default constructor", className);
// and proceed to fallback choices
}
}

// // Try to initialise LWJGL
// log.debug("Try to initialise LWJGLAudioFactory");
// activeAudioFactory = new LWJGLAudioFactory();
// if (activeAudioFactory.init()) return;
//
// // Next try JOAL
log.debug("Try to initialise JoalAudioFactory");
activeAudioFactory = new JoalAudioFactory();
if (activeAudioFactory.init()) return;

// fall-back to JavaSound
log.debug("Try to initialise JavaSoundAudioFactory");
activeAudioFactory = new JavaSoundAudioFactory();
if (activeAudioFactory.init()) return;

// Finally, if JavaSound fails, fall-back to a Null sound system
log.debug("Try to initialise NullAudioFactory");
activeAudioFactory = new NullAudioFactory();
activeAudioFactory.init();
// assumed to succeed.
}

/**
* Method used to initialise the manager and make connections
*/
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
// OK to write to static variables as we only do so if not initialised
@Override
public synchronized void init() {
if (!initialised) {
// // First try to initialise LWJGL
// activeAudioFactory = new LWJGLAudioFactory();
// log.debug("Try to initialise LWJGLAudioFactory");
//
// // If LWJGL fails, fall-back to JOAL
// if (!activeAudioFactory.init()) {
activeAudioFactory = new JoalAudioFactory();
log.debug("Try to initialise JoalAudioFactory");

// If JOAL fails, fall-back to JavaSound
if (!activeAudioFactory.init()) {
activeAudioFactory = new JavaSoundAudioFactory();
log.debug("Try to initialise JavaSoundAudioFactory");

// Finally, if JavaSound fails, fall-back to a Null sound system
if (!activeAudioFactory.init()) {
activeAudioFactory = new NullAudioFactory();
log.debug("Try to initialise NullAudioFactory");
activeAudioFactory.init();
}
}
// }


// create Factory of appropriate type
createFactory();

// Create default Listener and save in map
try {
Audio s = createNewAudio("IAL$", "Default Audio Listener");
Expand Down

0 comments on commit 1e6a034

Please sign in to comment.