KickJava   Java API By Example, From Geeks To Geeks.

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


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.ems.connection.settings.ConnectionSettings;
21 import org.openide.ErrorManager;
22
23 import java.beans.XMLDecoder JavaDoc;
24 import java.beans.XMLEncoder JavaDoc;
25 import java.beans.DefaultPersistenceDelegate JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.SortedSet JavaDoc;
33 import java.util.TreeSet JavaDoc;
34
35 /**
36  * TODO GH: Maybe just translate this to util.prefs storage...
37  * it would be nice not to lose connections this on upgrade
38  *
39  *
40  * @author Corby Page 09.07.02
41  * @author Greg Hinkle (ghinkle@users.sourceforge.net), December 2002
42  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
43  */

44 public class ConnectionSetDatabase {
45
46     private final static String JavaDoc DATABASE_FILENAME = "ConnectionNodes.xml";
47     private static SortedSet JavaDoc<ConnectionSettings> descriptors =
48         new TreeSet JavaDoc<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 JavaDoc getNodes() {
68         if (ConnectionSetDatabase.descriptors.isEmpty()) {
69             loadNodes();
70         }
71         return new TreeSet JavaDoc(ConnectionSetDatabase.descriptors);
72     }
73
74     protected synchronized static void saveNodes() {
75
76         FileOutputStream JavaDoc fos= null;
77         try {
78
79             fos = new FileOutputStream JavaDoc(getDatabaseFile(), false);
80             XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc(fos);
81             encoder.setPersistenceDelegate(
82                     File JavaDoc.class,
83                     new DefaultPersistenceDelegate JavaDoc(new String JavaDoc[] { "absolutePath" }));
84
85
86             encoder.writeObject(descriptors);
87
88             encoder.close();
89         } catch (FileNotFoundException JavaDoc e) {
90             e.printStackTrace();
91         } finally {
92             try {
93                 if ( fos != null ) {
94                     fos.close();
95                 }
96             }
97             catch( IOException JavaDoc 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 JavaDoc fis = null;
108         try {
109
110             fis = new FileInputStream JavaDoc(getDatabaseFile());
111             XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc(fis);
112             ConnectionSetDatabase.descriptors.clear();
113
114             descriptors = (SortedSet JavaDoc<ConnectionSettings>) decoder.readObject();
115
116         } catch (FileNotFoundException JavaDoc e) {
117             e.printStackTrace();
118         } finally {
119             try {
120                 if ( fis != null ) {
121                     fis.close();
122                 }
123             }
124             catch( IOException JavaDoc ioEx ) {
125                 org.openide.windows.IOProvider.getDefault().getStdOut().println(
126                 "Could not close inputStream" );
127             }
128         }
129     }
130
131     protected static File JavaDoc getDatabaseFile() {
132         File JavaDoc connectionsFile = null;
133
134         /*
135             InstalledFileLocator.getDefault().locate(
136                 DATABASE_FILENAME,
137                 "org.mc4j.console",
138                 false);
139         */

140
141         if (connectionsFile == null) {
142             connectionsFile = new File JavaDoc(DATABASE_FILENAME);
143         }
144
145         if (!connectionsFile.exists()) {
146             try {
147                 connectionsFile.createNewFile();
148             } catch (IOException JavaDoc ioe) {
149                 ErrorManager.getDefault().notify(ioe);
150             }
151         }
152         return connectionsFile;
153     }
154
155
156 }
Popular Tags