GWT Library creation - gwt

I have a gwt library which I include to my other project via:
inherits name="org.mylib.Mylib" />
All the classes in the library project is under org.mylib.client*
This works fine.
However I added packages that contains classes that I want to be included in the library, i.e to be called within a GWT client side code.
Will it work if I don't put the classes under org.mylib.client?
Packages like: x.io, x.nio etc. to be usable within a GWT client side code.

They need to be Java source files, not just classes. You may have meant this, but that part of your question is ambiguous.
Assuming they are sources, you do need to include them in a package that you've declared to GWT contains sources. Either by inheriting a module which has this, or by adding it yourself in your main module. In short, somewhere you need this:
<source path="/mySourcePath" />
where /mySourcePath is the location of the sources for this x.io pckage.

Related

How can I use a custom package structure in a GWT project?

I'm using the regular GWT project structure that is generated when creating a new GWT project via the Eclipse GWT plugin, e.g. com.mycompany.mygwtmodule.client for all the client stuff that is GWT-compiled into JavaScript.
Now I want to add some client code with a custom package structure, e.g. org.othercompany.somepackage...
Is that doable? And, if so, how?
I don't need a workaround, if it's not doable. However, it may still be helpful for you to know, why I want to do this: The custom package structure is from a 3rd-party GWT module that is used by my GWT project. I just need a small set of classes from it which I copied into my project instead of including the whole module which contains lots of stuff I don't care about. Those classes are using each other. As I do not want to touch their code at all, I am keeping their original package structure.
What I tried is to add <source path='my-eclipse-project-path/src/org/othercompany/somepackage'/> to my gwt.xml, but in hosted mode I'm getting the error: "No source code is available for type org.othercompany.somepackage.SomeClass; did you forget to inherit a required module?"
No, I didn't ;-) The code is all there, but I couldn't make GWT find it.
Thanks for any helpful comments!
Its Better you create two separate modules ,since you are taking the code from some other 3rd party jar.
First create module which contain the package structure
- com.mycompany.mygwtmodule (one module)
Second create another module
-org.othercompany.somepackage (another module)
Inherit the second module in first module.gwt.xml
<inherits name="org.othercompany.somepackage" />
You need to add the other source package in your sources.
This is done in your module.gwt.xml. There is an element source, that will list all source packages to be included for client:
<source path="client" />
<source path="shared" />
will include all packages in client and shared into the frontend code.
Details can be found here: http://www.gwtproject.org/doc/latest/DevGuideOrganizingProjects.html#DevGuidePathFiltering
Update: if your packages have no common package you need to create two seperate modules and one inherits the other.
We use this a lot, it works out very nice.

Can't return custom class from GWT Service

