Wednesday, April 10, 2019

Dynamic Traits with Java

Traits are reusable components representing a set of methods or behaviors that can be used to extend the functionality of multiple classes. Some programming languages like Groovy and Scala have been supporting Traits for a long time, and allow dynamic association of Traits to any object, at runtime.

Java being a strongly typed programming language, enforces a very strict discipline of type association, upfront whilst declaring any language element, e.g. variables, functions, etc; and the same is applicable for Classes too. Therefore when you implement a class in Java, the associative types for the class, must be specified as a part of the declaration of the class, which strongly binds the type interfaces to the class, and cannot be changed in runtime.

The concept of Traits is implemented in Java using Interfaces, which contain default methods and variables to allow maintaining an object's state. However, these interfaces then have to be tightly coupled with the class declaration, and as discussed above one cannot dynamically modify an interface association.

Java-DynaTraits is a very simple and minimalist library, that allows dynamic association of Traits/Interfaces; such that new interfaces could be added to your class at runtime, without really having to specify them, with the declaration of the class.

The core concept behind the code is to use a subscription model, which allows registering interfaces (or in other words Traits) with a class, in runtime, and then using an Invocation Handler to delegate function calls to the relevant subscriptions.

An Example Code

Following is a sample code, that demonstrates an example usage -


var catalog = (CatalogInterface & NamedEntity & PricedEntity) 
(new Catalog()).withTraits(new NamedEntity(){}, new PricedEntity(){});

// The setName function is rendered by the "NamedEntity" trait.
catalog.setName("Dynamic Traits with Java");
System.out.println(catalog.getName());

// The setPrice function is rendered by the "PricedEntity" trait
catalog.setPrice(20.00);
System.out.println(catalog.getPrice());


Please visit the project page on Github for further details.

Wednesday, September 28, 2011

Java 7, brings a simple yet a long awaited feature...

Would you be surprised to know that the API of java.io.File only supported getting the last modified time, and NOT the file's creation time, until Java 7...? Well, not many people seem to be aware of this and surprisingly even the Internet was very quiet on this topic all the while.

So if you are using anything below Java 7 and are desperate to fetch a file's creation time then, one solution would be to write some native code to call system routines and then call the native code using JNI. Most of this work seems to be already done for you in a library called JNA, though. Nevertheless, you will still need to do a little OS specific coding in Java for this, though, as you'll probably not find the same system calls available in Windows and Unix/Linux/BSD/OS X.

Monday, July 25, 2011

VMWare Cloud Foundry - Developing "cloud-ready" web applications has never been so easy

A lot has been said and done about services on the cloud lately and "Platform as a Service" (PaaS) is probably one of the most popular buzz-word that's been around for quite sometime now. However, I sincerely feel that the common man (read as "developer") is yet to get a taste of what it really means. Now, this very paradigm seems to be soon changing, with VMWare Cloud Foundry. Cloud Foundry is the world’s very first open Platform as a Service (PaaS). It is designed to help developers easily create web applications, using multiple programming frameworks including Spring for Java, Ruby on Rails and Sinatra for Ruby; that can run upon public and private cloud environments, with just about no additional learning curve. So, by now if you are dreaming of a possibility to port your existing web-applications to the cloud with minimal efforts, then let me give you the good news - "it's all possible here..."

Well, looks like i've done a lot of sales talks in favour of VMWare Cloud Foundry. Now lets get under the hood and get hands-on with developing an extreamly simple web application that demonstrates the following -

1. Setting up VMWare Cloud Foundry
2. Consuming Cloud Foundry's MySQL service in a JSP, using the standard JDBC way
3. Deploying a web archive and running the JSP on Cloud Foundry

Setting up VMWare Cloud Foundry

