001package org.intellimate.izou.system.file; 002 003import org.intellimate.izou.main.Main; 004import org.intellimate.izou.util.IzouModule; 005 006import java.io.File; 007import java.io.IOException; 008import java.net.URISyntaxException; 009import java.nio.file.Files; 010 011/** 012 * FileSystemManager is responsible for creating all the files and folders Izou needs to operate 013 */ 014public class FileSystemManager extends IzouModule { 015 /** 016 * Path to log files 017 */ 018 private final File izouParentLocation; 019 private final File izouJarLocation; 020 private final File libLocation; 021 private final File resourceLocation; 022 private final File propertiesLocation; 023 private final File logsLocation; 024 private final File systemLocation; 025 private final File systemDataLocation; 026 027 /** 028 * Creates a file system manager 029 * 030 * @param main an instance of main 031 */ 032 public FileSystemManager(Main main) { 033 super(main); 034 try { 035 izouJarLocation = new File(FileSystemManager.class.getProtectionDomain() 036 .getCodeSource().getLocation().toURI().getPath()); 037 if (Boolean.getBoolean("debug")) { 038 izouParentLocation = new File("."); 039 } else { 040 izouParentLocation = izouJarLocation.getParentFile(); 041 } 042 libLocation = new File(izouParentLocation.toString() + File.separator + "lib").getCanonicalFile(); 043 resourceLocation = new File(izouParentLocation.toString() + File.separator + "resources").getCanonicalFile(); 044 propertiesLocation = new File(izouParentLocation.toString() + File.separator + "properties").getCanonicalFile(); 045 logsLocation = new File(izouParentLocation.toString() + File.separator + "logs").getCanonicalFile(); 046 systemLocation = new File(izouParentLocation.toString() + File.separator + "system").getCanonicalFile(); 047 systemDataLocation = new File(izouParentLocation.toString() + File.separator + "system" 048 + File.separator + "data").getCanonicalFile(); 049 } catch (URISyntaxException | IOException e) { 050 error("unable to create the Izou-file system"); 051 throw new IllegalStateException("unable to create the Izou-file system"); 052 } 053 } 054 055 /** 056 * creates Izou file-system 057 * 058 * @throws IOException could throw IOException because working with files 059 */ 060 public void createIzouFileSystem() throws IOException { 061 createLibFolder(); 062 createResourceFolder(); 063 createPropertiesFolder(); 064 createLogsFolder(); 065 createSystemFolder(); 066 createSystemDataFolder(); 067 } 068 069 /** 070 * create lib folder 071 * 072 * @throws IOException could throw IOException because working with files 073 */ 074 private void createLibFolder() throws IOException { 075 if(!Files.exists(libLocation.toPath())) 076 Files.createDirectories(libLocation.toPath()); 077 } 078 079 /** 080 * create resource folder 081 * 082 * @throws IOException could throw IOException because working with files 083 */ 084 private void createResourceFolder() throws IOException { 085 if(!Files.exists(resourceLocation.toPath())) 086 Files.createDirectories(resourceLocation.toPath()); 087 } 088 089 /** 090 * create properties folder 091 * 092 * @throws IOException could throw IOException because working with files 093 */ 094 private void createPropertiesFolder() throws IOException { 095 if(!Files.exists(propertiesLocation.toPath())) 096 Files.createDirectories(propertiesLocation.toPath()); 097 } 098 099 /** 100 * Create logs folder 101 * 102 * @throws IOException could throw IOException because working with files 103 */ 104 private void createLogsFolder() throws IOException { 105 if(!Files.exists(logsLocation.toPath())) 106 Files.createDirectories(logsLocation.toPath()); 107 } 108 109 /** 110 * Create system folder 111 * 112 * @throws IOException could throw IOException because working with files 113 */ 114 private void createSystemFolder() throws IOException { 115 if(!Files.exists(systemLocation.toPath())) 116 Files.createDirectories(systemLocation.toPath()); 117 } 118 119 /** 120 * Create system/data folder 121 * 122 * @throws IOException could throw IOException because working with files 123 */ 124 private void createSystemDataFolder() throws IOException { 125 if(!Files.exists(systemDataLocation.toPath())) 126 Files.createDirectories(systemDataLocation.toPath()); 127 } 128 129 public File getIzouParentLocation() { 130 return izouParentLocation; 131 } 132 133 public File getLibLocation() { 134 return libLocation; 135 } 136 137 public File getResourceLocation() { 138 return resourceLocation; 139 } 140 141 public File getPropertiesLocation() { 142 return propertiesLocation; 143 } 144 145 public File getLogsLocation() { 146 return logsLocation; 147 } 148 149 public File getIzouJarLocation() { 150 return izouJarLocation; 151 } 152 153 public File getSystemLocation() { 154 return systemLocation; 155 } 156 157 public File getSystemDataLocation() { 158 return systemDataLocation; 159 } 160}