use of org.wso2.ballerinalang.compiler.util.Name in project jaggery by wso2.
the class DatabaseHostObject method jsConstructor.
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
int argsCount = args.length;
DatabaseHostObject db = new DatabaseHostObject();
//args count 1 for dataSource name
if (argsCount != 1 && argsCount != 3 && argsCount != 4) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "1", "string", args[0], true);
}
if (argsCount == 1) {
String dataSourceName = (String) args[0];
DataSourceManager dataSourceManager = new DataSourceManager();
try {
CarbonDataSource carbonDataSource = dataSourceManager.getInstance().getDataSourceRepository().getDataSource(dataSourceName);
DataSource dataSource = (DataSource) carbonDataSource.getDSObject();
db.conn = dataSource.getConnection();
db.context = cx;
return db;
} catch (DataSourceException e) {
log.error("Failed to access datasource " + dataSourceName, e);
} catch (SQLException e) {
log.error("Failed to get connection", e);
}
}
if (!(args[1] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "2", "string", args[1], true);
}
if (!(args[2] instanceof String) && !(args[2] instanceof Integer)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "3", "string", args[2], true);
}
NativeObject configs = null;
if (argsCount == 4) {
if (!(args[3] instanceof NativeObject)) {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "4", "object", args[3], true);
}
configs = (NativeObject) args[3];
}
String dbUrl = (String) args[0];
RDBMSConfiguration rdbmsConfig = new RDBMSConfiguration();
try {
if (configs != null) {
Gson gson = new Gson();
rdbmsConfig = gson.fromJson(HostObjectUtil.serializeJSON(configs), RDBMSConfiguration.class);
}
if (rdbmsConfig.getDriverClassName() == null || rdbmsConfig.getDriverClassName().equals("")) {
rdbmsConfig.setDriverClassName(getDriverClassName(dbUrl));
}
rdbmsConfig.setUsername((String) args[1]);
rdbmsConfig.setPassword((String) args[2]);
rdbmsConfig.setUrl(dbUrl);
try {
rdbmsDataSource = new RDBMSDataSource(rdbmsConfig);
} catch (DataSourceException e) {
throw new ScriptException(e);
}
db.conn = rdbmsDataSource.getDataSource().getConnection();
db.context = cx;
return db;
} catch (SQLException e) {
String msg = "Error connecting to the database : " + dbUrl;
log.warn(msg, e);
throw new ScriptException(msg, e);
}
}
use of org.wso2.ballerinalang.compiler.util.Name in project siddhi by wso2.
the class SiddhiDebuggerClient method start.
/**
* Start the {@link SiddhiDebuggerClient} and configure the breakpoints.
*
* @param siddhiApp the Siddhi query
* @param input the user input as a whole text
*/
public void start(final String siddhiApp, String input) {
SiddhiManager siddhiManager = new SiddhiManager();
info("Deploying the siddhi app");
final SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
// Add callbacks for all the streams
final Set<String> streamNames = SiddhiCompiler.parse(siddhiApp).getStreamDefinitionMap().keySet();
for (String streamName : streamNames) {
final String stream = streamName;
siddhiAppRuntime.addCallback(stream, new StreamCallback() {
@Override
public void receive(Event[] events) {
info("@Receive: Stream: " + stream + ", Event: " + Arrays.deepToString(events));
}
});
}
SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
final InputFeeder inputFeeder = new InputFeeder(siddhiAppRuntime, input);
System.out.println("Configure the breakpoints.\nYou can use the following commands:\n - " + ADD_BREAKPOINT + "<query name>:<IN/OUT>\n - " + REMOVE_BREAKPOINT + "<query name>:<IN/OUT>\n - " + START + "\n - " + STOP);
printNextLine();
final Scanner scanner = new Scanner(System.in, "UTF-8");
while (scanner.hasNextLine()) {
String userInput = scanner.nextLine().trim();
String command = userInput.toLowerCase();
if (command.startsWith(ADD_BREAKPOINT)) {
if (!command.contains(QUERY_DELIMITER)) {
error("Invalid add query. The query must be " + ADD_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
printNextLine();
continue;
}
String[] components = userInput.substring(ADD_BREAKPOINT.length(), userInput.length()).split(QUERY_DELIMITER);
String queryName = components[0];
String terminal = components[1].toLowerCase();
if (IN.equals(terminal)) {
siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
info("Added a breakpoint at the IN terminal of " + queryName);
printNextLine();
} else if (OUT.equals(terminal)) {
siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
info("Added a breakpoint at the OUT terminal of " + queryName);
printNextLine();
} else {
error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase() + ". Please try again");
printNextLine();
}
} else if (command.startsWith(REMOVE_BREAKPOINT)) {
if (!command.contains(QUERY_DELIMITER)) {
error("Invalid add query. The query must be " + REMOVE_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
printNextLine();
continue;
}
String[] components = command.substring(ADD_BREAKPOINT.length(), command.length()).split(QUERY_DELIMITER);
String queryName = components[0];
String terminal = components[1];
if (IN.equals(terminal)) {
siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
info("Removed the breakpoint at the IN terminal of " + queryName);
printNextLine();
} else if (OUT.equals(terminal)) {
siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
info("Removed the breakpoint at the OUT terminal of " + queryName);
printNextLine();
} else {
error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase());
printNextLine();
}
} else if (STOP.equals(command)) {
inputFeeder.stop();
siddhiAppRuntime.shutdown();
break;
} else if (START.equals(command)) {
inputFeeder.start();
info("Siddhi Debugger starts sending input to Siddhi");
System.out.println("You can use the following commands:\n - " + NEXT + "\n - " + PLAY + "\n - " + STATE + ":<query name>\n - " + STOP);
break;
} else {
error("Invalid command: " + command);
printNextLine();
}
}
siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {
@Override
public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
info("@Debug: Query: " + queryName + ", Terminal: " + queryTerminal + ", Event: " + event);
printNextLine();
while (scanner.hasNextLine()) {
String command = scanner.nextLine().trim().toLowerCase();
if (STOP.equals(command)) {
debugger.releaseAllBreakPoints();
debugger.play();
inputFeeder.stop();
siddhiAppRuntime.shutdown();
break;
} else if (NEXT.equals(command)) {
debugger.next();
break;
} else if (PLAY.equals(command)) {
debugger.play();
break;
} else if (command.startsWith(STATE)) {
if (!command.contains(QUERY_DELIMITER)) {
error("Invalid get state request. The query must be " + STATE + ":<query " + "name>. Please try again");
printNextLine();
continue;
}
String[] components = command.split(QUERY_DELIMITER);
String requestQueryName = components[1];
Map<String, Object> state = debugger.getQueryState(requestQueryName.trim());
System.out.println("Query '" + requestQueryName + "' state : ");
for (Map.Entry<String, Object> entry : state.entrySet()) {
System.out.println(" '" + entry.getKey() + "' : " + entry.getValue());
}
printNextLine();
continue;
} else {
error("Invalid command: " + command);
printNextLine();
}
}
}
});
inputFeeder.join();
if (inputFeeder.isRunning()) {
info("Input feeder has sopped sending all inputs. If you want to stop the execution, use " + "the STOP command");
printNextLine();
while (scanner.hasNextLine()) {
String command = scanner.nextLine().trim().toLowerCase();
if (STOP.equals(command)) {
inputFeeder.stop();
siddhiAppRuntime.shutdown();
break;
} else {
error("Invalid command: " + command);
printNextLine();
}
}
}
scanner.close();
info("Siddhi Debugger is stopped successfully");
}
use of org.wso2.ballerinalang.compiler.util.Name in project siddhi by wso2.
the class SiddhiAppRuntime method addCallback.
public void addCallback(String queryName, QueryCallback callback) {
callback.setContext(siddhiAppContext);
QueryRuntime queryRuntime = queryProcessorMap.get(queryName);
if (queryRuntime == null) {
throw new QueryNotExistException("No query found with name: " + queryName);
}
callback.setQuery(queryRuntime.getQuery());
queryRuntime.addCallback(callback);
}
use of org.wso2.ballerinalang.compiler.util.Name in project siddhi by wso2.
the class SiddhiAnnotationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// Iterate over all @Extension annotated elements.
for (Element element : roundEnv.getElementsAnnotatedWith(Extension.class)) {
// Check if a class has been annotated with @Extension.
if (element.getKind() == ElementKind.CLASS) {
String superClass = getMatchingSuperClass(element, new String[] { AnnotationConstants.SINK_MAPPER_SUPER_CLASS, AnnotationConstants.SINK_SUPER_CLASS, AnnotationConstants.FUNCTION_EXECUTOR_SUPER_CLASS, AnnotationConstants.AGGREGATION_ATTRIBUTE_SUPER_CLASS, AnnotationConstants.DISTRIBUTION_STRATEGY_SUPER_CLASS, AnnotationConstants.STREAM_PROCESSOR_SUPER_CLASS, AnnotationConstants.STREAM_FUNCTION_PROCESSOR_SUPER_CLASS, AnnotationConstants.STORE_SUPER_CLASS, AnnotationConstants.SOURCE_SUPER_CLASS, AnnotationConstants.SOURCE_MAPPER_SUPER_CLASS, AnnotationConstants.WINDOW_PROCESSOR_CLASS, AnnotationConstants.SCRIPT_SUPER_CLASS, AnnotationConstants.INCREMENTAL_ATTRIBUTE_AGGREGATOR_SUPER_CLASS });
AbstractAnnotationProcessor abstractAnnotationProcessor = null;
Extension annotation = element.getAnnotation(Extension.class);
String name = annotation.name();
String description = annotation.description();
String namespace = annotation.namespace();
Parameter[] parameters = annotation.parameters();
ReturnAttribute[] returnAttributes = annotation.returnAttributes();
SystemParameter[] systemParameters = annotation.systemParameter();
Example[] examples = annotation.examples();
String extensionClassFullName = element.asType().toString();
if (superClass != null) {
switch(superClass) {
case AnnotationConstants.DISTRIBUTION_STRATEGY_SUPER_CLASS:
abstractAnnotationProcessor = new DistributionStrategyValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.SINK_MAPPER_SUPER_CLASS:
abstractAnnotationProcessor = new SinkMapperValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.SINK_SUPER_CLASS:
abstractAnnotationProcessor = new SinkValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.FUNCTION_EXECUTOR_SUPER_CLASS:
abstractAnnotationProcessor = new FunctionExecutorValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.AGGREGATION_ATTRIBUTE_SUPER_CLASS:
abstractAnnotationProcessor = new AggregationAttributeValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.STREAM_PROCESSOR_SUPER_CLASS:
abstractAnnotationProcessor = new StreamProcessorValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.SOURCE_SUPER_CLASS:
abstractAnnotationProcessor = new SourceValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.SOURCE_MAPPER_SUPER_CLASS:
abstractAnnotationProcessor = new SourceMapperValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.STORE_SUPER_CLASS:
abstractAnnotationProcessor = new StoreValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.STREAM_FUNCTION_PROCESSOR_SUPER_CLASS:
abstractAnnotationProcessor = new StreamFunctionProcessorValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.WINDOW_PROCESSOR_CLASS:
abstractAnnotationProcessor = new WindowProcessorValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.SCRIPT_SUPER_CLASS:
abstractAnnotationProcessor = new ScriptValidationAnnotationProcessor(extensionClassFullName);
break;
case AnnotationConstants.INCREMENTAL_ATTRIBUTE_AGGREGATOR_SUPER_CLASS:
abstractAnnotationProcessor = new IncrementalAggregationAttributeValidationAnnotationProcessor(extensionClassFullName);
break;
default:
// Throw error if no matching super class.
showBuildError(MessageFormat.format("Default switch case executed as there is no " + "matching super class option for @{0}.", superClass), element);
break;
}
if (abstractAnnotationProcessor != null) {
try {
abstractAnnotationProcessor.basicParameterValidation(name, description, namespace);
abstractAnnotationProcessor.parameterValidation(parameters);
abstractAnnotationProcessor.returnAttributesValidation(returnAttributes);
abstractAnnotationProcessor.systemParametersValidation(systemParameters);
abstractAnnotationProcessor.examplesValidation(examples);
} catch (AnnotationValidationException e) {
showBuildError(e.getMessage(), element);
}
} else {
showBuildError(MessageFormat.format("Error while validation, " + "abstractAnnotationProcessor cannot be null.", superClass), element);
}
} else {
// Throw error if no matching super class.
showBuildError("Class does not have a matching Siddhi Extension super class.", element);
}
} else {
// Throw error if the element returned is method or package.
showBuildError(MessageFormat.format("Only classes can be annotated with @{0}.", Extension.class.getCanonicalName()), element);
}
}
// Returning false since this processor only validates.
return false;
}
use of org.wso2.ballerinalang.compiler.util.Name in project siddhi by wso2.
the class SingleInputStreamParser method parseInputStream.
/**
* Parse single InputStream and return SingleStreamRuntime
*
* @param inputStream single input stream to be parsed
* @param siddhiAppContext query to be parsed
* @param variableExpressionExecutors List to hold VariableExpressionExecutors to update after query parsing
* @param streamDefinitionMap Stream Definition Map
* @param tableDefinitionMap Table Definition Map
* @param windowDefinitionMap window definition map
* @param aggregationDefinitionMap aggregation definition map
* @param tableMap Table Map
* @param metaComplexEvent MetaComplexEvent
* @param processStreamReceiver ProcessStreamReceiver
* @param supportsBatchProcessing supports batch processing
* @param outputExpectsExpiredEvents is output expects ExpiredEvents
* @param queryName query name of single input stream belongs to.
*
* @return SingleStreamRuntime
*/
public static SingleStreamRuntime parseInputStream(SingleInputStream inputStream, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, MetaComplexEvent metaComplexEvent, ProcessStreamReceiver processStreamReceiver, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, String queryName) {
Processor processor = null;
EntryValveProcessor entryValveProcessor = null;
boolean first = true;
MetaStreamEvent metaStreamEvent;
if (metaComplexEvent instanceof MetaStateEvent) {
metaStreamEvent = new MetaStreamEvent();
((MetaStateEvent) metaComplexEvent).addEvent(metaStreamEvent);
initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, metaStreamEvent);
} else {
metaStreamEvent = (MetaStreamEvent) metaComplexEvent;
initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, metaStreamEvent);
}
// A window cannot be defined for a window stream
if (!inputStream.getStreamHandlers().isEmpty() && windowDefinitionMap != null && windowDefinitionMap.containsKey(inputStream.getStreamId())) {
for (StreamHandler handler : inputStream.getStreamHandlers()) {
if (handler instanceof Window) {
throw new OperationNotSupportedException("Cannot create " + ((Window) handler).getName() + " " + "window for the window stream " + inputStream.getStreamId());
}
}
}
if (!inputStream.getStreamHandlers().isEmpty()) {
for (StreamHandler handler : inputStream.getStreamHandlers()) {
Processor currentProcessor = generateProcessor(handler, metaComplexEvent, variableExpressionExecutors, siddhiAppContext, tableMap, supportsBatchProcessing, outputExpectsExpiredEvents, queryName);
if (currentProcessor instanceof SchedulingProcessor) {
if (entryValveProcessor == null) {
entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
if (first) {
processor = entryValveProcessor;
first = false;
} else {
processor.setToLast(entryValveProcessor);
}
}
Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
((SchedulingProcessor) currentProcessor).setScheduler(scheduler);
}
if (first) {
processor = currentProcessor;
first = false;
} else {
processor.setToLast(currentProcessor);
}
}
}
metaStreamEvent.initializeAfterWindowData();
return new SingleStreamRuntime(processStreamReceiver, processor, metaComplexEvent);
}
Aggregations