com.mantledillusion.metrics.trail.VaadinMetricsTrailSupport Maven / Gradle / Ivy
Show all versions of trail-metrics-support-vaadin-8 Show documentation
package com.mantledillusion.metrics.trail;
import com.vaadin.server.ErrorHandler;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinSession;
import com.vaadin.server.WebBrowser;
import com.vaadin.ui.UI;
import java.net.URI;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
* {@link MetricsTrail} support for Vaadin 7.
*
* By supporting a {@link VaadinService} using {@link #support(VaadinService)}, the {@link VaadinMetricsTrailSupport}
* will automatically begin new {@link MetricsTrail}s for every new {@link VaadinSession} of that service.
*/
public class VaadinMetricsTrailSupport extends AbstractVaadinMetricsTrailSupport {
private BiConsumer urlListener;
private VaadinMetricsTrailSupport(VaadinService service) {
super(service);
}
@Override
protected void observe(VaadinService service, Consumer sessionInitListener,
BiConsumer urlListener, Consumer sessionDestroyListener) {
service.addSessionInitListener(event -> sessionInitListener.accept(event.getSession()));
this.urlListener = urlListener;
service.addSessionDestroyListener(event -> sessionDestroyListener.accept(event.getSession()));
}
@Override
protected void hookSession(VaadinSession session, MetricsTrail trail) {
session.setAttribute(MetricsTrail.class, trail);
session.setAttribute(VaadinMetricsTrailSupport.class, this);
}
@Override
protected String getSessionId(VaadinSession session) {
return session.getSession().getId();
}
@Override
protected BrowserInfo getSessionBrowserInfo(VaadinSession session) {
WebBrowser browser = session.getBrowser();
String application = browser.getBrowserApplication();
BrowserInfo.BrowserType browserType = BrowserInfo.BrowserType.of(browser.isChrome(),
browser.isEdge(), browser.isFirefox(), browser.isIE(), browser.isOpera(), browser.isSafari());
String browserVersion = browser.getBrowserMajorVersion() + "." + browser.getBrowserMinorVersion();
BrowserInfo.SystemEnvironmentType environmentType = BrowserInfo.SystemEnvironmentType.of(browser.isAndroid(), browser.isIPad(),
browser.isIPhone(), browser.isLinux(), browser.isMacOSX(), browser.isWindows(), browser.isWindowsPhone());
return new BrowserInfo(application, browserType, browserVersion, environmentType);
}
@Override
protected MetricsTrail getSessionTrail() {
return getCurrent();
}
/**
* Returns the {@link MetricsTrail} of the current {@link VaadinSession}.
*
* @return The current {@link Thread} {@link VaadinSession}'s {@link MetricsTrail}, never null
* @throws IllegalStateException If the current {@link Thread} does not have a {@link VaadinSession}
*/
public static MetricsTrail getCurrent() throws IllegalStateException {
if (VaadinSession.getCurrent() == null) {
throw new IllegalStateException("There is no VaadinSession in the current thread");
}
return VaadinSession.getCurrent().getAttribute(MetricsTrail.class);
}
/**
* Permits {@link VaadinMetricsTrailSupport} to enable {@link MetricsTrail} support for the given {@link VaadinService}.
*
* The returned {@link VaadinMetricsTrailSupport} can be used to hook {@link MetricsTrailConsumer}s via
* {@link #hook(MetricsTrailConsumer)}.
*
* Will make the following {@link GeneralVaadinMetrics} available to all {@link MetricsConsumer}s:
* - {@link GeneralVaadinMetrics#SESSION_BEGIN}
* - {@link GeneralVaadinMetrics#SESSION_END}
* - {@link GeneralVaadinMetrics#BROWSER_INFO}
*
* @param service The {@link VaadinService} to observe; might not be null.
* @return A new {@link VaadinMetricsTrailSupport} instance, never null
*/
public static VaadinMetricsTrailSupport support(VaadinService service) {
if (service == null) {
throw new IllegalArgumentException("Cannot observe a null service");
}
return new VaadinMetricsTrailSupport(service);
}
/**
* Permits {@link VaadinMetricsTrailSupport} to enable {@link MetricsTrail} support for the given {@link UI}.
*
* For observation, the already existing {@link VaadinMetricsTrailSupport} of the given {@link UI}'s
* {@link VaadinSession} is used.
*
* Will make the following {@link GeneralVaadinMetrics} available to all {@link MetricsConsumer}s:
* - {@link GeneralVaadinMetrics#NAVIGATION}
*
* @param ui The {@link UI} instance to observe; might not be null.
*/
public static void support(UI ui) {
if (ui == null) {
throw new IllegalArgumentException("Cannot observe a null UI");
}
ui.getPage().addUriFragmentChangedListener(uriFragmentChangedEvent -> {
URI location = uriFragmentChangedEvent.getPage().getLocation();
ui.getSession().getAttribute(VaadinMetricsTrailSupport.class).urlListener.accept(location.getPath(), location.getQuery());
});
}
/**
* Permits {@link VaadinMetricsTrailSupport}s to enable {@link MetricsTrail} support for the given {@link ErrorHandler}.
*
* For observation, the already existing {@link VaadinMetricsTrailSupport} of the {@link VaadinSession} during the
* occuring error is used.
*
* The wrapping handler will dispatch metrics for all errors given to it before delegating them to its wrapped
* {@link ErrorHandler}.
*
* Will make the following {@link GeneralVaadinMetrics} available to all {@link MetricsConsumer}s:
* - {@link GeneralVaadinMetrics#ERROR}
*
* @param errorHandler The {@link ErrorHandler} instance to delegate to after dispatching; might not be null.
* @return A new {@link MetricsWrappingErrorHandler}, never null
*/
public static MetricsWrappingErrorHandler support(ErrorHandler errorHandler) {
if (errorHandler == null) {
throw new IllegalArgumentException("Cannot wrap a null error handler");
}
return new MetricsWrappingErrorHandler(errorHandler, () -> getCurrent());
}
}