1 18 package freecs.layout; 19 20 import freecs.Server; 21 import freecs.interfaces.IReloadable; 22 import freecs.util.FileMonitor; 23 24 import java.io.*; 25 import java.util.*; 26 27 public class TemplateManager implements IReloadable { 28 private HashMap tSets; 29 public static TemplateManager mgr; 30 public static File reloadable; 31 public static long lastModified=0; 32 33 public TemplateManager() throws IOException { 34 tSets = new HashMap(); 35 loadTemplates(); 36 TemplateManager.mgr = this; 37 lastModified = reloadable.lastModified(); 38 FileMonitor.getFileMonitor ().addReloadable (this); 39 } 40 41 public TemplateSet getTemplateSet () { 42 return getTemplateSet("default"); 43 } 44 public TemplateSet getTemplateSet (String name) { 45 if (name == null || !tSets.containsKey (name)) 46 return (TemplateSet) tSets.get("default"); 47 return (TemplateSet) tSets.get(name); 48 } 49 50 private void loadTemplates() throws IOException { 51 StringBuffer tsb = new StringBuffer (Server.BASE_PATH).append ("/templatesets"); 52 File tFile = new File(tsb.toString ()); 53 if (!tFile.exists()) { 54 if (!tFile.mkdir()) { throw new IOException("Unable to create directory 'templateset'"); } 55 tsb = new StringBuffer (Server.BASE_PATH).append ("/templatesets/default"); 56 tFile = new File(tsb.toString ()); 57 if (!tFile.exists()) { 58 if (!tFile.mkdir()) { throw new IOException("Unable to create directory 'default'"); } 59 } 60 throw new IOException("No templates available. (Directories created)"); 61 } 62 if (!tFile.isDirectory()) { 63 throw new IOException("'templatesets' isn't a directory"); 64 } 65 reloadable=tFile; 66 File defaultTs = new File (tFile.getCanonicalPath(), "default"); 67 if (!defaultTs.exists()) { 68 throw new IOException ("The default-layout was not present!"); 69 } 70 constructTemplateSet(defaultTs); 71 File fList[] = tFile.listFiles (); 72 for (int i = 0; i < fList.length; i++) { 73 if (!fList[i].isDirectory () || fList[i].getName().equals("default")) 74 continue; 75 constructTemplateSet(fList[i]); 76 } 77 if (!tSets.containsKey("default")) throw new IOException ("Unable to read default-template-set"); 78 } 79 80 private void constructTemplateSet (File f) throws IOException { 81 TemplateSet tSet = new TemplateSet (f, this); 82 if (!tSet.isValide ()) 83 return; 84 StringBuffer tsb = new StringBuffer ("parsed valide templateset "); 85 tsb.append (tSet.getName ()); 86 Server.log(this, tsb.toString (), Server.MSG_STATE, Server.LVL_MINOR); 87 tSets.put (f.getName (), tSet); 88 } 89 90 public String toString() { 91 return "[TemplateManager]"; 92 } 93 94 97 public File getFile() { 98 return reloadable; 99 } 100 101 104 public boolean filePresent() { 105 return reloadable.exists(); 106 } 107 108 111 public long lastModified() { 112 return lastModified; 113 } 114 115 118 public void changed() { 119 lastModified = reloadable.lastModified(); 120 if (!reloadable.isDirectory()) { 121 Server.log (this, "templateset-directory is a file NOT a directory!", Server.MSG_STATE, Server.LVL_MAJOR); 122 return; 123 } 124 File files[] = reloadable.listFiles(); 125 for (int i = 0; i < files.length; i++) { 126 TemplateSet ts = (TemplateSet) tSets.get(files[i].getName()); 127 if (ts == null) try { 128 Server.log (this, "constructing newly added templateset " + files[i].getName(), Server.MSG_STATE, Server.LVL_MAJOR); 129 constructTemplateSet (files[i]); 130 continue; 131 } catch (IOException ioe) { 132 Server.debug (this, "constructTemplateSet caused Exception for " + files[i].getName(), ioe, Server.MSG_ERROR, Server.LVL_MAJOR); 133 } 134 ts.reload(files[i]); 135 } 136 for (Iterator i = this.tSets.keySet().iterator(); i.hasNext(); ) { 137 String name = (String ) i.next(); 138 File subdir = new File(reloadable, name); 139 if (!subdir.exists()) { 140 Server.log (this, "removing templateset " + name, Server.MSG_STATE, Server.LVL_MAJOR); 141 i.remove(); 142 } 143 } 144 } 145 146 149 public void removed() { 150 Server.log (this, "templateset-directory has been removed!", Server.MSG_STATE, Server.LVL_MAJOR); 151 } 152 153 156 public void created() { 157 changed(); 158 } 159 } 160 | Popular Tags |