KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > app > PropertyFileApplicationStorage


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.app;
27
28 import org.snipsnap.config.Configuration;
29 import org.snipsnap.config.Globals;
30 import org.snipsnap.jdbc.UIDGenerator;
31 import org.radeox.util.logging.Logger;
32
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.FileFilter JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 public class PropertyFileApplicationStorage implements ApplicationStorage {
44   private final static String JavaDoc PREFIX_FILE = "prefix.properties";
45
46   Map JavaDoc applications;
47   File JavaDoc appFile = null;
48
49   public PropertyFileApplicationStorage() {
50     applications = new HashMap JavaDoc();
51     Configuration config = Application.get().getConfiguration();
52     File JavaDoc fileStore = new File JavaDoc(config.getGlobal(Globals.APP_FILE_STORE));
53     File JavaDoc[] instances = fileStore.listFiles(new FileFilter JavaDoc() {
54       public boolean accept(File JavaDoc file) {
55         return file.isDirectory();
56       }
57     });
58
59     for(int f = 0; f < instances.length; f++) {
60       File JavaDoc prefixInfo = new File JavaDoc(instances[f], PREFIX_FILE);
61       if(prefixInfo.exists()) {
62         Properties JavaDoc prefixProps = new Properties JavaDoc();
63         try {
64           prefixProps.load(new FileInputStream JavaDoc(prefixInfo));
65           applications.put(prefixProps.getProperty(PREFIX), prefixProps);
66         } catch (IOException JavaDoc e) {
67           Logger.warn("ignoring prefix: "+instances[f].getPath());
68         }
69       }
70     }
71   }
72
73
74   public Map JavaDoc getApplications() {
75     return applications;
76   }
77
78   public void removeApplication(String JavaDoc oid) {
79     Iterator JavaDoc it = applications.keySet().iterator();
80     while(it.hasNext()) {
81       String JavaDoc prefix = (String JavaDoc)it.next();
82       Properties JavaDoc prefixProps = (Properties JavaDoc)applications.get(prefix);
83       if(oid.equals(prefixProps.getProperty(OID))) {
84         File JavaDoc prefixFile = getPrefixFile(prefixProps);
85         prefixFile.renameTo(new File JavaDoc(prefixFile.getPath()+".removed"));
86         it.remove();
87       }
88     }
89   }
90
91   public Properties JavaDoc createApplication(String JavaDoc name, String JavaDoc prefix) {
92     String JavaDoc oid = UIDGenerator.generate(ApplicationStorage.class);
93     Properties JavaDoc prefixProps = new Properties JavaDoc();
94     prefixProps.setProperty(OID, oid);
95     prefixProps.setProperty(PREFIX, prefix);
96     prefixProps.setProperty(NAME, name);
97     applications.put(prefix, prefixProps);
98     save(prefixProps);
99     return prefixProps;
100   }
101
102   private void save(Properties JavaDoc prefixProps) {
103     try {
104       File JavaDoc prefixFile = getPrefixFile(prefixProps);
105       prefixFile.getParentFile().mkdirs();
106       prefixProps.store(new FileOutputStream JavaDoc(prefixFile), "$Revision: 1257 $");
107     } catch (IOException JavaDoc e) {
108       System.err.println("unable to store prefix properties: " + e);
109       e.printStackTrace();
110     }
111   }
112
113   private File JavaDoc getPrefixFile(Properties JavaDoc prefixProps) {
114     Globals config = Application.get().getConfiguration();
115     File JavaDoc fileStore = new File JavaDoc(config.getGlobal(Globals.APP_FILE_STORE));
116     File JavaDoc prefixDir = new File JavaDoc(fileStore, prefixProps.getProperty(OID));
117     return new File JavaDoc(prefixDir, PREFIX_FILE);
118   }
119 }
120
Popular Tags