KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > layout > TemplateManager


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

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 JavaDoc 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 JavaDoc tsb = new StringBuffer JavaDoc (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 JavaDoc (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 JavaDoc tsb = new StringBuffer JavaDoc ("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 JavaDoc toString() {
91         return "[TemplateManager]";
92     }
93
94     /* (non-Javadoc)
95      * @see freecs.interfaces.IReloadable#getFile()
96      */

97     public File getFile() {
98         return reloadable;
99     }
100
101     /* (non-Javadoc)
102      * @see freecs.interfaces.IReloadable#filePresent()
103      */

104     public boolean filePresent() {
105         return reloadable.exists();
106     }
107
108     /* (non-Javadoc)
109      * @see freecs.interfaces.IReloadable#lastModified()
110      */

111     public long lastModified() {
112         return lastModified;
113     }
114
115     /* (non-Javadoc)
116      * @see freecs.interfaces.IReloadable#changed()
117      */

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 JavaDoc name = (String JavaDoc) 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     /* (non-Javadoc)
147      * @see freecs.interfaces.IReloadable#removed()
148      */

149     public void removed() {
150         Server.log (this, "templateset-directory has been removed!", Server.MSG_STATE, Server.LVL_MAJOR);
151     }
152
153     /* (non-Javadoc)
154      * @see freecs.interfaces.IReloadable#created()
155      */

156     public void created() {
157         changed();
158     }
159 }
160
Popular Tags