Donation

If you found the contents in this blog useful, then please make a donation to keep this blog running. You can make donations via Skrill with email address shazin.sadakath@gmail.com

Tuesday, August 8, 2017

Java 9 : The Future 2

This is a continuation of Java 9 : Future 1 Post

Modules enforce Strong Encapsulation as we discussed in the previous post. Now a Package is not public by default to all other modules unless it is explicitly exported in the module-info.java Module Descriptor.

Can Class D from Module A access Class F from Module B?


With the Module Descriptors mentioned below, yes it is possible as Module A has mentioned as it requires Module B and Module B has exports package Z.

Strong Encapsulation

  • Module must require other Modules to be used in them.
  • Module exports packages to specific Modules or to all.
  • Module opens packages explicitly for reflection.
  • Accessibility is enforced by compiler, JVM and Reflection.

But why Modules again?

Remember packages starting with sun.* or com.* that were available in rt.jar (JVM Runtime) which were only specific for certain JVM Vendor Implementations and not all. Java programmers have been constantly warned not to use these packages as they will break Platform Independence. But up until Java 9 Modules there was no way to completely stop developers from using those packages.

Enter java.base Module

Following is the Module Descriptor for java.base module. With this as you can see only certain packages are exported and com.*/sun.* packages are only available within the module.

// module-info.java
module java.base { 
      exports java.lang;  
      exports java.io;  
      exports java.net;  
      exports java.util; 
}

Every Module in Java 9 implicitly will have require to java.base. Just like every class in Java will automatically import java.lang.*. 

So what does this means?

This means that now there can be a Java Application which is 100% modular with all Jar files being modules. Which means we can't use classpath in those scenarios. 

Enter Module Path

  • Path containing directories which contain modules
  • Modules can be exploded directories, or modular JARs (other forms too) 
  • The runtime searches the module path when looking for a module.
Plus a Java Application can be an hybrid of Modular as well as Backward Compatible Application in those cases there will be both a Module Path and Class Path.

To be continued..






Wednesday, August 2, 2017

Java 9 : The Future 1

There is much so talk and hype about Java 9 and its features. There are so many posts about Java 9 and specially on Java 9 Jigshaw or Modules. As a Java Developer for almost a decade now, I was really cautious and concerned about the new features of Java 9 and wanted to learn about it in depth. What better way than learning from the creators of Java 9 itself. Recently I got the chance to view an InfoQ video on a Presentation done by Karen Kinnear who is the Technical Lead for the Hotspot Java Virtual Machine Runtime team at Oracle. This lead me to realize Java 9 is far beyond Modules and the in depth nature of the Modules and the necessity for such time and resource consuming change in the Java Language Framework and Java Virtual Machine.

To start off if we look at all the features available in Java 9, we can list the followings;

102: Process API Updates
110: HTTP 2 Client
143: Improve Contended Locking
158: Unified JVM Logging
165: Compiler Control
193: Variable Handles
197: Segmented Code Cache
199: Smart Java Compilation, Phase Two
200: The Modular JDK
201: Modular Source Code
211: Elide Deprecation Warnings on Import Statements
212: Resolve Lint and Doclint Warnings
213: Milling Project Coin
214: Remove GC Combinations Deprecated in JDK 8
215: Tiered Attribution for javac
216: Process Import Statements Correctly
217: Annotations Pipeline 2.0
219: Datagram Transport Layer Security (DTLS)
220: Modular Run-Time Images
221: Simplified Doclet API
222: jshell: The Java Shell (Read-Eval-Print Loop)
223: New Version-String Scheme
224: HTML5 Javadoc
225: Javadoc Search
226: UTF-8 Property Files
227: Unicode 7.0
228: Add More Diagnostic Commands
229: Create PKCS12 Keystores by Default
231: Remove Launch-Time JRE Version Selection
232: Improve Secure Application Performance
233: Generate Run-Time Compiler Tests Automatically
235: Test Class-File Attributes Generated by javac
236: Parser API for Nashorn
237: Linux/AArch64 Port
238: Multi-Release JAR Files
240: Remove the JVM TI hprof Agent
241: Remove the jhat Tool
243: Java-Level JVM Compiler Interface
244: TLS Application-Layer Protocol Negotiation Extension
245: Validate JVM Command-Line Flag Arguments
246: Leverage CPU Instructions for GHASH and RSA
247: Compile for Older Platform Versions
248: Make G1 the Default Garbage Collector
249: OCSP Stapling for TLS
250: Store Interned Strings in CDS Archives
251: Multi-Resolution Images
252: Use CLDR Locale Data by Default
253: Prepare JavaFX UI Controls & CSS APIs for Modularization
254: Compact Strings
255: Merge Selected Xerces 2.11.0 Updates into JAXP
256: BeanInfo Annotations
257: Update JavaFX/Media to Newer Version of GStreamer
258: HarfBuzz Font-Layout Engine
259: Stack-Walking API
260: Encapsulate Most Internal APIs
261: Module System
262: TIFF Image I/O
263: HiDPI Graphics on Windows and Linux
264: Platform Logging API and Service
265: Marlin Graphics Renderer
266: More Concurrency Updates
267: Unicode 8.0
268: XML Catalogs
269: Convenience Factory Methods for Collections
270: Reserved Stack Areas for Critical Sections
271: Unified GC Logging
272: Platform-Specific Desktop Features
273: DRBG-Based SecureRandom Implementations
274: Enhanced Method Handles

