KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > word > IssueSearchFactory


1 package org.tigris.scarab.util.word;
2
3 /* ================================================================
4  * Copyright (c) 2003 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 // JDK classes
50

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 /**
60  * Creates new IssueSearch objects and acts as a regulator on the number
61  * of concurrent searches.
62  *
63  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
64  * @since 0.16.25
65  * @version $Id: IssueSearchFactory.java 9448 2005-03-12 12:31:24Z jorgeuriarte $
66  */

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     /**
75      * The number of objects currently in use
76      */

77     private int numActive = 0;
78
79     IssueSearchFactory()
80     {
81         maxInstances = getMaxInstances();
82         maxWait = getMaxWait();
83     }
84
85     /**
86      * Maximum number of concurrent searches. Given by the
87      * scarab.concurrent.search.max property. if not given
88      * or value is negative, unlimited concurrent searches are
89      * allowed (will then be limited by db connections). A
90      * value of 0 will disable searching.
91      * Implementation note: this method is package-private to
92      * facilitate testing.
93      */

94     int getMaxInstances()
95     {
96     // TODO: FIXME: Should be avalon componente
97
//int max = Turbine.getConfiguration()
98
// .getInt("scarab.concurrent.search.max", -1);
99
//return max;
100
return 2;
101     }
102
103     /**
104      * How long to wait (in seconds) for a search object if one is not
105      * immediately available. Given by the
106      * scarab.concurrent.search.wait property. if not given
107      * or value is negative, we block till an IssueSearch can be created.
108      * A value of 0 will fail immediately.
109      * Implementation note: this method is package-private to
110      * facilitate testing.
111      */

112     int getMaxWait()
113     {
114         // TODO: FIXME: Should be avalon component.
115
//int max = Turbine.getConfiguration()
116
// .getInt("scarab.concurrent.search.wait", -1);
117
int max = -1;
118         max *= 1000;
119         return max;
120         
121     }
122
123     public IssueSearch getInstance(Issue issue, ScarabUser searcher)
124         throws Exception JavaDoc, 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 JavaDoc, 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 JavaDoc, MaxConcurrentSearchException
142     {
143         register();
144         IssueSearch search = new IssueSearch(mitList, searcher);
145         return search;
146     }
147
148     void register()
149         throws ScarabException, InterruptedException JavaDoc
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                 // check if we can create one
161
while (numActive >= maxInstances)
162                 {
163                     // we can't create a new instance at this moment
164
try
165                     {
166                         if (maxWait > 0)
167                         {
168                             wait(maxWait);
169                         }
170                         else if (maxWait < 0)
171                         {
172                             wait();
173                         }
174                         else // maxWait == 0
175
{
176                             throw new MaxConcurrentSearchException(
177                                 L10NKeySet.ExceptionMaxConcurrentSearch,
178                                 ""+this.getMaxWait()
179                                 );
180                         }
181                     }
182                     catch(InterruptedException JavaDoc e)
183                     {
184                         notify();
185                         throw e; //EXCEPTION
186
}
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                 // normally always true, but false is a possibility
208
if (numActive > 0)
209                 {
210                     numActive--;
211                 }
212                 if (maxWait != 0)
213                 {
214                     this.notifyAll(); // give wait'ers a chance at it
215
}
216             }
217         }
218     }
219 }
220
221
Popular Tags