protected <T> T createInstance( EntityBuilder<?> builder ) { return (T) builder.newInstance(); }
public T newInstance( Module module ) { EntityBuilder<T> builder = module.currentUnitOfWork().newEntityBuilder( type ); build( builder.instance() ); return builder.newInstance(); } }
@Override public CRL create( X509CRL x509crl ) { EntityBuilder<CRL> crlBuilder = uowf.currentUnitOfWork().newEntityBuilder( CRL.class ); CRL crl = crlBuilder.instance(); crl.lastCRLNumber().set( BigInteger.ZERO ); crl = crlBuilder.newInstance(); FileWriter fileWriter = null; try { fileWriter = new FileWriter( crl.managedFile() ); fileWriter.write( cryptIO.asPEM( x509crl ).toString() ); fileWriter.flush(); return crl; } catch ( IOException ex ) { throw new QiPkiFailure( "Unable to revoke X509", ex ); } finally { try { if ( fileWriter != null ) { fileWriter.close(); } } catch ( IOException ex ) { throw new QiPkiFailure( "Unable to revoke X509", ex ); } } }
private BarEntity buildBarEntity( String cathedral ) { EntityBuilder<BarEntity> barBuilder = module.currentUnitOfWork().newEntityBuilder( BarEntity.class ); barBuilder.instance().cathedral().set( cathedral ); return barBuilder.newInstance(); }
private <C, V> V tryLoadPropertiesFile( UnitOfWork buildUow, Class<C> compositeType, Class<V> configType, String identity ) throws InstantiationException { EntityBuilder<V> configBuilder = buildUow.newEntityBuilder( configType, identity ); String resourceName = identity + ".properties"; InputStream asStream = getResource( compositeType, 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; }
@Override public X509ProfileAssignment create( KeyEscrowPolicy keyEscrowPolicy, X509Profile profile ) { EntityBuilder<X509ProfileAssignment> profileAssignmentBuilder = uowf.currentUnitOfWork().newEntityBuilder( X509ProfileAssignment.class ); X509ProfileAssignment profileAssignment = profileAssignmentBuilder.instance(); profileAssignment.keyEscrowPolicy().set( keyEscrowPolicy ); profileAssignment.x509Profile().set( profile ); return profileAssignmentBuilder.newInstance(); }
@Override public Revocation create( X509 x509, RevocationReason reason ) { EntityBuilder<Revocation> builder = uowf.currentUnitOfWork().newEntityBuilder( Revocation.class ); Revocation revocation = builder.instance(); revocation.x509().set( x509 ); revocation.reason().set( reason ); return builder.newInstance(); }
@Override public X509 create( X509Certificate cert, CA issuer, X509Profile profile ) { try { EntityBuilder<X509> x509Builder = uowf.currentUnitOfWork().newEntityBuilder( X509.class ); X509 x509 = x509Builder.instance(); x509.pem().set( cryptIO.asPEM( cert ).toString() ); x509.canonicalSubjectDN().set( cert.getSubjectX500Principal().getName( X500Principal.CANONICAL ) ); x509.canonicalIssuerDN().set( cert.getIssuerX500Principal().getName( X500Principal.CANONICAL ) ); x509.hexSerialNumber().set( cert.getSerialNumber().toString( 16 ) ); x509.validityInterval().set( commonValuesFactory.buildValidityInterval( cert.getNotBefore(), cert.getNotAfter() ) ); x509.md5Fingerprint().set( digester.hexDigest( cert.getEncoded(), new DigestParameters( DigestAlgorithm.MD5 ) ) ); x509.sha1Fingerprint().set( digester.hexDigest( cert.getEncoded(), new DigestParameters( DigestAlgorithm.SHA_1 ) ) ); x509.sha256Fingerprint().set( digester.hexDigest( cert.getEncoded(), new DigestParameters( DigestAlgorithm.SHA_256 ) ) ); x509.issuer().set( issuer ); x509.profile().set( profile ); return x509Builder.newInstance(); } catch ( CertificateEncodingException ex ) { throw new QiPkiFailure( "Unable to calculate X509Certificate fingerprints", ex ); } }
private void createLogRecord( UnitOfWork uow, LogType type, Composite composite, String category, String message, List<Serializable> params ) { if( composite instanceof ServiceComposite ) { EntityBuilder<ServiceLogRecord> builder = uow.newEntityBuilder( ServiceLogRecord.class ); ServiceLogRecord state = builder.instance(); setStandardStuff( type, composite, category, message, state, params ); state.source().set( ( (ServiceComposite) composite ).identity().get() ); ServiceLogRecord slr = builder.newInstance(); } else if( composite instanceof EntityComposite ) { EntityBuilder<EntityLogRecord> builder = uow.newEntityBuilder( EntityLogRecord.class ); EntityLogRecord state = builder.instance(); setStandardStuff( type, composite, category, message, state, params ); state.source().set( (EntityComposite) composite ); EntityLogRecord elr = builder.newInstance(); } else { EntityBuilder<CompositeLogRecord> builder = uow.newEntityBuilder( CompositeLogRecord.class ); CompositeLogRecord state = builder.instance(); setStandardStuff( type, composite, category, message, state, params ); state.source().set( composite ); CompositeLogRecord clr = builder.newInstance(); } }
private void createDebugRecord( UnitOfWork uow, Composite composite, String message, List<Serializable> params ) { if( composite instanceof ServiceComposite ) { EntityBuilder<ServiceDebugRecordEntity> builder = uow.newEntityBuilder( ServiceDebugRecordEntity.class ); ServiceDebugRecordEntity state = builder.instance(); setStandardStuff( composite, message, state, params ); state.source().set( ( (ServiceComposite) composite ).identity().get() ); ServiceDebugRecordEntity slr = builder.newInstance(); } else if( composite instanceof EntityComposite ) { EntityBuilder<EntityDebugRecordEntity> builder = uow.newEntityBuilder( EntityDebugRecordEntity.class ); EntityDebugRecordEntity state = builder.instance(); setStandardStuff( composite, message, state, params ); state.source().set( (EntityComposite) composite ); EntityDebugRecordEntity elr = builder.newInstance(); } else { EntityBuilder<CompositeDebugRecordEntity> builder = uow.newEntityBuilder( CompositeDebugRecordEntity.class ); CompositeDebugRecordEntity state = builder.instance(); setStandardStuff( composite, message, state, params ); state.source().set( composite ); CompositeDebugRecordEntity clr = builder.newInstance(); } }
private Schedule newPersistentOnceSchedule( Task task, DateTime runAt ) { UnitOfWork uow = module.currentUnitOfWork(); EntityBuilder<OnceSchedule> builder = uow.newEntityBuilder( OnceSchedule.class ); OnceSchedule builderInstance = builder.instance(); builderInstance.task().set( task ); builderInstance.start().set( runAt ); builderInstance.identity().set( uuid.generate( CronSchedule.class ) ); OnceSchedule schedule = builder.newInstance(); logger.info( "Schedule {} created: {}", schedule.presentationString(), schedule.identity().get() ); return schedule; } }
private CronSchedule newPersistentCronSchedule( Task task, String cronExpression, DateTime start ) { UnitOfWork uow = module.currentUnitOfWork(); EntityBuilder<CronSchedule> builder = uow.newEntityBuilder( CronSchedule.class ); CronSchedule builderInstance = builder.instance(); builderInstance.task().set( task ); builderInstance.start().set( start ); builderInstance.identity().set( uuid.generate( CronSchedule.class ) ); builderInstance.cronExpression().set( cronExpression ); CronSchedule schedule = builder.newInstance(); logger.info( "Schedule {} created: {}", schedule.presentationString(), schedule.identity().get() ); return schedule; }
@Override public EscrowedKeyPair create( AsymetricAlgorithm algorithm, Integer length ) { EntityBuilder<EscrowedKeyPair> builder = uowf.currentUnitOfWork().newEntityBuilder( EscrowedKeyPair.class ); EscrowedKeyPair ekp = builder.instance(); ekp.algorithm().set( algorithm ); ekp.length().set( length ); ekp = builder.newInstance(); KeyPair keyPair = asymGenerator.generateKeyPair( new AsymetricGeneratorParameters( algorithm, length ) ); writeToFile( cryptIO.asPEM( keyPair ), ekp.managedFile() ); return ekp; }
@Test public void givenPropertyIsModifiedWhenUnitOfWorkCompletesThenStoreState() throws UnitOfWorkCompletionException { TestEntity testEntity; String version; { UnitOfWork unitOfWork = module.newUnitOfWork(); EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder( TestEntity.class ); testEntity = builder.newInstance(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); testEntity.name().set( "Rickard" ); version = spi.entityStateOf( testEntity ).version(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); String newVersion = spi.entityStateOf( testEntity ).version(); assertThat( "version has changed", newVersion, not( equalTo( version ) ) ); unitOfWork.complete(); } }
@Test public void givenManyAssociationIsModifiedWhenUnitOfWorkCompletesThenStoreState() throws UnitOfWorkCompletionException { TestEntity testEntity; String version; { UnitOfWork unitOfWork = module.newUnitOfWork(); EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder( TestEntity.class ); testEntity = builder.newInstance(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); testEntity.manyAssociation().add( 0, testEntity ); version = spi.entityStateOf( testEntity ).version(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); String newVersion = spi.entityStateOf( testEntity ).version(); assertThat( "version has changed", newVersion, not( equalTo( version ) ) ); unitOfWork.complete(); } }
@Override public RootCA createRootCA( String name, Integer validityDays, DistinguishedName distinguishedName, KeyPairSpecValue keySpec, CryptoStore cryptoStore, String... crlDistPoints ) { try { // Self signed CA KeyPair keyPair = asymGenerator.generateKeyPair( new AsymetricGeneratorParameters( keySpec.algorithm().get(), keySpec.length().get() ) ); PKCS10CertificationRequest pkcs10 = x509Generator.generatePKCS10( distinguishedName, keyPair ); List<X509ExtensionHolder> extensions = generateCAExtensions( pkcs10.getPublicKey(), pkcs10.getPublicKey() ); X509Certificate cert = x509Generator.generateX509Certificate( keyPair.getPrivate(), distinguishedName, BigInteger.probablePrime( 120, new SecureRandom() ), distinguishedName, pkcs10.getPublicKey(), Duration.standardDays( validityDays ), extensions ); EntityBuilder<RootCA> caBuilder = uowf.currentUnitOfWork().newEntityBuilder( RootCA.class ); RootCA ca = caBuilder.instance(); createCa( ca, name, cryptoStore, keyPair, cert ); return caBuilder.newInstance(); } catch ( GeneralSecurityException ex ) { throw new QiPkiFailure( "Unable to create self signed keypair plus certificate", ex ); } }
@Test public void givenEntityIsNotModifiedWhenUnitOfWorkCompletesThenDontStoreState() throws UnitOfWorkCompletionException { TestEntity testEntity; String version; { UnitOfWork unitOfWork = module.newUnitOfWork(); EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder( TestEntity.class ); testEntity = builder.newInstance(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); version = spi.entityStateOf( testEntity ).version(); unitOfWork.complete(); } { UnitOfWork unitOfWork = module.newUnitOfWork(); testEntity = unitOfWork.get( testEntity ); String newVersion = spi.entityStateOf( testEntity ).version(); assertThat( "version has not changed", newVersion, equalTo( version ) ); unitOfWork.complete(); } }
@Override public CryptoStore create( String name, KeyStoreType storeType, char[] password ) { EntityBuilder<CryptoStoreEntity> csBuilder = uowf.currentUnitOfWork().newEntityBuilder( CryptoStoreEntity.class ); CryptoStoreEntity cryptoStore = csBuilder.instance(); cryptoStore.name().set( name ); cryptoStore.storeType().set( storeType ); cryptoStore.password().set( password ); cryptoStore = csBuilder.newInstance(); // Create and save a new empty crypto store KeyStore keystore = cryptIO.createEmptyKeyStore( cryptoStore.storeType().get() ); cryptIO.writeKeyStore( keystore, password, cryptoStore.managedFile() ); return cryptoStore; }
@Override public SubCA createSubCA( CA parentCA, String name, Integer validityDays, DistinguishedName distinguishedName, KeyPairSpecValue keySpec, CryptoStore cryptoStore, String... crlDistPoints ) { try { // Sub CA KeyPair keyPair = asymGenerator.generateKeyPair( new AsymetricGeneratorParameters( keySpec.algorithm().get(), keySpec.length().get() ) ); PKCS10CertificationRequest pkcs10 = x509Generator.generatePKCS10( distinguishedName, keyPair ); List<X509ExtensionHolder> extensions = generateCAExtensions( pkcs10.getPublicKey(), parentCA.certificate().getPublicKey() ); X509Certificate cert = x509Generator.generateX509Certificate( parentCA.privateKey(), parentCA.distinguishedName(), BigInteger.probablePrime( 120, new SecureRandom() ), distinguishedName, pkcs10.getPublicKey(), Duration.standardDays( validityDays ), extensions ); EntityBuilder<SubCA> caBuilder = uowf.currentUnitOfWork().newEntityBuilder( SubCA.class ); SubCA ca = caBuilder.instance(); createCa( ca, name, cryptoStore, keyPair, cert ); ca.issuer().set( parentCA ); return caBuilder.newInstance(); } catch ( GeneralSecurityException ex ) { throw new QiPkiFailure( "Unable to create self signed keypair plus certificate", ex ); } }
withPrototype( nameConstraints ).newInstance() ); return builder.newInstance();