Search in sources :

Example 11 with PropertyAccessException

use of org.ff4j.exception.PropertyAccessException in project ff4j by ff4j.

the class JdbcPropertyStore method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> ap) {
    Util.assertNotNull(ap);
    Connection sqlConn = null;
    PreparedStatement ps = null;
    try {
        sqlConn = getDataSource().getConnection();
        if (existProperty(ap.getName())) {
            throw new PropertyAlreadyExistException(ap.getName());
        }
        ps = sqlConn.prepareStatement(getQueryBuilder().createProperty());
        ps.setString(1, ap.getName());
        ps.setString(2, ap.getType());
        ps.setString(3, ap.asString());
        ps.setString(4, ap.getDescription());
        if (ap.getFixedValues() != null && !ap.getFixedValues().isEmpty()) {
            String fixedValues = ap.getFixedValues().toString();
            ps.setString(5, fixedValues.substring(1, fixedValues.length() - 1));
        } else {
            ps.setString(5, null);
        }
        ps.executeUpdate();
    } catch (SQLException sqlEX) {
        throw new PropertyAccessException("Cannot update properties database, SQL ERROR", sqlEX);
    } finally {
        // Connection is closed alse here within clos statement
        closeStatement(ps);
        closeConnection(sqlConn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException) PropertyAccessException(org.ff4j.exception.PropertyAccessException) PreparedStatement(java.sql.PreparedStatement)

Example 12 with PropertyAccessException

use of org.ff4j.exception.PropertyAccessException in project ff4j by ff4j.

the class JdbcPropertyStore method clear.

/**
 * {@inheritDoc}
 */
public void clear() {
    PreparedStatement ps = null;
    Connection sqlConn = null;
    try {
        sqlConn = getDataSource().getConnection();
        ps = buildStatement(sqlConn, getQueryBuilder().deleteAllProperties());
        ps.executeUpdate();
    } catch (SQLException sqlEX) {
        throw new PropertyAccessException("Cannot clear properties table, SQL ERROR", sqlEX);
    } finally {
        closeStatement(ps);
        closeConnection(sqlConn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) PropertyAccessException(org.ff4j.exception.PropertyAccessException) PreparedStatement(java.sql.PreparedStatement)

Example 13 with PropertyAccessException

use of org.ff4j.exception.PropertyAccessException in project ff4j by ff4j.

the class PropertyStoreHBase method deleteProperty.

/**
 * {@inheritDoc}
 */
@Override
public void deleteProperty(String name) {
    assertPropertyExist(name);
    try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
        try (Table table = hbConn.getTable(PROPERTIES_TABLENAME)) {
            List<Delete> list = new ArrayList<Delete>();
            Delete del = new Delete(name.getBytes());
            list.add(del);
            table.delete(list);
        }
    } catch (IOException e) {
        throw new PropertyAccessException("Cannot delete property ", e);
    }
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) Table(org.apache.hadoop.hbase.client.Table) HBaseConnection(org.ff4j.hbase.HBaseConnection) Connection(org.apache.hadoop.hbase.client.Connection) ArrayList(java.util.ArrayList) PropertyAccessException(org.ff4j.exception.PropertyAccessException) IOException(java.io.IOException)

Example 14 with PropertyAccessException

use of org.ff4j.exception.PropertyAccessException in project ff4j by ff4j.

the class PropertyStoreHttp method deleteProperty.

/**
 * {@inheritDoc}
 */
public void deleteProperty(String name) {
    Util.assertHasLength(name);
    ClientResponse cRes = getStore().path(name).delete(ClientResponse.class);
    if (Status.NOT_FOUND.getStatusCode() == cRes.getStatus()) {
        throw new PropertyNotFoundException(name);
    }
    if (Status.NO_CONTENT.getStatusCode() != cRes.getStatus()) {
        throw new PropertyAccessException("Cannot delete property, an HTTP error " + cRes.getStatus() + OCCURED);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) PropertyNotFoundException(org.ff4j.exception.PropertyNotFoundException) PropertyAccessException(org.ff4j.exception.PropertyAccessException)

Example 15 with PropertyAccessException

use of org.ff4j.exception.PropertyAccessException in project ff4j by ff4j.

the class PropertyStoreHttp method readAllProperties.

/**
 * {@inheritDoc}
 */
public Map<String, Property<?>> readAllProperties() {
    ClientResponse cRes = getStore().get(ClientResponse.class);
    if (Status.OK.getStatusCode() != cRes.getStatus()) {
        throw new PropertyAccessException("Cannot read properties, an HTTP error " + cRes.getStatus() + OCCURED);
    }
    String resEntity = cRes.getEntity(String.class);
    Property<?>[] pArray = PropertyJsonParser.parsePropertyArray(resEntity);
    Map<String, Property<?>> properties = new HashMap<String, Property<?>>();
    for (Property<?> pName : pArray) {
        properties.put(pName.getName(), pName);
    }
    return properties;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) HashMap(java.util.HashMap) PropertyAccessException(org.ff4j.exception.PropertyAccessException) Property(org.ff4j.property.Property)

Aggregations

PropertyAccessException (org.ff4j.exception.PropertyAccessException)18 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 JdbcUtils.closeConnection (org.ff4j.utils.JdbcUtils.closeConnection)8 Response (javax.ws.rs.core.Response)5 ResultSet (java.sql.ResultSet)4 PropertyNotFoundException (org.ff4j.exception.PropertyNotFoundException)4 Property (org.ff4j.property.Property)4 JdbcUtils.closeResultSet (org.ff4j.utils.JdbcUtils.closeResultSet)4 ClientResponse (com.sun.jersey.api.client.ClientResponse)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 WebTarget (javax.ws.rs.client.WebTarget)2 Connection (org.apache.hadoop.hbase.client.Connection)2 Table (org.apache.hadoop.hbase.client.Table)2 HBaseConnection (org.ff4j.hbase.HBaseConnection)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1