Inheriting methods from an other program wrote in another language - java

I'm making a system for an app, witch have for objective to allow the user to create custom script to interact with the program by handleing events and reacting to them by calling functions. The main program is wrote in java, but If want to allow the script to be wrote in JavaScript. What is the best way to do that ?
I've tryed to use the sockets to transfer data and events, but I thinks it's a bit overkill, because the app and the scripts are on the same machine. Does it exist a better way to do that?

If I understand correctly, what you want to do is called Remote Procedure Call and it doesn't help much that all your code (java and js) runs on the same computer. But you can probably at least get away without authentication or security.
There are a bunch of libraries that may save you some trouble. You may want to take a look at those options:
https://en.wikipedia.org/wiki/Remote_procedure_call#General
I only got the occasion of using one: json-rpc, which isn't necessarily the best option, but it is the only one I can give you more details about.
The specification of the protocol is available here https://www.jsonrpc.org/specification and there should be Java and Javascript libraries to ease the implementation.
For example:
Java - https://github.com/briandilley/jsonrpc4j
Javascript - https://github.com/jershell/simple-jsonrpc-js
Calling this Java hello method:
public String hello(#JsonRpcParam(value="message") String message) {
return message;
}
From your client, a call to hello would look like this:
var jrpc = simple_jsonrpc.connect_xhr('localhost:8080');
jrpc.call('hello', {message: 'hello world!'}).then(res => console.log(res));
Of course there's additional configuration, at least on sever (java) side to make this work.

Related

What is a good way to implement Sockets?

dear stackoverflow community.
Currently, I am working on a project. This project shall have a server and clients connecting to it. Because of their simplicity, I'd like to use Java's integrated ServerSockets and Sockets.
In my project, data shall be sent from the client to the server and opposite.
My initial idea is to just send JSON and then parse it as receiver and get the data from that.
I'm a little unsure about that though, since JSON isn't something that's integrated into Java, but comes from Java script. Also, I'm currently using a Multithreaded-Socket-Server, so I have a ClientHandler Thread class. In that class, the messages were received, parsed and the "action" parameter was read out of the JSON and then I did a switch statement with multiple actions and their functions. I don't think that's a good way of doing that either.
So, my question is:
How can I do that better, and maybe do I have to use something else?
Thanks in advance.
It is true that JSON grew out of JavaScript, but it is a reasonable definition language on its own and I don't see any reason you shouldn't use it. There are libraries for parsing it so you don't have to.
Assuming your JSON structures are different for different purposes, and complex enough to need different classes to represent them, I like the idea of the JSON having a parameter that identifies the class to which it belongs, after which you can hand off parsing to a class that understands the designated output. Then a class can read the JSON, get the type, and some the specific parsing routine can go from there to an object created for the purpose.
I don't see anything wrong with an action string, either; it's served well enough for Swing and some other UIs, after all. Instead of branching out to a function, depending on complexity again, you could have action classes that all implemented an interface, and the action 'verb' could tell you which one (out of a map, say?) to get and execute the 'performAction()' method on or whatever you want to call it.
I don't know how clear this is from a quick description; would be willing to chat about it in an SO chat room if you care about it.

How safely generate HTML pages on GWT?

I'm developing GWT application which has many different forms. Visibility of these forms depends on type of authorized user. In other words, I want to do the following:
String type = rpc.getUserType(); // ask from server only ONCE
if(type.equals("advancedUser"))
{
ContentPanel advPanel= new ContentPanel();
add(advPanel);
}
if(type.equals("admin"))
{
ContentPanel adminPanel= new ContentPanel();
add(adminPanel);
}
My question is the following:
Is getting user type from server only once and using this variable everywhere safe? I mean is there any possibility to change this variable (if it stored on client side)?
My friend told me that GWT prepares html on server side, so I can safely store type in one variable and use it - nobody can acces it.
But i think, that i have to make rpc call before each construction like if(userType=blah-blah-blah) add(something) because I think that GWT translates my Java code to Javascript which runs on client side and all variables can be modified with programms like ArtMoney.
My question is the following: Is getting user type from server only once and using this variable everywhere safe? I mean is there any possibility to change this variable (if it stored on client side)?
Yes that is the most possible and Robust solution. More over I would like to suggest, make it static
public static String USER_TYPE = rpc.getUserType();
That variable should initialize with a value in very first of onModuleLoad and should be in to whole application.
But i think, that i have to make rpc call before each construction like if(userType=blah-blah-blah) add(something)
No that raises performance issues and unnecessary server calls through the wire.
because I think that GWT translates my Java code to Javascript which runs on client side and all variables can be modified with programms like ArtMoney.
GWT secure enough and compile your module with the option Obfuscated, which makes your code not human readable in browser.
That Obfuscation alone protect from all vulnerable things, there are some other steps also needs to be taken to sleep happy :)
I'm suggesting to go through the GWT security documentation.
Besides all,
While saving or processing data on serverside that check you done on client side needs to be done strictly, So that you cn avoid such situation.
On server side
if(clientsideUserType.equals(serverSideUserType)) //
{
// Then only insert/process data
}

