Tuesday, October 8, 2013

PHP-In-Java : Execute PHP code from within Java


Introduction :
Years ago an enterprising company named Caucho decided to write the "most reliable application server in the Open Source market." Their core product is a Java EE compliant Web server they call Resin, and at its heart is Quercus: a fast PHP interpreter written in cross-platform Java.

What is Quercus :
Quercus is pioneering a new mixed Java/PHP approach to web applications and services. On Quercus, Java and PHP is tightly integrated with each other - PHP applications can choose to use Java libraries and technologies like JMS, EJB, SOA frameworks, Hibernate, and Spring. This revolutionary capability is made possible because 1) PHP code is interpreted/compiled into Java and 2) Quercus and its libraries are written entirely in Java. This lets PHP applications and Java libraries to talk directly with one another at the program level. To facilitate this new Java/PHP architecture, Quercus provides an API and interface to expose Java libraries to PHP.

Benefits of Quercus :
Quercus and Quercus' PHP libraries are written entirely in Java, thereby taking the advantages of Java applications and infusing them into PHP. PHP applications running on Quercus are simply faster, easier to develop, more capable, more secure, and more scalable than any other PHP solution.
Quercus gives both Java and PHP developers a fast, safe, and powerful alternative to the standard PHP intepreter. Developers ambitious enough to use PHP in combination with Java will benefit the most from what Quercus has to offer.

Share:

Friday, September 20, 2013

Snaq DBPool Connection Pool : Java Connection Pooling Part - 2


What is Connection Pooling? 

One of the most expensive database-related tasks is the initial creation of the connection. Once the connection has been made the transaction often takes place very quickly. A connection pool maintains a pool of opened connections so the application can simply grab one when it needs to, use it, and then hand it back, eliminating much of the long wait for the creation of connections.

There are so many Connection Pooling library available in Java, Some of them are as follows ;
  • Tomcat JDBC Connection Pool
  • Snaq DBPool
  • C3P0
  • BoneCP
We have already gone through with Tomcat JDBC Connection Pool example, today we will check Snaq DBPool connection pooling. 

What is DBPool?

A Java-based database connection pooling utility, supporting time-based expiry, statement caching, connection validation, and easy configuration using a pool manager.

How do I use it?

To use DBPool you need to have the JAR file in a location where it's available for use by the host system you are using. For standalone applications this usually means in the CLASSPATH, and for application servers there is usually a specific directory is recommended for JAR libraries (for example, when used with Apache Tomcat it can be placed in the /WEB-INF/lib directory).
Usually DBPool is used in two different ways:

1. Direct use of individual connection pools.
2. Using the ConnectionPoolManager to manage one or more connection pools.

If you have never used DBPool before it's recommended that you start by simply integrating asingle connection pool into your application/applet to see how it works and performs. This provides the simplest direct support for pooled database connections and will get you up and running quickly. Once you learn exactly how it behaves and the benefits it can give you can try the pool manager approach to manage multiple pools if necessary.

Share:

Thursday, August 29, 2013

Tomcat JDBC Connection Pool : Java Connection Pooling Part - 1


What is Connection Pooling? 

One of the most expensive database-related tasks is the initial creation of the connection. Once the connection has been made the transaction often takes place very quickly. A connection pool maintains a pool of opened connections so the application can simply grab one when it needs to, use it, and then hand it back, eliminating much of the long wait for the creation of connections.

There are so many Connection Pooling library available in Java, Some of them are as follows ;
  • Tomcat JDBC Connection Pool
  • Snaq DBPool
  • C3P0
  • BoneCP
Today we will go through with Tomcat JDBC Connection Pool. I'll give a brief introduction with basic example (not in-depth).

Introduction :

JDBC connection pooling is conceptually similar to any other form of object pooling. Database connections are often expensive to create because of the overhead of establishing a network connection and initializing a database connection session in the back end database. In turn, connection session initialization often requires time consuming processing to perform user authentication, establish transactional contexts and establish other aspects of the session that are required for subsequent database usage.

