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.opennms.netmgt.selta.scheduler.LegacyScheduler.java

/**
 * <p>pause</p>/* w w  w . ja v a  2 s .com*/
 */
public synchronized void pause() {
    Assert.state(m_worker != null, "The fiber has never been started");
    Assert.state(m_status != STOPPED && m_status != STOP_PENDING,
            "The fiber is not running or a stop is pending");

    if (m_status == PAUSED) {
        return;
    }

    m_status = PAUSE_PENDING;
    notifyAll();
}

From source file:com.duowan.common.spring.jdbc.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  ava  2 s  .  co 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.ng.services.scheduler.LegacyScheduler.java

/**
 * <p>pause</p>/*w  ww .jav  a  2  s  .c om*/
 */
@Override
public synchronized void pause() {
    Assert.state(m_worker != null, "The fiber has never been started");
    Assert.state(m_status != STOPPED && m_status != STOP_PENDING,
            "The fiber is not running or a stop is pending");

    if (m_status == PAUSED) {
        return;
    }

    m_status = PAUSE_PENDING;
    notifyAll();
}

From source file:net.sf.gazpachoquest.services.core.impl.ResearchServiceImpl.java

@Override
@Transactional(readOnly = false)/*from   w ww .j a v  a2  s  .  co  m*/
public Research save(Research research, Set<QuestionnaireDefinition> questionnaireDefinitions,
        Set<User> respondents) {
    research = saveOld(research);
    if (ResearchAccessType.BY_INVITATION.equals(research.getType())) {
        for (QuestionnaireDefinition questionnaireDefinition : questionnaireDefinitions) {

            questionnaireDefinition = questionnaireDefinitionRepository
                    .findOne(questionnaireDefinition.getId());

            Map<MailMessageTemplateType, MailMessageTemplate> templates = questionnaireDefinition
                    .getMailTemplates();
            MailMessageTemplate invitationTemplate = templates.get(MailMessageTemplateType.INVITATION);

            Group example = Group.with().name("Respondents").build();
            Group respondentsGroup = groupRepository.findOneByExample(example, new SearchParameters())
                    .orElseThrow(() -> new EmptyResultDataAccessException(
                            String.format("No %s entity with name %s found!", Group.class, "Respondents"), 1));

            for (User respondent : respondents) {
                Assert.state(!respondent.isNew(), "Persist all respondents before starting a research.");
                Questionnaire questionnaire = Questionnaire.with().status(EntityStatus.CONFIRMED)
                        .research(research).questionnaireDefinition(questionnaireDefinition)
                        .respondent(respondent).build();
                questionnaire = questionnaireRepository.save(questionnaire);
                // Create answers holder
                QuestionnaireAnswers questionnaireAnswers = new QuestionnaireAnswers();
                questionnaireAnswers = questionnaireAnswersRepository
                        .save(questionnaire.getQuestionnaireDefinition().getId(), questionnaireAnswers);
                questionnaire.setAnswersId(questionnaireAnswers.getId());

                String token = tokenGenerator.generate();

                respondent = userRepository.findOne(respondent.getId());
                // Grant permissions over questionnaire to respondent
                QuestionnairePermission permission = QuestionnairePermission.with().addPerm(Perm.READ)
                        .addPerm(Perm.UPDATE).user(respondent).target(questionnaire).build();
                questionnairePermissionRepository.save(permission);

                PersonalInvitation personalInvitation = PersonalInvitation.with().research(research)
                        .token(token).status(InvitationStatus.ACTIVE).respondent(respondent).build();
                invitationRepository.save(personalInvitation);

                MailMessage mailMessage = composeMailMessage(invitationTemplate, respondent, token);
                mailMessageRepository.save(mailMessage);

                if (groupRepository.isUserInGroup(respondent.getId(), "Respondents") == 0) {
                    respondentsGroup.assignUser(respondent);
                }
            }
        }
    } else {
        Assert.notEmpty(questionnaireDefinitions, "questionnairDefinitions required");
        Assert.state(questionnaireDefinitions.size() == 1,
                "Only one questionnairDefinitions supported for Open Access researches");
        String token = tokenGenerator.generate();

        AnonymousInvitation anonymousInvitation = AnonymousInvitation.with().research(research).token(token)
                .status(InvitationStatus.ACTIVE).build();
        invitationRepository.save(anonymousInvitation);
    }
    ResearchPermission permission = ResearchPermission.with().addPerm(Perm.READ).addPerm(Perm.UPDATE)
            .addPerm(Perm.DELETE).user(getAuthenticatedUser()).target(research).build();
    researchPermissionRepository.save(permission);
    return research;
}

