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:com.mx.core.dao.BeanPropRowMap.java

/**
 * Extract the values for all columns in the current row.
 * <p>Utilizes public setters and result set metadata.
 * @see java.sql.ResultSetMetaData//from   w  w w  .  j a v a  2s .  c  o m
 */
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    Assert.state(this.mappedClass != null, "Mapped class was not specified");
    T mappedObject = BeanUtils.instantiate(this.mappedClass);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
    initBeanWrapper(bw);

    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null);

    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase());
        if (pd != null) {
            try {
                Object value = getColumnValue(rs, index, pd);
                if (logger.isDebugEnabled() && rowNumber == 0) {
                    //logger.debug("Mapping column '" + column + "' to property '" +
                    //      pd.getName() + "' of type " + pd.getPropertyType());
                }
                try {
                    bw.setPropertyValue(pd.getName(), value);
                } catch (TypeMismatchException e) {
                    if (value == null && primitivesDefaultedForNullValue) {
                        logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '"
                                + column + "' with value " + value + " when setting property '" + pd.getName()
                                + "' of type " + pd.getPropertyType() + " on object: " + mappedObject);
                    } else {
                        throw e;
                    }
                }
                if (populatedProperties != null) {
                    populatedProperties.add(pd.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException(
                        "Unable to map column " + column + " to property " + pd.getName(), ex);
            }
        }
    }

    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields "
                + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
    }

    return mappedObject;
}

From source file:org.opennms.netmgt.selta.scheduler.LegacyScheduler.java

/**
 * <p>start</p>//w  w w  . j  a v  a  2  s .  co  m
 */
public synchronized void start() {
    Assert.state(m_worker == null, "The fiber has already run or is running");

    m_worker = new Thread(this, getName());
    m_worker.start();
    m_status = STARTING;

    log().info("start: scheduler started");
}

From source file:org.opennms.ng.services.scheduler.LegacyScheduler.java

/**
 * <p>start</p>/*from www .  j av  a2 s . c  o  m*/
 */
@Override
public synchronized void start() {
    Assert.state(m_worker == null, "The fiber has already run or is running");

    m_worker = new Thread(this, getName());
    m_worker.start();
    m_status = STARTING;

    LOG.info("start: scheduler started");
}

From source file:es.fcs.batch.integration.chunk.MyChunkMessageChannelItemWriter.java

/**
 * Get the next result if it is available (within the timeout specified in the gateway), otherwise do nothing.
 * /* www .  ja  v a 2  s .  c om*/
 * @throws AsynchronousFailureException If there is a response and it contains a failed chunk response.
 * 
 * @throws IllegalStateException if the result contains the wrong job instance id (maybe we are sharing a channel
 * and we shouldn't be)
 */
private void getNextResult() throws AsynchronousFailureException {
    ChunkResponse payload = (ChunkResponse) messagingGateway.receive();
    if (payload != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Found result: " + payload);
        }
        Long jobInstanceId = payload.getJobId();
        Assert.state(jobInstanceId != null, "Message did not contain job instance id.");
        Assert.state(jobInstanceId.equals(localState.getJobId()), "Message contained wrong job instance id ["
                + jobInstanceId + "] should have been [" + localState.getJobId() + "].");
        if (payload.isRedelivered()) {
            logger.warn(
                    "Redelivered result detected, which may indicate stale state. In the best case, we just picked up a timed out message "
                            + "from a previous failed execution. In the worst case (and if this is not a restart), "
                            + "the step may now timeout.  In that case if you believe that all messages "
                            + "from workers have been sent, the business state "
                            + "is probably inconsistent, and the step will fail.");
            localState.incrementRedelivered();
        }
        localState.pushStepContribution(payload.getStepContribution());
        localState.incrementActual();
        if (!payload.isSuccessful()) {
            throw new AsynchronousFailureException(
                    "Failure or interrupt detected in handler: " + payload.getMessage());
        }
    }
}

From source file:org.opennms.netmgt.selta.scheduler.LegacyScheduler.java

/**
 * <p>stop</p>//from  w  w  w.  j av  a 2s  .com
 */
public synchronized void stop() {
    Assert.state(m_worker != null, "The fiber has never been started");

    m_status = STOP_PENDING;
    m_worker.interrupt();
    m_runner.shutdown();

    log().info("stop: scheduler stopped");
}

From source file:com.insframework.common.spring.jdbc.mapper.BeanPropertyRowMapper.java

