KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Core > SearchOps


1 /*
2  * SearchOps.java
3  *
4  * Created on 28. duben 2004, 15:47
5  */

6
7 package SOFA.SOFAnet.Core;
8
9 import SOFA.SOFAnet.Repository.*;
10 import SOFA.SOFAnet.Search.*;
11 import SOFA.SOFAnode.TR.ComponentInfo;
12 import SOFA.SOFAnode.TR.Impl.ComponentInfoImpl;
13 import SOFA.Util.VMProperties;
14 import java.util.*;
15
16 /**
17  * Search operations of the SOFAnet.
18  *
19  * @author Ladislav Sobr
20  */

21 public class SearchOps
22 {
23   private static int componentSearchTimeout;
24   
25   private final boolean shareAllComponents;
26   private final String JavaDoc myNodeName;
27   private Repository rep;
28   private SearchInterface search;
29   private LocalOps localOps;
30   
31   
32   /** Creates a new instance of SearchOps */
33   public SearchOps()
34   {
35     String JavaDoc shareAllComponentsStr = System.getProperty(VMProperties.NET_SHARE_ALL_COMPONENTS, VMProperties.NET_SHARE_ALL_COMPONENTS_DEF).trim().toLowerCase();
36     shareAllComponents = shareAllComponentsStr.compareTo("1") == 0 || shareAllComponentsStr.compareTo("true") == 0;
37     
38     componentSearchTimeout = 5000;
39     String JavaDoc componentSearchTimeoutStr = System.getProperty(VMProperties.NET_SEARCH_TIMEOUT, VMProperties.NET_SEARCH_TIMEOUT_DEF);
40     
41     try
42     {
43       componentSearchTimeout = Integer.parseInt(componentSearchTimeoutStr);
44     }
45     catch (NumberFormatException JavaDoc e)
46     {
47     }
48     
49     myNodeName = NodeInfo.getLocalNodeName();
50   }
51   
52   public void setRepository(Repository repository)
53   {
54     rep = repository;
55   }
56   
57   public void setSearchInterface(SearchInterface search)
58   {
59     this.search = search;
60   }
61
62   public void setLocalOps(LocalOps localOps)
63   {
64     this.localOps = localOps;
65   }
66
67   /**
68    * Evaluates whether this node can satisfy the search request.
69    * If so, the search reply is created and sended to the requesting node.
70    *
71    * @param requestID identification of the request (in the search network)
72    * @param searchPattern describes, what is requested
73    * @param firstOnly if true, at most one reply item is requested
74    * @param shareGroup specification of the group that is been searched through; empty string if no group should be specified
75    */

76   public void evaluateRequest(SearchRequestID requestID, SearchPattern searchPattern, boolean firstOnly, String JavaDoc shareGroup)
77   {
78     boolean pattern = searchPattern.isPattern();
79     boolean component = searchPattern.isComponent();
80     int completeComponent = searchPattern.getCompleteComponent();
81     String JavaDoc str = searchPattern.getStr();
82     
83     List searchReplies = new LinkedList();
84     
85     LocalInfos localInfos = rep.getLocalInfos();
86     BundleContents sharedBundleContents = rep.getSharedBundleContents();
87     CompBundleMap sharedCompBundleMap = sharedBundleContents.getCompBundleMap();
88     
89     synchronized (localInfos)
90     {
91       //try bundles first
92
if (pattern)
93       {
94         synchronized (localInfos.map)
95         {
96           Iterator it = localInfos.map.entrySet().iterator();
97           while (it.hasNext())
98           {
99             Map.Entry entry = (Map.Entry)it.next();
100             String JavaDoc bundleName = (String JavaDoc)entry.getKey();
101             LocalInfo localInfo = (LocalInfo)entry.getValue();
102             ShareGroups shareGroups = localInfo.getSMShareGroups();
103             if (localInfo.isShareManager() &&
104                 (shareGroups == null || shareGroup.length() == 0 || shareGroups.contains(shareGroup)))
105             {
106               try
107               {
108                 if (BundleInfo.matchPattern(bundleName, str))
109                 {
110                   searchReplies.add(new SearchReplyItem(myNodeName, bundleName));
111                   if (firstOnly) break;
112                 }
113               }
114               catch (BundleInfo.InvalidBundleNameException e)
115               {
116               }
117             }
118           }
119         }
120       }
121       else
122       {
123         //str contains exact name (of bundle or component)
124
LocalInfo localInfo = localInfos.getLocalInfo(str);
125         
126         if (localInfo != null && localInfo.isShareManager())
127         {
128           ShareGroups shareGroups = localInfo.getSMShareGroups();
129           if (shareGroups == null || shareGroup.length() == 0 || shareGroups.contains(shareGroup))
130           {
131             searchReplies.add(new SearchReplyItem(myNodeName, localInfo.getName()));
132           }
133         }
134       }
135     } //localInfos
136

137     
138     //test correctness of name of the component (if it should be component)
139
ComponentInfo componentInfo = null;
140     if (component && !pattern)
141     {
142       //test correctness of str as name of component
143
BundleInfo bundleInfo = new BundleInfo();
144       try
145       {
146         bundleInfo.fromBundleName(str);
147       }
148       catch (BundleInfo.InvalidBundleNameException e)
149       {
150         Reporter.error("Evaluation of search request (" + requestID.getRequestMark() + ",'" + requestID.getSourceNode() + "') failed: Invalid name of component: " + str, e);
151         bundleInfo = null;
152       }
153       if (bundleInfo != null && !bundleInfo.isComponent()) bundleInfo = null;
154       if (bundleInfo != null) componentInfo = new ComponentInfoImpl(bundleInfo.getShortName(), bundleInfo.getVersion());
155     }
156     
157     if (!component || !pattern && componentInfo == null || firstOnly && !searchReplies.isEmpty())
158     {
159       if (searchReplies.isEmpty()) return;
160       
161       try
162       {
163         search.reply(new SearchReply(requestID, searchReplies));
164       }
165       catch (SearchException e)
166       {
167         Reporter.error("Evaluation of search request (" + requestID.getRequestMark() + ",'" + requestID.getSourceNode() + "') failed while replying", e);
168       }
169       return;
170     }
171     
172     //no we know "str" is component name/pattern
173

174     //go through contents of bundles (but not if completeComponent == CC_ONLY, because we cannot assure that the bundle contain complete component)
175
if (completeComponent != SearchPattern.CC_ONLY)
176     {
177       if (pattern)
178       {
179         synchronized (localInfos) {
180         synchronized (sharedBundleContents) {
181         synchronized (sharedCompBundleMap.map) //just to be sure
182
{
183           Iterator it = sharedCompBundleMap.map.entrySet().iterator();
184           while (it.hasNext())
185           {
186             Map.Entry entry = (Map.Entry)it.next();
187             String JavaDoc componentName = (String JavaDoc)entry.getKey();
188             String JavaDoc bundleName = (String JavaDoc)entry.getValue();
189             
190             try
191             {
192               if (BundleInfo.matchPattern(componentName, str))
193               {
194                 //ok, we found the component that matches search pattern, but still we have to find out, whether we are the share manager of the component
195
LocalInfo localInfo = localInfos.getLocalInfo(bundleName);
196                 if (localInfo != null && localInfo.isShareManager())
197                 {
198                   searchReplies.add(new SearchReplyItem(myNodeName, bundleName));
199                   if (firstOnly) break;
200                 }
201               }
202             }
203             catch (BundleInfo.InvalidBundleNameException e)
204             {
205             }
206           }
207         
208         } //sharedCompBundleMap.map
209
} //sharedBundleContents
210
} //localInfos
211
}
212       else
213       {
214         synchronized (localInfos)
215         {
216           String JavaDoc bundleName = sharedCompBundleMap.findBundle(str);
217           if (bundleName != null)
218           {
219             //ok, we found the component, but still we have to find out, whether we are the share manager of the component
220
LocalInfo localInfo = localInfos.getLocalInfo(bundleName);
221             if (localInfo != null && localInfo.isShareManager())
222             {
223               searchReplies.add(new SearchReplyItem(myNodeName, bundleName));
224             }
225           }
226         } //localInfos
227
}
228     }
229     
230     if (!shareAllComponents || firstOnly && !searchReplies.isEmpty())
231     {
232       if (searchReplies.isEmpty()) return;
233       
234       try
235       {
236         search.reply(new SearchReply(requestID, searchReplies));
237       }
238       catch (SearchException e)
239       {
240         Reporter.error("Evaluation of search request (" + requestID.getRequestMark() + ",'" + requestID.getSourceNode() + "') failed while replying", e);
241       }
242       return;
243     }
244     
245     //now we know, that shareAllComponents == true - we will try to create component-bundle from TR
246

247     boolean haveCompleteComponent = false;
248     //try to find complete components (if we are requested to do it)
249
if (completeComponent == SearchPattern.CC_TRY || completeComponent == SearchPattern.CC_ONLY)
250     {
251       if (pattern)
252       {
253         ComponentInfo[] comps = localOps.listComponentsPresentInTR();
254         for (int i = 0; i < comps.length; i++)
255         {
256           if (localOps.testComponentPresenceInTR(comps[i], true))
257           {
258             searchReplies.add(new SearchReplyItem(myNodeName, BundleInfo.makeComponentBundleName(comps[i].getName(), comps[i].getImplementationVersion(), true)));
259             haveCompleteComponent = true;
260             if (firstOnly) break;
261           }
262         }
263       }
264       else
265       {
266         if (localOps.testComponentPresenceInTR(componentInfo, true))
267         {
268           searchReplies.add(new SearchReplyItem(myNodeName, BundleInfo.makeComponentBundleName(componentInfo.getName(), componentInfo.getImplementationVersion(), true)));
269           haveCompleteComponent = true;
270         }
271       }
272     }
273     
274     //try to find single components (if we are requested to do it)
275
if (completeComponent == SearchPattern.CC_NO || completeComponent == SearchPattern.CC_TRY && !haveCompleteComponent)
276     {
277       if (pattern)
278       {
279         ComponentInfo[] comps = localOps.listComponentsPresentInTR();
280         for (int i = 0; i < comps.length; i++)
281         {
282           if (localOps.testComponentPresenceInTR(comps[i], false))
283           {
284             searchReplies.add(new SearchReplyItem(myNodeName, BundleInfo.makeComponentBundleName(comps[i].getName(), comps[i].getImplementationVersion(), false)));
285             if (firstOnly) break;
286           }
287         }
288       }
289       else
290       {
291         if (localOps.testComponentPresenceInTR(componentInfo, false))
292         {
293           searchReplies.add(new SearchReplyItem(myNodeName, BundleInfo.makeComponentBundleName(componentInfo.getName(), componentInfo.getImplementationVersion(), false)));
294           haveCompleteComponent = true;
295         }
296       }
297     }
298     
299     if (searchReplies.isEmpty()) return;
300
301     try
302     {
303       search.reply(new SearchReply(requestID, searchReplies));
304     }
305     catch (SearchException e)
306     {
307       Reporter.error("Evaluation of search request (" + requestID.getRequestMark() + ",'" + requestID.getSourceNode() + "') failed while replying", e);
308     }
309   }
310   
311   /**
312    * Searches for the node that can provide bundle containing specified component
313    *
314    * @param componentFullName full name of the component in form name[version]
315    * @return search reply item if successfull, null otherwise
316    */

317   public SearchReplyItem searchForBundleWithComponent(String JavaDoc componentFullName) throws CoreException
318   {
319     Reporter.startInfo("Searching for component '" + componentFullName + "'");
320
321     SearchPattern pattern = new SearchPattern();
322     pattern.setStr(componentFullName);
323     pattern.setPattern(false);
324     pattern.setComponent(true);
325     pattern.setCompleteComponent(SearchPattern.CC_TRY);
326     
327     SearchRequest request = new SearchRequest();
328     request.setSearchPattern(pattern);
329     request.setFirstOnly(true);
330     request.setShareGroup("");
331     request.setTimeout(componentSearchTimeout);
332     
333     try
334     {
335       search.request(request);
336     }
337     catch (SearchException e)
338     {
339       throw new CoreException(e.getMessage(), e);
340     }
341
342     List replies = request.getReplies();
343     if (request.getErrCode() == 0 && replies != null && replies.size() > 0)
344     {
345       SearchReplyItem replyItem = (SearchReplyItem)replies.get(0);
346       Reporter.stopInfo("Component '" + componentFullName + "' found at '" + replyItem.getNodeName() + "' in bundle '" + replyItem.getBundleName() + "'");
347       return replyItem;
348     }
349     else
350     {
351       Reporter.stopInfo("Component '" + componentFullName + "' not found");
352       return null;
353     }
354   }
355
356   /**
357    * Searches for the nodes and bundles that can satisfy request.
358    *
359    * @param searchPattern request itself - what are we looking for
360    * @param firstOnly return only one (first) satisfying reply
361    * @param shareGroup name of share group that we should search through (empty string means "all")
362    * @param timeout maximal time in milliseconds to wait for the reply(ies)
363    * @return list of replies (SearchReplyItem); null when no replies
364    */

365   public List search(SearchPattern searchPattern, boolean firstOnly, String JavaDoc shareGroup, int timeout) throws CoreException
366   {
367     Reporter.startInfo("Searching for '" + searchPattern.getStr() + "'");
368     
369     SearchRequest request = new SearchRequest();
370     request.setSearchPattern(searchPattern);
371     request.setFirstOnly(firstOnly);
372     request.setShareGroup(shareGroup);
373     request.setTimeout(timeout);
374
375     try
376     {
377       search.request(request);
378     }
379     catch (SearchException e)
380     {
381       throw new CoreException(e.getMessage(), e);
382     }
383
384     List replies = request.getReplies();
385     if (request.getErrCode() == 0 && replies != null && replies.size() > 0)
386     {
387       SearchReplyItem replyItem = (SearchReplyItem)replies.get(0);
388       Reporter.stopInfo("Bundles for '" + searchPattern.getStr() + "' found (e.g. '" + replyItem.getBundleName() + "' at '" + replyItem.getNodeName() + "')");
389       return replies;
390     }
391     else
392     {
393       Reporter.stopInfo("No bundles for '" + searchPattern.getStr() + "' + found");
394       return null;
395     }
396   }
397 }
398
Popular Tags