Example usage for com.vaadin.util CurrentInstance get

List of usage examples for com.vaadin.util CurrentInstance get

Introduction

In this page you can find the example usage for com.vaadin.util CurrentInstance get.

Prototype

public static <T> T get(Class<T> type) 

Source Link

Document

Gets the current instance of a specific type if available.

Usage

From source file:com.google.code.vaadin.junit.AbstractMVPTestBase.java

License:Apache License

@Inject
void init(ScopedUIProvider uiProvider, Injector injector) {
    logger = LoggerFactory.getLogger(getClass());

    this.uiProvider = uiProvider;
    this.injector = injector;
    isFirstTest = true;//from   w  ww .  j  a v  a 2  s  .  c om
    mockedRequest = mock(VaadinRequest.class);
    mockedSession = mock(VaadinSession.class);

    ui = CurrentInstance.get(UI.class);
    ui.setSession(mockedSession);
}

From source file:org.jdal.vaadin.beans.VaadinScope.java

License:Apache License

/**
 * {@inheritDoc}/* www  .j  a  v a 2 s .  c o m*/
 */
public String getConversationId() {
    Integer uiId = null;

    UI ui = UI.getCurrent();
    if (ui == null) {
        UIid id = CurrentInstance.get(UIid.class);
        if (id != null) {
            uiId = id.getUiId();
        }
    } else if (ui != null) {
        if (!sessions.containsKey(ui)) {
            ui.addDetachListener(this);
            sessions.put(ui, VaadinSession.getCurrent().getSession().getId());
        }

        uiId = ui.getUIId();
    }

    return uiId != null ? getConversationId(uiId) : null;
}

From source file:org.panifex.web.vaadin.runtime.PageletAwareUI.java

License:Open Source License

