KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > jdb > JdbSession


1 /*
2  * JdbSession.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: JdbSession.java,v 1.5 2003/06/29 17:50:41 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.jdb;
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.EOFException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Properties JavaDoc;
34 import org.armedbear.j.Directories;
35 import org.armedbear.j.Editor;
36 import org.armedbear.j.File;
37 import org.armedbear.j.Log;
38 import org.armedbear.j.Utilities;
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.ContentHandler JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 import org.xml.sax.XMLReader JavaDoc;
44 import org.xml.sax.helpers.DefaultHandler JavaDoc;
45
46 public final class JdbSession extends Properties JavaDoc
47 {
48     private List JavaDoc breakpointSpecifications;
49     private List JavaDoc breakpoints;
50
51     public JdbSession()
52     {
53     }
54
55     public String JavaDoc getName()
56     {
57         return getProperty("name", "");
58     }
59
60     private static File jdbDir;
61
62     private static File getSettingsDirectory()
63     {
64         if (jdbDir == null) {
65             jdbDir = File.getInstance(Directories.getEditorDirectory(), "jdb");
66             if (!jdbDir.isDirectory())
67                 jdbDir.mkdirs();
68         }
69         return jdbDir;
70     }
71
72     private static File sessionDir;
73
74     private File getDefaultSessionFile()
75     {
76         return File.getInstance(getSettingsDirectory(), "defaults.xml");
77     }
78
79     public static String JavaDoc[] getSessionNames()
80     {
81         return sessionDir.list();
82     }
83
84     public static void deleteSession(String JavaDoc name)
85     {
86         File file = File.getInstance(sessionDir, name);
87         if (file.isFile())
88             file.delete();
89     }
90
91     public String JavaDoc getMainClass()
92     {
93         return getProperty("mainClass", "");
94     }
95
96     public void setMainClass(String JavaDoc s)
97     {
98         put("mainClass", s);
99     }
100
101     public String JavaDoc getMainClassArgs()
102     {
103         return getProperty("mainClassArgs", "");
104     }
105
106     public void setMainClassArgs(String JavaDoc s)
107     {
108         put("mainClassArgs", s);
109     }
110
111     public String JavaDoc getClassPath()
112     {
113         return getProperty("classPath", "");
114     }
115
116     public void setClassPath(String JavaDoc s)
117     {
118         put("classPath", s);
119     }
120
121     public String JavaDoc getJavaHome()
122     {
123         return getProperty("javaHome", "");
124     }
125
126     public void setJavaHome(String JavaDoc s)
127     {
128         put("javaHome", s);
129     }
130
131     public String JavaDoc getJavaExecutable()
132     {
133         return getProperty("javaExecutable", "");
134     }
135
136     public void setJavaExecutable(String JavaDoc s)
137     {
138         put("javaExecutable", s);
139     }
140
141     public String JavaDoc getVMArgs()
142     {
143         return getProperty("vmArgs", "");
144     }
145
146     public void setVMArgs(String JavaDoc s)
147     {
148         put("vmArgs", s);
149     }
150
151     public boolean getStartSuspended()
152     {
153         String JavaDoc s = getProperty("startSuspended");
154         return s != null && s.equals("true");
155     }
156
157     public void setStartSuspended(boolean b)
158     {
159         put("startSuspended", b ? "true" : "false");
160     }
161
162     public String JavaDoc getSourcePath()
163     {
164         return getProperty("sourcePath", "");
165     }
166
167     public void setSourcePath(String JavaDoc s)
168     {
169         put("sourcePath", s);
170     }
171
172     public void saveDefaults()
173     {
174         save(getDefaultSessionFile());
175     }
176
177     public void loadDefaults()
178     {
179         clear();
180         File file = getDefaultSessionFile();
181         if (file != null && file.isFile())
182             load(file);
183     }
184
185     private void save(File file)
186     {
187         try {
188             OutputStream JavaDoc out = file.getOutputStream();
189             BufferedWriter JavaDoc writer =
190                 new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(out));
191             writer.write("<?xml version=\"1.0\"?>");
192             writer.newLine();
193             writer.write("<session version=\"" + getVersion() + "\">");
194             writer.newLine();
195             Enumeration JavaDoc propertyNames = propertyNames();
196             while (propertyNames.hasMoreElements()) {
197                 String JavaDoc name = (String JavaDoc) propertyNames.nextElement();
198                 String JavaDoc value = getProperty(name);
199                 writer.write(" ");
200                 writer.write(Utilities.propertyToXml(name, value));
201                 writer.newLine();
202             }
203             saveBreakpoints(writer);
204             writer.write("</session>");
205             writer.flush();
206             writer.close();
207         }
208         catch (IOException JavaDoc e) {
209             Log.error(e);
210         }
211     }
212
213     private void saveBreakpoints(BufferedWriter JavaDoc writer)
214     {
215         if (breakpoints != null && breakpoints.size() > 0) {
216             try {
217                 writer.write(" <breakpoints>");
218                 writer.newLine();
219                 Iterator JavaDoc iter = breakpoints.iterator();
220                 while (iter.hasNext()) {
221                     Object JavaDoc obj = iter.next();
222                     if (obj instanceof MethodBreakpoint) {
223                         MethodBreakpoint bp = (MethodBreakpoint) obj;
224                         if (!bp.isTemporary())
225                             writer.write(bp.toXml());
226                     } else if (obj instanceof LineNumberBreakpoint) {
227                         LineNumberBreakpoint bp = (LineNumberBreakpoint) obj;
228                         if (!bp.isTemporary())
229                             writer.write(bp.toXml());
230                     }
231                 }
232                 writer.write(" </breakpoints>");
233                 writer.newLine();
234             }
235             catch (IOException JavaDoc e) {
236                 Log.error(e);
237             }
238         }
239     }
240
241     private void load(File file)
242     {
243         XMLReader JavaDoc xmlReader = Utilities.getDefaultXMLReader();
244         if (xmlReader != null) {
245             xmlReader.setContentHandler(new Handler JavaDoc());
246             try {
247                 InputSource JavaDoc inputSource = new InputSource JavaDoc(file.getInputStream());
248                 xmlReader.parse(inputSource);
249             }
250             catch (EOFException JavaDoc ignored) {}
251             catch (Exception JavaDoc e) {
252                 Log.error(e);
253             }
254         }
255     }
256
257     public List JavaDoc getBreakpointSpecifications()
258     {
259         return breakpointSpecifications;
260     }
261
262     public void setBreakpoints(List JavaDoc breakpoints)
263     {
264         this.breakpoints = breakpoints;
265     }
266
267     private static final String JavaDoc getVersion()
268     {
269         return "1";
270     }
271
272     private class Handler extends DefaultHandler JavaDoc implements ContentHandler JavaDoc
273     {
274         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
275             Attributes JavaDoc attributes) throws SAXException JavaDoc
276         {
277             if (localName.equals("session") || qName.equals("session")) {
278                 String JavaDoc version = attributes.getValue("version");
279                 if (!version.equals(getVersion()))
280                     throw new SAXException JavaDoc("Unknown session format");
281             } else if (localName.equals("property") || qName.equals("property")) {
282                 // Session property.
283
String JavaDoc propertyName = attributes.getValue("name");
284                 String JavaDoc value = attributes.getValue("value");
285                 setProperty(propertyName, value);
286             } else if (localName.equals("breakpoints") || qName.equals("breakpoints")) {
287                 breakpointSpecifications = new ArrayList JavaDoc();
288             } else if (localName.equals("breakpoint") || qName.equals("breakpoint")) {
289                 BreakpointSpecification spec =
290                     new BreakpointSpecification(attributes);
291                 breakpointSpecifications.add(spec);
292             }
293         }
294     }
295 }
296
Popular Tags