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                InputStream is = null;
071                try {
072                        is = VersionLogger.class
073                                        .getResourceAsStream("/ca/uhn/hl7v2/hapi-version.properties");
074                        Properties p = new Properties();
075                        p.load(is);
076                        ourVersion = p.getProperty("version");
077                        LOG.info("HAPI version is: " + ourVersion);
078                } catch (IOException e) {
079                        LOG.warn("Unable to determine HAPI version information", e);
080                } finally {
081                        if (is != null) {
082                                try {
083                                        is.close();
084                                } catch (IOException e) {
085                                        // ignore
086                                }
087                        }
088                }
089        }
090
091        /**
092         * @return Returns the current version of HAPI
093         */
094        public static String getVersion() {
095                return ourVersion;
096        }
097
098}