1 /** 2 * Copyright (c) 2011, University of Konstanz, Distributed Systems Group 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of the University of Konstanz nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 package org.treetank.access.conf; 29 30 import static com.google.common.base.Objects.toStringHelper; 31 32 import java.io.File; 33 import java.io.FileReader; 34 import java.io.FileWriter; 35 import java.io.IOException; 36 import java.util.Objects; 37 38 import org.treetank.exception.TTIOException; 39 40 import com.google.gson.stream.JsonReader; 41 import com.google.gson.stream.JsonWriter; 42 43 /** 44 * <h1>Storage Configuration</h1> 45 * 46 * <p> 47 * Represents a configuration of a database. Includes all settings which have to be made when it comes to the 48 * creation of the database. 49 * </p> 50 * 51 * @author Sebastian Graf, University of Konstanz 52 */ 53 public final class StorageConfiguration { 54 55 /** 56 * Paths for a {@link org.treetank.access.Storage}. Each {@link org.treetank.access.Storage} has the 57 * same folder.layout. 58 */ 59 public enum Paths implements IConfigurationPath{ 60 61 /** File to store db settings. */ 62 ConfigBinary(new File("dbsetting.obj"), false), 63 /** File to store the data. */ 64 Data(new File("resources"), true); 65 66 /** Location of the file. */ 67 private final File mFile; 68 69 /** Is the location a folder or no? */ 70 private final boolean mIsFolder; 71 72 /** 73 * Constructor. 74 * 75 * @param pFile 76 * to be set 77 * @param pIsFolder 78 * to be set. 79 */ 80 private Paths(final File pFile, final boolean pIsFolder) { 81 this.mFile = pFile; 82 this.mIsFolder = pIsFolder; 83 } 84 85 /** 86 * Getting the file for the kind. 87 * 88 * @return the file to the kind 89 */ 90 public File getFile() { 91 return mFile; 92 } 93 94 /** 95 * Check if file is denoted as folder or not. 96 * 97 * @return boolean if file is folder 98 */ 99 public boolean isFolder() { 100 return mIsFolder; 101 } 102 103 } 104 105 /** Path to file. */ 106 public final File mFile; 107 108 /** 109 * Constructor with the path to be set. 110 * 111 * @param paramFile 112 * file to be set 113 */ 114 public StorageConfiguration(final File paramFile) { 115 mFile = paramFile; 116 } 117 118 /** 119 * {@inheritDoc} 120 */ 121 @Override 122 public String toString() { 123 return toStringHelper(this).add("mFile", mFile).toString(); 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override 130 public boolean equals(final Object pObj) { 131 return this.hashCode() == pObj.hashCode(); 132 } 133 134 /** 135 * {@inheritDoc} 136 */ 137 @Override 138 public int hashCode() { 139 return Objects.hash(mFile); 140 } 141 142 /** 143 * Serializing a {@link StorageConfiguration} to a json file. 144 * 145 * @param pConfig 146 * to be serialized 147 * @throws TTIOException 148 */ 149 public static void serialize(final StorageConfiguration pConfig) throws TTIOException { 150 try { 151 FileWriter fileWriter = 152 new FileWriter(new File(pConfig.mFile, Paths.ConfigBinary.getFile().getName())); 153 JsonWriter jsonWriter = new JsonWriter(fileWriter); 154 jsonWriter.beginObject(); 155 jsonWriter.name("file").value(pConfig.mFile.getAbsolutePath()); 156 jsonWriter.endObject(); 157 jsonWriter.close(); 158 fileWriter.close(); 159 } catch (IOException ioexc) { 160 throw new TTIOException(ioexc); 161 } 162 } 163 164 /** 165 * Generate a StorageConfiguration out of a file. 166 * 167 * @param pFile 168 * where the StorageConfiguration lies in as json 169 * @return a new {@link StorageConfiguration} class 170 * @throws TTIOException 171 */ 172 public static StorageConfiguration deserialize(final File pFile) throws TTIOException { 173 try { 174 FileReader fileReader = new FileReader(new File(pFile, Paths.ConfigBinary.getFile().getName())); 175 JsonReader jsonReader = new JsonReader(fileReader); 176 jsonReader.beginObject(); 177 jsonReader.nextName(); 178 File file = new File(jsonReader.nextString()); 179 jsonReader.endObject(); 180 jsonReader.close(); 181 fileReader.close(); 182 return new StorageConfiguration(file); 183 } catch (IOException ioexc) { 184 throw new TTIOException(ioexc); 185 } 186 } 187 }