try( InputStream asStream = getResource( configType.primaryType(), resourceName ) )
private Properties systemProperties() { Stream<Map.Entry<?, ?>> allProps = Stream.concat( System.getenv().entrySet().stream(), System.getProperties().entrySet().stream() ); Properties props = new Properties(); allProps.forEach( entry -> props.put( transform( (String) entry.getKey() ), entry.getValue() ) ); return props; }
Class<V> configType = serviceModel.configurationType(); V config = tryLoadPropertiesFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadJsonFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadYamlFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadXmlFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadSystemProperties( buildUow, entityDescriptor, identity ); if( config == null ) return (V) findConfigurationInstanceFor( serviceModel, identity, uow );
Class<V> configType = serviceModel.configurationType(); V config = tryLoadPropertiesFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadJsonFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadYamlFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadXmlFile( buildUow, entityDescriptor, identity ); if( config == null ) config = tryLoadSystemProperties( buildUow, entityDescriptor, identity ); if( config == null ) return (V) findConfigurationInstanceFor( serviceModel, identity, uow );
try( InputStream asStream = getResource( configType.primaryType(), resourceName ) )
private <V> V tryLoadPropertiesFile( UnitOfWork buildUow, EntityDescriptor configType, Identity identity ) throws InstantiationException { @SuppressWarnings( "unchecked" ) EntityBuilder<V> configBuilder = buildUow.newEntityBuilder( (Class<V>) configType.primaryType(), identity ); String resourceName = identity + ".properties"; try( InputStream asStream = getResource( configType.primaryType(), resourceName ) ) { if( asStream != null ) { try { PropertyMapper.map( asStream, (Composite) configBuilder.instance() ); return configBuilder.newInstance(); } catch( IOException e1 ) { InstantiationException exception = new InstantiationException( "Could not read underlying Properties file." ); exception.initCause( e1 ); throw exception; } } return null; } catch( IOException ignored ) { return null; } }
private <V> V tryLoadPropertiesFile( UnitOfWork buildUow, EntityDescriptor configType, Identity identity ) throws InstantiationException { @SuppressWarnings( "unchecked" ) EntityBuilder<V> configBuilder = buildUow.newEntityBuilder( (Class<V>) configType.primaryType(), identity ); String resourceName = identity + ".properties"; try( InputStream asStream = getResource( configType.primaryType(), resourceName ) ) { if( asStream != null ) { try { PropertyMapper.map( asStream, (Composite) configBuilder.instance() ); return configBuilder.newInstance(); } catch( IOException e1 ) { InstantiationException exception = new InstantiationException( "Could not read underlying Properties file." ); exception.initCause( e1 ); throw exception; } } return null; } catch( IOException ignored ) { return null; } }
@SuppressWarnings( "unchecked" ) public <V> V findConfigurationInstanceFor( ServiceDescriptor serviceModel, Identity serviceIdentity, UnitOfWork uow ) throws InstantiationException { Class<V> configurationType = serviceModel.configurationType(); V configuration; try { configuration = uow.get( configurationType, serviceIdentity ); uow.pause(); } catch( NoSuchEntityException | NoSuchEntityTypeException e ) { EntityDescriptor entityDescriptor = module.typeLookup().lookupEntityModel( configurationType ); if( entityDescriptor == null ) { throw new NoSuchConfigurationTypeException( configurationType, module.descriptor() ); } return (V) initializeConfigurationInstance( entityDescriptor, uow, serviceModel, serviceIdentity ); } return configuration; }
@SuppressWarnings( "unchecked" ) public <V> V findConfigurationInstanceFor( ServiceDescriptor serviceModel, Identity serviceIdentity, UnitOfWork uow ) throws InstantiationException { Class<V> configurationType = serviceModel.configurationType(); V configuration; try { configuration = uow.get( configurationType, serviceIdentity ); uow.pause(); } catch( NoSuchEntityException | NoSuchEntityTypeException e ) { EntityDescriptor entityDescriptor = module.typeLookup().lookupEntityModel( configurationType ); if( entityDescriptor == null ) { throw new NoSuchConfigurationTypeException( configurationType, module.descriptor() ); } return (V) initializeConfigurationInstance( entityDescriptor, uow, serviceModel, serviceIdentity ); } return configuration; }
@Override public synchronized T get() { if( configuration == null ) { Usecase usecase = UsecaseBuilder.newUsecase( "Configuration:" + me.identity().get() ); uow = uowf.newUnitOfWork( usecase ); try { ServiceDescriptor serviceModel = api.serviceDescriptorFor( me ); Identity identity = me.identity().get(); configuration = this.findConfigurationInstanceFor( serviceModel, identity, uow ); } catch( InstantiationException e ) { throw new IllegalStateException( e ); } } return configuration; }
@Override public synchronized T get() { if( configuration == null ) { Usecase usecase = UsecaseBuilder.newUsecase( "Configuration:" + me.identity().get() ); uow = uowf.newUnitOfWork( usecase ); try { ServiceDescriptor serviceModel = api.serviceDescriptorFor( me ); Identity identity = me.identity().get(); configuration = this.findConfigurationInstanceFor( serviceModel, identity, uow ); } catch( InstantiationException e ) { throw new IllegalStateException( e ); } } return configuration; }
private <V extends HasIdentity> V tryLoadJsonFile( UnitOfWork uow, EntityDescriptor configType, Identity identity ) { return readConfig( uow, configType, identity, Serialization.Format.JSON, ".json" ); }
private <V> V tryLoadSystemProperties( UnitOfWork buildUow, EntityDescriptor configType, Identity identity ) throws InstantiationException { @SuppressWarnings( "unchecked" ) EntityBuilder<V> configBuilder = buildUow.newEntityBuilder( (Class<V>) configType.primaryType(), identity ); PropertyMapper.map( systemProperties(), (Composite) configBuilder.instance() ); return configBuilder.newInstance(); }
private <V extends HasIdentity> V tryLoadXmlFile( UnitOfWork uow, EntityDescriptor configType, Identity identity ) { return readConfig( uow, configType, identity, Serialization.Format.XML, ".xml" ); }
private <V extends HasIdentity> V tryLoadYamlFile( UnitOfWork uow, EntityDescriptor configType, Identity identity ) { return readConfig( uow, configType, identity, Serialization.Format.YAML, ".yaml" ); }
private <V> V tryLoadSystemProperties( UnitOfWork buildUow, EntityDescriptor configType, Identity identity ) throws InstantiationException { @SuppressWarnings( "unchecked" ) EntityBuilder<V> configBuilder = buildUow.newEntityBuilder( (Class<V>) configType.primaryType(), identity ); PropertyMapper.map( systemProperties(), (Composite) configBuilder.instance() ); return configBuilder.newInstance(); }
private <V extends HasIdentity> V tryLoadYamlFile( UnitOfWork uow, EntityDescriptor configType, Identity identity ) { return readConfig( uow, configType, identity, Serialization.Format.YAML, ".yaml" ); }
private <V extends HasIdentity> V tryLoadJsonFile( UnitOfWork uow, EntityDescriptor configType, Identity identity ) { return readConfig( uow, configType, identity, Serialization.Format.JSON, ".json" ); }
private Properties systemProperties() { Stream<Map.Entry<?, ?>> allProps = Stream.concat( System.getenv().entrySet().stream(), System.getProperties().entrySet().stream() ); Properties props = new Properties(); allProps.forEach( entry -> props.put( transform( (String) entry.getKey() ), entry.getValue() ) ); return props; }
private <V extends HasIdentity> V tryLoadXmlFile( UnitOfWork uow, EntityDescriptor configType, Identity identity ) { return readConfig( uow, configType, identity, Serialization.Format.XML, ".xml" ); }