1 16 17 package org.mc4j.console.connection.persistence; 18 19 20 import org.mc4j.console.connection.install.finder.ServerInstallInfo; 21 import org.openide.ErrorManager; 22 import org.openide.windows.IOProvider; 23 24 import java.io.EOFException ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InvalidClassException ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 import java.io.StreamCorruptedException ; 34 import java.util.Collections ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.Set ; 38 39 43 public class ServerInstallSetDatabase { 44 45 private final static String DATABASE_FILENAME = "ServerInstalls.db"; 46 private static Set <ServerInstallInfo> descriptors = Collections.synchronizedSet(new HashSet ()); 47 48 public synchronized static boolean addNode(ServerInstallInfo descriptor) { 49 ObjectOutputStream outStream = null; 50 ServerInstallSetDatabase.descriptors.remove(descriptor); 51 ServerInstallSetDatabase.descriptors.add(descriptor); 52 53 54 try { 55 if (getDatabaseFile() == null) { 56 File file = new File (DATABASE_FILENAME); 57 file.createNewFile(); 58 } 59 outStream = 60 new ObjectOutputStream ( 61 new FileOutputStream ( 62 getDatabaseFile())); 63 64 int size = ServerInstallSetDatabase.descriptors.size(); 65 outStream.writeInt( size ); 66 for (ServerInstallInfo element : descriptors) { 67 outStream.writeObject(element); 68 } 69 } 70 catch( Exception ex ) { 71 72 IOProvider.getDefault().getStdOut().println( 73 "Caught exception " + ex + " when adding node" ); 74 75 ErrorManager.getDefault().notify(ex); 76 77 ServerInstallSetDatabase.descriptors.remove( descriptor ); 78 } 79 finally { 80 try { 81 outStream.close(); 82 } 83 catch( IOException ioEx ) { 84 IOProvider.getDefault().getStdOut().println( 85 "Could not close outputStream" ); 86 } 87 } 88 89 return true; 90 } 91 92 public synchronized static void removeNode(ServerInstallInfo descriptor) { 93 ObjectOutputStream outStream = null; 94 95 ServerInstallSetDatabase.descriptors.remove( descriptor ); 96 97 try { 98 outStream = 99 new ObjectOutputStream ( 100 new FileOutputStream ( 101 getDatabaseFile())); 102 103 int size = ServerInstallSetDatabase.descriptors.size(); 104 outStream.writeInt( size ); 105 Iterator iter = descriptors.iterator(); 106 while (iter.hasNext()) { 107 ServerInstallInfo element = (ServerInstallInfo)iter.next(); 108 outStream.writeObject( element ); 109 } 110 } catch( Exception ex ) { 111 IOProvider.getDefault().getStdOut().println( 112 "Caught exception " + ex + " when removing node" ); 113 ServerInstallSetDatabase.descriptors.add( descriptor ); 114 } finally { 115 try { 116 outStream.close(); 117 } catch( IOException ioEx ) { 118 IOProvider.getDefault().getStdOut().println( 119 "Could not close outputStream" ); 120 } 121 } 122 } 123 124 public synchronized static Set <ServerInstallInfo> getNodes() { 125 if (ServerInstallSetDatabase.descriptors.isEmpty()) { 126 loadNodes(); 127 } 128 Set nodes = new HashSet (); 129 nodes.addAll(ServerInstallSetDatabase.descriptors); 130 return nodes; } 132 133 protected synchronized static void loadNodes() { 134 ObjectInputStream inStream = null; 135 136 138 ServerInstallSetDatabase.descriptors.clear(); 139 140 try { 141 inStream = 142 new ObjectInputStream ( 143 new FileInputStream ( 144 getDatabaseFile())); 145 int size = inStream.readInt(); 147 for ( int i=0; i<size; i++ ) { 148 try { 149 ServerInstallInfo info = (ServerInstallInfo) inStream.readObject(); 150 descriptors.add( info ); 151 } catch (StreamCorruptedException sce) { 152 } catch (InvalidClassException ice) { 154 } catch(ClassCastException cce) { 156 } 158 } 159 } catch( FileNotFoundException ex ) { 160 IOProvider.getDefault().getStdOut().println( 161 "Node file missing: " + ex); 162 ErrorManager.getDefault().notify(ex); 163 } catch (EOFException eofe) { 164 } catch( Exception ex ) { 166 IOProvider.getDefault().getStdOut().println( 167 "Caught exception " + ex + " when getting nodes" ); 168 ErrorManager.getDefault().notify(ex); 169 } finally { 170 try { 171 if ( inStream != null ) { 172 inStream.close(); 173 } 174 } 175 catch( IOException ioEx ) { 176 IOProvider.getDefault().getStdOut().println( 177 "Could not close inputStream" ); 178 } 179 } 180 } 181 182 protected static File getDatabaseFile() { 183 File connectionsFile = null; 184 185 191 192 if (connectionsFile == null) { 193 connectionsFile = new File (DATABASE_FILENAME); 194 } 195 196 if (!connectionsFile.exists()) { 197 try { 198 connectionsFile.createNewFile(); 199 } catch (IOException ioe) { 200 ErrorManager.getDefault().notify(ioe); 201 } 202 } 203 return connectionsFile; 204 } 205 } | Popular Tags |