1 package org.tigris.scarab.util.word; 2 3 48 49 51 import org.tigris.scarab.om.Issue; 52 import org.tigris.scarab.om.IssueType; 53 import org.tigris.scarab.om.MITList; 54 import org.tigris.scarab.om.Module; 55 import org.tigris.scarab.om.ScarabUser; 56 import org.tigris.scarab.tools.localization.L10NKeySet; 57 import org.tigris.scarab.util.ScarabException; 58 59 67 public class IssueSearchFactory 68 { 69 public static final IssueSearchFactory INSTANCE = new IssueSearchFactory(); 70 71 private final int maxInstances; 72 private final int maxWait; 73 74 77 private int numActive = 0; 78 79 IssueSearchFactory() 80 { 81 maxInstances = getMaxInstances(); 82 maxWait = getMaxWait(); 83 } 84 85 94 int getMaxInstances() 95 { 96 return 2; 101 } 102 103 112 int getMaxWait() 113 { 114 int max = -1; 118 max *= 1000; 119 return max; 120 121 } 122 123 public IssueSearch getInstance(Issue issue, ScarabUser searcher) 124 throws Exception , MaxConcurrentSearchException 125 { 126 register(); 127 IssueSearch search = new IssueSearch(issue, searcher); 128 return search; 129 } 130 131 public IssueSearch 132 getInstance(Module module, IssueType issueType, ScarabUser searcher) 133 throws Exception , MaxConcurrentSearchException 134 { 135 register(); 136 IssueSearch search = new IssueSearch(module, issueType, searcher); 137 return search; 138 } 139 140 public IssueSearch getInstance(MITList mitList, ScarabUser searcher) 141 throws Exception , MaxConcurrentSearchException 142 { 143 register(); 144 IssueSearch search = new IssueSearch(mitList, searcher); 145 return search; 146 } 147 148 void register() 149 throws ScarabException, InterruptedException 150 { 151 if (maxInstances <= 0) 152 { 153 throw new MaxConcurrentSearchException(L10NKeySet.ExceptionSearchIsNotAllowed); 154 } 155 else 156 { 157 synchronized (this) 158 { 159 long starttime = System.currentTimeMillis(); 160 while (numActive >= maxInstances) 162 { 163 try 165 { 166 if (maxWait > 0) 167 { 168 wait(maxWait); 169 } 170 else if (maxWait < 0) 171 { 172 wait(); 173 } 174 else { 176 throw new MaxConcurrentSearchException( 177 L10NKeySet.ExceptionMaxConcurrentSearch, 178 ""+this.getMaxWait() 179 ); 180 } 181 } 182 catch(InterruptedException e) 183 { 184 notify(); 185 throw e; } 187 if(maxWait > 0 && 188 ((System.currentTimeMillis() - starttime) >= maxWait)) 189 { 190 throw new MaxConcurrentSearchException( 191 L10NKeySet.ExceptionMaxConcurrentSearch, 192 ""+this.getMaxWait() 193 ); 194 } 195 } 196 numActive++; 197 } 198 } 199 } 200 201 public void notifyDone() 202 { 203 if (maxInstances > 0) 204 { 205 synchronized (this) 206 { 207 if (numActive > 0) 209 { 210 numActive--; 211 } 212 if (maxWait != 0) 213 { 214 this.notifyAll(); } 216 } 217 } 218 } 219 } 220 221 | Popular Tags |