Search in sources :

Example 16 with Filter

use of io.fabric8.common.util.Filter in project fabric8 by jboss-fuse.

the class Log4jLogQuery method createPredicate.

private Predicate<LogEvent> createPredicate(LogFilter filter) {
    if (filter == null) {
        return null;
    }
    final List<Predicate<LogEvent>> predicates = new ArrayList<Predicate<LogEvent>>();
    final Set<String> levels = filter.getLevelsSet();
    if (levels.size() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                String level = event.getLevel();
                return level != null && levels.contains(level.toString());
            }
        });
    }
    final Long before = filter.getBeforeTimestamp();
    if (before != null) {
        final Date date = new Date(before);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.before(date);
            }
        });
    }
    final Long after = filter.getAfterTimestamp();
    if (after != null) {
        final Date date = new Date(after);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.after(date);
            }
        });
    }
    final String matchesText = filter.getMatchesText();
    if (matchesText != null && matchesText.length() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                if (contains(matchesText, event.getClassName(), event.getMessage(), event.getLogger(), event.getThread())) {
                    return true;
                }
                String[] throwableStrRep = event.getException();
                if (throwableStrRep != null && contains(matchesText, throwableStrRep)) {
                    return true;
                }
                Map properties = event.getProperties();
                if (properties != null && contains(matchesText, properties.toString())) {
                    return true;
                }
                return false;
            }
        });
    }
    if (predicates.size() == 0) {
        return null;
    } else if (predicates.size() == 1) {
        return predicates.get(0);
    } else {
        return new Predicate<LogEvent>() {

            @Override
            public String toString() {
                return "AndPredicate" + predicates;
            }

            @Override
            public boolean matches(LogEvent event) {
                for (Predicate<LogEvent> predicate : predicates) {
                    if (!predicate.matches(event)) {
                        return false;
                    }
                }
                return true;
            }
        };
    }
}
Also used : LogEvent(io.fabric8.insight.log.LogEvent) ArrayList(java.util.ArrayList) Map(java.util.Map) Date(java.util.Date) Predicate(io.fabric8.insight.log.support.Predicate)

Example 17 with Filter

use of io.fabric8.common.util.Filter in project fabric8 by jboss-fuse.

the class Log4jTest method assertLogQuery.

protected void assertLogQuery(LogQuerySupportMBean mBean) throws Exception {
    LogResults results = mBean.allLogResults();
    List<LogEvent> logEvents = assertNotEmpty(results);
    assertMatches(logEvents, messagePredicate("INFO", info1));
    assertMatches(logEvents, messagePredicate("INFO", info2));
    assertMatches(logEvents, messagePredicate("ERROR", error1));
    assertMatches(logEvents, messagePredicate("ERROR", error2));
    // test a filter
    LogFilter filter = new LogFilter();
    filter.setLevels(new String[] { "ERROR" });
    logEvents = assertNotEmpty(mBean.queryLogResults(filter));
    assertMatches(logEvents, messagePredicate("ERROR", error1));
    assertMatches(logEvents, messagePredicate("ERROR", error2));
    assertNotMatches(logEvents, messagePredicate("INFO", info1));
    assertNotMatches(logEvents, messagePredicate("INFO", info2));
}
Also used : LogResults(io.fabric8.insight.log.LogResults) LogEvent(io.fabric8.insight.log.LogEvent) LogFilter(io.fabric8.insight.log.LogFilter)

Example 18 with Filter

use of io.fabric8.common.util.Filter in project fabric8 by jboss-fuse.

the class LogQuerySupport method logResultsSince.

@Override
public LogResults logResultsSince(long time) throws IOException {
    LogFilter filter = new LogFilter();
    filter.setAfterTimestamp(time);
    return queryLogResults(filter);
}
Also used : LogFilter(io.fabric8.insight.log.LogFilter)

Example 19 with Filter

use of io.fabric8.common.util.Filter in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testDiscoveryOnRestartCurator.

