checkNotNull( "resource",
resource );
//Only files can be processed
if ( !Files.isRegularFile( resource ) ) {
return new IncrementalBuildResults( gav );
}
//Check a full build has been performed
if ( !isBuilt() ) {
throw new IllegalStateException( "A full build needs to be performed before any incremental operations." );
}
//Resource Type might require "external" validation (i.e. it's not covered by Kie)
final IncrementalBuildResults results = new IncrementalBuildResults( gav );
final BuildValidationHelper validator = getBuildValidationHelper( resource );
if ( validator != null ) {
final List<ValidationMessage> addedValidationMessages = validator.validate( Paths.convert( resource ) );
if ( !( addedValidationMessages == null || addedValidationMessages.isEmpty() ) ) {
for ( ValidationMessage validationMessage : addedValidationMessages ) {
results.addAddedMessage( convertValidationMessage( validationMessage ) );
}
}
final List<ValidationMessage> removedValidationMessages = nonKieResourceValidationHelperMessages.remove( resource );
if ( !( removedValidationMessages == null || removedValidationMessages.isEmpty() ) ) {
for ( ValidationMessage validationMessage : removedValidationMessages ) {
results.addRemovedMessage( convertValidationMessage( validationMessage ) );
}
}
nonKieResourceValidationHelpers.put( resource,
validator );
nonKieResourceValidationHelperMessages.put( resource,
addedValidationMessages );
}
//Add new resource
final String destinationPath = resource.toUri().toString().substring( projectPrefix.length() + 1 );
final InputStream is = ioService.newInputStream( resource );
final BufferedInputStream bis = new BufferedInputStream( is );
kieFileSystem.write( destinationPath,
KieServices.Factory.get().getResources().newInputStreamResource( bis ) );
addJavaClass( resource );
handles.put( destinationPath,
Paths.convert( resource ) );
//Incremental build
try {
final IncrementalResults incrementalResults = ( (InternalKieBuilder) kieBuilder ).createFileSet( destinationPath ).build();
for ( final Message message : incrementalResults.getAddedMessages() ) {
results.addAddedMessage( convertMessage( message ) );
}
for ( final Message message : incrementalResults.getRemovedMessages() ) {
results.addRemovedMessage( convertMessage( message ) );
}
//Tidy-up removed message handles
for ( Message message : incrementalResults.getRemovedMessages() ) {
handles.remove( RESOURCE_PATH + "/" + message.getPath() );
}
} catch ( LinkageError e ) {
final String msg = MessageFormat.format( ERROR_CLASS_NOT_FOUND,
e.getLocalizedMessage() );
logger.warn( msg );
results.addAddedMessage( makeWarningMessage( msg ) );
} catch ( Throwable e ) {
final String msg = e.getLocalizedMessage();
logger.error( msg );
results.addAddedMessage( makeErrorMessage( msg ) );
}
return results;
}
}