Coverage Report - ca.uhn.hl7v2.VersionLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
VersionLogger
72%
31/43
66%
8/12
2.5
 
 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  5
         private static boolean ourInitialized = false;
 19  
         private static String ourVersion;
 20  5
         private static final Logger LOG = LoggerFactory.getLogger(VersionLogger.class);
 21  
 
 22  
         /**
 23  
          * Non-instantiable
 24  
          */
 25  0
         private VersionLogger() {
 26  
                 // nothing
 27  0
         }
 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  5585
                 if (!ourInitialized) {
 34  5
                         printHapiVersion();
 35  5
                         checkStructureLibraries();
 36  5
                         checkDOMImplementation();
 37  5
                         ourInitialized = true;
 38  
                 }
 39  5585
         }
 40  
 
 41  
         private static void checkDOMImplementation() {
 42  
                 try {
 43  
                         // Check if proper XML support is available
 44  5
                         DOMImplementation impl = XMLUtils.getDOMImpl();
 45  5
                         if (impl == null) {
 46  0
                                 LOG.warn("DOM Level 3 (Load and Save) is NOT supported by the XML library found first on your classpath!");
 47  0
                                 LOG.warn("XML parsing and encoding as well as working with Conformance Profiles will fail.");
 48  
                         }
 49  0
                 } catch (Throwable e) {
 50  0
                         LOG.warn("Error occured while trying to retrieve a DOMImplementation.", e);
 51  0
                         LOG.warn("XML parsing and encoding as well as working with Conformance Profiles will fail.");
 52  5
                 }
 53  5
         }
 54  
 
 55  
         private static void checkStructureLibraries() {
 56  
                 // Check if any structures are present
 57  5
                 StringBuilder sb = new StringBuilder();
 58  5
                 for (Version v : Version.availableVersions()) {
 59  55
                         sb.append(v.getVersion());
 60  55
                         sb.append(sb.length() > 0 ? ", " : "");
 61  55
                 }
 62  5
                 if (sb.length() == 0) {
 63  0
                         LOG.warn("No HL7 structure libraries found on the classpath!");
 64  
                 } else {
 65  5
                         LOG.info("Default Structure libraries found for HL7 versions {}", sb.toString());
 66  
                 }
 67  5
         }
 68  
 
 69  
         private static void printHapiVersion() {
 70  5
                 InputStream is = null;
 71  
                 try {
 72  5
                         is = VersionLogger.class
 73  5
                                         .getResourceAsStream("/ca/uhn/hl7v2/hapi-version.properties");
 74  5
                         Properties p = new Properties();
 75  5
                         p.load(is);
 76  5
                         ourVersion = p.getProperty("version");
 77  5
                         LOG.info("HAPI version is: " + ourVersion);
 78  0
                 } catch (IOException e) {
 79  0
                         LOG.warn("Unable to determine HAPI version information", e);
 80  
                 } finally {
 81  5
                         if (is != null) {
 82  
                                 try {
 83  5
                                         is.close();
 84  0
                                 } catch (IOException e) {
 85  
                                         // ignore
 86  5
                                 }
 87  
                         }
 88  
                 }
 89  5
         }
 90  
 
 91  
         /**
 92  
          * @return Returns the current version of HAPI
 93  
          */
 94  
         public static String getVersion() {
 95  0
                 return ourVersion;
 96  
         }
 97  
 
 98  
 }