I have my GWT project, and another project that just has simple Domain Objects, and has nothing GWT specific.
In my Domain Objects project, I have a class Bob that has some primitive fields. Nothing special. He implements Serializable for the hell of it.
I wanted to return this type from a GWT service method. Learning that the service method can only return classes that implment GWT's IsSerializable interface, I created a subclass of Bob, BobSO (Bob Service Object), and had it implement IsSerializable.
Basic project structure
proj> Bob.java
proj_gwt
proj_gwt > projgwt.gwt.xml
proj_gwt.client
proj_gwt.server
proj_gwt.shared > BobSO.java
I'm getting an error when running the service. It seems that it's loading BobSO, and seeking the source for the superclass Bob, and can't find the source.
"Errors in [path to file]
Line 6: No source code is avaiable for Bob did you forget to inherit a required
Will by BobSO subclass work since he implements IsSerializable?
It looks like GWT knows where BobSO is. What do I need to do to so it knows where the source to Bob.java is? I've tried creating a gwt.xml in my DOmain Objects project, but I'm not sure if it needs to list each class that should be visible, and how to import it in my GWT project.
1. Serializability
Implement either Serializable or IsSerializable, plus make sure, that the class fulfills the remaining requirements for GWT serializability (a no-args constructor, no final fields, ...)
2. Locating the sources
You need to create a module to help GWT-RPC find the serializable classes [*].
The module "gwt.xml" can live in your domain project. Alternatively, if you you don't want to touch the domain project, it can also live in your GWT project - just make sure to put it in the correct package:
DomainProject
src
bar
domain
Bob.java
GwtProject
src
bar
Domain.gwt.xml (contains <src path='domain'/>)
foo
client
MyEntryPoint.java
shared
BobSO.java
Bar.gwt.xml (contains <src path='client'/>
<src path='shared'/>
<inherits name="foo.Domain"/>)
Important notes
Make sure to delete the gwt-unitCache folder after changing such dependencies. Otherwise, the compiler often fails in strange ways (tested with gwt 2.5.0.rc2).
Always supply the source folders to the GWT compiler for everything the client side needs to know about. In this case, this is also the source for bar.domain.Bob.
Note
It's also possible to put Domain.gwt.xml in the "bar/domain" package, and use <source path=""/>
[*] By the way, it may be surprising, that defining a module for the dependency is usually not even strictly necessary in GWT. (It is however required when GWT-RPC is involved.) Try it with a simple example, if you like - GWT will find the sources by itself - as long as they're on the GWT compiler's class path
Will by BobSO subclass work since he implements IsSerializable?
It should work, but note that IsSerializable is the "old" marker interface for GWT. You can now use java.io.Serializable instead.
It looks like GWT knows where BobSO is. What do I need to do to so it knows where the source to Bob.java is? I've tried creating a gwt.xml in my DOmain Objects project, but I'm not sure if it needs to list each class that should be visible, and how to import it in my GWT project.
I think that creating a GWT module in your domain project is the right approach. You don't have to list each class. The "path" attribute of the "source" element should contain the name of a child package:
<module>
<source path="mysubpackage" />
</module>
You can have more that one source element if you have multiple packages. You can also include/exclude specific classes from a package, e.g.:
<source path="somepackage">
<include name="SomeClass.java" />
<include name="stuff/OtherClass.java" />
<exclude name="blah/**" />
</source>
To import it in your original GWT module, simply add an "inherits" entry with the fully qualified name of the .gwt.xml file you created, but without the ".gwt.xml" extension. Assuming your new module file is called Domain.gwt.xml and is located in package "com.domainpackage", you would add:
<inherits name="com.domainpackage.Domain" />
You can package your project as a jar and add it to the class path of your GWT project.

user spcific packages with gwt client side

I am a newbie at GWT and I have the following query.
I have a scenario where I am trying to develop a web interface(using GWT) for an existing application.
In the client side class file I would like to invoke a user specific class file, i understand that this is due to the fact that i am trying to invoke classes other than the ones http://code.google.com/webtoolkit/doc/latest/RefJreEmulation.html
I would like to know how this code can be called from the client side of the ui.
Thanks and Regards,
Bhavya
If you have a source code for the class you want to use and it is using only the supported Java classes, you can simply add your specific classes to client packages. If this is a reusable code, you can create a new GWT module and inherit it in your application. You can also simply move those classes to your client packages OR add a new client folder in you .gwt.xml file (add one more <source> tag)
If your class is not compilable by GWT, then I think you need to use it through RPC service.
If the code is translatable into JavaScript, you could for example put your individual class files into a jar and add it to the Build Path - as you would do in a 'regular' Java Project.

JPA Entities for GWT

