KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > connection > persistence > ServerInstallSetDatabase


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InvalidClassException JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33 import java.io.StreamCorruptedException JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * @author Greg Hinkle (ghinkle@users.sourceforge.net), September 30, 2004
41  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
42  */

43 public class ServerInstallSetDatabase {
44
45     private final static String JavaDoc DATABASE_FILENAME = "ServerInstalls.db";
46     private static Set JavaDoc<ServerInstallInfo> descriptors = Collections.synchronizedSet(new HashSet JavaDoc());
47
48     public synchronized static boolean addNode(ServerInstallInfo descriptor) {
49         ObjectOutputStream JavaDoc outStream = null;
50         ServerInstallSetDatabase.descriptors.remove(descriptor);
51         ServerInstallSetDatabase.descriptors.add(descriptor);
52
53
54         try {
55             if (getDatabaseFile() == null) {
56                 File JavaDoc file = new File JavaDoc(DATABASE_FILENAME);
57                 file.createNewFile();
58             }
59             outStream =
60                 new ObjectOutputStream JavaDoc(
61                     new FileOutputStream JavaDoc(
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 JavaDoc 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 JavaDoc 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 JavaDoc outStream = null;
94
95         ServerInstallSetDatabase.descriptors.remove( descriptor );
96
97         try {
98             outStream =
99                 new ObjectOutputStream JavaDoc(
100                     new FileOutputStream JavaDoc(
101                         getDatabaseFile()));
102
103             int size = ServerInstallSetDatabase.descriptors.size();
104             outStream.writeInt( size );
105             Iterator JavaDoc iter = descriptors.iterator();
106             while (iter.hasNext()) {
107                 ServerInstallInfo element = (ServerInstallInfo)iter.next();
108                 outStream.writeObject( element );
109             }
110         } catch( Exception JavaDoc 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 JavaDoc ioEx ) {
118                 IOProvider.getDefault().getStdOut().println(
119                 "Could not close outputStream" );
120             }
121         }
122     }
123
124     public synchronized static Set JavaDoc<ServerInstallInfo> getNodes() {
125         if (ServerInstallSetDatabase.descriptors.isEmpty()) {
126             loadNodes();
127         }
128         Set JavaDoc nodes = new HashSet JavaDoc();
129         nodes.addAll(ServerInstallSetDatabase.descriptors);
130         return nodes; //(new HashSet()).addAll(ConnectionSetDatabase.descriptors);
131
}
132
133     protected synchronized static void loadNodes() {
134         ObjectInputStream JavaDoc inStream = null;
135
136         //IOProvider.getDefault().getStdOut().println("Loading connections");
137

138         ServerInstallSetDatabase.descriptors.clear();
139
140         try {
141             inStream =
142                 new ObjectInputStream JavaDoc(
143                     new FileInputStream JavaDoc(
144                         getDatabaseFile()));
145             //IOProvider.getDefault().getStdOut().println("Reading connections");
146
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 JavaDoc sce) {
152                     // Ignore. We'll throw this away later...
153
} catch (InvalidClassException JavaDoc ice) {
154                     // old version of class.. fur-get-about-it
155
} catch(ClassCastException JavaDoc cce) {
156                     // old version of class... don't load
157
}
158             }
159         } catch( FileNotFoundException JavaDoc ex ) {
160             IOProvider.getDefault().getStdOut().println(
161             "Node file missing: " + ex);
162             ErrorManager.getDefault().notify(ex);
163         } catch (EOFException JavaDoc eofe) {
164             // Happens when there are no connections... its cool
165
} catch( Exception JavaDoc 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 JavaDoc ioEx ) {
176                 IOProvider.getDefault().getStdOut().println(
177                 "Could not close inputStream" );
178             }
179         }
180     }
181
182     protected static File JavaDoc getDatabaseFile() {
183         File JavaDoc connectionsFile = null;
184
185         /*
186             InstalledFileLocator.getDefault().locate(
187                 DATABASE_FILENAME,
188                 "org.mc4j.console",
189                 false);
190         */

191
192         if (connectionsFile == null) {
193             connectionsFile = new File JavaDoc(DATABASE_FILENAME);
194         }
195
196         if (!connectionsFile.exists()) {
197             try {
198                 connectionsFile.createNewFile();
199             } catch (IOException JavaDoc ioe) {
200                 ErrorManager.getDefault().notify(ioe);
201             }
202         }
203         return connectionsFile;
204     }
205 }
Popular Tags