private ProvidedExtensionsImpl lookupImpl(boolean providesCanWrite) { Result<BaseAnnotationProvider> result = Lookup.getDefault(). lookup(new Lookup.Template(BaseAnnotationProvider.class)); for (Item<BaseAnnotationProvider> item : result.allItems()) { if (!item.getId().contains(ProvidedExtensionsTest.class.getSimpleName())) { continue; } BaseAnnotationProvider ap = item.getInstance(); InterceptionListener iil = ap.getInterceptionListener(); if (iil instanceof ProvidedExtensionsImpl) { ProvidedExtensionsImpl extension = (ProvidedExtensionsImpl) iil; if (ProvidedExtensionsAccessor.IMPL.providesCanWrite(extension) == providesCanWrite) { return (ProvidedExtensionsImpl) iil; } } } return null; }
@Override public <T> Item<T> lookupItem(Template<T> template) { boolean nodeQ = isNodeQuery(template.getType()); Item<T> i = checkNode().lookupItem(template); if ( nodeQ && i == null && template.getType().isInstance(node) && (template.getInstance() == null || template.getInstance() == node) ) { i = checkNode().lookupItem(wackohacko(template.getId(), template.getInstance())); } return nodeQ && i != null ? new FilterItem<T>(i, template.getType()) : i; }
@Override public void evaluate(SearchRequest request, SearchResponse response) { readKeywordsFromNewAnnotation(); for (Lookup.Item<OptionsCategory> entry : getODCategories()) { for (Map.Entry<String, Set<String>> kw : getKeywords(entry).entrySet()) { for (String keywords : kw.getValue()) { for (String keyword : Arrays.asList(keywords.split(","))) { if (keyword.toLowerCase().indexOf(request.getText().toLowerCase()) > -1) { String path = entry.getId().substring(entry.getId().indexOf("/") + 1); // NOI18N if (kw.getKey().contains("/")) { path = path + kw.getKey().substring(kw.getKey().indexOf("/")); //NOI18N } if (!response.addResult(new OpenOption(path), keyword)) { return; } } } } } } }
private int getPosition(Item<T> li) { int position = Integer.MAX_VALUE; try { Field foField = li.getClass().getDeclaredField("fo"); foField.setAccessible(true); FileObject fo = (FileObject) foField.get(li); if (fo != null) { Object positionObj = fo.getAttribute("position"); if (positionObj instanceof Integer) { position = (Integer) positionObj; } } } catch (Exception ex) { logger.log(Level.INFO, "Not able to retieve position from item "+li, ex); } return position; }
@Override public Collection<? extends T> allInstances() { //return new PathLookupCollection(orig.allInstances(), n); Collection<? extends Item<T>> items = getItems(); if (items == ORIG_ITEMS) { return orig.allInstances(); } ArrayList<T> list = new ArrayList<T>(items.size()); for (Item<T> item : items) { T obj = item.getInstance(); if (clazz.isInstance(obj)) { list.add(obj); } } return Collections.unmodifiableList(list); }
@Override public Set<Class<? extends T>> allClasses() { //return new PathLookupSet(orig.allClasses(), n); Collection<? extends Item<T>> items = getItems(); if (items == ORIG_ITEMS) { return orig.allClasses(); } Set<Class<? extends T>> s = new HashSet<Class<? extends T>>(); for (Item<T> item : items) { Class<? extends T> clazz = item.getType(); if (clazz != null) { s.add(clazz); } } s = Collections.unmodifiableSet(s); return s; }
public MavenQueryProvider() { grammars = new ArrayList<GrammarFactory>(); Result<GrammarFactory> result = Lookup.getDefault().lookupResult(GrammarFactory.class); Collection<? extends Item<GrammarFactory>> items = result.allItems(); for (Item<GrammarFactory> item : items) { grammars.add(item.getInstance()); } }
/** Updates the state of the lookup. * @return true if the lookup really changed */ public boolean updateLookup(Lookup l) { Collection<? extends Item<T>> oldPairs = (delegate != null) ? delegate.allItems() : null; synchronized (this) { if (delegate != null) { delegate.removeLookupListener(this); } delegate = l.lookup(template); if (template.getType().isAssignableFrom(node.getClass()) && delegate.allItems().isEmpty()) { delegate = l.lookup(wackohacko(template.getId(), template.getInstance())); } delegate.addLookupListener(this); } if (oldPairs == null) { // nobody knows about a change return false; } Collection<? extends Item<T>> newPairs = delegate.allItems(); return !oldPairs.equals(newPairs); }
private static MIMEResolver findResolver(String name) { final Collection<? extends Item<MIMEResolver>> arr = Lookups.forPath("Services/MIMEResolver").lookupResult(MIMEResolver.class).allItems(); for (Lookup.Item<MIMEResolver> i : arr) { if (i.getDisplayName().equals(name)) { return i.getInstance(); } } fail("Cannot find resolver name: " + name + " but found " + arr); return null; }
private void fillInstances(List<String> l, Result<T> lr, Set<String> s) { for (String className : l) { if (className.endsWith(HIDDEN)) { continue; } if (s != null && s.contains (className)) { continue; } fillClassInstance(className); } for (Item<T> li : lr.allItems()) { // TODO: We likely do not have the Item.getId() defined correctly. // We have to check the ContextAwareService.serviceID() String serviceName = getServiceName(li.getId()); //System.err.println("ID = '"+li.getId()+"' => serviceName = '"+serviceName+"'"); // We do not recognize method calls correctly if (s != null && (s.contains (serviceName) || s.contains (serviceName+"()"))) { continue; } //add(new LazyInstance<T>(service, li)); fillServiceInstance(li); } /* for (Object lri : lr.allInstances()) { if (lri instanceof ContextAwareService) { String className = ((ContextAwareService) lri).serviceName(); if (s != null && s.contains (className)) continue; fillClassInstance(className); } } */ }
private String getClassName(Item li) { String id = li.getId(); int i = id.lastIndexOf("/"); if (i >= 0) { id = id.substring(i+1); } return id.replace('-', '.'); }
private static <T> List<Item<T>> itemsJustForPath(Class<T> clazz, Result<T> result, String path) { int l = path.length() + 1; Collection<? extends Item<T>> allItems = result.allItems(); List<Item<T>> pathItems = new ArrayList<Item<T>>(allItems.size()); for (Item<T> it : allItems) { String filePath = it.getId(); assert filePath.startsWith(path) : "File path '"+filePath+"' does not start with searched path '"+path+"'"; if (filePath.indexOf('/', l) < l) { // This item is from current folder if (clazz.isInterface()) { // Check whether the lookup item is really declared as an instance of the class we search for: FileObject fo = FileUtil.getConfigFile(filePath+".instance"); if (fo != null) { Object io = fo.getAttribute("instanceOf"); // NOI18N if (io != null) { if (((String) io).indexOf(clazz.getName()) < 0) { continue; } } } } pathItems.add(it); } } if (pathItems.size() == allItems.size()) { return (List<Item<T>>) ORIG_ITEMS; } return pathItems; }
@Override public Collection<? extends Item<T>> allItems() { Collection<? extends Item<T>> items = getItems(); if (items == ORIG_ITEMS) { return orig.allItems(); } else { return items; } }
@Override public Collection<? extends Item<T>> allItems() { Collection<? extends Item<T>> res = delegate.allItems(); synchronized (this) { if (!res.isEmpty()) { allItems = res; } return allItems; } }
@Override public void resultChanged(LookupEvent ev) { if (ev.getSource() == nodes) { Collection<? extends Item<Provider>> arr = nodes.allItems(); if (arr.size() == 1 && arr.iterator().next().getInstance() == null) { return; } initValues(); return; } final LookupEvent mev = new LookupEvent(this); for (LookupListener ll : listeners) { ll.resultChanged(mev); } }
/** * Retrieves an action instance * @param category e.g., "Maps" * @param id e.g., "com-emxsys-worldwind-ribbon-actions-ToggleLayerAction" * @return the Action instance or null * @see http://forums.netbeans.org/topic44790.html */ @Deprecated public static Action getAction(String category, String id) { String folder = "Actions/" + category + "/"; Lookup pathLookup = Lookups.forPath(folder); Template<Action> actionTemplate = new Template<>(Action.class, folder + id, null); Item<Action> item = pathLookup.lookupItem(actionTemplate); if (item != null) { return item.getInstance(); } return null; }
public void testOSGiServicesVisibleInLookup() throws Exception { mgr.mutexPrivileged().enterWriteAccess(); FileObject fo; try { String mfBar = "Bundle-SymbolicName: org.bar\n" + "Bundle-Version: 1.1.0\n" + "Bundle-ManifestVersion: 2\n" + "Import-Package: org.foo\n" + "\n\n"; File j2 = changeManifest(new File(jars, "depends-on-simple-module.jar"), mfBar); Module m2 = mgr.create(j2, null, false, false, false); mgr.enable(m2); } finally { mgr.mutexPrivileged().exitWriteAccess(); } Bundle b = findBundle("org.bar"); assertNotNull("Bundle really found", b); IOException s = new IOException(); Hashtable dict = new Hashtable(); dict.put(Constants.SERVICE_DESCRIPTION, "tristatricettri"); ServiceRegistration sr = b.getBundleContext().registerService(IOException.class.getName(), s, dict); assertBundles("Nobody is using the service yet", 0, sr.getReference().getUsingBundles()); IOException found = Lookup.getDefault().lookup(IOException.class); assertNotNull("Result really found", found); assertBundles("Someone is using the service now", 1, sr.getReference().getUsingBundles()); Result<IOException> res = Lookup.getDefault().lookupResult(IOException.class); res.addLookupListener(this); assertEquals("One instance found", 1, res.allInstances().size()); Collection<? extends Item<IOException>> items = res.allItems(); assertEquals("One item found: " + items, 1, items.size()); Item<IOException> first = items.iterator().next(); String expectedServiceID = "OSGiService[" + sr.getReference().getProperty(Constants.SERVICE_ID) + "]"; assertEquals("Proper ID", expectedServiceID, first.getId()); assertEquals("Right display name", "tristatricettri", first.getDisplayName()); sr.unregister(); IOException notFound = Lookup.getDefault().lookup(IOException.class); assertNull("Result not found", notFound); assertEquals("No instance found", 0, res.allInstances().size()); assertEquals("One change", 1, cnt); }
@Override public Collection<? extends Item<T>> allItems() { return checkResult().allItems(); }
FilterItem(Item<T> d, Class<T> clazz) { this.delegate = d; this.clazz = clazz; }
private Iterable<? extends Lookup.Item<OptionsCategory>> getODCategories() { Lookup lookup = Lookups.forPath(CategoryModel.OD_LAYER_FOLDER_NAME); Lookup.Result<OptionsCategory> result = lookup.lookupResult(OptionsCategory.class); return result.allItems(); }
private void fillServiceInstance(Item<T> li) { String className = getClassName(li); Object instance = null; synchronized(instanceCache) { instance = instanceCache.get (className); Object origInstance = origInstanceCache.get(className); Item lastItem = lookupItemsCache.get(className); if (origInstance != null && li instanceof AbstractLookup.Pair && lastItem != null && lastItem.getId().equals(li.getId())) { // Check whether the cached original instance was created by this lookup item. // If yes, we can use it, if not, we should create a new instance. // Verify whether the item is from the same registry file, // since there can be several registrations for one class instance. try { java.lang.reflect.Method creatorOfMethod = AbstractLookup.Pair.class.getDeclaredMethod("creatorOf", Object.class); creatorOfMethod.setAccessible(true); Object isCreator = creatorOfMethod.invoke(li, origInstance); if (logger.isLoggable(Level.FINE)) { logger.fine("fillServiceInstance("+li+" [HASH = "+System.identityHashCode(li)+"]):"); logger.fine(" className = \""+className+"\", orig instance = "+origInstance+", instance = "+instance+", is creator of = "+isCreator); if (lastItem != null) { logger.fine(" last item was "+lastItem+" [HASH = "+System.identityHashCode(lastItem)+"])"); } if (!Boolean.TRUE.equals(isCreator)) { logger.fine("\n!!!\n"+li+" is not a creator of "+origInstance+" !\nCreating a new instance..."); } } if (!Boolean.TRUE.equals(isCreator)) { instance = null; instanceCache.remove(className); origInstanceCache.remove(className); lookupItemsCache.remove(className); } } catch (Exception ex) { Exceptions.printStackTrace(ex); } } } // remember the instance position for merge purpose: li.fo.getAttribute("position") int position = getPosition(li); while (elementPositions.size() < size()) { elementPositions.add(Integer.MAX_VALUE); } elementPositions.add(position); if (instance != null) { try { add(service.cast(instance), className); } catch (ClassCastException cce) { logger.log(Level.WARNING, null, cce); } listenOn(instance.getClass().getClassLoader()); } else { add(new LazyInstance<T>(service, li)); } }
public LazyInstance(Class<T> service, Item<T> lookupItem) { this.service = service; this.lookupItem = lookupItem; }
@Override public <T> T lookup(Class<T> clazz) { Item<T> item = lookupItem(new Template<T>(clazz)); return (item == null) ? null : item.getInstance(); }
private synchronized Collection<Item<T>> getItems() { if (items == null) { items = itemsJustForPath(clazz, orig, path); } return items; }
public <T> Item<T> lookupItem(Template<T> template) { return lookup.lookupItem(template); }