I have developed one GWT project . I have created one JPA entity called Employee. I want that entity to be persisted to the database. My Employee is
located under com.mygwt.client.bean. Now my question is that are all the entities meant to be located at server side? When I tried to create under
the server side I got the exception saying Forgot to inherit the module for the Employee. Is there any other way for creating the entities in server side instead of creating the entity in client side? Please suggest the way I am doing right or not.
Put in the server side and then add an additional <source path='..'/> to your .gwt.xml to tell GWT where the sources are.
The above answer is right. But you have to be careful what you put in the packages you add using <source path='..' />. The tag <source path='...' /> in your project gwt.xml file tells the GWT compiler where to look for client side-code to convert to JavaScript. So you can only put classes in there that can be converted to JavaScript. You can not just add the server-side package which contains your remote servlets, that is wrong and won't work.
I suggest the following structure:
com.mycompany.client
com.mycompany.shared
com.mycompany.server
Basically client code is in the client directory, and in shared you keep classes (transfer objects, models, validators...) which are used client-side and server-side. Then put these lines into the project gwt.xml file:
<source path='com.mycompany.client' />
<source path='com.mycompany.shared' />
You'll have to be careful to access the server library jars only from the com.mycompany.server tree.
Also, not try to send an object from the server libraries over the wire to the client. If you need to send server library objects over the wire you'll have to have the source to the library -- it's a mess. It is easier to create a DTO class in com.mycompany.shared that is created my the sevrlet of your application from the server side information.
Stuart

GWT with JPA

I'm trying to build database application using GWT 1.5.3. I use JPA annotations with my objects. It seems in hosted mode GWT's RPC works fine. But when I try to compile my app using GWT-compiler I get errors like: "The import javax.persistence cannot be resolved", "Entity cannot be resolved to a type". toplink-essentials.jar is already included in my project path. What settings else do I need to solve this problem?
You can use Gilead (http://sourceforge.net/projects/gilead/) library to seamlessly manage JPA entities with GWT.
Regards
You need to include the source code for the JPA annotations in the build path for your GWT project. See here for more details:
http://code.google.com/p/google-web-toolkit/issues/detail?id=1830&can=1&q=jpa
Specifically this jar file which will fix your problem:
http://google-web-toolkit.googlecode.com/issues/attachment?aid=1475633892125294312&name=jpa-annotations-source.jar
The general problem of the JPA and GWT is that GWT itself doesn't support fancy JPA classes, so you just do simple POJO persistent entities DTO that implements the java.io.Serializable and have simple JPA resource annotations. You need to create the entity classes in the scope of the GWT client either have it under the yourproject.client package or add them with
source path="client"
source path="folderOfYourEntities"
in the GWT project's YouProject.gwt.xml file. This will include the entity objects in the GWT client so they can used them on client side of the RPC as well. The DAO must be on the server side and can contain anything that you container supports.
The problem you have now is that when compiling, GWT compiler saids that it desn't know what those imports for JPA annonations are in the entity DTO classes. That is why you need the javax.persistence class and source codes. The jpa-annotation-source.jar reference by Rustmyself works. It is just the javax.persistence compiled class files and source codes files plus a Persistence.gwt.xml. It is a simple GWT module for the javax.persistence package. If you know how to make your own GWT module, you should have problem making all this work. By the way, the official source for the Java EE can be found on the glassfish dev site's build section wiki.glassfish.java.net
There are many other solutions that wrap your fancy PU entities to simple objects automatically using proxy or to lazy load them at run time. They work, but not optimal solutions. The best practice is to make things simple and robust from the start by having POJO JPA DTO entities on the GWT client context and full blown DAO on the server.
GWTPersistence Example
I have added an actual working example on how to make GWT and JPA work seamlessly. It is a NetBean project with source codes and deployment file. See GWTPersistence on NingZhang.info
Ok, I've found what I was missing. I needed to include jpa-annotations-source.jar in my GWT-compiler path in myapp-compile.cmd script (or in ant build file). By the way can anyone tell me the origin of this jpa-annotations-source.jar file?
I am also working with JPA <--> GWT data transformation etc.
In an effort to eliminate the DTO layer I used Gilead too.
My objection here is about translating javax.persistence. To avoid this I used XML JPA mapping declarations (orm.xml)
Simply, keep another version of your Entities but without the annotations!
Rebounding on synergetic's comment, you now (from GWT 1.5) only need to add
<source path='javax.persistence'/>
to your Module.gwt.xml