View Javadoc
1   package ca.uhn.hl7v2;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.Properties;
6   
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   import org.w3c.dom.DOMImplementation;
10  
11  import ca.uhn.hl7v2.util.XMLUtils;
12  
13  /**
14   * Class to log the HAPI version when HAPI is first used (mostly for troubleshooting purposes)
15   */
16  public class VersionLogger {
17  
18  	private static boolean ourInitialized = false;
19  	private static String ourVersion;
20  	private static final Logger LOG = LoggerFactory.getLogger(VersionLogger.class);
21  
22  	/**
23  	 * Non-instantiable
24  	 */
25  	private VersionLogger() {
26  		// nothing
27  	}
28  
29  	/**
30  	 * Logs the HAPI version on the first time this method is invoked, does nothing afterwards
31  	 */
32  	public static void init() {
33  		if (!ourInitialized) {
34  			printHapiVersion();
35  			checkStructureLibraries();
36  			checkDOMImplementation();
37  			ourInitialized = true;
38  		}
39  	}
40  
41  	private static void checkDOMImplementation() {
42  		try {
43  			// Check if proper XML support is available
44  			DOMImplementation impl = XMLUtils.getDOMImpl();
45  			if (impl == null) {
46  				LOG.warn("DOM Level 3 (Load and Save) is NOT supported by the XML library found first on your classpath!");
47  				LOG.warn("XML parsing and encoding as well as working with Conformance Profiles will fail.");
48  			}
49  		} catch (Throwable e) {
50  			LOG.warn("Error occured while trying to retrieve a DOMImplementation.", e);
51  			LOG.warn("XML parsing and encoding as well as working with Conformance Profiles will fail.");
52  		}
53  	}
54  
55  	private static void checkStructureLibraries() {
56  		// Check if any structures are present
57  		StringBuilder sb = new StringBuilder();
58  		for (Version v : Version.availableVersions()) {
59  			sb.append(v.getVersion());
60  			sb.append(sb.length() > 0 ? ", " : "");
61  		}
62  		if (sb.length() == 0) {
63  			LOG.warn("No HL7 structure libraries found on the classpath!");
64  		} else {
65  			LOG.info("Default Structure libraries found for HL7 versions {}", sb.toString());
66  		}
67  	}
68  
69  	private static void printHapiVersion() {
70          try (InputStream is = VersionLogger.class
71                  .getResourceAsStream("/ca/uhn/hl7v2/hapi-version.properties")) {
72              Properties p = new Properties();
73              p.load(is);
74              ourVersion = p.getProperty("version");
75              LOG.info("HAPI version is: " + ourVersion);
76          } catch (IOException e) {
77              LOG.warn("Unable to determine HAPI version information", e);
78          }
79          // ignore
80      }
81  
82  	/**
83  	 * @return Returns the current version of HAPI
84  	 */
85  	public static String getVersion() {
86  		return ourVersion;
87  	}
88  
89  }