/*
 * Copyright 2018 NAVER Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.navercorp.pinpoint.hbase.schema.reader.xml;

import com.navercorp.pinpoint.hbase.schema.definition.xml.HbaseSchema;
import com.navercorp.pinpoint.hbase.schema.reader.HbaseSchemaParseException;
import com.navercorp.pinpoint.hbase.schema.reader.xml.mapper.HbaseSchemaMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.InputStream;

/**
 * @author HyunGil Jeong
 */
class XmlHbaseSchemaParser {

    private static final String SCHEMA_VERSION = "1.0";
    private static final String XSD_FILE = "pinpoint-hbase-" + SCHEMA_VERSION + ".xsd";

    private static final Logger LOGGER = LoggerFactory.getLogger(XmlHbaseSchemaParser.class);

    private static final JAXBContext JAXB_CONTEXT = createJaxbContext();
    private static final Schema SCHEMA = createSchema();

    private final HbaseSchemaMapper mapper = new HbaseSchemaMapper(JAXB_CONTEXT, SCHEMA);

    private static JAXBContext createJaxbContext() {
        try {
            return JAXBContext.newInstance(HbaseSchema.class);
        } catch (JAXBException e) {
            throw new IllegalStateException("Unable to initialize parser", e);
        }
    }

    private static Schema createSchema() {
        InputStream xsdInputStream = XmlHbaseSchemaParser.class.getResourceAsStream(XmlHbaseSchemaParser.XSD_FILE);
        if (xsdInputStream == null) {
            LOGGER.warn("Unabled to find: {}, skipping validation", XmlHbaseSchemaParser.XSD_FILE);
            return null;
        }
        try {
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            return schemaFactory.newSchema(new StreamSource(xsdInputStream));
        } catch (SAXException e) {
            LOGGER.warn("Unable to parse: {}, skipping validation", XmlHbaseSchemaParser.XSD_FILE);
            return null;
        }
    }

    XmlHbaseSchemaParseResult parseSchema(InputSource inputSource) {
        try {
            Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
            unmarshaller.setSchema(SCHEMA);
            HbaseSchema hbaseSchema = (HbaseSchema) unmarshaller.unmarshal(inputSource);
            return mapper.map(hbaseSchema);
        } catch (JAXBException e) {
            Throwable linkedException = e.getLinkedException();
            if (linkedException == null) {
                throw new IllegalStateException("JAXB error", e);
            }
            throw new HbaseSchemaParseException(linkedException.getMessage(), linkedException);
        }
    }
}

@@ -1259,24 +1259,52 @@
 emaFactory;%0A
+import java.io.IOException;%0A
 import java.
@@ -2158,16 +2158,21 @@
         
+try (
 InputStr
@@ -2244,39 +2244,24 @@
 am(X
-mlHbaseSchemaParser.XSD_FILE);%0A
+SD_FILE)) %7B%0A    
     
@@ -2298,32 +2298,36 @@
 ) %7B%0A            
+    
 LOGGER.warn(%22Una
@@ -2366,37 +2366,16 @@
 ation%22, 
-XmlHbaseSchemaParser.
 XSD_FILE
@@ -2369,32 +2369,36 @@
 on%22, XSD_FILE);%0A
+    
             retu
@@ -2406,32 +2406,36 @@
 n null;%0A        
+    
 %7D%0A        try %7B%0A
@@ -2420,38 +2420,46 @@
       %7D%0A        
+ 
+   
 try %7B%0A
+    
             Sche
@@ -2555,24 +2555,28 @@
             
+  
+  
 return schem
@@ -2629,32 +2629,36 @@
 ream));%0A        
+    
 %7D catch (SAXExce
@@ -2672,32 +2672,36 @@
 ) %7B%0A            
+  
+  
 LOGGER.warn(%22Una
@@ -2745,36 +2745,199 @@
 %22, X
-mlHbaseSchemaParser.XSD_FILE
+SD_FILE);%0A                return null;%0A            %7D%0A        %7D catch (IOException e) %7B%0A            LOGGER.error(%22%7B%7D file I/O error, skipping validation. Error:%7B%7D%22, XSD_FILE, e.getMessage(), e
 );%0A 

Fix resource leak when reading schema definition file