@Override
protected void init(VaadinRequest request) {
    VaadinPagelet pagelet = pageletTracker.matchPathToPagelet(request.getPathInfo());
    if (pagelet != null) {
        try {//w w  w .j  a  va 2s.  com
            pagelet.service(request);
        } catch (Exception e) {
            log.error("Unable to service request", e);
            throw new RuntimeException(e);
        }
    } else {
        // pagelet not found - return HTTP 404 - Not found
        VaadinResponse response = CurrentInstance.get(VaadinResponse.class);
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.simplewebstack.app.vaadin.guice.uiscope.UIScopeProvider.java

License:Apache License

@Override
public T get() {//w w w.j ava  2s.c  o  m
    // get the scope cache for the current UI
    UIScope.logger.debug(String.format("Looking for a UIScoped instance of: [%s]", key));

    // get the current UIKey. It should always be there, as it is created before the UI
    UIKey uiKey = CurrentInstance.get(UIKey.class);
    // this may be null if we are in the process of constructing the UI
    ScopedUI currentUI = (ScopedUI) UI.getCurrent();
    String msg = "This should not be possible, unless perhaps you are testing and have not set up the test fixture correctly.  Try sub-classing AbstractMVPTestBase and run it with subclass of AbstractMVPApplicationTestModule.  If you are not testing please report a bug";
    if (uiKey == null) {
        if (currentUI == null) {
            throw new UIScopeException("ERROR: UI and uiKey are null. " + msg);
        } else {
            // this can happen when the framework switches UIs
            uiKey = currentUI.getInstanceKey();
            if (uiKey == null) {
                throw new UIScopeException("ERROR: uiKey is null and cannot be obtained from the UI. " + msg);
            }
        }
    }

    // currentUI may be null if we are in the process of constructing the UI
    // if not null just check that it hasn't got out of sync with its uikey
    if (currentUI != null) {
        if (!uiKey.equals(currentUI.getInstanceKey())) {
            throw new UIScopeException(
                    "ERROR: The UI and its UIKey have got out of sync.  Results are unpredictable. " + msg);
        }
    }

    UIScope.logger.debug(String.format("Looking for cache for key: [%s]", uiKey));
    Map<Key<?>, Object> scopedObjects = this.uiScope.getScopedObjectMap(uiKey);

    // retrieve an existing instance if possible

    @SuppressWarnings("unchecked")
    T current = (T) scopedObjects.get(key);

    if (current != null) {
        UIScope.logger.debug(
                String.format("Returning existing instance of [%s]", getInstanceSimpleClassName(current)));
        return current;
    }

    // or create the first instance and cache it
    current = unscoped.get();
    scopedObjects.put(key, current);
    UIScope.logger.debug(String.format("New instance of [%s] created, as none in cache",
            getInstanceSimpleClassName(current)));
    return current;
}

From source file:org.vaadin.spring.internal.UIStore.java

License:Apache License

public UIID currentUIID() {
    final UI currentUI = UI.getCurrent();
    if (currentUI != null && currentUI.getUIId() != -1) {
        return new UIID(currentUI);
    } else {/*  w  w  w. j  a  v  a2 s.  c  o m*/
        UIID currentIdentifier = CurrentInstance.get(UIID.class);
        Assert.notNull(currentIdentifier, String.format("Found no valid %s instance!", UIID.class.getName()));
        return currentIdentifier;
    }
}

From source file:pl.exsio.frameset.vaadin.bootstrap.spring.scope.UIScope.java

License:Open Source License

@Override
public String getConversationId() {
    Integer uiId = null;/* ww w  . j  av a 2 s  . co m*/

    UI ui = UI.getCurrent();
    if (ui == null) {
        UIid id = CurrentInstance.get(UIid.class);
        if (id != null) {
            uiId = id.getUiId();
        }
    } else if (ui != null) {
        if (!sessions.containsKey(ui)) {
            ui.addDetachListener(this);
            sessions.put(ui, VaadinSession.getCurrent().getSession().getId());
        }

        uiId = ui.getUIId();
    }

    return uiId != null ? getConversationId(uiId) : null;
}

From source file:uk.co.q3c.v7.base.guice.uiscope.UIScope.java

License:Apache License

@Override
public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) {
    return new Provider<T>() {
        @Override//from   w ww. ja  v  a2 s .c  o m
        public T get() {
            // get the scope cache for the current UI
            log.debug("looking for a UIScoped instance of {}", key.getClass().getName());

            // get the current UIKey. It should always be there, as it is created before the UI
            UIKey uiKey = CurrentInstance.get(UIKey.class);
            // this may be null if we are in the process of constructing the UI
            ScopedUI currentUI = (ScopedUI) UI.getCurrent();
            final String msg = "This should not be possible, unless perhaps you are testing and have not set up the test fixture correctly.  Try sub-classing UITestBase and calling createTestUI() or createBasicUI() to prepare the UIScope correctly.  If you are not testing please report a bug";
            if (uiKey == null) {
                if (currentUI == null) {
                    throw new UIScopeException("UI and uiKey are null. " + msg);
                } else {
                    // this can happen when the framework switches UIs
                    uiKey = currentUI.getInstanceKey();
                    if (uiKey == null) {
                        throw new UIScopeException("uiKey is null and cannot be obtained from the UI. " + msg);
                    }
                }
            }

            // currentUI may be null if we are in the process of constructing the UI
            // if not null just check that it hasn't got out of sync with its uikey
            if (currentUI != null) {
                if (!uiKey.equals(currentUI.getInstanceKey())) {
                    throw new UIScopeException(
                            "The UI and its UIKey have got out of sync.  Results are unpredictable. " + msg);
                }
            }

            log.debug("looking for cache for key: " + uiKey);
            Map<Key<?>, Object> scopedObjects = getScopedObjectMap(uiKey);
            // this line should fail tests but having trouble setting up a decent test. TestBench needed?
            // Map<Key<?>, Object> scopedObjects = getScopedObjectMap(CurrentInstance.get(UIKey.class));

            // retrieve an existing instance if possible

            @SuppressWarnings("unchecked")
            T current = (T) scopedObjects.get(key);

            if (current != null) {
                log.debug("returning existing instance of " + current.getClass().getSimpleName());
                return current;
            }

            // or create the first instance and cache it
            current = unscoped.get();
            scopedObjects.put(key, current);
            log.debug("new instance of " + current.getClass().getSimpleName() + " created, as none in cache");
            return current;
        }
    };
}

From source file:uk.q3c.krail.core.guice.uiscope.UIScopeProvider.java

License:Apache License

@Override
public T get() {//from ww w .  ja  va2 s.  c om
    // get the scope cache for the current UI
    log.debug("looking for a UIScoped instance of {}", key.getClass().getName());

    // get the current UIKey. It should always be there, as it is created before the UI
    UIKey uiKey = CurrentInstance.get(UIKey.class);
    // this may be null if we are in the process of constructing the UI
    ScopedUI currentUI = (ScopedUI) UI.getCurrent();
    final String msg = "This can happen if you include UIScoped components in your ScopedUIProvider, "
            + "or you are testing and have not set up the test fixture correctly.  For the latter, use TestUIScopeModule, or "
            + "try sub-classing UITestBase and calling createTestUI() or createBasicUI() to prepare the UIScope "
            + "correctly.  If you are not testing please report a bug";
    if (uiKey == null) {
        if (currentUI == null) {
            throw new UIScopeException("UI and uiKey are null. " + msg);
        } else {
            // this can happen when the framework switches UIs
            uiKey = currentUI.getInstanceKey();
            if (uiKey == null) {
                throw new UIScopeException("uiKey is null and cannot be obtained from the UI. " + msg);
            }
        }
    }

    // currentUI may be null if we are in the process of constructing the UI
    // if not null just check that it hasn't got out of sync with its uikey
    if (currentUI != null) {
        if (!uiKey.equals(currentUI.getInstanceKey())) {
            throw new UIScopeException(
                    "The UI and its UIKey have got out of sync.  Results are unpredictable. " + msg);
        }
    }

    log.debug("looking for cache for key: " + uiKey);
    Map<Key<?>, Object> scopedObjects = this.uiScope.getScopedObjectMap(uiKey);

    // retrieve an existing instance if possible

    @SuppressWarnings("unchecked")
    T current = (T) scopedObjects.get(key);

    if (current != null) {
        log.debug("returning existing instance of " + current.getClass().getSimpleName());
        return current;
    }

    // or create the first instance and cache it
    current = unscoped.get();
    scopedObjects.put(key, current);
    log.debug("new instance of " + current.getClass().getSimpleName() + " created, as none in cache");
    return current;
}