Compile a GWT library into a JavaScript library - java

I want to know what is the best approach to take to build a GWT library into a JavaScript library. When this GWT library to be compiled into Java script does not have any GWT server side component in it.
Just plain front-end components. Including only things like AJAX calls, etc.
In Java the GWT library is used like this:
TheGWTLibrary api = new TheGWTLibrary();
api.setServer("http://www.somewhere/api");
api.post(stuff, new Callback(){
void success(){
}
void fail(){
}
});
api.get(new Callback(){
void success(){
}
void fail(){
}
});
Such that the GWT library project would be usable in any HTML project without making it a GWT app.
If this is possible where will the dependencies of this GWT library be compiled to? Will it be included in a one single JavaScript (js) file?

[EDIT 2016]
The way to go with new GWT (2.8.0) is using JsInterop, read the documentation API. Note that it's still in 2.8.0-SNAPSHOT but will be released in few weeks. Also, JsInterop in 2.7.0 has been deprecated and removed in 2.8, so don't use it.
[END EDIT]
Apart from writing your JSI method, there are two easy ways to deal with this.
1.- GwtExporter
You might be interested on reading this article I wrote some years ago:
https://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en
It uses gwt-exporter, and you can take a look to a couple of projects using this approach:
JsUpload wich is a port of gwtUpload:
https://code.google.com/p/gwtupload/wiki/JsUpload_Documentation
https://github.com/manolo/gwtupload/tree/master/jsupload
GwtChismes is a very old library useless right now and not maintained anymore, but it is exported as well
https://code.google.com/p/gwtchismes/wiki/JsChismes_Documentation
https://code.google.com/p/gwtchismes/source/browse/#svn%2Ftrunk%2FGWTChismes%2Fsrc%2Fjschismes
I also did some experiments exporting gwtquery to js (jsQuery):
https://code.google.com/p/gwtquery/wiki/JsQuery
Chronoscope was another gwt library using this approach
https://code.google.com/p/gwt-chronoscope/wiki/JavaScriptAPI
2.- JsInterop
If you want to play with new stuff, you might read about JsInterop a beta feature in GWT-2.7 which will be fully functional in GWT-3.0.
It allows you to export java classes to JS. You have to use the -XjsInteropMode JS and some annotations.
There is no so much documentation right now, but there is a document explaining the API, and an interesting presentation.
https://docs.google.com/document/d/1tir74SB-ZWrs-gQ8w-lOEV3oMY6u6lF2MmNivDEihZ4/edit
http://gokdogan.appspot.com/gwtcreate2013/#1

It sounds like you're looking for something like GWT Exporter. It allows you to export a GWT library as a publicly accessible javascript API.
It looks like you'll still need to compile the GWT library as a web application to use it, but if you don't have any server-side code, it should be as simple as compiling your API, copying the war folder and adding a reference to war/[yourapp].nocache.js.
If that's not what you're looking for, you can check out this question for other options.

Related

How to convert swagger JSON objects into Java class objects

Sorry for my naive question, I am looking for some tools that would automatically convert JSON objects (that are generated in swagger UI) to a Java class objects in Eclipse.
Are there any such tools or plugins that can integrate swagger code with Eclipse Java and Groovy Grails framework. It would save a lot of time by not rewriting a huge amount of Java code every time when I want to change something in JSON objects.
I suggest to use autorest tool to generate code and models. It supports multiple languages including Java. Here is the github repository autorest
Just follow simple steps-
npm install -g autorest
autorest --input-file=https://******url******/swagger/v1/swagger.json --java --namespace=****namespace*****
You can use Google Gson Library in java.
You could use that or Groovys JSONsluprer but I depending on what you're trying to achieve it might just be easiest if you have a swagger to start with swagger codegen https://github.com/swagger-api/swagger-codegen tools and have it make those for you. If not you can always look to investigate how they did it.

No source code available for Scribe in GWT

I'm getting this error trying to Compile my GWT Project:
No source code is available for type org.scribe.oauth.OAuthService; did you forget to inherit a required module?
I have included the scribe .jar file in my "war/WEB-INF/lib", and imported it. Maybe I have to add it to the app_name.gwt.xml? I have tried to do it, but Scribe does not contains any .gwt.xml file.
In GWT you are restricted to use only some of the java classes since it is not possible to convert all the java classes to javascript. You can find a list of the allowed classes in this link: List of Classes
And it is not possible use classes related to Threads,IO,Network, etc...because of the compatabilty.
I use Scribe with no problems. But it's a server-side library. You should have no references to it in your GWT code.
You have to have the Java source code for Scribe in order for the GWT compiler to compile it (translate it into javascript). If it is available then you can add it to your source tree and and specify that it is "inherited" in the gwt.xml document.
Of course, check with Google's GWT forum for any updates. I know they are working on a better solution to this problem.
I just checked and Google has OAuth available as an extension to GWT. That is your best solution. Look here:
code.google.com/p/gwt-oauth2/

How do I use MUpdf in an existing java project

Mupdf documentation shows me how to use the library as an application and deploy it. However, I want to suck it into an existing java project and build my application on top of it. Can this be done? If so, how do I bring just the pieces needed, into my project?
Take a look at jMuPdf. I never used it, but it seems to be active.
Otherwise you will need to create Java Native Bindings (JNA or JNI).

Simple way to apply template to existing HTML code

Suppose I have a bunch of (very simple) HTML pages, that I want to apply a common 'theme".
These files are downloaded using various Groovy scripts, and I would like to apply to them this styling during a maven build. How could I do that ?
Using which framework/library could I do that ?
Furthermore notice I want to do that in a static fashion, that's to say I want to have the following process to occur
Files are downloaded by Groovy scripts
They are processed (in a "magical" fashion) by this library
They may be sent by FTP/SCP to an hosting server
Do you know such an easy to use library ?
Depends on the details of the task but having in mind the steps you've described you can consider using velocity templates.
I would suggest using sitemesh decorator. I am a user of old version but a new release is being worked on that allows you to do exactly what you are asking for. Do a google search on sitemesh and you should find lots of examples.
In a nutshell sitemesh decoration: basic html + template = decorated page.

Google App Engine "repackaged" package

What is the purpose of the classes in this package?
I want to use Base64 encoding in my app. As I'm typing away in Eclipse, I am prompted if I want to import a class called "com.google.appengine.repackaged.com.google.common.util.Base64"
I can't find any documentation about what this class does. No javadoc, or no mention in the Google App Engine manual (that I can see). Is this some kind of "hidden" API that I'm not supposed to have access to?
Is this some kind of "hidden" API that I'm not supposed to have access to?
Yes.
The purpose of repackaging Java classes is to have a private copy of a library that otherwise might conflict with another version of that some library (that the application developer adds to his project as a jar file).
It is one possible answer to JAR-hell.
Even the JDK makes use of this mechanism, e.g. with com.sun.org.apache.xerces which is an XML parsing library developed by the Apache Project that Sun choose to include (repackaged).
Do not call these classes directly. (You could, and they would probably work okay, but as they are not part of the official API, they could disappear in the next version).

Categories