KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > packaging > ProjectManager


1 /*
2  
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5  
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8  
9  */

10
11 package org.mmbase.applications.packaging;
12
13 import java.io.DataOutputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.util.*;
17
18
19 import org.mmbase.applications.packaging.projects.Project;
20 import org.mmbase.applications.packaging.projects.creators.CreatorInterface;
21 import org.mmbase.applications.packaging.util.ExtendedDocumentReader;
22 import org.mmbase.util.XMLEntityResolver;
23 import org.mmbase.util.logging.Logger;
24 import org.mmbase.util.logging.Logging;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NamedNodeMap JavaDoc;
27
28 /**
29  * project manager, keeps track of projects we are working on, Projects are
30  * not a MMBase defined object they are more like projects inside a IDE
31  * containers to keep track of the different projects you are working on.
32  *
33  * @author Daniel Ockeloen
34  * @since MMBase-1.7
35  */

36 public class ProjectManager {
37
38     // create a logger for this class
39
private static final Logger log = Logging.getLoggerInstance(ProjectManager.class);
40
41     // state of this manager is running or not
42
private static boolean state = false;
43
44     // list of all the defined projects (xml file)
45
private static Map JavaDoc projects = new HashMap();
46  
47     // list of all the defined creators this manager can work with (xml file)
48
private static Map JavaDoc creators = new HashMap();
49
50     // defines needed for the xml readers to find the dtd's
51
public static final String JavaDoc DTD_PROJECTS_1_0 = "projects_1_0.dtd";
52     public static final String JavaDoc DTD_CREATORS_1_0 = "creators_1_0.dtd";
53
54     public static final String JavaDoc PUBLIC_ID_PROJECTS_1_0 = "-//MMBase//DTD projects config 1.0//EN";
55     public static final String JavaDoc PUBLIC_ID_CREATORS_1_0 = "-//MMBase//DTD creators config 1.0//EN";
56
57     static {
58         XMLEntityResolver.registerPublicID(PUBLIC_ID_PROJECTS_1_0, "DTD_PROJECTS_1_0", ProjectManager.class);
59         XMLEntityResolver.registerPublicID(PUBLIC_ID_CREATORS_1_0, "DTD_CREATORS_1_0", ProjectManager.class);
60     }
61
62     
63
64     /**
65     * start this manager, it reads all the defined creators and projects
66     */

67     public static synchronized void init() {
68         if (!isRunning()) {
69             readCreators();
70             readProjects();
71             state=true;
72         }
73     }
74
75     /**
76     * is this manager running ?
77     *
78     * @return true if running, false if its not
79     *
80     */

81     public static boolean isRunning() {
82         return state;
83     }
84
85
86
87     /**
88      * get all the projects we have defined (xml file)
89      *
90      * @return projects
91      */

92     public static Iterator getProjects() {
93         return projects.values().iterator();
94     }
95
96
97     /**
98     * get a Project based on its name
99     *
100     * @return Project or null if not found
101     */

102     public static Project getProject(String JavaDoc name) {
103         return (Project)projects.get(name);
104     }
105
106
107     /**
108     * add a new project to the projectlist (xml file) changes
109     * both the memory list and the file on disc.
110     *
111     * @return true if the add worked, false if something went wrong
112     */

113     public static boolean addProject(String JavaDoc name,String JavaDoc path) {
114         Project pr = new Project(name,path);
115         if (pr.save()) {
116             projects.put(name,pr);
117             save();
118         }
119         return true;
120     }
121
122
123     /**
124     * change project name/path (xml file) changes
125     * both the memory list and the file on disc.
126     *
127     * @return true if the change worked, false if something went wrong
128     */

129     public static boolean changeProjectSettings(String JavaDoc oldname,String JavaDoc newname,String JavaDoc newpath) {
130         Project p = (Project)projects.get(oldname);
131         if (p != null) {
132             projects.remove(oldname);
133             addProject(newname,newpath);
134             save();
135         }
136         return true;
137     }
138
139     /**
140     * delete a project to the projectlist (xml file) changes
141     * both the memory list and the file on disc.
142     *
143     * @return true if the delete worked, false if something went wrong
144     */

145     public static boolean deleteProject(String JavaDoc name) {
146         projects.remove(name);
147         save();
148         return true;
149     }
150
151     /**
152     * read the Projects from disc, the file is defined in your
153     * config directory. Uses a xml reader and the dtd's found as
154     * resources.
155     */

156     public static void readProjects() {
157
158         // XXX should use ResourceLoader here
159

160         String JavaDoc filename = PackageManager.getConfigPath()+File.separator+"packaging"+File.separator+"projects.xml";
161         File JavaDoc file = new File JavaDoc(filename);
162         if(file.exists()) {
163             ExtendedDocumentReader reader = new ExtendedDocumentReader(filename,ProjectManager.class);
164             if(reader!=null) {
165
166                 // decode projects
167
for(Iterator ns=reader.getChildElements("projects","project");ns.hasNext(); ) {
168                     Element JavaDoc n=(Element JavaDoc)ns.next();
169                     NamedNodeMap JavaDoc nm=n.getAttributes();
170                     if (nm!=null) {
171                         String JavaDoc name=null;
172                         String JavaDoc path=null;
173
174                         // decode name
175
org.w3c.dom.Node JavaDoc n3=nm.getNamedItem("name");
176                         if (n3!=null) {
177                             name=n3.getNodeValue();
178                         }
179
180                         // decode path
181
n3=nm.getNamedItem("path");
182                         if (n3!=null) {
183                             path=n3.getNodeValue();
184                         }
185
186                         if (path!=null && name!=null) {
187                             Project p=new Project(name,path);
188                             projects.put(name,p);
189                         }
190                     }
191                 }
192             }
193         } else {
194             log.error("missing projects file : "+filename);
195         }
196     }
197
198
199     /**
200     * read the Creators from disc, the file is defined in your
201     * config directory. Uses a xml reader and the dtd's found as
202     * resources.
203     */

204     public static void readCreators() {
205         creators = new HashMap();
206
207         // XXX Should use ResourceLoader here
208

209         String JavaDoc filename = PackageManager.getConfigPath() + File.separator + "packaging" + File.separator + "creators.xml";
210
211         File JavaDoc file = new File JavaDoc(filename);
212         if(file.exists()) {
213             ExtendedDocumentReader reader = new ExtendedDocumentReader(filename, ProjectManager.class);
214             if(reader != null) {
215                 for(Iterator ns = reader.getChildElements("creators","creator");ns.hasNext(); ) {
216                     Element JavaDoc n = (Element JavaDoc)ns.next();
217                     NamedNodeMap JavaDoc nm = n.getAttributes();
218                     if (nm != null) {
219                         String JavaDoc type = null;
220                         String JavaDoc classname = null;
221
222                         // decode type
223
org.w3c.dom.Node JavaDoc n2 = nm.getNamedItem("type");
224                         if (n2 != null) {
225                             type = n2.getNodeValue();
226                         }
227
228                         // decode the class
229
n2 = nm.getNamedItem("class");
230                         if (n2 != null) {
231                             classname = n2.getNodeValue();
232                         }
233                         try {
234                             Class JavaDoc newclass = Class.forName(classname);
235                             CreatorInterface cr = (CreatorInterface)newclass.newInstance();
236                             cr.setType(type);
237                             if (cr !=null) creators.put(type,cr);
238                         } catch (Exception JavaDoc e) {
239                             log.error("Can't create creator : "+classname+" for type "+type);
240                             e.printStackTrace();
241                         }
242                     }
243                 }
244             }
245         } else {
246             log.error("missing creator file : "+filename);
247         }
248     }
249
250     /**
251     * get all the defined creators we can use (xml file)
252     *
253     * @return creators
254     */

255     public static Map JavaDoc getCreators() {
256         return creators;
257     }
258
259     /**
260     * get a creator based on its mimetype
261     *
262     * @return creator or null if non is found for the asked mimetype
263     */

264     public static CreatorInterface getCreatorByType(String JavaDoc type) {
265         Object JavaDoc o = creators.get(type);
266         if (o != null) return (CreatorInterface)o;
267         return null;
268     }
269
270
271    /**
272    * save/sync the projects file to disk.
273    *
274    * @return true if file saved/synced, false if something went wrong
275    */

276    public static boolean save() {
277        String JavaDoc body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
278        body += "<!DOCTYPE projects PUBLIC \"-//MMBase/DTD projects config 1.0//EN\" \"http://www.mmbase.org/dtd/projects_1_0.dtd\">\n";
279        body += "<projects>\n";
280        Iterator e=projects.values().iterator();
281        while (e.hasNext()) {
282            Project pr = (Project)e.next();
283            body += "\t<project name=\""+pr.getName()+"\" path=\""+pr.getPath()+"\" />\n";
284        }
285        body += "</projects>\n";
286        String JavaDoc filename = PackageManager.getConfigPath()+File.separator+"packaging"+File.separator+"projects.xml";
287        File JavaDoc sfile = new File JavaDoc(filename);
288        try {
289             DataOutputStream JavaDoc scan = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(sfile));
290             scan.writeBytes(body);
291             scan.flush();
292             scan.close();
293         } catch(Exception JavaDoc f) {
294             log.error(Logging.stackTrace(f));
295         }
296         return true;
297     }
298 }
299
Popular Tags