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