1 16 17 package org.mc4j.console.connection.persistence; 18 19 20 import org.mc4j.ems.connection.settings.ConnectionSettings; 21 import org.openide.ErrorManager; 22 23 import java.beans.XMLDecoder ; 24 import java.beans.XMLEncoder ; 25 import java.beans.DefaultPersistenceDelegate ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.util.Set ; 32 import java.util.SortedSet ; 33 import java.util.TreeSet ; 34 35 44 public class ConnectionSetDatabase { 45 46 private final static String DATABASE_FILENAME = "ConnectionNodes.xml"; 47 private static SortedSet <ConnectionSettings> descriptors = 48 new TreeSet <ConnectionSettings>(); 49 50 51 52 53 54 public synchronized static boolean addNode(ConnectionSettings descriptor) { 55 boolean found = descriptors.add(descriptor); 56 57 saveNodes(); 58 return found; 59 } 60 61 public synchronized static void removeNode(ConnectionSettings descriptor) { 62 descriptors.remove(descriptor); 63 saveNodes(); 64 65 } 66 67 public synchronized static Set getNodes() { 68 if (ConnectionSetDatabase.descriptors.isEmpty()) { 69 loadNodes(); 70 } 71 return new TreeSet (ConnectionSetDatabase.descriptors); 72 } 73 74 protected synchronized static void saveNodes() { 75 76 FileOutputStream fos= null; 77 try { 78 79 fos = new FileOutputStream (getDatabaseFile(), false); 80 XMLEncoder encoder = new XMLEncoder (fos); 81 encoder.setPersistenceDelegate( 82 File .class, 83 new DefaultPersistenceDelegate (new String [] { "absolutePath" })); 84 85 86 encoder.writeObject(descriptors); 87 88 encoder.close(); 89 } catch (FileNotFoundException e) { 90 e.printStackTrace(); 91 } finally { 92 try { 93 if ( fos != null ) { 94 fos.close(); 95 } 96 } 97 catch( IOException ioEx ) { 98 org.openide.windows.IOProvider.getDefault().getStdOut().println( 99 "Could not close inputStream" ); 100 } 101 } 102 } 103 104 105 protected synchronized static void loadNodes() { 106 107 FileInputStream fis = null; 108 try { 109 110 fis = new FileInputStream (getDatabaseFile()); 111 XMLDecoder decoder = new XMLDecoder (fis); 112 ConnectionSetDatabase.descriptors.clear(); 113 114 descriptors = (SortedSet <ConnectionSettings>) decoder.readObject(); 115 116 } catch (FileNotFoundException e) { 117 e.printStackTrace(); 118 } finally { 119 try { 120 if ( fis != null ) { 121 fis.close(); 122 } 123 } 124 catch( IOException ioEx ) { 125 org.openide.windows.IOProvider.getDefault().getStdOut().println( 126 "Could not close inputStream" ); 127 } 128 } 129 } 130 131 protected static File getDatabaseFile() { 132 File connectionsFile = null; 133 134 140 141 if (connectionsFile == null) { 142 connectionsFile = new File (DATABASE_FILENAME); 143 } 144 145 if (!connectionsFile.exists()) { 146 try { 147 connectionsFile.createNewFile(); 148 } catch (IOException ioe) { 149 ErrorManager.getDefault().notify(ioe); 150 } 151 } 152 return connectionsFile; 153 } 154 155 156 } | Popular Tags |