private void withTypeInfo( JsonObjectBuilder builder, ValueType valueType ) { builder.add( settings.getTypeInfoPropertyName(), valueType.primaryType().getName() ); }
private void withTypeInfo( JsonObjectBuilder builder, ValueType valueType ) { builder.add( settings.getTypeInfoPropertyName(), valueType.primaryType().getName() ); }
@SuppressWarnings( "unchecked" ) private <T> Converter<T> lookupConverter( ValueType valueType ) { Converter<T> converter = lookupConvertedByConverter( valueType ); if( converter == null ) { converter = castConverter( converters.keySet().stream() .collect( closestType( valueType ) ) .map( converters::get ) .orElse( null ) ); } if( converter == null && valueType.primaryType().isEnum() ) { converter = new EnumConverter( valueType.primaryType() ); } return converter; }
@SuppressWarnings( "unchecked" ) private <T> Converter<T> lookupConverter( ValueType valueType ) { Converter<T> converter = lookupConvertedByConverter( valueType ); if( converter == null ) { converter = castConverter( converters.keySet().stream() .collect( closestType( valueType ) ) .map( converters::get ) .orElse( null ) ); } if( converter == null && valueType.primaryType().isEnum() ) { converter = new EnumConverter( valueType.primaryType() ); } return converter; }
private Object deserializeArray( ModuleDescriptor module, ArrayType arrayType, Node xml ) { if( arrayType.isArrayOfPrimitiveBytes() ) { return Base64.getDecoder().decode( xml.getNodeValue().getBytes( UTF_8 ) ); } CollectionType collectionType = CollectionType.listOf( arrayType.collectedType() ); List collection = (List) deserializeCollection( module, collectionType, xml ); Object array = Array.newInstance( arrayType.collectedType().primaryType(), collection.size() ); for( int idx = 0; idx < collection.size(); idx++ ) { Array.set( array, idx, collection.get( idx ) ); } return array; }
private boolean isSupported( ValueType valueType ) { if( valueType instanceof CollectionType ) { CollectionType collectionType = (CollectionType) valueType; return isSupported( collectionType.collectedType() ); } Class<?> primaryType = valueType.primaryType(); return Number.class.isAssignableFrom( primaryType ) || Boolean.class.isAssignableFrom( primaryType ) || Character.class.isAssignableFrom( primaryType ) || Enum.class.isAssignableFrom( primaryType ) || String.class.isAssignableFrom( primaryType ) || Identity.class.isAssignableFrom( primaryType ) // TODO javax.time support in indexing-sql // || Date.class.isAssignableFrom( primaryType ) // || DateTime.class.isAssignableFrom( primaryType ) // || LocalDateTime.class.isAssignableFrom( primaryType ) // || LocalDate.class.isAssignableFrom( primaryType ) || valueType.hasType( ValueComposite.class ); } };
private <T> Converter<T> lookupConvertedByConverter( ValueType valueType ) { ConvertedBy convertedBy = Annotations.annotationOn( valueType.primaryType(), ConvertedBy.class ); if( convertedBy != null ) { return converterInstanceOf( convertedBy.value() ); } return null; }
private <T> Converter<T> lookupConvertedByConverter( ValueType valueType ) { ConvertedBy convertedBy = Annotations.annotationOn( valueType.primaryType(), ConvertedBy.class ); if( convertedBy != null ) { return converterInstanceOf( convertedBy.value() ); } return null; }
@Override public void exportDataSources() throws MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException { for ( ServiceReference<DataSource> dataSource : dataSources ) { ModuleDescriptor module = spi.moduleOf( dataSource ); EntityDescriptor descriptor = module.entityDescriptor( DataSourceConfiguration.class.getName() ); List<MBeanAttributeInfo> attributes = new ArrayList<>(); Map<String, AccessibleObject> properties = new LinkedHashMap<>(); descriptor.state().properties().forEach(persistentProperty -> { if ( !persistentProperty.isImmutable() ) { String propertyName = persistentProperty.qualifiedName().name(); String type = persistentProperty.valueType().primaryType().getName(); attributes.add( new MBeanAttributeInfo( propertyName, type, propertyName, true, true, type.equals( "java.lang.Boolean" ) ) ); properties.put( propertyName, persistentProperty.accessor() ); } } ); List<MBeanOperationInfo> operations = new ArrayList<>(); operations.add( new MBeanOperationInfo( "restart", "Restart DataSource", new MBeanParameterInfo[ 0 ], "void", MBeanOperationInfo.ACTION_INFO ) ); String mbeanName = dataSource.identity().toString(); MBeanInfo mbeanInfo = new MBeanInfo( DataSourceConfiguration.class.getName(), mbeanName, attributes.toArray( new MBeanAttributeInfo[ attributes.size() ] ), null, operations.toArray( new MBeanOperationInfo[ operations.size() ] ), null ); Object mbean = new ConfigurableDataSource( dataSourceService, mbeanInfo, mbeanName, properties ); ObjectName configurableDataSourceName = new ObjectName( "Polygene:application=" + application.name() + ",class=Datasource,name=" + mbeanName ); server.registerMBean( mbean, configurableDataSourceName ); configurationNames.add( configurableDataSourceName ); } }
@Override public void exportDataSources() throws MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException { for ( ServiceReference<DataSource> dataSource : dataSources ) { ModuleDescriptor module = spi.moduleOf( dataSource ); EntityDescriptor descriptor = module.entityDescriptor( DataSourceConfiguration.class.getName() ); List<MBeanAttributeInfo> attributes = new ArrayList<>(); Map<String, AccessibleObject> properties = new LinkedHashMap<>(); descriptor.state().properties().forEach(persistentProperty -> { if ( !persistentProperty.isImmutable() ) { String propertyName = persistentProperty.qualifiedName().name(); String type = persistentProperty.valueType().primaryType().getName(); attributes.add( new MBeanAttributeInfo( propertyName, type, propertyName, true, true, type.equals( "java.lang.Boolean" ) ) ); properties.put( propertyName, persistentProperty.accessor() ); } } ); List<MBeanOperationInfo> operations = new ArrayList<>(); operations.add( new MBeanOperationInfo( "restart", "Restart DataSource", new MBeanParameterInfo[ 0 ], "void", MBeanOperationInfo.ACTION_INFO ) ); String mbeanName = dataSource.identity().toString(); MBeanInfo mbeanInfo = new MBeanInfo( DataSourceConfiguration.class.getName(), mbeanName, attributes.toArray( new MBeanAttributeInfo[ attributes.size() ] ), null, operations.toArray( new MBeanOperationInfo[ operations.size() ] ), null ); Object mbean = new ConfigurableDataSource( dataSourceService, mbeanInfo, mbeanName, properties ); ObjectName configurableDataSourceName = new ObjectName( "Polygene:application=" + application.name() + ",class=Datasource,name=" + mbeanName ); server.registerMBean( mbean, configurableDataSourceName ); configurationNames.add( configurableDataSourceName ); } }
private Object deserializeArray( ModuleDescriptor module, ArrayType arrayType, Value value ) throws IOException { if( arrayType.isArrayOfPrimitiveBytes() ) { return value.asBinaryValue().asByteArray(); } CollectionType collectionType = CollectionType.listOf( arrayType.collectedType() ); List collection = (List) deserializeCollection( module, collectionType, value.asArrayValue() ); Object array = Array.newInstance( arrayType.collectedType().primaryType(), collection.size() ); for( int idx = 0; idx < collection.size(); idx++ ) { Array.set( array, idx, collection.get( idx ) ); } return array; }
private Object deserializeArray( ModuleDescriptor module, ArrayType arrayType, JsonValue json ) { if( arrayType.isArrayOfPrimitiveBytes() && json.getValueType() == JsonValue.ValueType.STRING ) { byte[] bytes = asString( json ).getBytes( UTF_8 ); return Base64.getDecoder().decode( bytes ); } if( json.getValueType() == JsonValue.ValueType.ARRAY ) { CollectionType collectionType = CollectionType.listOf( arrayType.collectedType() ); List<Object> collection = (List<Object>) deserializeCollection( module, collectionType, requireJsonArray( json ) ); Object array = Array.newInstance( arrayType.collectedType().primaryType(), collection.size() ); for( int idx = 0; idx < collection.size(); idx++ ) { Array.set( array, idx, collection.get( idx ) ); } return array; } throw new SerializationException( "Don't know how to deserialize " + arrayType + " from " + json ); }
private Object deserializeArray( ModuleDescriptor module, ArrayType arrayType, JsonValue json ) { if( arrayType.isArrayOfPrimitiveBytes() && json.getValueType() == JsonValue.ValueType.STRING ) { byte[] bytes = asString( json ).getBytes( UTF_8 ); return Base64.getDecoder().decode( bytes ); } if( json.getValueType() == JsonValue.ValueType.ARRAY ) { CollectionType collectionType = CollectionType.listOf( arrayType.collectedType() ); List<Object> collection = (List<Object>) deserializeCollection( module, collectionType, requireJsonArray( json ) ); Object array = Array.newInstance( arrayType.collectedType().primaryType(), collection.size() ); for( int idx = 0; idx < collection.size(); idx++ ) { Array.set( array, idx, collection.get( idx ) ); } return array; } throw new SerializationException( "Don't know how to deserialize " + arrayType + " from " + json ); }
if( EnumType.class.isAssignableFrom( valueTypeClass ) ) return (T) Enum.valueOf( (Class) valueType.primaryType(), xml.getNodeValue() );
private void serializePropertyTypes( final EntityDescriptor entityDescriptor, final Graph graph, final URI entityTypeUri ) { ValueFactory values = graph.getValueFactory(); // Properties entityDescriptor.state().properties().forEach( persistentProperty -> { URI propertyURI = values.createURI( persistentProperty.qualifiedName().toURI() ); graph.add( propertyURI, Rdfs.DOMAIN, entityTypeUri ); graph.add( propertyURI, Rdfs.TYPE, Rdfs.PROPERTY ); // TODO Support more types URI type = dataTypes.get( persistentProperty.valueType().primaryType().getName() ); if( type != null ) { graph.add( propertyURI, Rdfs.RANGE, type ); } } ); } }
value = module.instance().newValue( valueType.primaryType() );
@SuppressWarnings( "unchecked" ) private <T> T doGuessDeserialize( ModuleDescriptor module, ValueType valueType, JsonValue json ) { switch( json.getValueType() ) { case OBJECT: JsonObject object = (JsonObject) json; String typeInfo = object.getString( settings.getTypeInfoPropertyName(), valueType.primaryType().getName() ); StatefulAssociationCompositeDescriptor descriptor = statefulCompositeDescriptorFor( module, typeInfo ); if( descriptor != null ) { return (T) deserializeStatefulAssociationValue( ( (CompositeDescriptor) descriptor ).module(), descriptor.valueType(), object ); } default: throw new SerializationException( "Don't know how to deserialize " + valueType + " from " + json ); } }
@SuppressWarnings( "unchecked" ) private <T> T doGuessDeserialize( ModuleDescriptor module, ValueType valueType, JsonValue json ) { switch( json.getValueType() ) { case OBJECT: JsonObject object = (JsonObject) json; String typeInfo = object.getString( settings.getTypeInfoPropertyName(), valueType.primaryType().getName() ); StatefulAssociationCompositeDescriptor descriptor = statefulCompositeDescriptorFor( module, typeInfo ); if( descriptor != null ) { return (T) deserializeStatefulAssociationValue( ( (CompositeDescriptor) descriptor ).module(), descriptor.valueType(), object ); } default: throw new SerializationException( "Don't know how to deserialize " + valueType + " from " + json ); } }
if( EnumType.class.isAssignableFrom( valueTypeClass ) ) return (T) Enum.valueOf( (Class) valueType.primaryType(), value.asStringValue().asString() );
private Object createParametersComposite( RestForm form, Class<?> argType ) { ValueBuilder<?> vb = vbf.newValueBuilderWithState( argType, descriptor -> { FormField field = form.field( descriptor.qualifiedName().name() ); if( field == null ) { return null; } Class<?> propertyType = descriptor.valueType().primaryType(); Property<String> value = field.value(); if( value == null ) { return null; } return convertPropertyValue( value.get(), propertyType ); }, descriptor -> null, descriptor -> null, descriptor -> null ); return vb.newInstance(); }