@Test
public void testDiscoveryOnRestartCurator() throws Exception {
    underTest = new ActiveMQServiceFactory();
    underTest.curator = curator;
    Properties props = new Properties();
    props.put("config", "amq.xml");
    props.put("broker-name", "amq");
    props.put("group", "amq");
    props.put("connectors", "openwire");
    props.put("openwire-port", "0");
    props.put("container.ip", "localhost");
    underTest.updated("b", props);
    final AtomicReference<CuratorFramework> curatorFrameworkAtomicReference = new AtomicReference<>(curator);
    OsgiFabricDiscoveryAgent osgiFabricDiscoveryAgent = new OsgiFabricDiscoveryAgent(new BundleContext() {

        @Override
        public String getProperty(String s) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public Bundle installBundle(String s, InputStream inputStream) throws BundleException {
            return null;
        }

        @Override
        public Bundle installBundle(String s) throws BundleException {
            return null;
        }

        @Override
        public Bundle getBundle(long l) {
            return null;
        }

        @Override
        public Bundle[] getBundles() {
            return new Bundle[0];
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener, String s) throws InvalidSyntaxException {
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void removeServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void addBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void removeBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void addFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public void removeFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public ServiceRegistration<?> registerService(String[] strings, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration<?> registerService(String s, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public <S> ServiceRegistration<S> registerService(Class<S> aClass, S s, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceReference<?>[] getServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?>[] getAllServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?> getServiceReference(String s) {
            return null;
        }

        @Override
        public <S> ServiceReference<S> getServiceReference(Class<S> aClass) {
            return null;
        }

        @Override
        public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> aClass, String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public <S> S getService(ServiceReference<S> serviceReference) {
            return (S) curatorFrameworkAtomicReference.get();
        }

        @Override
        public boolean ungetService(ServiceReference<?> serviceReference) {
            return false;
        }

        @Override
        public File getDataFile(String s) {
            return null;
        }

        @Override
        public Filter createFilter(String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public Bundle getBundle(String s) {
            return null;
        }
    });
    final LinkedBlockingQueue<DiscoveryEvent> discoveryEvents = new LinkedBlockingQueue<DiscoveryEvent>(10);
    osgiFabricDiscoveryAgent.setDiscoveryListener(new DiscoveryListener() {

        @Override
        public void onServiceAdd(DiscoveryEvent discoveryEvent) {
            discoveryEvents.offer(discoveryEvent);
        }

        @Override
        public void onServiceRemove(DiscoveryEvent discoveryEvent) {
        }
    });
    // will call into dummy bundle and get curator
    osgiFabricDiscoveryAgent.addingService(null);
    osgiFabricDiscoveryAgent.setGroupName("amq");
    osgiFabricDiscoveryAgent.start();
    DiscoveryEvent event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("event: " + event);
    assertNotNull("got added service", event);
    underTest.deleted("b");
    // swap curator ref
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(15000).retryPolicy(new RetryNTimes(5000, 1000));
    curator = builder.build();
    LOG.debug("Starting new curator " + curator);
    curator.start();
    curatorFrameworkAtomicReference.get().close();
    curatorFrameworkAtomicReference.set(curator);
    // will call into dummy bundle and get new curator ref
    osgiFabricDiscoveryAgent.addingService(null);
    // start broker again
    underTest.curator = curator;
    underTest.updated("b", props);
    event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("new event: " + event);
    assertNotNull("got newly added service", event);
    underTest.deleted("b");
}
Also used : CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) DiscoveryEvent(org.apache.activemq.command.DiscoveryEvent) Properties(java.util.Properties) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryNTimes(org.apache.curator.retry.RetryNTimes) InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) OsgiFabricDiscoveryAgent(io.fabric8.mq.fabric.discovery.OsgiFabricDiscoveryAgent) Collection(java.util.Collection) File(java.io.File) DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener) Test(org.junit.Test)

Example 20 with Filter

use of io.fabric8.common.util.Filter in project fabric8 by jboss-fuse.

the class FabricHTTPGateway method getGatewayVersion.

/**
 * Returns the default profile version used to filter out the current versions of services
 * if no version expression is used the URI template
 */
String getGatewayVersion() {
    assertValid();
    Container currentContainer = fabricService.get().getCurrentContainer();
    if (currentContainer != null) {
        Version version = currentContainer.getVersion();
        if (version != null) {
            return version.getId();
        }
    }
    return null;
}
Also used : Container(io.fabric8.api.Container) Version(io.fabric8.api.Version)

Aggregations

List (java.util.List)18 Map (java.util.Map)18 Collectors (java.util.stream.Collectors)18 IOException (java.io.IOException)16 Pod (io.fabric8.kubernetes.api.model.Pod)12 ArrayList (java.util.ArrayList)12 File (java.io.File)11 HashMap (java.util.HashMap)11 Optional (java.util.Optional)11 TimeUnit (java.util.concurrent.TimeUnit)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 InputStream (java.io.InputStream)8 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)7 Test (org.junit.Test)7 Volume (io.fabric8.kubernetes.api.model.Volume)6 Arrays (java.util.Arrays)6 PodList (io.fabric8.kubernetes.api.model.PodList)5 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)5 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)5