Additionally, the database's ongoing management of all of its connection sessions can impose a major limiting factor on the scalability of your application. Valuable database resources such as locks, memory, cursors, transaction logs, statement handles and temporary tables all tend to increase based on the number of concurrent connection sessions.

All in all, JDBC database connections are both expensive to initially create and then maintain over time. Therefore, as we shall see, they are an ideal resource to pool.

you can checkout the detail description and features of JDBC Connection Pool here and here.

Share:

Thursday, August 22, 2013

Export or Backup MySQL Database in separate file using Shell Script


Live projects database are always critical for Clients and Companies and if the development phase is still in progress than you need to take backup of database on daily basis for safety concerns. i came across with the same situation and for that i've created a simple script which will take backup of your database.

it will export table wise separate sql file rather than one big SQL file. you can also make zip file for all the tables with gzipped option (dbname.gz).

This script is created for MySQL database only and it;s been tested on Mac OS X and Ubuntu OS.
You need to set path for MySQLDump and MySQL in script file.

Share:

Monday, August 19, 2013

Push Notification for iOS and Android with RESTlet Framework


Introduction :

Push notifications let your application notify a user of new messages or events even when the user is not actively using your application. On Android devices, when a device receives a push notification, your application's icon and a message appear in the status bar. When the user taps the notification, they are sent to your application. At iOS devices, it will appear as an alert box with buttons and header text and custom message.

Below are some prerequisites for Google Cloud Messaging (GCM) and Apple Push Notification Service (APNS).

1. GCM :
- Google Account.
- Register your application on GCM from Google APIs Console.
- Android API Key
- Android Device Push String (Android 2.2+)

2. iOS :
- iOS developer account
- Register your application on iOS developer account
- Download Push Notification P12 Certificate
- Add Device Profile on iOS developer account
- iOS device

Share:

Friday, August 2, 2013

Import MySQL .sql script from Java

Sometimes it is nice to programmatically run .sql scripts on a MySQL database using Java. This is easily achieved by using a simple ScriptRunner Class from the iBatis apache project. iBatis project is having it's own dependencies and if you want to check that class you need to add all the dependency. To overcome this issue i came across with a Slightly modified version of the com.ibatis.common.jdbc.ScriptRunner class from the iBATIS Apache project. it only removed dependency on Resource class and a constructor.

I made a simple java project which will execute the .sql script in local MySQL database.

Below are the steps to create new project and execute the .sql script in Java project.

Share:

Tuesday, June 4, 2013

Configure Apache Cassandra (Mac OS X)




Apache Cassandra Configuration Steps for Mac OS X:

1. Download And Install Apache Cassandra :
  • Download Apache Cassandra : http://cassandra.apache.org/download/
  • Now check weather JAVA_HOME and PATH environment variables has been set or not, if not then put this command in terminal 
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home"
export PATH="/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin:$PATH"
  • You also need to create three folders under /var/lib/cassandra/ folder for data, commit logs and saved caches, as follows.
/var/lib/cassandra/data
/var/lib/cassandra/commitlog
/var/lib/cassandra/saved_caches
  • Now start up Cassandra by invoking {cassandrafolderpath}$ bin/cassandra -f  from the command line.
  • Assuming you don't see messages with scary words like "error", or "fatal", or anything that looks like a Java stack trace, then everything should be working.
  • Press "Control-C" to stop Cassandra.
Share:

Thursday, May 23, 2013

Eclipse character encoding for Java projects

Introduction :

On one of my recent Java projects I had to work with a character encoding other than the default ANSI encoding. Thus, I managed to set up the database connection properly to use the required UTF-8 encoding, as well as handle the returned data correctly as UTF-8 in the Java code.

When I tried to output some of the data, for logging purposes, in the console, the output couldn’t not be read correctly. The first thought was to double-check the source code, connection properties etc, and despite the fact that everything looked just fine, the console output still was unreadable.

