Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 org.deephacks.tools4j.config.internal.core.xml;
 
 import static org.deephacks.tools4j.config.model.Events.CFG101_SCHEMA_NOT_EXIST;
 import static org.deephacks.tools4j.config.model.Events.CFG202_XML_SCHEMA_FILE_MISSING;
 
 import java.io.File;
 import java.util.Map;
 
 
 
 
 public final class XmlSchemaManager extends SchemaManager {
     private static final Logger log = LoggerFactory.getLogger(XmlSchemaManager.class);
     static final String XML_CONFIG_SCHEMA_FILE_STORAGE_DIR_PROP = "config.spi.schema.xml.dir";
     static final String XML_SCHEMA_FILE_NAME = "schema.xml";
     static final String XML_EMPTY_FILE = "<schema-xml></schema-xml>";
     private static final SystemProperties PROP = SystemProperties.createDefault();
     private static final long serialVersionUID = 8979172640204086999L;
     private static final ConcurrentHashMap<StringSchemaschemas = new ConcurrentHashMap<>();
 
     @Override
     public Map<StringSchemagetSchemas() {
         return readValues();
     }
 
     @Override
     public Schema getSchema(String schemaName) {
         Map<StringSchemavalues = readValues();
         Schema schema = values.get(schemaName);
         if (schema == null) {
             throw CFG101_SCHEMA_NOT_EXIST(schemaName);
         }
         return schema;
     }
 
     @Override
     public void regsiterSchema(Schema... schemas) {
         Map<StringSchemavalues = readValues();
         for (Schema schema : schemas) {
             values.put(schema.getName(), schema);
             writeValues(values);
         }
     }
 
     @Override
     public void removeSchema(String schemaName) {
         Map<StringSchemavalues = readValues();
         values.remove(schemaName);
         writeValues(values);
     }
 
     private Map<StringSchemareadValues() {
         if (!.isEmpty()) {
             return ;
         }
         File dir = getStorageDir();
         File file = new File(dir);
 
         try {
             if (!file.exists()) {
                 Files.write(file, Charset.defaultCharset());
             }
             .trace("Reading schema from {}"file.getAbsolutePath());
             FileInputStream in = new FileInputStream(file);
             JAXBContext context = JAXBContext.newInstance(XmlSchemas.class);
             Unmarshaller unmarshaller = context.createUnmarshaller();
             XmlSchemas xmlschemas = (XmlSchemasunmarshaller.unmarshal(in);
             .putAll(xmlschemas.getSchemas());
             return ;
        } catch (FileNotFoundException e) {
            throw CFG202_XML_SCHEMA_FILE_MISSING(file);
        } catch (IOException | JAXBException e) {
            throw new RuntimeException(e);
        }
    }
    private void writeValues(Map<StringSchemavalues) {
        .putAll(values);
        File dir = getStorageDir();
        if (!dir.exists()) {
            try {
                dir.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        File file = new File(dir);
        .trace("Writing schema to {}"file.getAbsolutePath());
        PrintWriter pw = null;
        try {
            XmlSchemas schemas = new XmlSchemas(values);
            pw = new PrintWriter(file"UTF-8");
            JAXBContext context = JAXBContext.newInstance(XmlSchemas.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(.true);
            marshaller.marshal(schemaspw);
        } catch (JAXBException | IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (pw != null) {
                pw.flush();
                pw.close();
            }
        }
    }
    static File getStorageDir() {
        if (dirValue == null || "".equals(dirValue)) {
            dirValue = System.getProperty("java.io.tmpdir");
        }
        File dir = new File(dirValue);
        return dir;
    }
New to GrepCode? Check out our FAQ X