1 19 20 21 package org.openidex.search; 22 23 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Set ; 33 import org.openide.ErrorManager; 34 35 import org.openide.cookies.InstanceCookie; 36 import org.openide.filesystems.FileSystem; 37 import org.openide.filesystems.Repository; 38 import org.openide.loaders.DataObject; 39 import org.openide.util.Lookup; 40 import org.openide.util.NbBundle; 41 import org.openide.nodes.Node; 42 43 45 46 55 public class DataObjectSearchGroup extends SearchGroup { 56 57 58 64 protected void add(SearchType searchType) { 65 boolean ok = false; 66 Class [] classes = searchType.getSearchTypeClasses(); 67 for (int i = 0; i < classes.length; i++) { 68 if (classes[i] == DataObject.class) { 69 ok = true; 70 break; 71 } 72 } 73 if (ok) { 74 super.add(searchType); 75 } 76 } 77 78 84 public void doSearch() { 85 Node[] nodes = normalizeNodes( 86 (Node[]) searchRoots.toArray(new Node[searchRoots.size()])); 87 88 lowMemoryWarning = false; 89 lowMemoryWarningCount = 0; 90 assureMemory(REQUIRED_PER_ITERATION, true); 91 92 for (int i = 0; i < nodes.length; i++) { 93 Node node = nodes[i]; 94 SearchInfo info = Utils.getSearchInfo(node); 95 if (info != null) { 96 for (Iterator j = info.objectsToSearch(); j.hasNext(); ) { 97 if (stopped) return; 98 assureMemory(REQUIRED_PER_ITERATION, false); 99 processSearchObject( j.next()); 100 } 101 } 102 } 103 } 104 105 106 private static boolean lowMemoryWarning = false; 107 private static int lowMemoryWarningCount = 0; 108 private static int MB = 1024 * 1024; 109 private static int REQUIRED_PER_ITERATION = 2 * MB; 110 private static int REQUIRED_PER_FULL_GC = 7 * MB; 111 112 117 private static void assureMemory(int estimate, boolean tryGC) { 118 Runtime rt = Runtime.getRuntime(); 119 long total = rt.totalMemory(); 120 long max = rt.maxMemory(); long required = Math.max(total/13, estimate + REQUIRED_PER_FULL_GC); 122 if (total == max && rt.freeMemory() < required) { 123 if (tryGC) { 125 try { 126 byte[] gcProvocation = new byte[(int)required]; 127 gcProvocation[0] = 75; 128 gcProvocation = null; 129 return; 130 } catch (OutOfMemoryError e) { 131 throwNoMemory(); 132 } 133 } else { 134 lowMemoryWarning = true; 135 } 136 } else if (lowMemoryWarning) { 137 lowMemoryWarning = false; 138 lowMemoryWarningCount ++; 139 } 140 if (lowMemoryWarningCount > 7 || (total == max && rt.freeMemory() < REQUIRED_PER_FULL_GC)) { 142 throwNoMemory(); 143 } 144 145 } 146 147 private static void throwNoMemory() { 148 RuntimeException ex = new RuntimeException ("Low memory condition"); String msg = NbBundle.getMessage(DataObjectSearchGroup.class, "EX_memory"); 150 ErrorManager.getDefault().annotate(ex, ErrorManager.USER, null, msg, null, null); 151 throw ex; 152 } 153 154 242 243 246 public Node getNodeForFoundObject(Object object) { 247 if (!(object instanceof DataObject)) { 248 return null; 249 } 250 return ((DataObject) object).getNodeDelegate(); 251 } 252 253 267 private static Node[] normalizeNodes(Node[] nodes) { 268 269 270 if (nodes.length < 2) { 271 return nodes; 272 } 273 274 319 320 HashMap badNodes = new HashMap (2 * nodes.length, 0.75f); 321 HashMap goodNodes = new HashMap (2 * nodes.length, 0.75f); 322 ArrayList path = new ArrayList (10); 323 ArrayList result = new ArrayList (nodes.length); 324 325 326 for (int i = 0; i < nodes.length; i++) { 327 badNodes.put(nodes[i], Boolean.FALSE); 328 } 329 330 main_cycle: 331 for (int i = 0; i < nodes.length; i++) { 332 path.clear(); 333 boolean isBad = false; 334 for (Node n = nodes[i].getParentNode(); n != null; 335 n = n.getParentNode()) { 336 if (badNodes.containsKey(n)) { 337 isBad = true; 338 break; 339 } 340 if (goodNodes.containsKey(n)) { 341 break; 342 } 343 path.add(n); 344 } 345 if (isBad) { 346 badNodes.put(nodes[i], Boolean.TRUE); 347 for (Iterator j = path.iterator(); j.hasNext(); ) { 348 badNodes.put(j.next(), Boolean.TRUE); 349 } 350 } else { 351 for (Iterator j = path.iterator(); j.hasNext(); ) { 352 goodNodes.put(j.next(), Boolean.TRUE); 353 } 354 result.add(nodes[i]); 355 } 356 } 357 return (Node[]) result.toArray(new Node[result.size()]); 358 } 359 360 } 361 | Popular Tags |