Environment sharing between request threads in Google App Engine - java

I have noticed that when I create a new thread using the request thread factory provided by GAE then the new thread has the same Environment as the parent thread. (The identityHashCode of the current environment is the same in both threads.)
On the one hand, this is nice because the newly created thread starts with the same context as the parent.
The problem is that the Environment is not immutable. It contains the ".currentNamespace" attribute which is used in namespace handling. If one of the threads changes the current namespace it is applied on all threads which is clearly not what I want.
My idea to fix this was that I created an own Environment implementation and when a new thread is created I copy the content of the current environment into this new environment and set this environment as current on the new thread. So the new thread starts with the same context but it can independently change later.
This solution worked during initial testing but then I run into a problem
Caused by: java.lang.ClassCastException: MyEnvironmentImplementation cannot be cast to com.google.apphosting.runtime.ApiProxyImpl$EnvironmentImpl
at com.google.apphosting.runtime.ApiProxyImpl.log(ApiProxyImpl.java:67)
I have no access to the code of com.google.apphosting.runtime.ApiProxyImpl but it is clear that this method tries to cast the interface it received into its own implementation class without checking the type.
I find this strange because there is a void setEnvironmentFactory(ApiProxy.EnvironmentFactory factory) in the ApiProxy so it is expected that someone might use a different implementation of the Environment interface than the default one.
Is there another way to use different namespaces in different request threads?
Is this unchecked casting considered a bug or is it fundamentally wrong to use my own Environment implementation?
I use app engine standard with 1.9.84 of the java sdk.
Edit:
It is actually documented that "This should not be used from user-code." on the
ApiProxy.setEnvironmentForCurrentThread() and ApiProxy.setEnvironmentFactory() methods. So my suggested workaround is not expected to work. You shouldn't try something like it either.

No need to use Google API threads. Do not let Google API's cause you problems. Do this yourself outside of the browser as I describe here.
Write a program that sub-classes your browser, and subclass each window that is opened. Then run a javascript on each page that saves your result to the location bar where it is easy to collect it later. Then collect that information from the location bar and then put the previous (which was in the location bar) back into it so that it will be seen as being the same as it was before.
If you want to use that data in a different web page then put it there via your separate program.
Each opened browser window could theoretically be running a separate google api process.
With your program subclassing all of the browser windows separately, have your program to share information between them without the google api getting confused.
Like this:
Write a program (in VB6 sp5 I did this years ago. Never use any later version of Visual Studio for anything. In C++11 this should work.).
Using FireFox as a browser for this example.
(1) Have your program start and Subclass FireFox.
(2) Have your program tell Firefox to open up a new window (might need to make this a FireFox "new tab" or maybe not).
(3) Tell your program to get the pre-handle of the newly opening window. Do this quickly and keep trying (up to 30 seconds if you have an overloaded operating system) until you get the pre-handle, then assign a new Window's handle to that new window or new tab.
(4) Use that new handle and send a javascript to the address bar (minus the "j"), meaning that you send an entire "avascript..." to that address bar, then add the previous "j" since if FireFox detects that you placed any command into the addressbar with the entire word "javascript" it will stop you from doing some things (if I recall correctly).
(5) Run that javascript obtaining from or placing into each page your changes.
(6) The web pages in the browser, having the javascript running in them do part of the work.
(7) No need to use Google API threads.

Related

Blue Prism fails to highlight Java application components

I am dealing with a peculiar issue.
After spying on a Java GUI that I have made (this might be important or it might not!), I am able to highlight correctly the components of the window.
But when I close the window and then open it again, it fails twice to highlight any component and then it highlights them correctly from that point on.
In order to spy on the elements I have used the Java Mode (I assume that this based on the Java Access Bridge).
Also, when I try to Open Application Navigator, but that also failed, although I have pasted the related files from the java access bridge.
It is really weird and there seems to be no easy answer for my problem.
This is not something specific to Java, and is observed for many applications. Check whether the application needs to load some data from a backend server to function properly. The UI elements of the application might get loaded quickly, however some their attributes might still be loading from a backend server, and until it is full loaded the attributes would not match with the values you captured.

Java Control Panel Proxy Settings - where are they stored?