The default character encoding in Java for MAC OS X is MacRoman. The default font encoding on some other platforms is ISO-Latin-1 or WinLatin-1; unlike MacRoman, these encodings are subsets of UTF-8. Programs that assume that filenames can be turned into UTF-8 by just turning a byte into a char will cause problems in MAC OS X.

The simplest way to work around this problem is to specify a font encoding explicitly rather than assuming one. Specifying a font encoding besides UTF-8 and UTF-16 is not recommended.

After spending some time trying to find a relevant answer in google, i came to know that it was indeed in Eclipse, everybody knows Eclipse is an excellent IDE, however some of its preferences may not be that straight-forward. I stumbled upon the “Common” tab in the Configurations panel. And indeed this is where I found the answer to the encoding problem!

Share:

Monday, March 25, 2013

What's New In Java 8 : Default Methods (Defender Methods)



We all know that interfaces in Java contain only method declarations and no implementations and any non-abstract class implementing the interface had to provide the implementation. Lets look at an example :

public interface SimpleInterface {
  public void doSomeWork();
}
 
class SimpleInterfaceImpl implements SimpleInterface{
  @Override
  public void doSomeWork() {
    System.out.println("Do Some Work implementation in the class");
  }
 
  public static void main(String[] args) {
    SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl();
    simpObj.doSomeWork();
  }
}
Now what if I add a new method in the SimpleInterface?

public interface SimpleInterface {
  public void doSomeWork();
  public void doSomeOtherWork();
}
and if we try to compile the code we end up with:

$javac .\SimpleInterface.java
.\SimpleInterface.java:18: error: SimpleInterfaceImpl is not abstract and does not 
override abstract method doSomeOtherWork() in SimpleInterface
class SimpleInterfaceImpl implements SimpleInterface{
^
1 error
And this limitation makes it almost impossible to extend/improve the existing interfaces and APIs. The same challenge was faced while enhancing the Collections API in Java 8 to support lambda expressions in the API. To overcome this limitation a new concept is introduced in Java 8 called default methods which is also referred to as Defender Methods or Virtual extension methods.

Default methods are those methods which have some default implementation and helps in evolving the interfaces without breaking the existing code. Lets look at an example:

public interface SimpleInterface {
  public void doSomeWork();
   
  //A default method in the interface created using "default" keyword
  default public void doSomeOtherWork(){
    System.out.println("DoSomeOtherWork implementation in the interface");
  }
}

class SimpleInterfaceImpl implements SimpleInterface{
  @Override
  public void doSomeWork() {
    System.out.println("Do Some Work implementation in the class");
  }
  /*
   * Not required to override to provide an implementation 
   * for doSomeOtherWork.
   */
 
