Search in sources :

Example 1 with InstanceFilter

use of org.apache.sling.discovery.InstanceFilter in project sling by apache.

the class DefaultTopologyViewTest method testFind.

@Test
public void testFind() throws Exception {
    DefaultTopologyView newView = TopologyHelper.createTopologyView(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    TopologyHelper.createAndAddInstanceDescription(newView, newView.getClusterViews().iterator().next());
    try {
        newView.findInstances(null);
        fail("should complain");
    } catch (IllegalArgumentException iae) {
    // ok
    }
    final DefaultInstanceDescription id = TopologyHelper.createAndAddInstanceDescription(newView, newView.getClusterViews().iterator().next());
    TopologyHelper.createAndAddInstanceDescription(newView, newView.getClusterViews().iterator().next());
    assertEquals(4, newView.findInstances(new InstanceFilter() {

        public boolean accept(InstanceDescription instance) {
            return true;
        }
    }).size());
    assertEquals(1, newView.findInstances(new InstanceFilter() {

        public boolean accept(InstanceDescription instance) {
            return instance.getSlingId().equals(id.getSlingId());
        }
    }).size());
    assertEquals(1, newView.findInstances(new InstanceFilter() {

        public boolean accept(InstanceDescription instance) {
            return instance.isLeader();
        }
    }).size());
    assertEquals(1, newView.findInstances(new InstanceFilter() {

        boolean first = true;

        public boolean accept(InstanceDescription instance) {
            if (!first) {
                return false;
            }
            first = false;
            return true;
        }
    }).size());
}
Also used : InstanceFilter(org.apache.sling.discovery.InstanceFilter) InstanceDescription(org.apache.sling.discovery.InstanceDescription) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) Test(org.junit.Test)

Example 2 with InstanceFilter

use of org.apache.sling.discovery.InstanceFilter in project sling by apache.

the class TopologyWebConsolePlugin method renderProperties.

/**
     * Render the properties page of a particular instance
     */
private void renderProperties(final PrintWriter pw, final String contextPath, final String nodeId) {
    if (logger.isDebugEnabled()) {
        logger.debug("renderProperties: nodeId=" + nodeId);
    }
    final TopologyView tv = this.currentView;
    @SuppressWarnings("unchecked") Set<InstanceDescription> instances = (tv == null ? (Set<InstanceDescription>) Collections.EMPTY_SET : tv.findInstances(new InstanceFilter() {

        @Override
        public boolean accept(InstanceDescription instance) {
            String slingId = instance.getSlingId();
            if (logger.isDebugEnabled()) {
                logger.debug("renderProperties/picks: slingId={}", slingId);
            }
            return (slingId.equals(nodeId));
        }
    }));
    if (instances != null && instances.size() == 1) {
        InstanceDescription instance = instances.iterator().next();
        pw.println("Properties of " + instance.getSlingId() + ":<br/>");
        pw.println("<table class=\"adapters nicetable ui-widget tablesorter\">");
        pw.println("<thead>");
        pw.println("<tr>");
        pw.println("<th class=\"header ui-widget-header\">Key</th>");
        pw.println("<th class=\"header ui-widget-header\">Value</th>");
        pw.println("</tr>");
        pw.println("</thead>");
        pw.println("<tbody>");
        boolean odd = true;
        for (Iterator<Entry<String, String>> it = instance.getProperties().entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> entry = it.next();
            String oddEven = odd ? "odd" : "even";
            odd = !odd;
            pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
            pw.println("<td>" + entry.getKey() + "</td>");
            pw.println("<td>" + entry.getValue() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</tbody>");
        pw.println("</table>");
    }
}
Also used : InstanceFilter(org.apache.sling.discovery.InstanceFilter) Entry(java.util.Map.Entry) HashSet(java.util.HashSet) Set(java.util.Set) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Example 3 with InstanceFilter

use of org.apache.sling.discovery.InstanceFilter in project sling by apache.

the class TopologyWebConsolePlugin method renderProperties.

/**
     * Render the properties page of a particular instance
     */
private void renderProperties(final PrintWriter pw, final String contextPath, final String nodeId) {
    if (logger.isDebugEnabled()) {
        logger.debug("renderProperties: nodeId=" + nodeId);
    }
    final TopologyView tv = this.currentView;
    @SuppressWarnings("unchecked") Set<InstanceDescription> instances = (tv == null ? (Set<InstanceDescription>) Collections.EMPTY_SET : tv.findInstances(new InstanceFilter() {

        @Override
        public boolean accept(InstanceDescription instance) {
            String slingId = instance.getSlingId();
            if (logger.isDebugEnabled()) {
                logger.debug("renderProperties/picks: slingId={}", slingId);
            }
            return (slingId.equals(nodeId));
        }
    }));
    if (instances != null && instances.size() == 1) {
        InstanceDescription instance = instances.iterator().next();
        pw.println("Properties of " + instance.getSlingId() + ":<br/>");
        pw.println("<table class=\"adapters nicetable ui-widget tablesorter\">");
        pw.println("<thead>");
        pw.println("<tr>");
        pw.println("<th class=\"header ui-widget-header\">Key</th>");
        pw.println("<th class=\"header ui-widget-header\">Value</th>");
        pw.println("</tr>");
        pw.println("</thead>");
        pw.println("<tbody>");
        boolean odd = true;
        for (Iterator<Entry<String, String>> it = instance.getProperties().entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> entry = it.next();
            String oddEven = odd ? "odd" : "even";
            odd = !odd;
            pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
            pw.println("<td>" + entry.getKey() + "</td>");
            pw.println("<td>" + entry.getValue() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</tbody>");
        pw.println("</table>");
    }
}
Also used : InstanceFilter(org.apache.sling.discovery.InstanceFilter) Entry(java.util.Map.Entry) Set(java.util.Set) HashSet(java.util.HashSet) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Aggregations

InstanceDescription (org.apache.sling.discovery.InstanceDescription)3 InstanceFilter (org.apache.sling.discovery.InstanceFilter)3 HashSet (java.util.HashSet)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 TopologyView (org.apache.sling.discovery.TopologyView)2 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)1 Test (org.junit.Test)1