Example usage for org.springframework.util Assert state

List of usage examples for org.springframework.util Assert state

Introduction

In this page you can find the example usage for org.springframework.util Assert state.

Prototype

public static void state(boolean expression, Supplier<String> messageSupplier) 

Source Link

Document

Assert a boolean expression, throwing an IllegalStateException if the expression evaluates to false .

Usage

From source file:org.springframework.boot.devtools.remote.client.RemoteClientConfiguration.java

private ClientHttpRequestInterceptor getSecurityInterceptor() {
    RemoteDevToolsProperties remoteProperties = this.properties.getRemote();
    String secretHeaderName = remoteProperties.getSecretHeaderName();
    String secret = remoteProperties.getSecret();
    Assert.state(secret != null, "The environment value 'spring.devtools.remote.secret' "
            + "is required to secure your connection.");
    return new HttpHeaderInterceptor(secretHeaderName, secret);
}

From source file:org.springframework.boot.devtools.restart.Restarter.java

/**
 * Return the active {@link Restarter} instance. Cannot be called before
 * {@link #initialize(String[]) initialization}.
 * @return the restarter/*  ww  w  . j  a va  2 s. c  o  m*/
 */
public synchronized static Restarter getInstance() {
    Assert.state(instance != null, "Restarter has not been initialized");
    return instance;
}

From source file:org.springframework.boot.devtools.restart.server.HttpRestartServer.java

/**
 * Handle a server request./*from w ww.j  a va2s  .  c om*/
 * @param request the request
 * @param response the response
 * @throws IOException in case of I/O errors
 */
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
    try {
        Assert.state(request.getHeaders().getContentLength() > 0, "No content");
        ObjectInputStream objectInputStream = new ObjectInputStream(request.getBody());
        ClassLoaderFiles files = (ClassLoaderFiles) objectInputStream.readObject();
        objectInputStream.close();
        this.server.updateAndRestart(files);
        response.setStatusCode(HttpStatus.OK);
    } catch (Exception ex) {
        logger.warn("Unable to handler restart server HTTP request", ex);
        response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

From source file:org.springframework.boot.devtools.tunnel.client.TunnelClient.java

/**
 * Start the client and accept incoming connections on the port.
 * @throws IOException in case of I/O errors
 *//*from w w  w  .ja v a 2 s. c  o  m*/
public synchronized void start() throws IOException {
    Assert.state(this.serverThread == null, "Server already started");
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort));
    logger.trace("Listening for TCP traffic to tunnel on port " + this.listenPort);
    this.serverThread = new ServerThread(serverSocketChannel);
    this.serverThread.start();
}

From source file:org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload.java

/**
 * Return the {@link HttpTunnelPayload} for the given message or {@code null} if there
 * is no payload.//from  w  w  w  . j av  a  2 s . co  m
 * @param message the HTTP message
 * @return the payload or {@code null}
 * @throws IOException in case of I/O errors
 */
public static HttpTunnelPayload get(HttpInputMessage message) throws IOException {
    long length = message.getHeaders().getContentLength();
    if (length <= 0) {
        return null;
    }
    String seqHeader = message.getHeaders().getFirst(SEQ_HEADER);
    Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header");
    ReadableByteChannel body = Channels.newChannel(message.getBody());
    ByteBuffer payload = ByteBuffer.allocate((int) length);
    while (payload.hasRemaining()) {
        body.read(payload);
    }
    body.close();
    payload.flip();
    return new HttpTunnelPayload(Long.valueOf(seqHeader), payload);
}

From source file:org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload.java

/**
 * Return the payload data for the given source {@link ReadableByteChannel} or null if
 * the channel timed out whilst reading.
 * @param channel the source channel/*from   ww w.  j  a va 2  s  .  c  o  m*/
 * @return payload data or {@code null}
 * @throws IOException in case of I/O errors
 */
public static ByteBuffer getPayloadData(ReadableByteChannel channel) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
    try {
        int amountRead = channel.read(buffer);
        Assert.state(amountRead != -1, "Target server connection closed");
        buffer.flip();
        return buffer;
    } catch (InterruptedIOException ex) {
        return null;
    }
}

From source file:org.springframework.boot.devtools.tunnel.server.RemoteDebugPortProvider.java

@Override
public int getPort() {
    Assert.state(isRemoteDebugRunning(), "Remote debug is not running");
    return getRemoteDebugPort();
}

From source file:org.springframework.boot.jta.narayana.DataSourceXAResourceRecoveryHelper.java

private XAResource getDelegate(boolean required) {
    Assert.state(this.delegate != null || !required, "Connection has not been opened");
    return this.delegate;
}

From source file:org.springframework.boot.loader.tools.Repackager.java

private LayoutFactory getLayoutFactory() {
    if (this.layoutFactory != null) {
        return this.layoutFactory;
    }//from  ww  w .ja va2  s.  c o m
    List<LayoutFactory> factories = SpringFactoriesLoader.loadFactories(LayoutFactory.class, null);
    if (factories.isEmpty()) {
        return new DefaultLayoutFactory();
    }
    Assert.state(factories.size() == 1, "No unique LayoutFactory found");
    return factories.get(0);
}

From source file:org.springframework.boot.test.context.SpringBootTestContextBootstrapper.java

protected Class<?>[] getOrFindConfigurationClasses(MergedContextConfiguration mergedConfig) {
    Class<?>[] classes = mergedConfig.getClasses();
    if (containsNonTestComponent(classes) || mergedConfig.hasLocations()
            || !mergedConfig.getContextInitializerClasses().isEmpty()) {
        return classes;
    }//w  w  w .j  a  va2  s.c  o m
    Class<?> found = new SpringBootConfigurationFinder().findFromClass(mergedConfig.getTestClass());
    Assert.state(found != null, "Unable to find a @SpringBootConfiguration, you need to use "
            + "@ContextConfiguration or @SpringBootTest(classes=...) " + "with your test");
    logger.info(
            "Found @SpringBootConfiguration " + found.getName() + " for test " + mergedConfig.getTestClass());
    return merge(found, classes);
}