Can android execute string as code? - java

So basically I would like my app to read info from a database, this info would be a string that is valid java code. Once read, I would like my app to execute this string as if it were code. Is there any functionality packaged with the android sdk that supports this?
Essentially I want the phone to populate some data with information queried from a database. One of these pieces of information would be a statement like:
"return data.ZombieKillTotal >= 100000;"
Which would be used inside a statement like:
registerAchievement(new Achievement("Zombie Killer", new AchievementValidator() {
public boolean isSatisfied(Data data) { ExecStringAsCode(query result) }
});
I just don't know what to use for 'ExecStringAsCode' :(

I ended up using the JRE library and imported javax.script.*;
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

The Java programming language doesn't really provide a feature for the execution of code in text strings.
You could embed an interpreter for a more script-friendly language (perhaps Python) and execute code that way.

if you want to execute code loaded dynamicly you will have to use somthing like COdeDOM to create code and compile it then use reflection to load and excute it.

Related

How to use SchemaCrawler's Offline Snapshots in Java code

I think the header explains it all: Is there a nice way to create and load offline snapshots of database schema using SchemaCrawler without using command line? If yes, can you provide some example code / link please? If not, some example java code to use command line options would be helpful too (I don't have much experience with that)!
Thanks for any kind of help!
PS: I managed to create offline snapshot with this code:
final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
// Set what details are required in the schema - this affects the
// time taken to crawl the schema
options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
options.setRoutineInclusionRule(new ExcludeAll());
options.setSchemaInclusionRule(new RegularExpressionInclusionRule(/* some regex here*/));
options.setTableInclusionRule(new RegularExpressionExclusionRule(/*some regex here*/));
outputOptions.setCompressedOutputFile(Paths.get("./test_db_snapshot.xml"));
final String command = "serialize";
final Executable executable = new SchemaCrawlerExecutable(command);
executable.setSchemaCrawlerOptions(options);
executable.setOutputOptions(outputOptions);
executable.execute(getConnection());
Not sure how to connect to it though.
You need to use the schemacrawler.tools.offline.OfflineSnapshotExecutable along with an schemacrawler.tools.offline.jdbc.OfflineConnection to "connect" to your database snapshoot.
Please take a look at the following code:
OfflineSnapshotTest.offlineSnapshotExecutable()
And #ZidaneT, to load an offline snapshot, use code like that in LoadSnapshotTest.java
Sualeh Fatehi, SchemaCrawler

How to retrieve from Azure mobile services using android studio

I am new to Android and Windows Azure. I have successfully inserted data from Android application but how do I retrieve single data and post that data on a TextView?
The read function after the gettable class is also not working. What is the exact function use for it? I have followed these instructions but they did not work for me, also I do not understand the documentation.
Currently, I just can provide some tutorials about how to use query data from azure database. I recommend you can refer to this official document about how to use Azure Client Library using Java: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-how-to-use-client-library . You can focus on two part: “how to query data from a mobile service” and “how to bind data to the UI”.
At the same time, you can view this video from Channel 9: https://channel9.msdn.com/Series/Windows-Azure-Mobile-Services/Android-Getting-Started-With-Data-Connecting-your-app-to-Windows-Azure-Mobile-Services.
The sample code project of this tutorial, please go to the GitHub link https://github.com/Azure/mobile-services-samples/tree/master/GettingStartedWithData .
For the ‘getTable(Class )’ function is not working, please double check whether the class name is same as table name. If they are same, you can use it like below:
MobileServiceTable<ToDoItem> mToDoTable = mClient.getTable(ToDoItem.class);
If not, you can write you code like this:
MobileServiceTable<ToDoItem> mToDoTable = mClient.getTable("ToDoItemBackup", ToDoItem.class);
For further better support, please share more detail about your code snippet .

How to get the list of all projects/jobs in jenkins using java programmatically?

I am trying to access the list of all jobs/projects in jenkins and their project files in java not groovy and parsing XML files.
I suggest you to use other ways to do this rather then use Java. Consider to use Ruby or Python API wrappers, Groovy, CLI API, Script Console etc. Refer also to Remote Access API for more information.
But if you still need Java, well, there is no Java API, but there is Rest API. And you may use some Java's http client to communicate, for example. Here are required steps:
1. Get a list of jobs.
This can be done requesting http://jenkins_url:port/api/json?tree=jobs[name,url].
Response example:
{
"jobs" : [
{
"name" : "JOB_NAME1",
"url" : "http://jenkins_url:port/job/JOB_NAME1/"
},
{
"name" : "JOB_NAME2",
"url" : "http://jenkins_url:port/job/JOB_NAME2/"
},
...
}
From there you can retrieve job names and urls.
2. Get build artifacts.
Having job url, download from job_url/lastSuccessfulBuild/artifact/*zip*/archive.zip
3. Or get workspace files.
Having job url, download from job_url/JOB_NAME1/ws/*zip*/workspace.zip
Beware, some of this operations require proper Jenkins credentials, anonymous access. Otherwise, request will fail.
More detailed information about Rest API available at your Jenkins: http://jenkins_url:port/api/
Like #Vitalii said its better to do in groovy or some other scripting languages or to parse the api/xml file to get the workspace job list.
For your case you can get by making your class extending the Trigger and using the job object of the class trigger.
Note: include all the other default classes the jenkins plugin requires and make sure that the plugin runs every minute for this code to execute properly.
public class xyz extends Trigger<BuildableItem>
{
#Override
public void run()
{
LOGGER.info("Project Name"+job.getName());
}
}

How to run a .m (matlab) file through java and matlab control?

I have 2 .m files. One is the function and the other one (read.m) reads then function and exports the results into an excel file. I have a java program that makes some changes to the .m files. After the changes I want to automate the execution/running of the .m files. I have downloaded the matlabcontrol.jar and I am looking for a way to use it to invoke and run the read.m file that then reads the function.
Can anyone help me with the code? Thanks
I have tried this code but it does not work.
public static void tomatlab() throws MatlabConnectionException, MatlabInvocationException {
MatlabProxyFactoryOptions options =
new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
proxy.eval("addpath('C:\\path_to_read.m')");
proxy.feval("read");
proxy.eval("rmpath('C:\\path_to_read.m')");
// close connection
proxy.disconnect();
}
Based on the official tutorial in the Wiki of the project, it seems quite straightforward to start with this API.
The path-manipulation might be a bit tricky, but I would give a try to loading the whole script into a string and passing it to eval (please note I have no prior experience with this specific Matlab library). That could be done quite easily (with joining Files.readAllLines() for example).
Hope that helps something.

Running Talend Job from within Java application

I am developing a web app using Spring MVC. Simply put, a user uploads a file which can be of different types (.csv, .xls, .txt, .xml) and the application parses this file and extracts data for further processing. The problem is that I format of the file can change frequently. So there must be some way for quick and easy customization. Being a bit familiar with Talend, I decided to give it a shot and use it as ETL tool for my app. This short tutorial shows how to run Talend job from within Java app - http://www.talendforge.org/forum/viewtopic.php?id=2901
However, jobs created using Talend can read from/write to physical files, directories or databases. Is it possible to modify Talend job so that it can be given some Java object as a parameter and then return Java object just as usual Java methods?
For example something like:
String[] param = new String[]{"John Doe"};
String talendJobOutput = teaPot.myjob_0_1.myJob.main(param);
where teaPot.myjob_0_1.myJob is the talend job integrated into my app
I did something similar I guess. I created a mapping in tallend using tMap and exported this as talend job (java se programm). If you include the libraries of that job, you can run the talend job as described by others.
To pass arbitrary java objects you can use the following methods which are present in every talend job:
public Object getValueObject() {
return this.valueObject;
}
public void setValueObject(Object valueObject) {
this.valueObject = valueObject;
}
In your job you have to cast this object. e.g. you can put in a List of HashMaps and use Java reflection to populate rows. Use tJavaFlex or a custom component for that.
Using this method I can adjust the mapping of my data visually in Talend, but still use the generated code as library in my java application.
Now I better understand your willing, I think this is NOT possible because Talend's architecture is made like a standalone app, with a "main" entry point merely as does the Java main() method :
public String[][] runJob(String[] args) {
int exitCode = runJobInTOS(args);
String[][] bufferValue = new String[][] { { Integer.toString(exitCode) } };
return bufferValue;
}
That is to say : the Talend execution entry point only accepts a String array as input and doesn't returns anything as output (except as a system return code).
So, you won't be able link to Talend (generated) code as a library but as an isolated tool that you can only parameterize (using context vars, see my other response) before launching.
You can see that in Talend help center or forum the only integration described is as an "external" job execution ... :
Talend knowledge base "Calling a Talend Job from an external Java application" article
Talend Community Forum "Java Object to Talend" topic
May be you have to rethink the architecture of your application if you want to use Talend as the ETL tool for your purpose.
Now from Talend ETL point of view : if you want to parameter the execution environment of your Jobs (for exemple the physical directory of the uploaded files), you should use context variables that can be loaded at execution time from a configuration file as mentioned here :
https://help.talend.com/display/TalendOpenStudioforDataIntegrationUserGuide53EN/2.6.6+Context+settings

Categories

Resources