Windows Setup

  1. Apply for a Cloud Foundry account at www.cloudfoundry.com; you will be notified by email when your account is activated
  2. Install Ruby from www.rubyinstaller.org; The Cloud Foundry cloud-controller command line is built in Ruby so this is required. As the installer runs, make sure to check the boxes to add the ruby directory to your command path
  3. Start the Windows command line client (Start Menu -> Run -> "cmd")
  4. To install the cloud-controller tool type : gem install vmc (If you are behind a firewall then : gem install --http-proxy http://proxy.vmware.com:3128 vmc)
Congratulations! The Cloud Foundry Cloud Controller is now installed. From here on, you may type Cloud Foundry commands into the Windows command window.

  1. Inform Cloud-Foundry which cloud you want to connect to : vmc target api.cloudfoundry.com
  2. To login to Cloud Foundry type : vmc login (enter your account credentials when prompted)

Linux Setup

The document here is the best source of information for setting up the cloud-controller upon various Linux flavours.

Consuming Cloud Foundry's MySQL service

There are plenty of elaborate tutorials available on the web, that illustrate accessing the MySQL service at Cloud foundry using Spring. However, this one is a bit different, because it does not use Spring at all and demonstrates the same functionality with plain and simple Java and standard JDBC API.

The following code snippet demonstrates the approach -

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="java.sql.*,javax.sql.*"%>
<%@page import="org.cloudfoundry.services.*"%>
<%
String query = "Select * FROM users";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cloud Foundry with Simple Java</title>
</head>
<body>
Welcome to the simple web application on cloud-foundry...
<%
Connection connection = null;

try {
// establish connection to MySQL Service
ServiceManager services = ServiceManager.INSTANCE;
connection = (Connection) services.getInstance(CloudFoundryServices.MYSQL);

if (connection != null && !connection.isClosed()) {
out.println("<p>Successfully connected to MySQL service</p>");

// creating a database table and populating some values
Statement s = connection.createStatement();
int count;
s.executeUpdate("DROP TABLE IF EXISTS animal");
s.executeUpdate("CREATE TABLE animal ("
+ "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
+ "PRIMARY KEY (id),"
+ "name CHAR(40), category CHAR(40))");

out.println("<p>[1] Table successfully created.</p>");

count = s.executeUpdate("INSERT INTO animal (name, category)"
+ " VALUES"
+ "('snake', 'reptile'),"
+ "('frog', 'amphibian'),"
+ "('tuna', 'fish'),"
+ "('racoon', 'mammal')");

out.println("<p>[2] " + count + " rows were inserted.</p>");

count = 0;
ResultSet rs = s.executeQuery("select * from animal");
while (rs.next()) {
count++;
}
out.println("<p>[3] " + count + " rows were fetched.</p>");

s.close();
}
} catch (Exception e) {
out.println(e.getMessage());
} finally {
if (connection != null && !connection.isClosed()) {
connection.close();
}

connection = null;
}
%>
</body>
</html>

In the above code, org.cloudfoundry.services is a custom package which primarily contains a Singleton implementation, namely "ServiceManager"; and an interface to hold the constants, namely "CloudFoundryServices". Following are the sources for the same -

CloudFoundryServices.java


package org.cloudfoundry.services;

public interface CloudFoundryServices {
public static final int MYSQL = 1;
}

ServiceManager.java


package org.cloudfoundry.services;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import argo.jdom.JdomParser;
import argo.jdom.JsonNode;
import argo.jdom.JsonRootNode;

public enum ServiceManager implements CloudFoundryServices {

INSTANCE;

private static final String NULL_STRING = "";

public Object getInstance(int service_type) throws Exception {
if (service_type == MYSQL) {
return getMySQLConnection();
} else {
throw new IllegalArgumentException("Service for id " + service_type + " not found...");
}
}

/*
* This method is responsible for establishing a valid connection to the MySQL service,
* using the credentials available in the environment variable, namely "VCAP_SERVICES".
*
* The content of VCAP_SERVICES environment variable is a JSON string, thus this method
* uses standard interfaces from the Argo JSON parsing API to extract the credentials.
*/

private Object getMySQLConnection() throws SQLException {
String vcap_services = System.getenv("VCAP_SERVICES");

String hostname = NULL_STRING;
String dbname = NULL_STRING;
String user = NULL_STRING;
String password = NULL_STRING;
String port = NULL_STRING;

if (vcap_services != null && vcap_services.length() > 0) {
try {
JsonRootNode root = new JdomParser().parse(vcap_services);

JsonNode mysqlNode = root.getNode("mysql-5.1");
JsonNode credentials = mysqlNode.getNode(0).getNode("credentials");

dbname = credentials.getStringValue("name");
hostname = credentials.getStringValue("hostname");
user = credentials.getStringValue("user");
password = credentials.getStringValue("password");
port = credentials.getNumberValue("port");

String dbUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbname;

Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(dbUrl, user, password);
return connection;
} catch (Exception e) {
throw new SQLException(e);
}
}

return null;
}
}

Now all you need to do now is, bundle everything together and get yourself a standard WAR (web archive) file.

Deploying a web archive and running the JSP on Cloud Foundry

Once you have the WAR file in place, browse to the location where the file is placed, using your command-line console; and execute the following cloud-controller commands -
  • In case you are not yet logged in to the cloud, type : vmc login (and, provide the required credentials when prompted)
  • vmc push
  • Enter a unique application name when prompted, could be anything like in my case its my name "rahulr"
  • Select 'Y' when prompted to bind a service and select the MySQL service from the offered list of services
Finally the status "Starting Application : OK", at the end of the command execution, indicates a successful deployment. So, now you are all set to start-up a browser and fire-up your first "cloud-enabled" web application, at the url <app-name>.cloudfoundry.com. Optionally, you might want to check the one that I've hosted at http://rahulr.cloudfoundry.com

Thats all for now, I hope you liked the post and should you have any issues getting this up and running feel free to reach out to me.

Additional Resources

Following are some links which I found useful -

Thursday, July 21, 2011

Demystifying Enterprise Architecture with TOGAF - Understanding the Architecture Development Methodology (ADM)


In the previous article, we glanced through Enterprise Architecture as a whole and also discussed the need for an Enterprise Architecture Framework, like TOGAF.

With this article we shall continue exploring TOGAF further, and discusses TOGAF's Architecture Development Methodology (ADM).

The Architecture Development Methodology (ADM)

The Architecture Development Methodology (ADM) provides a proven and repeatable process for developing architectures.

The Scope

The scope of ADM includes or encompasses the below listed activities, which are generally carried out in iterative cycles of continuous architecture definition and realization; thus aiding a controlled transformation of an enterprises in response to business goals and opportunities -
  • Establishing an architecture framework
  • Developing architecture content
  • Transitioning
  • Governing the realization of architectures

Implementation Phases

The implementation of ADM could be envisioned across the following phases -


Preliminary Initiation
The Preliminary Initiation phase describes the preparation and initiation activities, required for meeting the business directive for a new enterprise architecture, including the definition of an Organization-Specific Architecture framework and the definition of core principles.

Architecture Vision Setup
The Architecture Vision Setup phase describes the initial phase of an architecture development cycle. It includes information about the following activities -
  • Defining the scope
  • Identifying the stakeholders
  • Creating the architectural vision statement
  • Obtaining the initial approval

Business Architecture Evolution
The Business Architecture Evolution phase describes the development of a Business Architecture and its alignment to support an agreed Architecture Vision.

Information Systems Architecture Definition
The Information Systems Architecture Definition phase describes the development of Information Systems Architectures required to support the Architecture Vision. This phase typically involves -
  • Identification and development of Data Architectures
  • Development of various Application Architectures

Technology Architecture Evolution
The Technology Architecture Evolution phase describes identification and development of the requisite Technology Architecture, with respect to supporting the Information Systems Architecture.

Opportunities & Solutions Identification
The Opportunities & Solutions Identification phase typically involves the following activities -
  • Conducting initial implementation planning
  • Identifying single or multiple delivery channels for the requisite architectures

Migration Planning
The Migration Planning phase addresses the formulation of a set of detailed sequence of transition architectures with a supporting Implementation and Migration Plan.

Implementation Governance
The Implementation Governance phase involves providing an architectural oversight of the implementation.

Tuesday, July 19, 2011

Demystifying Enterprise Architecture with TOGAF

Lately I've been studying/reading The Open Group Architecture Framework (TOGAF), and I simply could not resist from appreciating the structural approach that it introduces towards envisioning and capturing Enterprise Architecture. I therefore thought of initiating a series of articles herein, with the sole purpose of sharing, simplifying and promoting the framework especially amongst the architecture aspirants out there who follow and read my blog.

This write-up, which is the first one in the series, intends to provide a brief overview on Enterprise Architecture and then further goes on to illustrate the need for an Enterprise Architecture Framework.

Preface

ISO/IEC 42010: 2007 defines "architecture" as:

"The fundamental organization of a system, embodied in its components, their relationships to each other and the environment, and the principles governing its design and evolution."

TOGAF embraces but does not strictly adhere to ISO/IEC 42010: 2007 terminology. In TOGAF, "architecture" has two meanings depending upon the context:

  • A formal description of a system, or a detailed plan of the system at component level to guide its implementation
  • The structure of components, their inter-relationships, and the principles and guidelines governing their design and evolution over time
Understanding Enterprise Architecture

The Definition of Enterprise

The term "enterprise" refers to any collection of organizations that has a common set of goals. For example, an enterprise could be a government agency, a whole corporation, a division of a corporation, a single department, or a chain of geographically distant organizations linked together by common ownership.

It is important to here note that the term "enterprise" in the context of "enterprise architecture" can be used to denote both [1] an entire enterprise as a collection encompassing all of its information and technology services, processes, and infrastructure — and [2] a specific domain within the enterprise. Nevertheless in both cases, the architecture crosses multiple systems and multiple functional groups within the enterprise.

Defining Enterprise Architecture

Enterprise architecture is a structural approach that optimizes the often fragmented organization wide processes (both manual and automated) into an integrated environment that is responsive to change and enables delivering the enterprise's business strategy.

Domains of Enterprise Architecture

There are four architecture domains that are commonly accepted as subsets of an overall enterprise architecture, all of which TOGAF is designed to support.
  • Business Architecture
    The Business Architecture defines the business strategy, governance, organization, and key business processes.

  • Data Architecture
    The Data Architecture describes the structure of an organization’s logical and physical data assets and data management resources.

  • Application/Solution Architecture
    The Application or Solution Architecture provides a blueprint for the individual application systems to be deployed, their interactions, and their relationships to the core business processes of the organization.

  • Technology/Deployment Architecture
    The Technology or Deployment Architecture describes the logical software and hardware capabilities that are required to support the deployment of business, data, and application services. This includes IT infrastructure, middleware, networks, communications, processing, standards, etc.

Understanding TOGAF

The Open Group Architecture Framework (TOGAF) provides the core methods and tools for assisting in the acceptance, production, use, and maintenance of an enterprise architecture. It is based on an iterative process model supported by best practices and a re-usable set of existing architecture assets.

Illustrating the need for TOGAF

Architecture design is a technically complex process, and the design of heterogeneous, multi-vendor architectures is particularly complex. TOGAF plays an important role in helping to de-mystify and de-risk the architecture development process. TOGAF provides a platform for adding value, and enables users to build genuinely open systems-based solutions to address their business issues and needs.

Saturday, May 21, 2011

Easy 3D with WebGL and Three.js

Lately a lot has been happening in 3D around the world. From movies to electronic gadgets like televisions, projectors and gaming consoles, everything seems to be heading toward rendering that additional 3rd dimension. A 3D experience in itself so very enticing that one would just falling it love with it; and thus the creative ones amongst us are constantly thinking about various areas wherein it could be applied. Some of the obvious areas of application would be bio-technology, clinical research, engineering drawings and entertainment media; but meanwhile some creative minds amongst us have gone ahead thinking differently and even made simple software applications that we use in our everyday lives, to render a user interface with 3D. For instance, a few months ago I came across a program called 3DNA Desktop which improves the way we work with Windows and the Web. With 3DNA Desktop one can choose from different 3D worlds to explore and customize the same, to create an immersive and entertaining 3D desktop usage experience.

Screenshot of 3DNA Desktop

As a developer, I've always thought about how these awesome experiences are created. For a while I did try to play around with Adobe Flash, but to by candor found it too exhaustive to understand in a short time; therefore all the time continued to wonder, if there is something simpler, and above all free of cost, which could yield the same results or even better. Until recently, that I came across WebGL.

WebGL is a cross-platform, royalty-free web standard for a low-level 3D graphics API based on OpenGL ES 2.0, exposed through the HTML-5's Canvas element in a Document Object Model (DOM). So, what it essentially means is - now you can have Adobe Flash like experiences created within standard HTML using the Canvas element. You must have seen so many fancy user experiences built atop Adobe Flash, so obviously are wondering how exactly does WebGL compare against Adobe Flash? Therefore, to augment your curiosity here is an example that I built to demonstrate the capabilities WebGL, which shows a real-time day light view of our planet. Just try interacting with the globe using your mouse or touch-pad to get the real feel of the thing. Meanwhile, a quick note about the support – you need to make sure that your browser supports WebGL content rendering for running this example. Ideally one would need Google Chrome, Firefox 4+ or IE 9+ to run this one at its best.

Screenshot of the WebGL and three.js example

A software programming enthusiast would furthermore be amazed to know that this is entirely done using standard Javascript and Mr.Doob's awesome script library called three.js, in just over 80 lines of code (LoC); you might want to right-click and choose to view the source on the example page to have a look at the code for yourselves. Three.js is a framework library developed by Mr. Doob that skillfully abstracts away the complex mathematics involved in implementing 3D, and provides simple APIs with which you can create cameras, objects, lights, materials and more; with a choice of renderers, which means you can decide if you want your scene to be drawn using HTML-5's canvas, WebGL or SVG within a standard browser.

Now, should this little article and the example inspire you to get your hands dirty with some code. The best place to start would be the three.js sources and bundled examples. You may download the latest release of three.js from GitHub (https://github.com/mrdoob/three.js) and get started with simple examples in absolutely no time. :)

Wishing you happy exploration of WebGL and three.js...

Have fun!!!

Monday, July 19, 2010

Table2Visualization : Rendering Google Visualization Charts with HTML Tables

With the advent of the analytics age, one would find applications spring-up every month that lets you track, monitor, and analyze just about anything, from human habits to system behaviors. Such applications generally have a standard implementation pattern wherein they primarily collect, upload, process, and display data. Now, somebody trying to develop one such application would obviously face challenges, of different kinds, at each of these points depending upon the domain of applicability. However, the challenges in displaying the data, in a way that it is understandable to non-professionals, are the same for all of them.

Oftentimes, we rely upon statistical charts and graphs to render such data. These days one would find a plenty of charting libraries that make this possible easily (and some of them without any cost), for example Visualize, MooCharts, ProtoChart etc. One such player in the world of data visualization is Google itself.

Google Visualization is a set of JavaScript libraries that offer a wide range of data representation options with an easily pluggable API. It enables a developer to render statistical charts and graphs from a wide range of data-sources. However, as a developer, I always felt that a very simple and basic data source was always missing in that exhaustive list of data sources; and that is HTML tables. My searched across the web for an answer, either resulted in vein or a complex process with involved importing the data in a Google Docs spreadsheet and then rendering a chart from there. In simple words, there was nothing “Simple and Sweet”.

So, here I had a problem in-hand and decided to solve it for myself and all my fellow developers around the world. The implementation is now available on Google Code at the following location, with a sample to help you jump-start

http://code.google.com/p/table2visualization