Modular JDK feature is just a small part of it but is a significant one, in this part I am planning to talk about it in depth with some coding examples;

The Why of Modular JDK?

The main reason for a Modular JDK was to avoid Classpath Hell. People who have experience Deploying Java Applications in Production or Pre Production Environments would agree that creating and maintaining the Classpath for a Java Application with the focus to enable future maintenance is a Nightmare and can become a Hell very soon if not done properly. 

OSGi Framework tried to address this by creating an Engine in which Classloader Isolated Bundles can run with Strict Contracts to Export and Import Dependency. This was a good approach to solve Classpath hell but the problem was an OSGi Bundle is a Jar on Steroids (Had to have a META-INF/MANIFEST.MF Bundle Descriptor). So Normal Jar Files had to be converted to a OSGi Bundle before those can be used inside an OSGi Engine. Making OSGi Bundles of already existing millions of Jars and maintaining them through latest versions in its entirety could be classified as Hell. So OSGi didn't catch up to its fullest potential. 

Plus JVM Classpath scanning was Sequential where when a Class is requested by an Application running in JVM it will scan the Classpath Jars one after the other until it finds the requested Class. This is an O(n) problem which needed to be addressed to improve performance.

The Official Java Team had to come up with something off the shelf. This is the reason Why Modular JDK was created with all the difficulties.

The How of the Modular JDK?

Up until Java 8, there were only 2 concepts of code containers; 

1. Package (Public to all)
2. Class inside Package (default or package private, Public to all)

From Java 9 there are 3 concepts of code containers;

1. Module (A module is group of packages with a Module Descriptor)
2. Package inside Module (Public to All modules which can access the module contain the package)
3. Class inside Package (Public within module, Public to certain modules, Public to all modules)


How to create a Module in Java 9?

A module is a same old Java Jar file with a Module Descriptor. A Module Descriptor is a Java Source file with the name module-info.java residing in the root of the source directory. 

The Structure of the Module Descriptor


References


Monday, July 31, 2017

Launching https://www.techtalks.lk

I have been quiet for sometime and didn't write much because I was busy building https://www.techtalks.lk. This is a platform for Sri Lanka IT Industry Experts and New Comers, Prospective New Comers to come together and share knowledge, expertise and best practices to improve the quality and standard of the IT Industry.

As someone who is part of the Sri Lankan IT Industry for almost a decade now, I experienced the huge gap between what New Comers, University Students learn and what really happens in the IT Industry. Many struggle to catch up the high demands of the Sri Lankan IT Industry it terms of Technical Knowledge, Best Practices, Communication, Interview Facing etc. 

Thus born Techtalks.lk which facilitates Real Technical Experts of the Sri Lanka IT Industry and New Comers, Prospective New Comers to the Sri Lankan IT Industry to come together to one platform to get things straightened out. 

The launching is just the beginning and there is a long way to go but as the famous quote states, 

"A journey of a Thousand Miles begins with a Single Step" - Lao Tzu


Monday, June 12, 2017

My Visit to Belgium and Europe

I was invited by my Client in Belgium (Immoweb) for an On Site visit to their Head Quarters in Brussels, Belgium. This was a dream come true for me as I have been wanting to visit Europe since I was a Teenager. I was invited by the CTO as he was about to leave the Company and There were crucial tasks which needed to be handled before he left and which needed my physical presence in the company. During that time I have been working for Immoweb as a Remote Employee for more than one and a half years. The timing was really nice and early notification of this visit from my client enabled me to plan my trip and make some travel arrangements to visit some neighboring countries of Belgium also. I flew to Belgium via transit at Doha, Qatar on Qatar Airways on Friday the 3rd of February 2017 and Landed in Belgium early hours of Saturday the 4th of February 2017. I stayed there till Monday the 13th of February 2017.




I was picked from the Airport by Sudesh, A Sri Lankan Colleague who is residing in Belgium on Saturday and he drove me to Hotel Izan Avenue, Louise in the center of Brussels. It was a great Hotel right in the middle of Brussels' most prestigious area. After I got myself checked-in in my room, Sudesh bid farewell to me. I spent sometime settling down in my room then I went out to roam around the city and to visit a famous park in the City by the name Jubel Park also called Parc du Cinquantenaire. I went to that park spent some time and co-incidentally the park had a Mosque, An Air Craft Museum and An Automobile Museum which I visited during my stay there.