  public static void main(String[] args) {
    SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl();
    simpObj.doSomeWork();
    simpObj.doSomeOtherWork();
  }
}
and the output is:

Do Some Work implementation in the class
DoSomeOtherWork implementation in the interface
This is a very brief introduction to default methods. One can read in depth about default methods here.

References :



Share:

Wednesday, March 20, 2013

Selecting your Java Collections library


Introduction :

Java collections are one of the most commonly used data-structures by all Java professionals. But are you using the right collection class that would best suits your need. Most programmers usually use Vectors, ArrayList, HashMap or the Hashtable. There are many other collection classes available with the JDK that you can use instead of re-inventing logic to suite your needs.

We will be trying to understand the different types of classes and when each Collection class could be used. We wouldn't be looking into the implementation details of any collection, for that please refer the latest Java Collection API docs.

The Core Collection Framework Interfaces :

The core collection frameworks are depicted in the following image.

Java Collection Interfaces

Share:

Monday, March 18, 2013

Openfire : Installation and Configuration Guide




Overview :
Openfire is a powerful instant messaging (IM) and chat server that implements the XMPP protocol. This document will guide you through installing Openfire.

Download :
please visit the Openfire website to download latest Openfire server: http://www.igniterealtime.org/projects/openfire/

Files in the Distribution :
The files in your distribution should be as follows (some sub-directories omitted for brevity):

openfire/
 |- readme.html
 |- license.html
 |- conf/
 |- bin/
 |- jre/
 |- lib/
 |- plugins/
     |- admin/
 |- resources/
     |-database/
     |-security/
 |- documentation/
  • The conf directory is where Openfire stores configuration files.
  • The bin directory contains the server executables. Depending on which distribution you installed, different executables will be available.
  • The jre directory contains a Java 5 runtime that is bundled with the Windows and RPM versions of Openfire.
  • The lib directory contains libraries necessary for running Openfire.
  • The plugins directory contains server plugins. By default, Openfire ships with a web-based admin console plugin.
  • The resources/database directory contains SQL schema files to create new Openfire databases, as well as upgrade scripts for existing installations.
  • The resources/security directory is where Openfire maintains keystores to support SSL connection security.
  • The documentation directory contains server documentation.
Share:

Thursday, March 14, 2013

NoSQL - Offers and Gives Up


Deciding between a NoSQL database or a relational database system is about understanding the trade-offs that led to the creation of NoSQL to begin with. NoSQL systems have advantages over traditional SQL databases because they give up certain RDBMS features in order to gain other performance, scalability and developer usability capabilities.

What NoSQL gives up (this varies by NoSQL engine) :

  • Relationships between entities (like tables) are limited to non-existent. For example, you usually can't join tables or models together in a query. Traditional concepts like data normalization don't really apply. But you still must do proper modeling based on the capabilities of the particular NoSQL system. NoSQL data modeling varies by product and whether you are using a document vs column based NoSQL engine. For example, how you might model your data in MongoDB vs HBase varies because each solution offers significantly different capabilities.
  • Limited ACID transactions. The level of read consistency and atomic write/commit capabilities across one or more tables/entities varies by NoSQL engine.
  • No standard domain language like SQL for expressing ad-hoc queries. Each NoSQL has its own API and some of the NoSQL vendors have limited ad-hoc query capability.
  • Less structured and rigid data model. NoSQL typically forces/gives more responsibility at the application layer for the developer to "do the right thing" when it comes to data relationships and consistency. Think of NoSQL as a schema on read instead of the traditional schema on write.

Share:

Wednesday, March 13, 2013

JavaFX for Android or iOS?


There has been big news recently in the world of JavaFX regarding many more components of JavaFX being open sourced as advertised at JavaOne 2012. In February Open Source Update, Richard Bair compiled a table of JavaFX projects that have been open sourced as of that post's writing (Monday, 11 February 2013). As exciting as all that open sourcing is, there was something even more exciting highlighted below the table: "We’re also going to open source our iOS and Android implementations over the next couple months."

Bair adds some timing and background information to this significant announcement:

  • The first bits and pieces for iOS should be out next week, with the rest of iOS and Android coming out at about the same time as the rest of prism (there is some timing dependency there). Both our ports are based on an as-yet unreleased version of JavaSE Embedded for iOS/Android.

After expressing the expected caveat "I’m not a lawyer", Bair also addresses licensing issues on iOS and points out that "both OpenJFX and OpenJDK are both licensed with the same GPLv2 with Classpath Extension." He further describes his understanding of the licensing situation: "this means that if you take OpenJFX + OpenJDK (minus any binary stubs released under a different license), then you can safely combine this with your application and release your application under your own license as a single application co-bundle." I am sure we'll hear more about the licensing details in the future as this develops.

Being able to develop Android and iOS applications with JavaFX will likely be a game-changer for JavaFX. I echo Bair's concluding sentence: "I am looking forward to seeing what you all will do with this contribution, and hope to be running many Java apps on my phone / iPad in the near future." I look forward to using (and maybe even writing) some JavaFX-based apps on my Droid!

References :
javafx coming soon to android or ios
Share: