1 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 21 public class SearchOps 22 { 23 private static int componentSearchTimeout; 24 25 private final boolean shareAllComponents; 26 private final String myNodeName; 27 private Repository rep; 28 private SearchInterface search; 29 private LocalOps localOps; 30 31 32 33 public SearchOps() 34 { 35 String 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 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 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 76 public void evaluateRequest(SearchRequestID requestID, SearchPattern searchPattern, boolean firstOnly, String shareGroup) 77 { 78 boolean pattern = searchPattern.isPattern(); 79 boolean component = searchPattern.isComponent(); 80 int completeComponent = searchPattern.getCompleteComponent(); 81 String 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 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 bundleName = (String )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 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 } 137 138 ComponentInfo componentInfo = null; 140 if (component && !pattern) 141 { 142 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 174 if (completeComponent != SearchPattern.CC_ONLY) 176 { 177 if (pattern) 178 { 179 synchronized (localInfos) { 180 synchronized (sharedBundleContents) { 181 synchronized (sharedCompBundleMap.map) { 183 Iterator it = sharedCompBundleMap.map.entrySet().iterator(); 184 while (it.hasNext()) 185 { 186 Map.Entry entry = (Map.Entry)it.next(); 187 String componentName = (String )entry.getKey(); 188 String bundleName = (String )entry.getValue(); 189 190 try 191 { 192 if (BundleInfo.matchPattern(componentName, str)) 193 { 194 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 } } } } 212 else 213 { 214 synchronized (localInfos) 215 { 216 String bundleName = sharedCompBundleMap.findBundle(str); 217 if (bundleName != null) 218 { 219 LocalInfo localInfo = localInfos.getLocalInfo(bundleName); 221 if (localInfo != null && localInfo.isShareManager()) 222 { 223 searchReplies.add(new SearchReplyItem(myNodeName, bundleName)); 224 } 225 } 226 } } 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 247 boolean haveCompleteComponent = false; 248 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 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 317 public SearchReplyItem searchForBundleWithComponent(String 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 365 public List search(SearchPattern searchPattern, boolean firstOnly, String 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 |