Skip to content

Commit

Permalink
Using ServiceLoader in FormatFactory, formats is now non-static (and …
Browse files Browse the repository at this point in the history
…thread safe)

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Jul 31, 2013
1 parent 18d5e2a commit ffd62d9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/META-INF/ioformats.datafiles
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
META-INF/services/org.openscience.cdk.io.formats.IChemFormatMatcher
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
org.openscience.cdk.io.formats.ABINITFormat
org.openscience.cdk.io.formats.Aces2Format
org.openscience.cdk.io.formats.ADFFormat
org.openscience.cdk.io.formats.CACheFormat
org.openscience.cdk.io.formats.CDKOWLFormat
org.openscience.cdk.io.formats.CIFFormat
org.openscience.cdk.io.formats.CMLFormat
org.openscience.cdk.io.formats.CrystClustFormat
org.openscience.cdk.io.formats.CTXFormat
org.openscience.cdk.io.formats.DaltonFormat
org.openscience.cdk.io.formats.GamessFormat
org.openscience.cdk.io.formats.Gaussian03Format
org.openscience.cdk.io.formats.Gaussian90Format
org.openscience.cdk.io.formats.Gaussian92Format
org.openscience.cdk.io.formats.Gaussian94Format
org.openscience.cdk.io.formats.Gaussian95Format
org.openscience.cdk.io.formats.Gaussian98Format
org.openscience.cdk.io.formats.GhemicalMMFormat
org.openscience.cdk.io.formats.GhemicalSPMFormat
org.openscience.cdk.io.formats.HINFormat
org.openscience.cdk.io.formats.INChIFormat
org.openscience.cdk.io.formats.INChIPlainTextFormat
org.openscience.cdk.io.formats.JaguarFormat
org.openscience.cdk.io.formats.MDLFormat
org.openscience.cdk.io.formats.MDLRXNFormat
org.openscience.cdk.io.formats.MDLRXNV3000Format
org.openscience.cdk.io.formats.MDLV2000Format
org.openscience.cdk.io.formats.MDLV3000Format
org.openscience.cdk.io.formats.Mol2Format
org.openscience.cdk.io.formats.MOPAC2002Format
org.openscience.cdk.io.formats.MOPAC7Format
org.openscience.cdk.io.formats.MOPAC7InputFormat
org.openscience.cdk.io.formats.MOPAC93Format
org.openscience.cdk.io.formats.MOPAC97Format
org.openscience.cdk.io.formats.MoSSOutputFormat
org.openscience.cdk.io.formats.NWChemFormat
org.openscience.cdk.io.formats.PDBFormat
org.openscience.cdk.io.formats.PMPFormat
org.openscience.cdk.io.formats.PubChemASNFormat
org.openscience.cdk.io.formats.PubChemCompoundsXMLFormat
org.openscience.cdk.io.formats.PubChemCompoundXMLFormat
org.openscience.cdk.io.formats.PubChemSubstancesASNFormat
org.openscience.cdk.io.formats.PubChemSubstancesXMLFormat
org.openscience.cdk.io.formats.PubChemSubstanceXMLFormat
org.openscience.cdk.io.formats.QChemFormat
org.openscience.cdk.io.formats.RGroupQueryFormat
org.openscience.cdk.io.formats.SDFFormat
org.openscience.cdk.io.formats.ShelXFormat
org.openscience.cdk.io.formats.SpartanFormat
org.openscience.cdk.io.formats.VASPFormat
org.openscience.cdk.io.formats.ZMatrixFormat
29 changes: 4 additions & 25 deletions src/main/org/openscience/cdk/io/FormatFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.StringTokenizer;

import org.openscience.cdk.annotations.TestClass;
Expand All @@ -57,12 +58,10 @@
*/
@TestClass("org.openscience.cdk.io.FormatFactoryTest")
public class FormatFactory {

private final static String IO_FORMATS_LIST = "io-formats.set";

private int headerLength;

private static List<IChemFormatMatcher> formats = null;
private List<IChemFormatMatcher> formats = new ArrayList<IChemFormatMatcher>(100);

/**
* Constructs a ReaderFactory which tries to detect the format in the
Expand All @@ -84,28 +83,8 @@ public FormatFactory(int headerLength) {
}

private void loadFormats() {
if (formats == null) {
formats = new ArrayList<IChemFormatMatcher>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream(IO_FORMATS_LIST)
));
int formatCount = 0;
while (reader.ready()) {
// load them one by one
String formatName = reader.readLine();
formatCount++;
try {
Class<? extends Object> formatClass = this.getClass().getClassLoader().loadClass(formatName);
Method getinstanceMethod = formatClass.getMethod("getInstance", new Class[0]);
IChemFormatMatcher format = (IChemFormatMatcher)getinstanceMethod.invoke(null, new Object[0]);
formats.add(format);
} catch (ClassNotFoundException exception) {
} catch (Exception exception) {
}
}
} catch (Exception exception) {
}
for(IChemFormatMatcher format : ServiceLoader.load(IChemFormatMatcher.class)){
formats.add(format);
}
}

Expand Down

0 comments on commit ffd62d9

Please sign in to comment.