Example usage for org.springframework.util Assert isInstanceOf

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

Introduction

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

Prototype

public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<String> messageSupplier) 

Source Link

Document

Assert that the provided object is an instance of the provided class.

Usage

From source file:org.springframework.integration.handler.support.MessagingMethodInvokerHelper.java

private String resolveExpression(String value, String msg) {
    String resolvedValue = resolve(value);

    if (!(resolvedValue.startsWith("#{") && value.endsWith("}"))) {
        return resolvedValue;
    }/*from ww  w  .j a  v a2  s  .c  o m*/

    Object evaluated = this.resolver.evaluate(resolvedValue, this.expressionContext);
    Assert.isInstanceOf(String.class, evaluated, msg);
    return (String) evaluated;
}

From source file:org.springframework.integration.redis.outbound.RedisCollectionPopulatingMessageHandler.java

private Object assertMapEntry(Message<?> message, boolean property) {
    Object mapKey = this.mapKeyExpression.getValue(this.evaluationContext, message);
    Assert.notNull(mapKey, "Can not determine a map key for the entry. The key is determined by evaluating "
            + "the 'mapKeyExpression' property.");
    Object payload = message.getPayload();
    if (property) {
        Assert.isInstanceOf(String.class, mapKey, "For property, key must be a String");
        Assert.isInstanceOf(String.class, payload, "For property, payload must be a String");
    }//from   w  w  w.j a  v a 2  s  .com
    Assert.isTrue(mapKey != null,
            "Failed to determine the key for the " + "Redis Map entry. Payload is not a Map and '"
                    + RedisHeaders.MAP_KEY + "' header is not provided");
    return mapKey;
}

From source file:org.springframework.integration.redis.outbound.RedisCollectionPopulatingMessageHandler.java

private double determineScore(Message<?> message) {
    Object scoreHeader = message.getHeaders().get(RedisHeaders.ZSET_SCORE);
    if (scoreHeader == null) {
        return Double.valueOf(1);
    } else {/*from  w w  w. j  a  va 2  s  . c  om*/
        Assert.isInstanceOf(Number.class, scoreHeader,
                "Header " + RedisHeaders.ZSET_SCORE + " must be a Number");
        Number score = (Number) scoreHeader;
        return Double.valueOf(score.toString());
    }
}

From source file:org.springframework.integration.redis.outbound.RedisStoreWritingMessageHandler.java

private void writeToProperties(final RedisProperties properties, Message<?> message) {
    final Object payload = message.getPayload();
    if (this.extractPayloadElements && payload instanceof Properties) {
        this.processInPipeline(new PipelineCallback() {
            public void process() {
                properties.putAll((Properties) payload);
            }/*from   w  w w.  j a v  a2 s  .c om*/
        });
    } else {
        Assert.isInstanceOf(String.class, payload, "For property, payload must be a String.");
        Object key = this.determineMapKey(message, true);
        properties.put(key, payload);
    }
}

From source file:org.springframework.integration.redis.outbound.RedisStoreWritingMessageHandler.java

private Object determineMapKey(Message<?> message, boolean property) {
    Object mapKey = this.mapKeyExpression.getValue(this.evaluationContext, message);
    Assert.notNull(mapKey, "Cannot determine a map key for the entry. The key is determined by evaluating "
            + "the 'mapKeyExpression' property.");
    if (property) {
        Assert.isInstanceOf(String.class, mapKey, "For property, key must be a String");
    }// ww  w. ja v  a2s  . c  om
    Assert.isTrue(mapKey != null,
            "Failed to determine the key for the " + "Redis Map entry. Payload is not a Map and '"
                    + RedisHeaders.MAP_KEY + "' header is not provided");
    return mapKey;
}

From source file:org.springframework.kafka.config.MethodKafkaListenerEndpoint.java

private String resolve(String value) {
    if (getResolver() != null) {
        Object newValue = getResolver().evaluate(value, getBeanExpressionContext());
        Assert.isInstanceOf(String.class, newValue, "Invalid @SendTo expression");
        return (String) newValue;
    } else {//from   w  w  w . j av  a  2s .  c o  m
        return value;
    }
}

From source file:org.springframework.messaging.simp.broker.OrderedMessageSender.java