On Sunday the 5th of February 2017, I traveled to Cologne, Germany which was some 221 Kilometers away. I traveled on the Bullet Train which enabled me to travel really fast and get to Cologne earlier than I expected. The city was one amazing experience. The city is built around the Rhine River and has a nice cool vibe to it. The people are super friendly and nice which enabled me to travel roam around the city. It was a day well spent!.

From Monday the 6th of February 2017 to 10th of February I was in Brussels, Belgium working at Immoweb. My Colleagues at Immoweb were super friendly and took me to a different restaurant everyday for lunch. They liked the presence of me around them as I am being the First Sri Lankan most of them have met so everyday the conversations were good. It gave me a nice time to bond with them which has greatly improved my work as well. And after Office Hours, I went through the Brussels City Monuments whenever I could. One night I visited Grand Palace, which is the Central Square of Brusells, an area filled with Good Restaurants, Markets etc. It also has photogenic buildings and some of those are categorized as UNESCO World Heritage Sites. On another night I went to the Atomium which is considered as the Symbol of Brussels City.





On Saturday 11th February 2017 I got the chance to visit Paris, France. This was one of the most memorable days of my life as I was a huge fan and admirer of the engineering behind Eiffel Tower. I was lucky enough not only to see it in person but go to the top most of the tower and see the Paris City from there. It was a great experience which I advise everyone to experience at least once. I also visited Arc de Triomphe.





On Sunday 12th of February 2017, I traveled to Amsterdam, Netherlands which was also amazing City to visit. Amsterdam is a worth traveling if you love Civil Engineering. It shows the Effort the Early Engineers have put in order to Build A highly Thriving City on top of Wooden Piles on ground soil which is too soft to hold huge buildings. The reason behind Buildings in Amsterdam being narrow and tall instead of short of wide was also due to this, The Municipality has taxed Each building based on the width. It was overall an amazing city to visit and learn about.




That was weekend was arguably the longest weekend in my life as I traveled to two new countries and close to 510 Kilometers. And on Monday the 13th of February 2017 I bid farewell to Belgium and Europe and Boarded my flight bound to Sri Lanka via Doha, Qatar on Qatar Airways around 4.15 p.m. Belgium time with a lot memories and thirst to come back again. I learned a lot from this visit and it has opened my eyes to see the World in a whole new perspective. Meeting new people, Going to new places certainly has a deep impact on your soul and I advise everyone to visit these places and more places at least once in their life time.



Tuesday, January 10, 2017

Spring data JPA with dynamic where clause

It may be a Common Use Case where you need to dynamically submit a WHERE Clause to a Spring Data Repository to get an Entity instead of Writing findBy* methods or @Query annotated methods. You can do the following;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by shazi on 1/11/2017.
 */
@NoRepositoryBean
public interface IBaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

    T findOne(String filter, Map<String, Object> params);

}
And Implement it as follows.
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.Map;

/**
 * Created by shazi on 1/11/2017.
 */
public class BaseRepositoryImpl<T, ID extends Serializable>
        extends SimpleJpaRepository<T, ID> implements IBaseRepository<T, ID> {

    private final EntityManager entityManager;

    private final JpaEntityInformation entityInformation;

    public BaseRepositoryImpl(JpaEntityInformation entityInformation,
                            EntityManager entityManager) {
        super(entityInformation, entityManager);

        // Keep the EntityManager around to used from the newly introduced methods.
        this.entityManager = entityManager;
        this.entityInformation = entityInformation;
    }

    @Override
    public T findOne(String filter, Map<String, Object> params) {
        final String jpql = "FROM " + entityInformation.getEntityName() + " WHERE " + filter;
        Query query = entityManager.createQuery(jpql);
        for (Map.Entry<String, Object> value:params.entrySet()) {
            query.setParameter(value.getKey(), value.getValue());
        }
        return (T) query.getSingleResult();
    }
}
And configure it as follows
@Configuration
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class)
@EnableTransactionManagement
public class RepoConfig {
or in XML
<repositories base-class="….BaseRepositoryImpl" />
Finally you can use it as follows;
User found = userRepository.findOne("name = :name", Collections.singletonMap("name", "name"));
But you have to make sure that your query WHERE is such that the Query will always return 1 result only.

References 


Monday, December 26, 2016

Hello, World of Google AndroidThings IoT Platform with Raspberry Pi 3

Earlier this month Google Announced AndroidThings a Platform with a Stripped down version of Android Operating System which can be run on multiple existing IoT Enabled Hardware like Raspberry Pi 3, Intel Edison and NXP Pico. On the same day I took this picture;



AndroidThings is primarily focused on reducing the development and deployment time when it comes to IoT. Android being the most popular Mobile Operating System thus far has now made its way into the IoT arena with AndroidThings IoT Platform.

The purpose of this article is to try out the AndroidThings platform and evaluate its Developer friendliness.

Flashing the AndroidThings Operating System

This can be considered as the First Step of the whole process of developing apps for AndroidThings IoT Platform. With your Hardware Device Raspberry Pi 3, Intel Edison or NXP Pico you can download the correct System Image from https://developer.android.com/things/preview/download.html

And you can follow the Hardware Device's instruction on how to flash the System Image into the Hardware Device. I used a Raspberry Pi 3 so I followed the Instruction available for that.

Setting Up Development Environment

If you were an Android Developer already just like me, you probably have Development Environment setup already and you can skip this step. If not you can Download Android Studio from https://developer.android.com/studio/index.html and install it. Make sure to download the package with Android SDK if you are not comfortable with configuring PATH manually.


Configuring the AndroidThings Operating System to use Wifi

This is the third step as it enables to connect to your device over Wifi for Easy Deployment and Debugging of Apps. I followed this https://developer.android.com/things/hardware/raspberrypi.html as I used a Raspberry Pi 3 but you can follow the correct link based on your Hardware listed here https://developer.android.com/things/preview/index.html.

Connecting to AndroidThings Operating System via adb command

After finding the Ip Address of the Device you can issue the following command to connect to the AndroidThings operating system via adb command

adb connect <ip-address>

This will connect the your development machine and Hardware device. Now it is just like developing any Android App. You can just use the Android Studio IDE to create apps and run it just like you run an app in an Android Mobile Phone.

Demo (Blink and Button App)





Evaluation of the AndroidThings IoT Platform

Pros