From source file:cn.uncode.schedule.quartz.MethodInvokingJobDetailFactoryBean.java

/**
 * Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
 *//*from ww  w  .j  a va 2s. c  o m*/
@Override
public Class<?> getTargetClass() {
    Class<?> targetClass = super.getTargetClass();
    if (targetClass == null && this.targetBeanName != null) {
        Assert.state(this.beanFactory != null, "BeanFactory must be set when using 'targetBeanName'");
        targetClass = this.beanFactory.getType(this.targetBeanName);
    }
    return targetClass;
}

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

/**
 * <p>resume</p>//  w  w  w . j  ava  2s. com
 */
public synchronized void resume() {
    Assert.state(m_worker != null, "The fiber has never been started");
    Assert.state(m_status != STOPPED && m_status != STOP_PENDING,
            "The fiber is not running or a stop is pending");

    if (m_status == RUNNING) {
        return;
    }

    m_status = RESUME_PENDING;
    notifyAll();
}

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

/**
 * <p>resume</p>/* w ww. ja  v a  2  s.c o  m*/
 */
@Override
public synchronized void resume() {
    Assert.state(m_worker != null, "The fiber has never been started");
    Assert.state(m_status != STOPPED && m_status != STOP_PENDING,
            "The fiber is not running or a stop is pending");

    if (m_status == RUNNING) {
        return;
    }

    m_status = RESUME_PENDING;
    notifyAll();
}

From source file:cn.uncode.schedule.quartz.MethodInvokingJobDetailFactoryBean.java

/**
 * Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
 *//*from w  ww.  j av  a 2s . com*/
@Override
public Object getTargetObject() {
    Object targetObject = super.getTargetObject();
    if (targetObject == null && this.targetBeanName != null) {
        Assert.state(this.beanFactory != null, "BeanFactory must be set when using 'targetBeanName'");
        targetObject = this.beanFactory.getBean(this.targetBeanName);
    }
    return targetObject;
}

From source file:demo.tomcat.SciTomcatEmbeddedServletContainerFactory.java

protected void customizeConnector(Connector connector) {
    int port = (getPort() >= 0 ? getPort() : 0);
    connector.setPort(port);// w ww  .  j a  v a2  s.  co  m
    if (connector.getProtocolHandler() instanceof AbstractProtocol) {
        if (getAddress() != null) {
            ((AbstractProtocol<?>) connector.getProtocolHandler()).setAddress(getAddress());
        }
    }
    if (getUriEncoding() != null) {
        connector.setURIEncoding(getUriEncoding());
    }

    // If ApplicationContext is slow to start we want Tomcat not to bind to the socket
    // prematurely...
    connector.setProperty("bindOnInit", "false");

    if (getSsl() != null && getSsl().isEnabled()) {
        Assert.state(connector.getProtocolHandler() instanceof AbstractHttp11JsseProtocol,
                "To use SSL, the connector's protocol handler must be an "
                        + "AbstractHttp11JsseProtocol subclass");
        configureSsl((AbstractHttp11JsseProtocol<?>) connector.getProtocolHandler(), getSsl());
        connector.setScheme("https");
        connector.setSecure(true);
    }

    for (TomcatConnectorCustomizer customizer : this.tomcatConnectorCustomizers) {
        customizer.customize(connector);
    }
}

From source file:org.codehaus.grepo.query.jpa.repository.DefaultJpaRepository.java

/**
 * @return Returns the transaction manager.
 * @throws IllegalStateException in case factory is null
 *//*  w  ww . j  a v  a 2 s. com*/
protected EntityManager getTransactionalEntityManager() throws IllegalStateException {
    Assert.state(getEntityManagerFactory() != null, "No EntityManagerFactory specified");
    return EntityManagerFactoryUtils.getTransactionalEntityManager(getEntityManagerFactory(),
            getJpaPropertyMap());
}