Friday, February 24, 2012

All about imports in Java

In any Java source file first line after package declarations are import statements.  These statements enable us to use different classes from outside package. Compiler always looks at the import statements to find class definition.
Instead of import statements we can write fully qualified name. For ex:
java.io. File file = new java.io.File(filepath);
This is a perfectly valid statement. Only problem is it looks littlie complicated to read.  Good codes are not only run well but also easy to read.
So we will stick to
File file = new File(filepath);
To do so we have to write import statement at the beginning right after the package declaration.
package mej.java.example;
import java.io.File;
If we use more than one classes or interface of java.io packages we need to write multiple import statements.
package mej.java.example;
import java.io.File;
import java. FileInputStream;
or we can use wild characters like
java,io.*;
At first impression it looks simple, there are few side effects with this. It takes down the performance of compiler, as compiler has to look whole package for match.  But it doesn’t affect runtime performance of code in anyway. Many time developers misunderstood it.  There can be cases if we use multiple wild character imports. And if both the package contains class with same name, compiler will not able to resolve. And it also reduces the readability of code. It’s difficult to read which class from which package is being used. So the best way is to use import individually for each class or interface.
Another type of imports are static imports. To use it I can say something like
import static java.lang.Match;
We can directly write  ceil(4.83) instead of Math.ceil(4.38).  By static imports we can directly use the methods or members instead of referring through their class name. This is widely used in junit test cases to refer Assert class methods.
In this case also too much use of static imports decrease code maintainability.  
Please let me know your thoughts about java imports.

Thursday, February 16, 2012

localhost to www




Here we will discuss how to host your web application in internet. Once the application is built and running on your local machine and the testings are over next step is to take it to internet.
First we need a domain name, like for my blog I use www.myexperimentswithjava.com. To check and book your preferred domain there are lots of service providers are available for example: google apps and  godady
Domains are normally sell for 1 year , 2 years or  for some fixed terms.
If the domain name what you are looking for is taken by some one. And still you want it you can try for resale, godady facilitate resales of domain. But normally these are expensive. With domains usually service providers give some free email account, which you can use for business purpose like admin@yoursitename.com.
Once you are ready with the domain. Next thing is the Web servers,application server and database server.
Depending upon the load and traffic you expect these three can be in one machine, or individual machine or for each multiple machine. If you expect more than 1000 users are going to access then professional consultancy is required.
If you have decided to host in a share resource (popular name is cloud), you can check for service provider. I had used Amazon elastic cloud, which is pretty good. For dedicated servers you need to buy physical hardware and the set it up to run it. It should have good connectivity and fixed IP. Now next thing is that to set up each layer. I prefer first to install data base, configure security setting backups. And then create application specific objects and load initial data. Then install application server and install the apps. Check the connectivity with database. And some testing with apps. Once the application looks fine, configure the app server to run on port 80.  Then by just typing the IP of your application server you should able to access your application on internet. And the application server IP must be a static one.

Next we have to configure our domain name to application server.  From domain service provide we will get a control panel to manage the domain. There we can set redirect, configure sub domain etc. In controll panel there will be an option to add MX record. We need to add two entry like for ex:
1) http://myexperimentswithjava.com
2) www.myexperimentswithjava.com

and these entries need to map to application server.
After the configuration it takes some time to work. Normally in a day or two your application can be reachable on internet by your domain.

All the best for your hosting experiments, do post your experience.

Friday, January 13, 2012

Something is in Air

If some on follow computers actually (nowadays its other way computers follow us) must heard about cloud. And the computer guys always copy the terms from real world to use. Like Inheritance, Cloud , Mouse etc ... sometime the original meaning of these words must be thinking to sue for copyright violation.
Well copying and stealing is never a problem in this industry. It is always appreciated.
"Cloud" is such a term today no one can afford to miss it. Companies, Developers and Customers  everybody like to have it.
Well lets understand what does Cloud means and how it make sense. To start we can say your computer is connected to another computer. Then networking is there from more than last 20 years. Each year its becoming more powerful. Computer can connect with each other they can send data and receive from another computer. The next thing was application which run on network by using resource of multiple computer. Web applications are fall in this category. They run in server and user can work with it remotely.
The service providers are hosting the service in server and can cater to multiple user, that brings down end user cost of using service. The application is customized as per users preference. This is something called application on cloud. Actually it is nothing but same web application. And it is customized as per user or groups of user.
Another thing is Computer infrastructure or platform on cloud. Its like you will able to use remote server, database or application server. Amazon, Oracle and Google provide these kind of service. Here the application developers can use these platform to develop, test and host. And its easy to scale as per usage. So its a very cost effective way of using resource. Still there are some concerns with data security, organisations are comfortable to store there important data in remote data center.
And another third category of cloud services are kind of hybrid between platform and application, Salesforce, Oracle public cloud and Apple's iCloud are such example. These gives developers platform to write application on cloud.
Certainly for medium/small scale industries can use cloud services for their IT needs. It enable to have good IT system in lower price.