[NIFI-3688] refactor CTL & SQL properties

This commit is contained in:
dlukyanov 2017-11-12 14:34:32 +02:00
parent 020a1f2a44
commit b3eecec901
1 changed files with 29 additions and 12 deletions

View File

@ -326,10 +326,12 @@ public class ExecuteGroovyScript extends AbstractProcessor {
for (Map.Entry e : (Set<Map.Entry>) CTL.entrySet()) { for (Map.Entry e : (Set<Map.Entry>) CTL.entrySet()) {
if (e.getValue() instanceof OSql) { if (e.getValue() instanceof OSql) {
OSql sql = (OSql) e.getValue(); OSql sql = (OSql) e.getValue();
if (!sql.getConnection().getAutoCommit()) {
sql.commit(); sql.commit();
} }
} }
} }
}
/** /**
* finalize controller services * finalize controller services
@ -339,8 +341,10 @@ public class ExecuteGroovyScript extends AbstractProcessor {
if (e.getValue() instanceof OSql) { if (e.getValue() instanceof OSql) {
OSql sql = (OSql) e.getValue(); OSql sql = (OSql) e.getValue();
try { try {
//sql.commit();
sql.getConnection().setAutoCommit(true); //default autocommit value in nifi sql.getConnection().setAutoCommit(true); //default autocommit value in nifi
} catch (Throwable ei) {
}
try {
sql.close(); sql.close();
sql = null; sql = null;
} catch (Throwable ei) { } catch (Throwable ei) {
@ -357,7 +361,9 @@ public class ExecuteGroovyScript extends AbstractProcessor {
if (e.getValue() instanceof OSql) { if (e.getValue() instanceof OSql) {
OSql sql = (OSql) e.getValue(); OSql sql = (OSql) e.getValue();
try { try {
if (!sql.getConnection().getAutoCommit()) {
sql.rollback(); sql.rollback();
}
} catch (Throwable ei) { } catch (Throwable ei) {
} }
} }
@ -372,15 +378,7 @@ public class ExecuteGroovyScript extends AbstractProcessor {
GroovyProcessSessionWrap session = new GroovyProcessSessionWrap(_session, toFailureOnError); GroovyProcessSessionWrap session = new GroovyProcessSessionWrap(_session, toFailureOnError);
HashMap CTL = new HashMap() { HashMap CTL = new AccessMap("CTL");
@Override
public Object get(Object key) {
if (!containsKey(key)) {
throw new RuntimeException("The `CTL." + key + "` not defined in processor properties");
}
return super.get(key);
}
};
try { try {
Script script = getGroovyScript(); //compilation must be moved to validation Script script = getGroovyScript(); //compilation must be moved to validation
@ -445,8 +443,27 @@ public class ExecuteGroovyScript extends AbstractProcessor {
return new PropertyDescriptor.Builder().name(propertyDescriptorName).required(false).description("Controller service accessible from code as `" + propertyDescriptorName + "`") return new PropertyDescriptor.Builder().name(propertyDescriptorName).required(false).description("Controller service accessible from code as `" + propertyDescriptorName + "`")
.dynamic(true).identifiesControllerService(ControllerService.class).build(); .dynamic(true).identifiesControllerService(ControllerService.class).build();
} }
if (propertyDescriptorName.startsWith("SQL.")) {
return new PropertyDescriptor.Builder().name(propertyDescriptorName).required(false).description("The `groovy.sql.Sql` object created from DBCP Controller service and accessible from code as `" + propertyDescriptorName + "`")
.dynamic(true).identifiesControllerService(DBCPService.class).build();
}
return new PropertyDescriptor.Builder().name(propertyDescriptorName).required(false).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).expressionLanguageSupported(true).dynamic(true) return new PropertyDescriptor.Builder().name(propertyDescriptorName).required(false).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).expressionLanguageSupported(true).dynamic(true)
.build(); .build();
} }
/** simple HashMap with exception on access of non-existent key */
private class AccessMap extends HashMap {
private String parentKey;
AccessMap(String parentKey){
this.parentKey=parentKey;
}
@Override
public Object get(Object key) {
if (!containsKey(key)) {
throw new RuntimeException("The `" + parentKey + "." + key + "` not defined in processor properties");
}
return super.get(key);
}
}
} }