Fixed potential class pool override in integration tests.
[controller.git] / opendaylight / config / config-persister-directory-autodetect-adapter / src / main / java / org / opendaylight / controller / config / persist / storage / directory / autodetect / FileType.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.persist.storage.directory.autodetect;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Charsets;
12 import com.google.common.io.Files;
13 import org.apache.commons.lang3.StringUtils;
14 import org.opendaylight.controller.config.persist.storage.directory.DirectoryPersister;
15 import org.opendaylight.controller.config.persist.storage.file.xml.model.ConfigSnapshot;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.Arrays;
20
21 enum FileType {
22
23     plaintext, xml;
24
25     public static final String XML_STORAGE_FIRST_LINE = "<" + ConfigSnapshot.SNAPSHOT_ROOT_ELEMENT_NAME + ">";
26     private static final String XML_FILE_DEFINITION_LINE = "<?xml";
27
28     static FileType getFileType(File file) {
29         String firstLine = readFirstLine(file);
30         if(isPlaintextStorage(firstLine)) {
31             return plaintext;
32         } else if(isXmlStorage(firstLine))
33             return xml;
34
35         throw new IllegalArgumentException("File " + file + " is not of permitted storage type: " + Arrays.toString(FileType.values()));
36     }
37
38     private static boolean isXmlStorage(String firstLine) {
39         boolean isXml = false;
40         isXml |= firstLine.startsWith(XML_STORAGE_FIRST_LINE);
41         isXml |= firstLine.startsWith(XML_FILE_DEFINITION_LINE);
42         return isXml;
43     }
44
45     private static boolean isPlaintextStorage(String firstLine) {
46         return firstLine.startsWith(DirectoryPersister.MODULES_START);
47
48     }
49
50     @VisibleForTesting
51     static String readFirstLine(File file) {
52         FirstLineReadingProcessor callback = new FirstLineReadingProcessor();
53         try {
54             return Files.readLines(file, Charsets.UTF_8, callback);
55         } catch (IOException e) {
56             throw new IllegalArgumentException("Unable to detect file type of file " + file, e);
57         }
58     }
59
60
61     private static class FirstLineReadingProcessor implements com.google.common.io.LineProcessor<String> {
62         private String firstNonBlankLine;
63
64         @Override
65         public boolean processLine(String line) throws IOException {
66             if(isEmptyLine(line)) {
67                 return true;
68             } else {
69                 firstNonBlankLine = line.trim();
70                 return false;
71             }
72         }
73
74         private boolean isEmptyLine(String line) {
75             return StringUtils.isBlank(line);
76         }
77
78         @Override
79         public String getResult() {
80             return firstNonBlankLine;
81         }
82     }
83 }