  1. Hardware Device Agnostic
  2. Very Gradual Learning Curve (If you are familiar with Android App Development)
  3. Ability use many many Android Tutorials available
  4. Great Community of Developers backed by Google

Cons

  1. Limited no of Hardware Devices supported yet (Only 3 when writing this)
  2. Lack of Libraries for reading Sensors like DHT22 and many more in Java. (It would take some time to on board all sensor libraries to Java)
  3. A Steep Learning Curve (If you are not familiar with Android App Development)

Conclusion

Google AndroidThings IoT Platform is definitely a step in the right direction. There are so many Hardware Devices and Operating Systems for IoT it is almost too easy to get lost. With this Platform Google has tried to bring in the "Write Once, Run on Any Hardware" concept to IoT arena which must be commendable. But as far as I think it is still a work in progress and I am pretty sure with the resources Google has within couple years time this platform has the potential to become a Game changer and backbone of IoT.

Trackbacks/Pings

Tuesday, November 8, 2016

RFID Parking Solution using Raspberry Pi

Sri Lanka has a lot of Parking Lots Managed by RDA (Road Development Authority) but almost all use paper receipts to maintain Timings of Parked Vehicles. This process is error prone and difficult to keep on days where vehicle come and go out frequently.

This motivated me to do a small PoC on a Low Cost RFID based Parking Solution. I had a HZ-1050 RFID Reader lying around in my Hack Space and 1 RFID Card with 2 RFID Tags so I decided to use those.



Above is the Setup which is really simple, Just need to wire up Raspberry Pi 5v -> HZ-1050 5v, Raspberry Pi Ground -> HZ-1050 Ground, HZ-1050 D0 -> Raspberry Pi Pin 14, HZ-1050 D1 -> Raspberry Pi Pin 15

Finally the Code.

Reading RFID Card/Tag and Storing in Database

I used PiGPIO Library with a Small a Python Script modified from their Example to read the Wiegand Format RFID No and Store it in a SQLite3 Database.


Displaying in Dashboard


I wrote a small Dashboard which has a PHP backend and runs on Light Web Server and connect to the same SQLite3 Database to display data. The Front End Bootstrap with JQuery and Javascript.

Following is a Demonstration on everything working together. I will upload code once I clean those up. This can store different Vehicle Types and their Charges for the First Hour and Additional Hours and Calculate the Duration and Charge. Which will be great for the Parking Operator.



References
  1. http://www.penguintutor.com/linux/light-webserver
  2. http://abyz.co.uk/rpi/pigpio/
Parts can be bought at IoT Researcher Shop
  1. HZ-1050 RFID Reader - http://shop.iotresearcher.com/index.php/en/sensors-c-1/hz-1050-rfid-reader-p-9
  2. RFID Card - http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-card-p-7
  3. RFID Tag - http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-tag-p-6
Trackbacks/Pings
  1. Hackaday - http://hackaday.com/2016/11/21/faulty-parking-meter-tracking-system-rfid-to-the-rescue/
  2. BricoGeek - http://blog.bricogeek.com/noticias/raspberry-pi/control-de-parking-con-rfid-y-raspberry-pi/