/**
 * Extract the values for all columns in the current row.
 * <p>Utilizes public setters and result set metadata.
 * @see java.sql.ResultSetMetaData//from  ww  w.  j  av  a 2  s  . c  o m
 */
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    Assert.state(this.mappedClass != null, "Mapped class was not specified");
    T mappedObject = BeanUtils.instantiate(this.mappedClass);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
    initBeanWrapper(bw);

    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null);

    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase());
        if (pd != null) {
            try {
                Object value = getColumnValue(rs, index, pd);
                if (logger.isDebugEnabled() && rowNumber == 0) {
                    logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type "
                            + pd.getPropertyType());
                }
                try {
                    //add by guom
                    if (pd.getPropertyType() != null
                            && "java.lang.String".equals(pd.getPropertyType().getName())) {
                        if (value != null) {
                            bw.setPropertyValue(pd.getName(), String.valueOf(value));
                        } else {
                            bw.setPropertyValue(pd.getName(), "");
                        }
                    } else if (pd.getPropertyType() != null
                            && "double".equals(pd.getPropertyType().getName())) {
                        if (value != null) {
                            bw.setPropertyValue(pd.getName(), value);
                        } else {
                            bw.setPropertyValue(pd.getName(), 0d);
                        }
                    } else {
                        bw.setPropertyValue(pd.getName(), value);
                    }

                } catch (TypeMismatchException e) {
                    if (value == null && primitivesDefaultedForNullValue) {
                        logger.info("Intercepted TypeMismatchException for row " + rowNumber + " and column '"
                                + column + "' with value " + value + " when setting property '" + pd.getName()
                                + "' of type " + pd.getPropertyType() + " on object: " + mappedObject);
                    } else {
                        throw e;
                    }
                }
                if (populatedProperties != null) {
                    populatedProperties.add(pd.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException(
                        "Unable to map column " + column + " to property " + pd.getName(), ex);
            }
        }
    }

    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields "
                + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
    }

    return mappedObject;
}

From source file:org.opennms.ng.services.scheduler.LegacyScheduler.java

/**
 * <p>stop</p>/* ww w.  j a v a 2s.c  o  m*/
 */
@Override
public synchronized void stop() {
    Assert.state(m_worker != null, "The fiber has never been started");

    m_status = STOP_PENDING;
    m_worker.interrupt();
    m_runner.shutdown();

    LOG.info("stop: scheduler stopped");
}

From source file:com.github.hateoas.forms.spring.AffordanceBuilder.java

/**
 * Builds affordance with one or multiple rels which must have been defined previously using {@link #rel(String)} or
 * {@link #reverseRel(String, String)}.//  w  w  w.ja  v  a2 s.c  om
 * <p>
 * The motivation for multiple rels is this statement in the web linking rfc-5988: &quot;Note that link-values can convey multiple links
 * between the same target and context IRIs; for example:
 * </p>
 * 
 * <pre>
 * Link: &lt;http://example.org/&gt;
 *       rel="start http://example.net/relation/other"
 * </pre>
 * 
 * Here, the link to 'http://example.org/' has the registered relation type 'start' and the extension relation type
 * 'http://example.net/relation/other'.&quot;
 *
 * @return affordance
 * @see <a href="https://tools.ietf.org/html/rfc5988#section-5.5">Web Linking Examples</a>
 */
public Affordance build() {
    Assert.state(!(rels.isEmpty() && reverseRels.isEmpty()),
            "no rels or reverse rels found, call rel() or rev() before building the affordance");
    final Affordance affordance;
    affordance = new Affordance(new PartialUriTemplate(toString()), actionDescriptors,
            rels.toArray(new String[rels.size()]));
    for (Map.Entry<String, List<String>> linkParamEntry : linkParams.entrySet()) {
        final List<String> values = linkParamEntry.getValue();
        for (String value : values) {
            affordance.addLinkParam(linkParamEntry.getKey(), value);
        }
    }
    for (String reverseRel : reverseRels) {
        affordance.addRev(reverseRel);
    }
    affordance.setCollectionHolder(collectionHolder);
    return affordance;
}

From source file:org.opennms.ng.services.trapd.TrapQueueProcessor.java

@Override
public void afterPropertiesSet() throws IllegalStateException {
    Assert.state(m_eventConfDao != null, "property eventConfDao must be set");
    Assert.state(m_eventMgr != null, "property eventMgr must be set");
    Assert.state(m_newSuspect != null, "property newSuspect must be set");
    Assert.state(m_trapNotification != null, "property trapNotification must be set");
}