I have... a situation. Imagine you have many, many PCs (Windows 7 to 8) that run a Java Application which is started by using a .jnlp. Now finally there has been a version-jump from java 6 to 8 (took way to much time), and suddenly this application wont start any more (can't parse first jnlp, javaws tells me).
To solve this we have to insert the correct proxy-setting inside java-control panel (wasn't necessary with the old version).
So... doing this by hand is a task that most users can't perform (sadly their level of knowledge isn't sufficient), but doing ths by myself at all PCs would take... uh... a really long time.
So we decided to create a little batch-file, that does perform all the stuff necessary (installing latest jre, clearing cache and so on) which just has to be runned once at every users PC. In Theory.
We were able to place all steps necessary in that .bat, except for setting the proxy at the java control panel. I wonder if this is possible at all. The javaws arguments don't contain such an argument, and I wasn't able to find a physical storage whatever inside the jre or the java cache.
And I can't use these -dHTTP.xxx.... flags for that .jnlp file I'm afraid, because that java webstart is the one that need to know the correct proxy-settings, not the application.
So is there a way to change the proxy setting you can access with the java control panel by some command line argument or something like this, enabling myself to complete this "all new" script?
Greetings, ConfusedMerlin
Java control panel settings are located in %userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties
to add proxy you can use something like
(
(echo(deployment.proxy.bypass.local=true)
(echo(deployment.proxy.type=1)
(echo(deployment.proxy.http.host=10.10.10.10)
(echo(deployment.proxy.http.port=10)
)>>"%userprofile%\AppData\LocalLow\Sun\Java\Deployment\deployment.properties"
though if there are already set proxy you'll need to replace the data.

JNLP SingleInstanceService Use in Command Line JAR Application

I have a desktop Java application that is run from the command line, which takes in some arguments and performs some actions based on these arguments.
Currently, the application is instantiated periodically, performs its function and then exits.
The issue is that the users are unhappy with the amount of time it takes for the application to initialize. In order to work around this, I thought of simply toggling the visibility of the application when it is finished and setting up some kind of IDLE state.
I was trying to figure out a way to pass in new arguments next time the application needs to do work. I found out about SingleInstanceService and was wondering if it is possible to make this work with my application? It's unclear to me what I need to do so that the Single Instance Service runs on the client PC.
Alternatively, is there another solution for my communication problem? I would rather not depend on File I/O to trigger the application's logic.
Thanks.
AFAIK The JNLP API is available only if you launch your application using java web start (JWS) technology: read more here: http://java.com/en/download/faq/java_webstart.xml
If that is an option for you, oracle has some example of how to use the SingleInstanceService here
Implement and Register SingleInstanceListener. It will be invoked with the main-args when new instances of your application is launched.

How does a browser interact with a Flash Player or a Java Applet?

I've been trying to understand how flash animations or a Java Applet work within a browser.
I can think of a couple of ways -
The Flash Player/Java Applet are machine code that's dynamically linked it, and given
some parameters about the area of the screen that belongs to them; after that, they
run within the same process space.
The browser exposes an API that the player/applet use to talk to it and they live
in a separate process. (Presumably they talk via sockets?) The API could correspond to
openGL/X11/some custom calls.
These possibilities still don't explain things like how a button click can make the
player full-screen, how it can play music, how it can inspect the DOM, etc. For that matter,
is the video displayed by decoding to a sequence of images, and rendering them
one at a time, or is there a more efficient way, e.g., of pushing the deltas in the image?
The Wikipedia page on Java Applets (1)
talks about how the applet is run in a sandbox (presumably a separate process), but
it doesn't say how the browser and the applet communicate.
Perhaps the answer depends on the underlying platform?
Any pointers to systematic discussion of this topic would be appreciated (as would
a reference to the APIs).
(My interest in this stems from an insatiable curiosity.)
I'm pretty sure plugins like Java applets and Flash run via NPAPI in most browsers. I looked into this matter myself some time ago and NPAPI was the answer I found.
In the case of browser and Java applets, the applets are typically run within the Java plugin, which runs as a separate process (you can see it e.g. in the task administrator in Windows).
The plugin creates an object for each applet in the DOM, and you can thus interact with the applet from Javascript. Anyway, calls to the applet that take a while to return do have the effect to freeze the browser, therefore I'd say the communication with the plugin runs in the same thread as the main refresh loop. This seems at least to be the case with Firefox.

Launching a website from within a program, and inputting data to specific fields

Although I've been programming for a few years I've only really dabbled in the web side of things, it's been more application based for computers up until now. I was wondering, in java for example, what library defined function or self defined function I would use to have a program launch a web browser to a certain site? Also as an extension to this how could I have it find a certain field in the website like a search box for instance (if it wasnt the current target of the cursor) and then populate it with a string and submit it to the server? (maybe this is a kind of find by ID scenario?!)
Also, is there a way to control whethere this is visible or not to the user. What I mean is, if I want to do something as a background task whilst the user carries on using the program, I will want the program to be submitting data to a webpage without the whole visual side of things that would interrupt the user?
This may be basic but like I say, I've never tried my hand at it so perhaps if someone could just provide some rough code outlines I'd really appreciate it.
Many thanks
I think Selenium might be what you are looking for.
Selenium allows you to start a Web browser, launch it to a certain website and interact with it. Also, there is a Java API (and a lot of other languages, by the way) allowing you to control the launched browser from a Java application.
There are some tweaking to do, but you can also launch Selenium in background, using a headless Web browser.
as i understand it you want to submit data to a server via the excisting webinterface?
in that case you need to find out how the URL for the request is build and then make a http-call using the corresponding URL
i advice reading this if it involves a POST submit

Categories