KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > settings > ExceptionsRepos


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ExceptionsRepos.java
22  *
23  * Created on December 4, 2006, 5:43 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.exceptions.settings;
30
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.BufferedOutputStream JavaDoc;
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.ObjectInputStream JavaDoc;
39 import java.io.ObjectOutputStream JavaDoc;
40 import java.io.OutputStream JavaDoc;
41 import java.net.HttpURLConnection JavaDoc;
42 import java.util.Arrays JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.LinkedList JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.LogRecord JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50 import org.netbeans.modules.exceptions.ExceptionsHandler;
51 import org.netbeans.modules.exceptions.ExceptionLogger;
52 import org.netbeans.modules.exceptions.Sender;
53 import org.openide.filesystems.FileLock;
54 import org.openide.filesystems.FileObject;
55 import org.openide.filesystems.Repository;
56 import org.openide.util.NbBundle;
57
58 /**
59  *
60  * @author jindra
61  */

62 public class ExceptionsRepos {
63     private static ExceptionsRepos instance = null;
64     private static String JavaDoc dirName = "Exceptions"; // NOI18N
65
private static String JavaDoc reposName = "ExceptionsRepos"; // NOI18N
66
private static String JavaDoc componentsName = "Components"; // NOI18N
67
private static LinkedList JavaDoc<LogRecord JavaDoc> logs=null;
68     private static FileObject reposfile = null;
69     private static HashMap JavaDoc/*<String, String[]>*/ components = null; // keys = components, values = subcomponents
70

71     //public static final String uri_server = "http://localhost:8080/ServerExceptions/"; // NOI18N
72
public static final String JavaDoc uri_server = "http://qa-sol10-s1.czech:8080/ServerExceptions/"; // NOI18N
73

74     private static final String JavaDoc uri = uri_server + "ComponentsServlet"; // NOI18N /** Creates a new instance of ExceptionsRepos */
75
private ExceptionsRepos() {
76         
77     }
78     
79     public static synchronized ExceptionsRepos getInstance(){
80         if (instance == null) instance = new ExceptionsRepos();
81         return instance;
82     }
83     
84     public synchronized void addRecord(LogRecord JavaDoc rec){
85         rec.setThrown(ExceptionLogger.convert(rec.getThrown()));
86         getRecords().add(rec);
87     }
88     
89     public boolean contains(LogRecord JavaDoc rec){
90         LinkedList JavaDoc<LogRecord JavaDoc> list = getRecords();
91         Throwable JavaDoc thrown = rec.getThrown();
92         for (Iterator JavaDoc<LogRecord JavaDoc> it = list.iterator(); it.hasNext();) {
93             LogRecord JavaDoc logRecord = it.next();
94             if (ExceptionsHandler.equalsThrows(logRecord.getThrown(), thrown)) return true;
95         }
96         return false;
97     }
98     
99     public void close(){
100         if (logs != null) saveReposToFile();
101         if (components!= null) saveComponentsToFile();
102     }
103     
104     public synchronized String JavaDoc[] getComponents(){
105         if (components == null) components = loadComponents();
106         if (components == null) return new String JavaDoc[0];
107         String JavaDoc[] array = new String JavaDoc[components.size()];
108         int i = 0;
109         Iterator JavaDoc it = components.keySet().iterator();
110         while (it.hasNext()){
111             array[i++] = it.next().toString();
112         }
113         Arrays.sort(array);
114         return array;
115     }
116     
117     public synchronized String JavaDoc[] getSubcomponents(String JavaDoc componentName){
118         if (components == null) components = loadComponents();
119         if (components == null) return new String JavaDoc[0];
120         Object JavaDoc o = components.get(componentName);
121         String JavaDoc[] array = null;
122         if (o instanceof String JavaDoc[]) array = (String JavaDoc[]) o;
123         Arrays.sort(array);
124         return array;
125     }
126     
127     private void saveReposToFile(){
128         Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "Saving repos to file"); // NOI18N
129
FileLock lock = null;
130         try{
131             lock = reposfile.lock();
132             OutputStream JavaDoc oStream= getRepos().getOutputStream(lock);
133             ObjectOutputStream JavaDoc objectOStream = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(oStream));
134             objectOStream.writeObject(logs);
135             objectOStream.close();
136             oStream.close();
137         }catch(IOException JavaDoc ex){
138             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.SEVERE,
139                     ex.getMessage(),
140                     ex);
141         }finally{
142             if (lock!= null) lock.releaseLock();
143         }
144     }
145     
146     private void saveComponentsToFile(){
147         Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "Saving components to file"); // NOI18N
148
FileLock lock = null;
149         try{
150             FileObject file = getFile(componentsName);
151             lock = file.lock();
152             OutputStream JavaDoc oStream= file.getOutputStream(lock);
153             ObjectOutputStream JavaDoc objectOStream = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(oStream));
154             objectOStream.writeObject(components);
155             objectOStream.close();
156             oStream.close();
157         }catch(IOException JavaDoc ex){
158             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.SEVERE,
159                     ex.getMessage(),
160                     ex);
161         }finally{
162             if (lock!= null) lock.releaseLock();
163         }
164     }
165     
166     private HashMap JavaDoc/*<String, String[]> */loadComponents(){
167         Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.INFO, "Loading components"); // NOI18N
168
FileLock lock = null;
169         HashMap JavaDoc/*<String, String[]> */map = null;
170         try {
171             FileObject file = getFile(componentsName);
172             if (file.getSize() < 100){// there is nothing in this files
173
// loading data from server
174
map = loadFromServer();
175                 //saving them into a file
176
if (map != null) saveComponentsToFile();
177             }else{
178                 Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "Loading components from file"); // NOI18N
179
InputStream JavaDoc stream = file.getInputStream();
180                 ObjectInputStream JavaDoc iStream = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(stream));
181                 map = read(iStream);
182                 iStream.close();
183                 stream.close();
184             }
185             /* if there is an exception it can't be catched again by handler, as it would cause a recursive error */
186         } catch (ClassNotFoundException JavaDoc ex) {
187             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.INFO,
188                     ex.getMessage());
189         } catch (IOException JavaDoc ex) {
190             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.WARNING,
191                     NbBundle.getBundle(ExceptionsRepos.class).getString("NO_CONNECTION"));
192         }finally{
193             if (lock != null) lock.releaseLock();
194         }
195         if (map != null)Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "DONE loading components "+Integer.toString(map.size())); // NOI18N
196
else Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "No Components Loaded"); // NOI18N
197
return map;
198     }
199     
200     private HashMap JavaDoc/*<String, String[]> */loadFromServer() throws IOException JavaDoc, ClassNotFoundException JavaDoc{
201         Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "Loading components from server"); // NOI18N
202
HashMap JavaDoc/*<String, String[]> */map = null;
203         HttpURLConnection JavaDoc conn = Sender.getHttpConnection(uri);
204         if (conn != null){
205             InputStream JavaDoc stream = conn.getInputStream();
206             ByteArrayOutputStream JavaDoc bStream = new ByteArrayOutputStream JavaDoc();
207             int i;
208             while ((i = stream.read())!= -1) bStream.write(i);
209             ObjectInputStream JavaDoc iStream = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bStream.toByteArray()));
210             map = read(iStream);
211             iStream.close();
212             stream.close();
213             conn.disconnect();
214         }
215         return map;
216     }
217     
218     private HashMap JavaDoc read(ObjectInputStream JavaDoc iStream) throws IOException JavaDoc, ClassNotFoundException JavaDoc{
219         Object JavaDoc o = iStream.readObject();
220         if (o instanceof HashMap JavaDoc){
221             return (HashMap JavaDoc) o;
222         }
223         return null;
224     }
225     
226     private LinkedList JavaDoc<LogRecord JavaDoc> getRecords(){
227         FileLock lock = null;
228         if (logs==null){
229             try{
230                 lock = getRepos().lock();
231                 InputStream JavaDoc iStream = getRepos().getInputStream();
232                 if (iStream.available() > 0){
233                     logs = readLogs(iStream);
234                 }
235                 iStream.close();
236             } catch (IOException JavaDoc ex) {
237                 java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.WARNING,
238                         ex.getMessage(),
239                         ex);
240             }finally{
241                 if (lock != null) lock.releaseLock();
242             }
243         }
244         if (logs == null) logs = new LinkedList JavaDoc<LogRecord JavaDoc>();// create new empty logs list
245
return logs;
246     }
247     
248     private LinkedList JavaDoc<LogRecord JavaDoc> readLogs(InputStream JavaDoc iStream){
249         LinkedList JavaDoc<LogRecord JavaDoc> lgs = new LinkedList JavaDoc<LogRecord JavaDoc>();
250         try{
251             ObjectInputStream JavaDoc objectInputStream = null;
252             objectInputStream = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(iStream));
253             Object JavaDoc o = objectInputStream.readObject();
254             List JavaDoc list = null;
255             LogRecord JavaDoc rec = null;
256             if (o instanceof List JavaDoc) list = (List JavaDoc) o;
257             if (list!= null){
258                 for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
259                     Object JavaDoc object = it.next();
260                     if (object instanceof LogRecord JavaDoc) rec= (LogRecord JavaDoc) object;
261                     lgs.add(rec);
262                 }
263             }
264             objectInputStream.close();
265         } catch (IOException JavaDoc ex) {
266             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.WARNING,
267                     ex.getMessage(),
268                     ex);
269         } catch (ClassNotFoundException JavaDoc ex) {
270             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.WARNING,
271                     ex.getMessage(),
272                     ex);
273         }
274         return lgs;
275     }
276     
277     
278     private FileObject getRepos() throws IOException JavaDoc{
279         if (reposfile == null) reposfile = getFile(reposName);
280         return reposfile;
281     }
282     
283     private FileObject getFile(String JavaDoc fileName) throws IOException JavaDoc{
284         FileObject dir = directory();
285         FileObject file = dir.getFileObject(fileName);
286         if (file == null) {
287             try {
288                 file = dir.createData(fileName);
289             } catch (IOException JavaDoc ex) {
290                 FileNotFoundException JavaDoc t = new FileNotFoundException JavaDoc("Impossible to create dataFile"); // NOI18N
291
t.initCause(ex);
292                 throw t;
293             }
294         }
295         return file;
296     }
297     
298     private FileObject directory() throws IOException JavaDoc{
299         FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
300         FileObject dir = null;
301         FileLock lock = null;
302         dir = root.getFileObject(dirName);
303         try {
304             if ((dir == null) || (!dir.isFolder())) {
305                 if (dir!=null) {
306                     lock = dir.lock();
307                     dir.delete(lock);
308                 }
309                 dir = root.createFolder(dirName);
310             }
311         } catch (IOException JavaDoc ex) {
312             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.SEVERE,
313                     ex.getMessage(),
314                     ex);
315         } finally {
316             if (lock != null)
317                 lock.releaseLock();
318         }
319         if ((dir == null) || (!dir.isFolder()))
320             throw new FileNotFoundException JavaDoc("Impossible to find repository directory"); // NOI18N
321
return dir;
322     }
323     
324 }
325
Popular Tags