use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-rdbms by datanucleus.
the class AbstractLargeBinaryRDBMSMapping method getObjectForBytes.
protected Object getObjectForBytes(byte[] bytes, int param) {
String typeName = getJavaTypeMapping().getType();
if (getJavaTypeMapping() instanceof TypeConverterMapping) {
// Using TypeConverterMapping so use the datastore type for the converter
TypeConverter conv = ((TypeConverterMapping) getJavaTypeMapping()).getTypeConverter();
Class datastoreType = storeMgr.getNucleusContext().getTypeManager().getDatastoreTypeForTypeConverter(conv, getJavaTypeMapping().getJavaType());
typeName = datastoreType.getName();
}
// Use Java serialisation, else byte-streaming, and if not determined then Java serialisation
if (getJavaTypeMapping().isSerialised()) {
// Serialised field so just perform basic Java deserialisation for retrieval
try {
BlobImpl blob = new BlobImpl(bytes);
return blob.getObject();
} catch (SQLException sqle) {
// Impossible (JDK 1.6 use of free())
return null;
}
} else if (typeName.equals(ClassNameConstants.BOOLEAN_ARRAY)) {
return TypeConversionHelper.getBooleanArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.BYTE_ARRAY)) {
return bytes;
} else if (typeName.equals(ClassNameConstants.CHAR_ARRAY)) {
return TypeConversionHelper.getCharArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.DOUBLE_ARRAY)) {
return TypeConversionHelper.getDoubleArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.FLOAT_ARRAY)) {
return TypeConversionHelper.getFloatArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.INT_ARRAY)) {
return TypeConversionHelper.getIntArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.LONG_ARRAY)) {
return TypeConversionHelper.getLongArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.SHORT_ARRAY)) {
return TypeConversionHelper.getShortArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_BOOLEAN_ARRAY)) {
return TypeConversionHelper.getBooleanObjectArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_BYTE_ARRAY)) {
return TypeConversionHelper.getByteObjectArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_CHARACTER_ARRAY)) {
return TypeConversionHelper.getCharObjectArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_DOUBLE_ARRAY)) {
return TypeConversionHelper.getDoubleObjectArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_FLOAT_ARRAY)) {
return TypeConversionHelper.getFloatObjectArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_INTEGER_ARRAY)) {
return TypeConversionHelper.getIntObjectArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_LONG_ARRAY)) {
return TypeConversionHelper.getLongObjectArrayFromByteArray(bytes);
} else if (typeName.equals(ClassNameConstants.JAVA_LANG_SHORT_ARRAY)) {
return TypeConversionHelper.getShortObjectArrayFromByteArray(bytes);
} else if (typeName.equals(BigDecimal[].class.getName())) {
return TypeConversionHelper.getBigDecimalArrayFromByteArray(bytes);
} else if (typeName.equals(BigInteger[].class.getName())) {
return TypeConversionHelper.getBigIntegerArrayFromByteArray(bytes);
} else if (getJavaTypeMapping().getJavaType() != null && getJavaTypeMapping().getJavaType().getName().equals("java.util.BitSet")) {
return TypeConversionHelper.getBitSetFromBooleanArray(TypeConversionHelper.getBooleanArrayFromByteArray(bytes));
} else if (getJavaTypeMapping().getJavaType() != null && getJavaTypeMapping().getJavaType().getName().equals("java.awt.image.BufferedImage")) {
try {
return ImageIO.read(new ByteArrayInputStream(bytes));
} catch (IOException e) {
throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
}
} else {
// Fallback to just perform basic Java deserialisation for retrieval
try {
BlobImpl blob = new BlobImpl(bytes);
return blob.getObject();
} catch (SQLException sqle) {
// Impossible (JDK 1.6 use of free())
return null;
}
}
}
use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getValueMapping.
/**
* Convenience accessor for the mapping of the value for a map.
* @param table The container
* @param mmd MetaData for the field/property containing the map that this value is for
* @param clr ClassLoader resolver
* @return The mapping
*/
protected JavaTypeMapping getValueMapping(Table table, AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
if (mmd.getMap() == null) {
// TODO Localise this
throw new NucleusException("Attempt to get value mapping for field " + mmd.getFullFieldName() + " that has no map!").setFatal();
}
MappingConverterDetails mcd = null;
Class mc = null;
String userTypeConverterName = null;
String userMappingClassName = null;
if (mmd.getValueMetaData() != null) {
userTypeConverterName = mmd.getValueMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME);
userMappingClassName = mmd.getValueMetaData().getValueForExtension("mapping-class");
}
if (userTypeConverterName != null) {
TypeConverter conv = storeMgr.getNucleusContext().getTypeManager().getTypeConverterForName(userTypeConverterName);
if (conv == null) {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " VALUE has been specified to use type converter " + userTypeConverterName + " but not found!");
}
// TODO Could be TypeConverterMultiMapping?
mcd = new MappingConverterDetails(TypeConverterMapping.class, conv);
} else if (userMappingClassName != null) {
// User has defined their own mapping class for this value so use that
try {
mc = clr.classForName(userMappingClassName);
} catch (NucleusException jpe) {
throw new NucleusUserException(Localiser.msg("041014", userMappingClassName)).setFatal();
}
} else {
boolean serialised = (mmd.hasMap() && mmd.getMap().isSerializedValue());
boolean embedded = (mmd.hasMap() && mmd.getMap().isEmbeddedValue());
boolean embeddedPC = (mmd.hasMap() && mmd.getMap().valueIsPersistent() && mmd.getMap().isEmbeddedValue()) || (mmd.getValueMetaData() != null && mmd.getValueMetaData().getEmbeddedMetaData() != null);
boolean valuePC = (mmd.hasMap() && mmd.getMap().valueIsPersistent());
Class valueCls = clr.classForName(mmd.getMap().getValueType());
boolean valueReference = ClassUtils.isReferenceType(valueCls);
if (serialised) {
if (valuePC) {
// Serialised PC value
mc = SerialisedValuePCMapping.class;
} else if (valueReference) {
// Serialised Reference value
mc = SerialisedReferenceMapping.class;
} else {
// Serialised Non-PC element
mc = SerialisedMapping.class;
}
} else if (embedded) {
if (embeddedPC) {
// Embedded PC key
mc = EmbeddedValuePCMapping.class;
} else if (valuePC) {
// "Embedded" PC type but no <embedded> so dont embed for now. Is this correct?
mc = PersistableMapping.class;
} else {
// Embedded Non-PC type
mcd = getMappingClass(valueCls, serialised, embedded, mmd.getValueMetaData() != null ? mmd.getValueMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
} else {
// Normal value mapping
mcd = getMappingClass(valueCls, serialised, embedded, mmd.getValueMetaData() != null ? mmd.getValueMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
}
if (mcd != null && mcd.typeConverter == null) {
mc = mcd.mappingClass;
}
if (mc != null && (mcd == null || mcd.typeConverter == null)) {
// Create the mapping of the selected type
JavaTypeMapping m = null;
try {
m = (JavaTypeMapping) mc.getDeclaredConstructor().newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_VALUE);
m.initialize(mmd, table, clr);
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
} else if (mcd != null && mcd.typeConverter != null) {
try {
JavaTypeMapping m = mcd.mappingClass.getDeclaredConstructor().newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_VALUE);
if (m instanceof TypeConverterMapping) {
((TypeConverterMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
} else if (m instanceof TypeConverterMultiMapping) {
((TypeConverterMultiMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
}
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc != null ? mc.getName() : null, e), e).setFatal();
}
}
throw new NucleusException("Unable to create mapping for value of map at " + mmd.getFullFieldName() + " - no available mapping");
}
use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getMappingClass.
/**
* Accessor for the mapping class for the specified type.
* Provides special handling for interface types and for classes that are being embedded in a field.
* Refers others to its mapping manager lookup.
* @param javaType Class to query
* @param serialised Whether the field is serialised
* @param embedded Whether the field is embedded
* @param colmds Metadata for column(s) (optional)
* @param fieldName The full field name (for logging only)
* @return The mapping class for the class
*/
protected MappingConverterDetails getMappingClass(Class javaType, boolean serialised, boolean embedded, ColumnMetaData[] colmds, String fieldName) {
ApiAdapter api = storeMgr.getApiAdapter();
if (api.isPersistable(javaType)) {
// Persistence Capable field
if (serialised) {
// Serialised PC field
return new MappingConverterDetails(SerialisedPCMapping.class);
} else if (embedded) {
// Embedded PC field
return new MappingConverterDetails(EmbeddedPCMapping.class);
} else {
// PC field
return new MappingConverterDetails(PersistableMapping.class);
}
}
if (javaType.isInterface() && !storeMgr.getMappingManager().isSupportedMappedType(javaType.getName())) {
// Interface field
if (serialised) {
// Serialised Interface field
return new MappingConverterDetails(SerialisedReferenceMapping.class);
} else if (embedded) {
// Embedded interface field - just default to an embedded PCMapping!
return new MappingConverterDetails(EmbeddedPCMapping.class);
} else {
// Interface field
return new MappingConverterDetails(InterfaceMapping.class);
}
}
if (javaType == java.lang.Object.class) {
// Object field
if (serialised) {
// Serialised Object field
return new MappingConverterDetails(SerialisedReferenceMapping.class);
} else if (embedded) {
// Embedded Object field - do we ever want to support this ? I think not ;-)
throw new NucleusUserException(Localiser.msg("041042", fieldName)).setFatal();
} else {
// Object field as reference to PC object
return new MappingConverterDetails(ObjectMapping.class);
}
}
if (javaType.isArray()) {
// Array field
if (api.isPersistable(javaType.getComponentType())) {
// Array of PC objects
return new MappingConverterDetails(ArrayMapping.class);
} else if (javaType.getComponentType().isInterface() && !storeMgr.getMappingManager().isSupportedMappedType(javaType.getComponentType().getName())) {
// Array of interface objects
return new MappingConverterDetails(ArrayMapping.class);
} else if (javaType.getComponentType() == java.lang.Object.class) {
// Array of Object reference objects
return new MappingConverterDetails(ArrayMapping.class);
}
// Other array types will be caught by the default mappings
}
// Find a suitable mapping for the type and column definition (doesn't allow for serialised setting)
MappingConverterDetails mcd = getDefaultJavaTypeMapping(javaType, colmds);
if (mcd == null || mcd.mappingClass == null) {
Class superClass = javaType.getSuperclass();
while (superClass != null && !superClass.getName().equals(ClassNameConstants.Object) && (mcd == null || mcd.mappingClass == null)) {
mcd = getDefaultJavaTypeMapping(superClass, colmds);
superClass = superClass.getSuperclass();
}
}
if (mcd == null) {
if (storeMgr.getMappingManager().isSupportedMappedType(javaType.getName())) {
// "supported" type yet no FCO mapping !
throw new NucleusUserException(Localiser.msg("041001", fieldName, javaType.getName()));
}
// start in this class
Class superClass = javaType;
while (superClass != null && !superClass.getName().equals(ClassNameConstants.Object) && (mcd == null || mcd.mappingClass == null)) {
Class[] interfaces = superClass.getInterfaces();
for (int i = 0; i < interfaces.length && (mcd == null || mcd.mappingClass == null); i++) {
mcd = getDefaultJavaTypeMapping(interfaces[i], colmds);
}
superClass = superClass.getSuperclass();
}
if (mcd == null) {
if (!serialised) {
TypeConverter converter = storeMgr.getNucleusContext().getTypeManager().getAutoApplyTypeConverterForType(javaType);
if (converter != null) {
// Fall back to the auto-apply converter for this member type
return new MappingConverterDetails(TypeConverterMapping.class, converter);
}
}
// Treat as serialised TODO If !serialised should we throw an exception?
NucleusLogger.PERSISTENCE.warn("Trying to find mapping for type " + javaType.getName() + " but none found, and only serialised available! Falling back to SerialisedMapping");
return new MappingConverterDetails(SerialisedMapping.class);
}
}
return mcd;
}
use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getDefaultJavaTypeMapping.
/**
* Method to return the default java type mapping class for a specified java type.
* @param javaType java type
* @param colmds Metadata for the column(s) (optional)
* @return The mapping class to use (by default)
*/
protected MappingConverterDetails getDefaultJavaTypeMapping(Class javaType, ColumnMetaData[] colmds) {
// Check for an explicit mapping
Class<? extends JavaTypeMapping> cls = storeMgr.getMappingManager().getMappingType(javaType.getName());
if (cls == null) {
// No explicit mapping for this java type, so fall back to TypeConverter if available
TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
if (colmds != null && colmds.length > 0) {
if (colmds.length > 1) {
// Find TypeConverter with right number of columns
Collection<TypeConverter> converters = typeMgr.getTypeConvertersForType(javaType);
if (converters != null && !converters.isEmpty()) {
for (TypeConverter conv : converters) {
if (conv instanceof MultiColumnConverter) {
if (((MultiColumnConverter) conv).getDatastoreColumnTypes().length == colmds.length) {
return new MappingConverterDetails(TypeConverterMultiMapping.class, conv);
}
}
}
}
} else {
JdbcType jdbcType = colmds[0].getJdbcType();
if (jdbcType != null) {
// JDBC type specified so don't just take the default
TypeConverter conv = null;
if (MetaDataUtils.isJdbcTypeString(jdbcType)) {
conv = typeMgr.getTypeConverterForType(javaType, String.class);
} else if (MetaDataUtils.isJdbcTypeNumeric(jdbcType)) {
conv = typeMgr.getTypeConverterForType(javaType, Long.class);
} else if (jdbcType == JdbcType.TIMESTAMP) {
conv = typeMgr.getTypeConverterForType(javaType, Timestamp.class);
} else if (jdbcType == JdbcType.TIME) {
conv = typeMgr.getTypeConverterForType(javaType, Time.class);
} else if (jdbcType == JdbcType.DATE) {
conv = typeMgr.getTypeConverterForType(javaType, Date.class);
}
if (conv != null) {
return new MappingConverterDetails(TypeConverterMapping.class, conv);
}
}
}
}
TypeConverter conv = typeMgr.getDefaultTypeConverterForType(javaType);
if (conv != null) {
if (conv instanceof MultiColumnConverter) {
return new MappingConverterDetails(TypeConverterMultiMapping.class, conv);
}
return new MappingConverterDetails(TypeConverterMapping.class, conv);
}
NucleusLogger.PERSISTENCE.debug(Localiser.msg("041000", javaType.getName()));
return null;
}
return new MappingConverterDetails(cls);
}
use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getKeyMapping.
/**
* Convenience accessor for the mapping of the key of a map.
* @param table The container
* @param mmd MetaData for the field containing the map that this key is for
* @param clr ClassLoader resolver
* @return The mapping
*/
protected JavaTypeMapping getKeyMapping(Table table, AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
if (mmd.getMap() == null) {
// TODO Localise this
throw new NucleusException("Attempt to get key mapping for field " + mmd.getFullFieldName() + " that has no map!").setFatal();
}
MappingConverterDetails mcd = null;
Class<? extends JavaTypeMapping> mc = null;
String userTypeConverterName = null;
String userMappingClassName = null;
if (mmd.getKeyMetaData() != null) {
userTypeConverterName = mmd.getKeyMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME);
userMappingClassName = mmd.getKeyMetaData().getValueForExtension("mapping-class");
}
if (userTypeConverterName != null) {
TypeConverter conv = storeMgr.getNucleusContext().getTypeManager().getTypeConverterForName(userTypeConverterName);
if (conv == null) {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " KEY has been specified to use type converter " + userTypeConverterName + " but not found!");
}
// TODO Could be TypeConverterMultiMapping?
mcd = new MappingConverterDetails(TypeConverterMapping.class, conv);
} else if (userMappingClassName != null) {
// User has defined their own mapping class for this key so use that
try {
mc = clr.classForName(userMappingClassName);
} catch (NucleusException jpe) {
throw new NucleusUserException(Localiser.msg("041014", userMappingClassName)).setFatal();
}
} else {
boolean serialised = (mmd.hasMap() && mmd.getMap().isSerializedKey());
boolean embedded = (mmd.hasMap() && mmd.getMap().isEmbeddedKey());
boolean embeddedPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent() && mmd.getMap().isEmbeddedKey()) || (mmd.getKeyMetaData() != null && mmd.getKeyMetaData().getEmbeddedMetaData() != null);
boolean keyPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent());
Class keyCls = clr.classForName(mmd.getMap().getKeyType());
boolean keyReference = ClassUtils.isReferenceType(keyCls);
if (serialised) {
if (keyPC) {
// Serialised PC key
mc = SerialisedKeyPCMapping.class;
} else if (keyReference) {
// Serialised Reference key
mc = SerialisedReferenceMapping.class;
} else {
// Serialised Non-PC element
mc = SerialisedMapping.class;
}
} else if (embedded) {
if (embeddedPC) {
// Embedded PC key
mc = EmbeddedKeyPCMapping.class;
} else if (keyPC) {
// "Embedded" PC type but no <embedded> so dont embed for now. Is this correct?
mc = PersistableMapping.class;
} else {
// Embedded Non-PC type
mcd = getMappingClass(keyCls, serialised, embedded, mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
} else {
// Normal key mapping
mcd = getMappingClass(keyCls, serialised, embedded, mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
}
if (mcd != null && mcd.typeConverter == null) {
mc = mcd.mappingClass;
}
if (mc != null && (mcd == null || mcd.typeConverter == null)) {
// Create the mapping of the selected type
JavaTypeMapping m = null;
try {
m = mc.getDeclaredConstructor().newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_KEY);
m.initialize(mmd, table, clr);
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
} else if (mcd != null && mcd.typeConverter != null) {
try {
JavaTypeMapping m = mcd.mappingClass.getDeclaredConstructor().newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_KEY);
if (m instanceof TypeConverterMapping) {
((TypeConverterMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
} else if (m instanceof TypeConverterMultiMapping) {
((TypeConverterMultiMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
}
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc != null ? mc.getName() : null, e), e).setFatal();
}
}
throw new NucleusException("Unable to create mapping for key of map at " + mmd.getFullFieldName() + " - no available mapping");
}
Aggregations