how to share this same instance of lib by two programs

I wrote program in java which is using some kind of win lib and now I want to write one more program to simulate other one. I mean, it should be going like that :
first program asking lib for some simple data ( just true false)
and other program in this same time by using function from this lib setting some variable in this lib which might be return to first program...
both programs are independent first (lets say "getter") in java and second ("setter") in c++... I have already set all variables in lib as static but it didn't solved problem.
Is this kind of solution even possible? or I have to use maybe some kind of socket or else
thanks for replay
I've been working with this kind of stuff (Java + dll + another programs) and I can tell that the libraries executed from another program and Java doesn't share the static variables, so I think you won't be able to do it that way.
The example that I have uses a window, whose size is 0, to exchange messages between the two programs (Java and VB 6.0), the first call between the two programs share the window handler, but I think this isn't the best way to do it, and, in addition, it has some limitations.
I expose the ways I think that could match your problem:
Shared file: pretty easy, just must take care with the encoding.
Memory area: You can use in the dll a memory area for data exchange, this is a truly "static" context
Socket: Maybe is the most flexible since it will work with any program/system.
The last one would be the one that I'll use if I must implement something like that, but that depends on you.

A unique Java and Javascript intertwining act

I have a java program that processes information, but I want to make it so the end user can write javascripts that dictate what to do with this info. Like this
//Java
private void newData(int var1) {
script.newData(var1);
}
and then
//Javascript
function newData(var var1) {
someVar = var1;
processVar();
}
I have looked into something called rhino, but I really am having trouble understanding the concept of rhino. Anyone know what to do?
You want Rhino. Rhino is a javascript runtime implemented in Java. It is suitable for embedding in Java applications.
What you want to do is create your Java classes and objects and then make them accessible to a Javascript environment. Fortunately this is very easy with Rhino. Read this tutorial and pay close attention to the first and second sections (RunScript: A simple embedding and Expose Java APIs).
The first section is about executing Javascript within a Java application. You will need to adapt their sample code a little to provide some way for the end user to hand javascript code to you (in a file or stream) for you to execute. It won't be difficult.
The second section is about making your Java stuff available to the Javascript stuff. In the simplest case you don't need to do anything--all of Java is available to Rhino javascript automatically. But you can very easily pretty up the interface and provide something easier for the end user to use if you want.
With Rhino you can also go in the other direction--you can make Javascript objects available to the Java environment. This is a little more complicated, but is covered in the rest of the tutorial. You may not need to do this either.

User inputed formula parsing (eval)

Good day all,
I am trying to figure out how to allow users to call a method on some specified data.
I would like to provide a predefined set of functions:
Moving average, moving_ave(x,5) ..would be a 5 day moving average on x.
3*x+y....and so on...
So basically, i will provide the users with various data series (x,y,z....) and a set of functions moving_ave, + - / * ....and they should be able to write simple formulas (restricted to the functions i provide).
how can this be done?
I will be deploying this on App Engine for Java.
so for i have found out about JSR-223...but i'm not sure if its appropriate? I am thinking i can use the Eval function.
Thanks,
It sounds like what you want is an interpreter for a simple grammar. Be very wary of approaches such as that suggested by Aerosteak; allowing your user to call functions in your code directly is dangerous, and it's easy to make mistakes sandboxing it, resulting in security vulnerabilities. It'll also require you to write your own parser.
The easiest approach is probably to use an existing language - Javascript probably fits very well, and you can use Rhino, a Javascript interpreter written in Java.
You will need to use Reflection to call unknow Method. Look a Apache BeanUtil.
You can have a TextBox with the value: 1,2,3, Convert these values to Object Array.
Have another ComBo Box with all you possible Method you can call.
Then use Bean Util to call the method with the Object Array.
For Exemple:
class MyMathManager{
public void doCalculationType1(Object args...){..}
public void doCalculationType2(Object args...){..}
public void doCalculationType3(Object args...){..}
Then Look at the Java of BeanUtil to call these Method.
Good Luck.
This sounds like something that could probably be done on the client, rather than the server. You could write a few handy javascript functions that call a restful API on the server to provide the needed data to a few more handy javascript functions that do the useful calculations. It's almost always safe to allow users to eval on their own clients.. they can do it in any case.

Categories