Blog do projektu Open Source JavaHotel

wtorek, 22 stycznia 2013

Just another MVC framework

Introduction
I came to the conclusion that creating Web interface (even if one uses GWT) is complicated and dotted with problems so I decided to create next MVC framework utilizing what I have done so far. There are plenty of MVC frameworks so why create next one instead of using existing already.
I do not know but I really like the idea of having separated UI and business rule, Web/AJAX approach allowing interaction to every user action (not only submit/response) and rich programming language to create business rule with full access to all background environment (datastore). "Programming language" on one hand simple enough without all Java/C++ bargains like interfaces/headers/declarations but on the other hand rich enough to do what you want, "hot deployment" during development, modify source code and framework runs it immediately. Cutting long story short - I mean Python/Jython. And also do not bother with all details related Web/Ajax programming especially asynchronous requests and responses. And also ready to run on any Web container and interacting with any database.
Source code for the first version of framework is available here (Cache, GwtUI, JythonUI, Test). Source code for the first sample application is available here. Sample CRUD application is deployed to Google App Engine infrastructure. Also Tomcat (not Google App Engine) application has been created. How to assemble Tomcat (and Junit test) Eclipse project is described in this document.
General idea
  • User interfaces are described in XML document. Current xsd schema file is available here. Sample xml user interfaces definition file for CRUD application is available here. Framework provides default layout but it is planned to add also the possibility to specify HTMLPanel for more rich layout. 
  • There are four user action for CRUD dialog : read all records, add new, remove and modify. There is also special "before" action for the form initialization. For every user action a Jython script is called on the server side. Sample Jython script is available here. The current field values (Web form)  are available for Jython script as map/dictionary (pairs: "key"->"value", "field name" -> "field value"). Jython script should recognize user action ("action" parameter in "dialogaction" method), read current field values and perform server action. For "add" action add new record to datastore. It is also possible to return error message (for instance key is duplicated). Results are also stored as Jython dictionary.
  • Server side of the framework translates again Jython dictionary to Java Map and sends this map back to the client.
  • Client reads the answer and performs next actions. For instance displays error message in case of error, refresh list in case of success, navigate to next dialog etc.
  • For the time being very limited subset of actions and user interface widgets are implemented but it will be enhanced gradually.
  • Developing client application with this framework means adding new XML documents and developing business logic with Jython scripts. Database access can be achieved via Jython directly or (like in the sample application) by creating Java DAO (Data Access Objects). Jython can interact with Java smoothly so any method is feasible.
Framework structure, source code
  • GwtUI. There are three packages: client code, server code and shared (between client and server) code. It is a framework interacting directly with GWT. Is is based on publish/subscribe pattern I found well fitted for asynchronous nature of Ajax applications. It adds a logic for displaying and executing forms, lists, menus, CRUD lists etc. Firstly I wanted to develop client application by using this framework directly but after some time and developing a lot of code I realized that it was too complicated.
  • JythonUI. Consists of three packages: client code, server code and shared code. Is based on GwtUI (does not interact with GWT directly) and provides interaction between XML documents and Jython user actions.
  • Cache : Support for GAE MemCache. Allows using JCache in application. For Tomcat application there is a simple Map implementation
  • Test : Meaningful implementation is only for Google App Engine JUnit tests. Allows creating the same JUnit test suite for Google App Engine datastore and JPA implementation.
Third party tools
  • Jython. Interpreted language for developing server side business logic. During development Jython code can be modified directly and being effective immediately without any recompiling, one can call it hot reloading. For production Jython can be translated to byte code and executed directly without recompiling. JythonUI server code translates Java Map to Jython dictionary and vice versa.
  • Guice. Although now it ties together only several modules (sample AbstractModule) I found it very useful. For every four applications (WebGooleAppEngine, WebTomcat, JunitTestGoogleAppEngine and JUnitJPA) I created separated AbstractModule and this way I was able to put together all pieces. For instance: provide JPA implementation for datastore in case of JPA application and Google App Engine implementation of the same datastore for Google App Engine application.
  • GIN (GWT INjection) : Code injection framework for GWT. Used intensively in GwtUI.
Third party tools used for Sample application
  • Objectify : I decided to use Objectify instead of JPA/JDO Google App Engine implementation. I fully agree with a simple statement found here:  " The GAE datastore is not an RDBMS" and found that Objectify is a good answer to that issue. The general idea behind JPA/JDO is that we put simple interface on more complex RBDM/JDBC concept. But in case of Google App Engine it is just opposite : JPA/JDO is something more complicated put on something simple: NoSQL key/value database. 
  • EclipseLink : Outside Google App Engine world I still think that traditional RDBS is a good choice and JPA layer gives a lot of advantages comparing to bare JDBC.
  • Apache Derby : Good choice in case of testing or developing. Does not require installing or tuning, just put derby.jar everywhere on java class path, create URL, connect and use it.
Future development
It is only the first stage, "proof of concept". It looks promising I created fully tested two (simple) web applications for Google App Engine and Tomcat (JPA/RDBS). Future development means enabling more GWT widgets and user actions. I'm also planning to extend business logic programming by adding more JVM bases languages, Java and also add bridge to CPython (by means of JNI).
Problems
The most important problem is running Jython on Google App Engine. Because Google App Engine can create a new JVM at any time  so also Jython should be initialized. It means very long delays unexpectedly (10-15 sec) which makes all solution almost useless. For Tomcat based application it is only a delay once at the beginning of the session and then it runs smoothly all the time. I will try to understand what is the reason for this delay, may be deployment of already compiled Jython packages will resolve the problem.  

Brak komentarzy:

Prześlij komentarz