1 19 20 28 29 package org.netbeans.modules.exceptions.settings; 30 31 import java.io.BufferedInputStream ; 32 import java.io.BufferedOutputStream ; 33 import java.io.ByteArrayInputStream ; 34 import java.io.ByteArrayOutputStream ; 35 import java.io.FileNotFoundException ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.io.ObjectInputStream ; 39 import java.io.ObjectOutputStream ; 40 import java.io.OutputStream ; 41 import java.net.HttpURLConnection ; 42 import java.util.Arrays ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.LinkedList ; 46 import java.util.List ; 47 import java.util.logging.Level ; 48 import java.util.logging.LogRecord ; 49 import java.util.logging.Logger ; 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 62 public class ExceptionsRepos { 63 private static ExceptionsRepos instance = null; 64 private static String dirName = "Exceptions"; private static String reposName = "ExceptionsRepos"; private static String componentsName = "Components"; private static LinkedList <LogRecord > logs=null; 68 private static FileObject reposfile = null; 69 private static HashMap components = null; 71 public static final String uri_server = "http://qa-sol10-s1.czech:8080/ServerExceptions/"; 74 private static final String uri = uri_server + "ComponentsServlet"; 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 rec){ 85 rec.setThrown(ExceptionLogger.convert(rec.getThrown())); 86 getRecords().add(rec); 87 } 88 89 public boolean contains(LogRecord rec){ 90 LinkedList <LogRecord > list = getRecords(); 91 Throwable thrown = rec.getThrown(); 92 for (Iterator <LogRecord > it = list.iterator(); it.hasNext();) { 93 LogRecord 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 [] getComponents(){ 105 if (components == null) components = loadComponents(); 106 if (components == null) return new String [0]; 107 String [] array = new String [components.size()]; 108 int i = 0; 109 Iterator 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 [] getSubcomponents(String componentName){ 118 if (components == null) components = loadComponents(); 119 if (components == null) return new String [0]; 120 Object o = components.get(componentName); 121 String [] array = null; 122 if (o instanceof String []) array = (String []) 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"); FileLock lock = null; 130 try{ 131 lock = reposfile.lock(); 132 OutputStream oStream= getRepos().getOutputStream(lock); 133 ObjectOutputStream objectOStream = new ObjectOutputStream (new BufferedOutputStream (oStream)); 134 objectOStream.writeObject(logs); 135 objectOStream.close(); 136 oStream.close(); 137 }catch(IOException 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"); FileLock lock = null; 149 try{ 150 FileObject file = getFile(componentsName); 151 lock = file.lock(); 152 OutputStream oStream= file.getOutputStream(lock); 153 ObjectOutputStream objectOStream = new ObjectOutputStream (new BufferedOutputStream (oStream)); 154 objectOStream.writeObject(components); 155 objectOStream.close(); 156 oStream.close(); 157 }catch(IOException 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 loadComponents(){ 167 Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.INFO, "Loading components"); FileLock lock = null; 169 HashMap map = null; 170 try { 171 FileObject file = getFile(componentsName); 172 if (file.getSize() < 100){ map = loadFromServer(); 175 if (map != null) saveComponentsToFile(); 177 }else{ 178 Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "Loading components from file"); InputStream stream = file.getInputStream(); 180 ObjectInputStream iStream = new ObjectInputStream (new BufferedInputStream (stream)); 181 map = read(iStream); 182 iStream.close(); 183 stream.close(); 184 } 185 186 } catch (ClassNotFoundException ex) { 187 java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.INFO, 188 ex.getMessage()); 189 } catch (IOException 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())); else Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "No Components Loaded"); return map; 198 } 199 200 private HashMap loadFromServer() throws IOException , ClassNotFoundException { 201 Logger.getLogger(ExceptionsRepos.class.getName()).log(Level.FINEST, "Loading components from server"); HashMap map = null; 203 HttpURLConnection conn = Sender.getHttpConnection(uri); 204 if (conn != null){ 205 InputStream stream = conn.getInputStream(); 206 ByteArrayOutputStream bStream = new ByteArrayOutputStream (); 207 int i; 208 while ((i = stream.read())!= -1) bStream.write(i); 209 ObjectInputStream iStream = new ObjectInputStream (new ByteArrayInputStream (bStream.toByteArray())); 210 map = read(iStream); 211 iStream.close(); 212 stream.close(); 213 conn.disconnect(); 214 } 215 return map; 216 } 217 218 private HashMap read(ObjectInputStream iStream) throws IOException , ClassNotFoundException { 219 Object o = iStream.readObject(); 220 if (o instanceof HashMap ){ 221 return (HashMap ) o; 222 } 223 return null; 224 } 225 226 private LinkedList <LogRecord > getRecords(){ 227 FileLock lock = null; 228 if (logs==null){ 229 try{ 230 lock = getRepos().lock(); 231 InputStream iStream = getRepos().getInputStream(); 232 if (iStream.available() > 0){ 233 logs = readLogs(iStream); 234 } 235 iStream.close(); 236 } catch (IOException 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 <LogRecord >(); return logs; 246 } 247 248 private LinkedList <LogRecord > readLogs(InputStream iStream){ 249 LinkedList <LogRecord > lgs = new LinkedList <LogRecord >(); 250 try{ 251 ObjectInputStream objectInputStream = null; 252 objectInputStream = new ObjectInputStream (new BufferedInputStream (iStream)); 253 Object o = objectInputStream.readObject(); 254 List list = null; 255 LogRecord rec = null; 256 if (o instanceof List ) list = (List ) o; 257 if (list!= null){ 258 for (Iterator it = list.iterator(); it.hasNext();) { 259 Object object = it.next(); 260 if (object instanceof LogRecord ) rec= (LogRecord ) object; 261 lgs.add(rec); 262 } 263 } 264 objectInputStream.close(); 265 } catch (IOException ex) { 266 java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.WARNING, 267 ex.getMessage(), 268 ex); 269 } catch (ClassNotFoundException 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 { 279 if (reposfile == null) reposfile = getFile(reposName); 280 return reposfile; 281 } 282 283 private FileObject getFile(String fileName) throws IOException { 284 FileObject dir = directory(); 285 FileObject file = dir.getFileObject(fileName); 286 if (file == null) { 287 try { 288 file = dir.createData(fileName); 289 } catch (IOException ex) { 290 FileNotFoundException t = new FileNotFoundException ("Impossible to create dataFile"); t.initCause(ex); 292 throw t; 293 } 294 } 295 return file; 296 } 297 298 private FileObject directory() throws IOException { 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 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 ("Impossible to find repository directory"); return dir; 322 } 323 324 } 325 | Popular Tags |