Coverage Report - ca.uhn.hl7v2.VersionLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
VersionLogger
71%
30/42
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  1
         private static boolean ourInitialized = false;
 19  
         private static String ourVersion;
 20  1
         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  470
                 if (!ourInitialized) {
 34  1
                         printHapiVersion();
 35  1
                         checkStructureLibraries();
 36  1
                         checkDOMImplementation();
 37  1
                         ourInitialized = true;
 38  
                 }
 39  470
         }
 40  
 
 41  
         private static void checkDOMImplementation() {
 42  
                 try {
 43  
                         // Check if proper XML support is available
 44  1
                         DOMImplementation impl = XMLUtils.getDOMImpl();
 45  1
                         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 (Exception 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  1
                 }
 53  1
         }
 54  
 
 55  
         private static void checkStructureLibraries() {
 56  
                 // Check if any structures are present
 57  1
                 StringBuilder sb = new StringBuilder();
 58  1
                 for (Version v : Version.availableVersions()) {
 59  8
                         sb.append(v.getVersion());
 60  8
                         sb.append(sb.length() > 0 ? ", " : "");
 61  8
                 }
 62  1
                 if (sb.length() == 0) {
 63  0
                         LOG.warn("No HL7 structure libraries found on the classpath!");
 64  
                 } else {
 65  1
                         LOG.info("Default Structure libraries found for HL7 versions {}", sb.toString());
 66  
                 }
 67  1
         }
 68  
 
 69  
         private static void printHapiVersion() {
 70  1
                 InputStream is = null;
 71  
                 try {
 72  1
                         is = VersionLogger.class
 73  
                                         .getResourceAsStream("/ca/uhn/hl7v2/hapi-version.properties");
 74  1
                         Properties p = new Properties();
 75  1
                         p.load(is);
 76  1
                         ourVersion = p.getProperty("version");
 77  1
                         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  1
                         if (is != null) {
 82  
                                 try {
 83  1
                                         is.close();
 84  0
                                 } catch (IOException e) {
 85  
                                         // ignore
 86  1
                                 }
 87  
                         }
 88  
                 }
 89  1
         }
 90  
 
 91  
         /**
 92  
          * @return Returns the current version of HAPI
 93  
          */
 94  
         public static String getVersion() {
 95  0
                 return ourVersion;
 96  
         }
 97  
 
 98  
 }