/**
 * Install or remove an {@link ExecutorChannelInterceptor} that invokes a
 * completion task once the message is handled.
 * @param channel the channel to configure
 * @param preservePublishOrder whether preserve order is on or off based on
 * which an interceptor is either added or removed.
 *//*from  ww w.  ja  v a2s.  c o m*/
static void configureOutboundChannel(MessageChannel channel, boolean preservePublishOrder) {
    if (preservePublishOrder) {
        Assert.isInstanceOf(ExecutorSubscribableChannel.class, channel,
                "An ExecutorSubscribableChannel is required for `preservePublishOrder`");
        ExecutorSubscribableChannel execChannel = (ExecutorSubscribableChannel) channel;
        if (execChannel.getInterceptors().stream().noneMatch(i -> i instanceof CallbackInterceptor)) {
            execChannel.addInterceptor(0, new CallbackInterceptor());
        }
    } else if (channel instanceof ExecutorSubscribableChannel) {
        ExecutorSubscribableChannel execChannel = (ExecutorSubscribableChannel) channel;
        execChannel.getInterceptors().stream().filter(i -> i instanceof CallbackInterceptor).findFirst()
                .map(execChannel::removeInterceptor);

    }
}

From source file:org.springframework.osgi.iandt.web.HttpClient.java

public static HttpResponse getResponse(String address) throws Exception {

    // on JDK 1.5 there is a dedicated method - but not on 1.4...
    System.setProperty(CON_TIMEOUT_SYS_PROP, DEFAULT_TIMEOUT);
    System.setProperty(READ_TIMEOUT_SYS_PROP, READ_TIMEOUT);

    Assert.notNull(address);/* w ww  . j a v  a  2 s .  c o  m*/
    if (!address.startsWith(HTTP_PROTOCOL))
        address = HTTP_PROTOCOL.concat(PROTOCOL_DELIMITER).concat(address);

    log.info("creating connection to [" + address + "] ...");
    // create the URL
    URL url = new URL(address);

    URLConnection urlCon = url.openConnection();
    Assert.isInstanceOf(HttpURLConnection.class, urlCon, "only http(s) connections supported");

    HttpURLConnection http = (HttpURLConnection) urlCon;
    //
    // adjust the connection settings (before connecting)
    //
    // no cache
    http.setUseCaches(false);

    try {
        http.connect();
        return new HttpResponse(http);
    } finally {
        http.disconnect();
        // erase the timeout property (since it's system wide)
        System.setProperty(CON_TIMEOUT_SYS_PROP, "");
        System.setProperty(READ_TIMEOUT_SYS_PROP, "");
    }
}

From source file:org.springframework.security.access.intercept.AfterInvocationProviderManager.java

public void setProviders(List<?> newList) {
    checkIfValidList(newList);//from  ww w .  j ava  2  s  . c o  m
    providers = new ArrayList<>(newList.size());

    for (Object currentObject : newList) {
        Assert.isInstanceOf(AfterInvocationProvider.class, currentObject, () -> "AfterInvocationProvider "
                + currentObject.getClass().getName() + " must implement AfterInvocationProvider");
        providers.add((AfterInvocationProvider) currentObject);
    }
}

From source file:org.springframework.security.acls.cassandra.CassandraMutableAclService.java

public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    Assert.notNull(objectIdentity, "Object Identity required");

    if (LOG.isDebugEnabled()) {
        LOG.debug("BEGIN createAcl: objectIdentity: " + objectIdentity);
    }/*from w w w . ja  va 2 s .  co m*/

    // Need to retrieve the current principal, in order to know who "owns"
    // this ACL (can be changed later on)
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    PrincipalSid sid = new PrincipalSid(auth);

    AclObjectIdentity newAoi = new AclObjectIdentity(objectIdentity);
    newAoi.setOwnerId(sid.getPrincipal());
    newAoi.setOwnerPrincipal(true);
    newAoi.setEntriesInheriting(false);

    try {
        aclRepository.saveAcl(newAoi);
    } catch (AclAlreadyExistsException e) {
        throw new AlreadyExistsException(e.getMessage(), e);
    }

    // Retrieve the ACL via superclass (ensures cache registration, proper retrieval etc)
    Acl acl = readAclById(objectIdentity);
    Assert.isInstanceOf(MutableAcl.class, acl, "MutableAcl should be been returned");

    if (LOG.isDebugEnabled()) {
        LOG.debug("END createAcl: acl: " + acl);
    }
    return (MutableAcl) acl;
}