Saturday, September 29. 2012
Couchbase Manager for Glassfish: JavaEE examples
After the announce for the couchbase-manager-0.1 this post is going to show how it deals with some JavaEE sample applications. Before the releasing I made it work with some of them in order to make the manager compatible with part of the typical JavaEE elements (EJB, CDI, JSF Managed Beans, JavaEE security and so on). As it is said in the release notes for 0.1 version the manager is still not fully tested but, at least, some applications work.
JSF Beans, CDI and EJBs.
JSF and EJB are two important technologies in the JavaEE world and I recovered the old CertSecurityCustom project for the occasion. If you remember that project lets us login using a certificate or a typical username/password form and checks the user against an LDAP repository. The project mainly consists in a EJB and a session JSF bean. After making all the objects to implement the Serializable interface and fixing some issues with the manager (mainly the memcached transcoder must use a special glassfish Input/Output object stream) it worked like a champ.
Here I realized another thing, an application can get and attribute from the session, change it and not set it back (it is supposed that it is the same object and no set is necessary). So in 0.1 version the manager marks as dirty any session when any attribute is got (that means mainly all sessions are always set or cas, for the moment touch is only used when the attribute retrieved is a simple object -Integer, String and some other classes that cannot be changed internally-).
Besides I inject the JSF bean using CDI (@Named) and typical bean (@ManagedBean), and same with the ldap ejb (@EJB and @Inject). The four combinations worked after the commented changes.
JavaEE security
JavaEE security is another hard issue I wanted to be functional in the first realease. The application that I chose was the CertSecurityJavaEE project (the second example in the certificate login series which uses again an LDAP server but doing standard JavaEE security).
More problems here were found. Glassfish declares the principal property of the session as transient (keyword that means the property has to not be serialized and therefore saved). Looking at the replicated glassfish implementation, I saw that only the username property is kept replicated. If the session is used in another server (in which the user did not logged in), the replicated implementation logs the used silently using a special method in the realm (createFailOveredPrincipal). I did the same trick. Now the manager session has a username property that is saved in couchbase with the other data, when the session is filled that property is checked and if it is different from the principal in the session (null mainly) the principal is recovered from the realm calling the same method.
That was very nice but it didn't work with the LDAP realm. I opened a bug with the issue but silence is the only answer til the moment (some collaboration please!). Changing the realm from ldap implementation to file (users and groups are stored in a plain file) my solution works as expected. The user logins to one application server and if it changes (non-sticky load balancing or a failure in sticky configuration) the user is logged in the background. Take in mind that this happens the first time the user access a new server (the manager sees the principal is null but the username is filled and it uses the realm to reassemble the principals of the logged user).
With all those changes my little application started working using the couchbase manager.
Java Pet Store 2.0EA
Finally I tested the typical Pet Store application. This application was for along time a kind of a reference application for JavaEE, now it is abandoned and its last version 2.0EA is only JavaEE4 compliant (not 5). Nevertheless I think it is a good test cos it mixes several technologies of the JavaEE specification.
With the pet store application I faced with a glassfish issue when using mod_jk and mod_deflate in the apache. But after fixing that issue the manager started to work smoothly. All the session beans were transformed into serializable objects (in version 0.1 the manager uses standard java serialization to convert the session into a byte array, that means all the objects that are going to be saved inside the session should implement the Serializable interface).
And here it is the video. First the custom ldap login application is shown (the application refused to change me from one server to the other but finally it did), you can see how the couchbase console shows one session and, as soon as I log out, the session disappeared. Then the standard JavaEE security application is accessed. Both servers maintain my logged session using the file realm. Finally I enter Pet Store application and click over some of the pages which work smoothly.
Well, the couchbase-manager-0.1 is working with some (more or less) complicated JavaEE applications. I am conscious that there are more tests to do, and that is the reason to release the first version, let more people testing it. If anyone of you is interested in the manager please install it and test it with your applications (use github or this blog to report issues). Of course I guarantee nothing about the fix.
ciao!
Saturday, September 15. 2012
WBXML stream: First bits
Another of my new initiatives is a Java streaming implementation for the WBXML format. The WAP Binary XML (WBXML) is a binary representation of the XML language thought to transmit documents in a more compact manner over mobile networks. The format is maintained by the Open Mobile Alliance and its complete specification can be downloaded from their website. Currently several mobile phones languages use it, for example SyncML (Synchronization Markup Language), WML (Wireless Markup Language) or WV (Wireless Village), and, although it is not very common, I need it for another idea.
On the other hand now Java SE (version 6 and 7) defines different technologies to deal with XML documents like SAX (Simple API for XML), DOM (Document Object Model) or StAX (Streaming API for XML). All of them are different ways of reading, writing and managing XML documents. But, more interestingly, all can be used together with JAXB (Java Architecture for XML Binding) to construct Java Objects from XML documents and vice-versa. I admit that all the XML stuff in Java is horribly complicated but the final JAXB idea is absolutely awesome. Remember when I developed some RESTful Web Services via JSON, how easy was integrating the parsing and decoding part, and all of that was because JAX-RS uses JAXB.
So in this project the idea is simple, implementing a StAX (reader and writer) that let us encode and parse WBXML documents. Why StAX? Just because it is simple, modern and I worked with it before. I need to say that I used intensively the current libwbxml C implementation to understand the confusing specification (as you see it is also an ancient project).
The library is now divided in different packages:
es.rickyepoderi.wbxml.definition: The package is used to define known WBXML languages. The format specification (not very friendly I have to say) assigns numerical identifiers for XML tags and attributes (besides some other things). This way SyncML or any other language should be defined in order to parse and encode documents of that type. I decided to use a plain properties file for every language definition. The definition file is quite complicated to understand but, please, blame WBXML specification and not me. Finally the current initialization class loads all properties files at startup, but I admit that that class is too over-elaborated and I will surely change the way it is working now.
es.rickyepoderi.wbxml.document: All the classes in that package are a memory representation of a WBXML exchange document. Following libwbxml idea all the parsing and decoding stuff first uses this Java (memory or object) representation and then the StAX part uses it to write or read the XML.
es.rickyepoderi.wbxml.stream: Finally this package implements a XMLStreamReader and XMLStreamWriter for StAX. For the moment only those two classes are implemented (no event reader or writer, no factories).
es.rickyepoderi.wbxml.tools: Mainly an application to have the Java implementation of the wbxml2xml and xml2wbxml binaries. The Converter class let us transform an XML file into WBXML and backward. It is used to test my implementation against the C counterpart.
I think the implementation is very weak nowadays but I have successfully used it to convert some XML files to WBXML and viceversa using libwbxml to check results (the idea is the wbxml2xml binary can decode my WBXML files and my Java program can decode a WBXML file encoded using xml2wbxml binary, and the same with XML files). Currently I have tested with SyncML, WV and SI (Service Indication) languages.
I present the first example of how my library works using it to write the WBXML representation of a DOM SyncML document:
// read the XML using DOM InputStream in = new FileInputStream("syncml-001.xml"); DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance(); domFact.setNamespaceAware(true); domFact.setIgnoringElementContentWhitespace(true); DocumentBuilder domBuilder = domFact.newDocumentBuilder(); Document doc = domBuilder.parse(in); Element element = doc.getDocumentElement(); // locate the definition of the WBXML using the root element WbXmlDefinition definition = WbXmlInitialization.getDefinitionByRoot( element.getLocalName(), element.getNamespaceURI()); // create the StAX stream writer using the definition OutputStream out = new FileOutputStream("syncml-001.wbxml"); XMLStreamWriter xmlStreamWriter = new WbXmlStreamWriter(out, definition); // create a transformer to convert DOM into StAX Transformer xformer = TransformerFactory.newInstance().newTransformer(); Source domSource = new DOMSource(doc); StAXResult staxResult = new StAXResult(xmlStreamWriter); xformer.transform(domSource, staxResult);
Besides I have created some JAXB classes (using xjc command over the DTDs of some simple languages like SI) and they can be used without problem. For example this sample lines load a SI element from a WBXML file and write it into a XML one.
// read the wbxml input file from a StAX stream InputStream in = new FileInputStream("si-001.wbxml"); XmlStreamReader xmlStreamReader = new WbXmlStreamReader(in); // create the object from the StAX stream reader JAXBContext jc = JAXBContext.newInstance(Si.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); Si si = unmarshaller.unmarshal(xmlStreamReader); // create a default marshaller for XML output OutputStream out = new FileOutputStream("si-001.xml"); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(obj, out);
Please note no stream object is being closed in the examples. And that's all! As usual I created a new project in my github space. I know there is plenty room for improvement but it was so many hours understanding and coding the annoying format that I wanted to share the new info with all of you.
<goodbye/>
Sunday, September 2. 2012
My new Samsung Galaxy Y Duos
As you know sometimes the entries in this blog are just for me (this is the best place to keep some things I do not want to forget), this one is one of those. Some months ago I bought a Samsung Y Duos (GT-S6102) in order to avoid carrying two mobile phones (company and personal one) always in my pocket (I lost my company device twice last year but, luckily, I also found it again). This samsung model is a dual SIM device with android 2.3, not very powerful but also not very expensive. I realized past month that there are already custom roms and kernels for that device. During my vacation I have been playing with them and, cos I am not a fan of these things, I am going to put here a bunch of links of how to perform typical Android tasks in that specific device.
CWM and Unroot
ClockworkMod Recovery (CWM) is a popular custom recovery for Android phones done by Koushik Dutta (Koush). It allows you to perform several advanced recovery, restoration, installation and maintenance operations on your Android device. Besides it is one of the most common ways used to gain root access, back up device data, install custom ROMs, kernels, themes, mods and so on.
An unrooted Android device consists in installing the su binary inside the system in order to make that some applications can run with root privileges.
The interesting links about those issues for my device are the following:
TechnoDUOS 1.5
TechnoDUOS is a custom rom and kernel for the dual SIM device introduced by a XDA developer forum member Millan.SIS (now there is another custom rom called Xperia-Duos). That rom is supposed to be better tweaked for my phone model.
Again some nice links:
DroidWall Modules
The DroidWall is just an Android Firewall based on iptables. In order to install it a rooted device (obviously the firewall needs root access) and some modules in the kernel are needed. The BDSky kelnel (TechnoDUOS kernel) does not have those modules but it supports init.d startup. So I added the DroidWall modules following this link (it is curious that this was my first intention, what a hell for such a tiny thing):
At least it is done, I have a rooted Galaxy Y Duos with a tweaked MOD and init.d kernel which loads the required modules to have the DroidWall application configured. It seems to work! I do not trust in Android (and even less in Android applications), so a firewall seems to be a good choice, I am also considering restricting some of them.
Cheerio!
Comments