/*
* Copyright (c) 2014 Carman Consulting, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.carmanconsulting.cassidy.util;
import com.carmanconsulting.cassidy.exception.CassidyException;
import com.carmanconsulting.cassidy.exception.SerializerNotFoundException;
import com.carmanconsulting.cassidy.util.serializer.ByteSerializer;
import me.prettyprint.cassandra.serializers.CharSerializer;
import me.prettyprint.cassandra.serializers.ObjectSerializer;
import me.prettyprint.cassandra.serializers.SerializerTypeInferer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.lang.reflect.Field;
public final class CassidyUtils {
//----------------------------------------------------------------------------------------------------------------------
// Fields
//----------------------------------------------------------------------------------------------------------------------
private static final Logger LOGGER = LoggerFactory.getLogger(CassidyUtils.class);
//----------------------------------------------------------------------------------------------------------------------
// Static Methods
//----------------------------------------------------------------------------------------------------------------------
public static void createColumnFamily(Cluster cluster, ColumnFamilyDefinition definition) {
try {
LOGGER.info("Attempting to create column family {}:{}...", definition.getKeyspaceName(), definition.getName());
final KeyspaceDefinition keyspaceDefinition = cluster.describeKeyspace(definition.getKeyspaceName());
for (ColumnFamilyDefinition columnFamilyDefinition : keyspaceDefinition.getCfDefs()) {
if (columnFamilyDefinition.getName().equals(definition.getName())) {
LOGGER.info("Column family {}:{} already exists.", keyspaceDefinition.getName(), definition.getName());
return;
}
}
cluster.addColumnFamily(definition, true);
LOGGER.info("Column family {}:{} created successfully.", definition.getKeyspaceName(), definition.getName());
}
catch (HInvalidRequestException e) {
LOGGER.warn("Unable to create column family {}:{}!", definition.getKeyspaceName(), definition.getName(), e);
throw new CassidyException(String.format("Unable to create column family %s:%s!", definition.getKeyspaceName(), definition.getName()), e);
}
}
private static String getClassName(Object target) {
return target == null ? "null" : target.getClass().getName();
}
public static Object getFieldValue(Object target, String fieldName) {
try {
return FieldUtils.readField(target, fieldName, true);
}
catch (IllegalAccessException e) {
throw new CassidyException(String.format("Unable to read field %s value from object of type %s.", fieldName, getClassName(target)), e);
}
}
@SuppressWarnings("unchecked")
public static <T> Serializer<T> inferSerializer(Class<T> type) {
if (type.equals(Byte.class) || type.equals(byte.class)) {
return (Serializer<T>) ByteSerializer.get();
}
if (type.equals(char.class)) {
return (Serializer<T>) CharSerializer.get();
}
Serializer<?> serializer = SerializerTypeInferer.getSerializer(type);
if (serializer == null && Serializable.class.isAssignableFrom(type)) {
serializer = ObjectSerializer.get();
}
if (serializer == null) {
throw new SerializerNotFoundException(type);
}
return (Serializer<T>) serializer;
}
@SuppressWarnings("unchecked")
public static <T> Serializer<T> inferSerializer(T object) {
if (Byte.class.isInstance(object)) {
return (Serializer<T>) ByteSerializer.get();
}
Serializer<?> serializer = SerializerTypeInferer.getSerializer(object);
if (serializer == null) {
throw new SerializerNotFoundException(object);
}
return (Serializer<T>) serializer;
}
public static <T> T instantiate(Class<T> type) {
try {
LOGGER.debug("Instantiating object of type {}...", type.getCanonicalName());
return Validate.notNull(type.newInstance());
}
catch (InstantiationException e) {
throw new CassidyException(String.format("Unable to instantiate object of type %s.", type.getName()), e);
}
catch (IllegalAccessException e) {
throw new CassidyException(String.format("Type %s has no accessible default constructor.", type.getName()), e);
}
}
public static void setFieldValue(Object target, String fieldName, Object value) {
try {
LOGGER.debug("Setting field {} to value {} on object {}...", fieldName, value, target);
FieldUtils.writeField(target, fieldName, value, true);
}
catch (IllegalAccessException e) {
throw new CassidyException(String.format("Unable to write field %s value on object of type %s.", fieldName, getClassName(target)), e);
}
}
public static Class<?> getFieldType(Object target, String fieldName) {
Field field = FieldUtils.getDeclaredField(target.getClass(), fieldName, true);
return field.getType();
}
//----------------------------------------------------------------------------------------------------------------------
// Constructors
//----------------------------------------------------------------------------------------------------------------------
private CassidyUtils() {
}
}