001package ca.uhn.hl7v2;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.util.Properties;
006
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009import org.w3c.dom.DOMImplementation;
010
011import ca.uhn.hl7v2.util.XMLUtils;
012
013/**
014 * Class to log the HAPI version when HAPI is first used (mostly for troubleshooting purposes)
015 */
016public class VersionLogger {
017
018        private static boolean ourInitialized = false;
019        private static String ourVersion;
020        private static final Logger LOG = LoggerFactory.getLogger(VersionLogger.class);
021
022        /**
023         * Non-instantiable
024         */
025        private VersionLogger() {
026                // nothing
027        }
028
029        /**
030         * Logs the HAPI version on the first time this method is invoked, does nothing afterwards
031         */
032        public static void init() {
033                if (!ourInitialized) {
034                        printHapiVersion();
035                        checkStructureLibraries();
036                        checkDOMImplementation();
037                        ourInitialized = true;
038                }
039        }
040
041        private static void checkDOMImplementation() {
042                try {
043                        // Check if proper XML support is available
044                        DOMImplementation impl = XMLUtils.getDOMImpl();
045                        if (impl == null) {
046                                LOG.warn("DOM Level 3 (Load and Save) is NOT supported by the XML library found first on your classpath!");
047                                LOG.warn("XML parsing and encoding as well as working with Conformance Profiles will fail.");
048                        }
049                } catch (Throwable e) {
050                        LOG.warn("Error occured while trying to retrieve a DOMImplementation.", e);
051                        LOG.warn("XML parsing and encoding as well as working with Conformance Profiles will fail.");
052                }
053        }
054
055        private static void checkStructureLibraries() {
056                // Check if any structures are present
057                StringBuilder sb = new StringBuilder();
058                for (Version v : Version.availableVersions()) {
059                        sb.append(v.getVersion());
060                        sb.append(sb.length() > 0 ? ", " : "");
061                }
062                if (sb.length() == 0) {
063                        LOG.warn("No HL7 structure libraries found on the classpath!");
064                } else {
065                        LOG.info("Default Structure libraries found for HL7 versions {}", sb.toString());
066                }
067        }
068
069        private static void printHapiVersion() {
070        try (InputStream is = VersionLogger.class
071                .getResourceAsStream("/ca/uhn/hl7v2/hapi-version.properties")) {
072            Properties p = new Properties();
073            p.load(is);
074            ourVersion = p.getProperty("version");
075            LOG.info("HAPI version is: " + ourVersion);
076        } catch (IOException e) {
077            LOG.warn("Unable to determine HAPI version information", e);
078        }
079        // ignore
080    }
081
082        /**
083         * @return Returns the current version of HAPI
084         */
085        public static String getVersion() {
086                return ourVersion;
087        }
088
089}