Sunday, November 25, 2012

Access annotated dependencies through injector instance


Following sample will demonstrate how to access guice dependency annotated with some annotation through direct Guice injector instance.

Bar.java

 import java.lang.annotation.Retention;  
 import java.lang.annotation.RetentionPolicy;  
 import com.google.inject.BindingAnnotation;  
 @Retention(RetentionPolicy.RUNTIME)  
 @BindingAnnotation  
 public @interface Bar {  
 }  

Foo.java

 import java.lang.annotation.Retention;  
 import java.lang.annotation.RetentionPolicy;  
 import com.google.inject.BindingAnnotation;  
 @Retention(RetentionPolicy.RUNTIME)  
 @BindingAnnotation  
 public @interface Foo {  
 }  

TestDepdency.java

 public interface TestDepdency {  
      String getValue();  
 }  

FooTestDependencyImpl.java

 public class FooTestDependencyImpl implements TestDepdency {  
      @Override  
      public String getValue() {  
           return "Foo";  
      }  
 }  

BarTestDependencyImpl.java

 public class BarTestDependencyImpl implements TestDepdency {  
      @Override  
      public String getValue() {  
           return "Bar";  
      }  
 }  

Test.java

 import com.google.inject.AbstractModule;  
 import com.google.inject.Guice;  
 import com.google.inject.Injector;  
 import com.google.inject.Key;  
 public class Test {  
      public static void main(String[] args) {  
           Injector injector = Guice.createInjector(new AbstractModule() {  
                @Override  
                protected void configure() {  
                     bind(TestDepdency.class).annotatedWith(Foo.class).to(FooTestDependencyImpl.class);  
                     bind(TestDepdency.class).annotatedWith(Bar.class).to(BarTestDependencyImpl.class);  
                }  
           });  
           TestDepdency foo = injector.getInstance(Key.get(TestDepdency.class, Foo.class));  
           System.out.println(foo.getValue());  
           TestDepdency bar = injector.getInstance(Key.get(TestDepdency.class, Bar.class));  
           System.out.println(bar.getValue());  
      }  
 }  


Final output

 Foo  
 Bar  


In the similar way you can retrieve the annotated provider.

Singleton Factory


Singleton factory for single threaded environment:

It is simple to create Singleton factory for single threaded application.

 public class SingleThreadSingleton {  
      private static SingleThreadSingleton     instance;  
      private SingleThreadSingleton() {  
           // Initialise  
      }  
      public static SingleThreadSingleton getInstance() {  
           if (instance == null) {  
                instance = new SingleThreadSingleton();  
           }  
           return instance;  
      }  
 }  

Singleton factory for multi threaded application:

When you want to create Singleton factory for multi threaded application. In which you want single instance to be maintained across all thread. You need to synchronize the creation of a instance. In which case if you synchronize the complete getInstance method in above example you are creating the bottleneck to access the instance. The requirement is to just synchronize the creation of instance. Following is sample Singleton factory for multi threaded application.

1:  public class MultiThreadSingleton {  
2:       private static MultiThreadSingleton     instance;  
3:       private MultiThreadSingleton() {  
4:            // Initialise  
5:       }  
6:       public static MultiThreadSingleton getInstance() {  
7:            if (instance == null) {  
8:                 synchronized (MultiThreadSingleton.class) {  
9:                      if (instance == null) {  
10:                           instance = new MultiThreadSingleton();  
11:                      }  
12:                 }  
13:            }  
14:            return instance;  
15:       }  
16:  }  

As there is possibility that multiple thread calls getInstance method simultenously  before instance is initialized. In that case there is possibility that multiple threads get inside initialization block ( ref line no. 7). As inside code
is synchronized only one thread would get access to it at given instant of time. Other thread will wait till the time first thread release the lock. First thread gets a access it will initialize the instance. When first thread goes out of synchronized block other next thread waiting for the access will get inside synchronized block. Now if we don't add null check for instance inside synchronized block ( ref line no. 9) the second thread will initialize the instance again. Same will happen if there are more threads waiting for the access. To avoid reinitialization we have to add null check for instance inside Synchronized block also. Once instance is created then there is no need to have synchronization to access the instance.

Wednesday, November 21, 2012

Free or less costly tools and services helpful for Software Startup


When you are looking to start on your own Software Start-up you need to look for what all free services, tools that can be help full to start with as saving on Money is very important for a startup.

  • Most important for any start-up is to have email for their custom domain. And I suppose most of the people know which is the best and free option to start with Google Apps for 10 users
    • Its the custom gmail for your domain and you have most of the other free service from Google available with it like Google Drive, Calendar, Task and Sites.
  • Most important for Software start-up is where to keep or host source code repository. And most use full and feature free service upto 5 users that I was able locate is https://bitbucket.org/. It has following features
    • Free private source code repository hosting upto 5 users with mercurial.
    • It has Wiki pages hosting not that great but can be useful.
    • Up to some extent you can have access management on repository.
    • It comes with Jira integeration, which good amount features which can make your life easy to trac the issue and changes.
    • You can add commit commands while committing the code which can link, resolve the respective issue in JIRA.
    • It has services like send commit mail, send diff and issue update notifications.
    • Most importantly you can have unlimited private repositories.
    • You can even configure custom domain.
    • And it also supports code review process. 
  • The product you are developing is having GUI to interact with then most probably you would also require to create mock pages. So that it will be easier for you to communicate and help full to get your imagination on paper before you give it a finishing touch. I found one such tool very help full https://gomockingbird.com/.
    • It is a web based service, so you can access it from anywhere.
    • You can easily drag and drop the widgets and design the page very fast.
    • Most important feature which I liked about it is to link one page corresponding hyper link or tab on other page.
    • You can collaborate with other people.
    • You can download pdf file / images.
    • It is a paid service, which start with 9$/ Month for 2 active projects and unlimited collaborators. But most important part is you can archive your project, in case you are not going to work on it for a month or more than a month. If you don't have any active project for any month you don't have to pay for that month. You can reactivate archived project and work on it again.
  • Project planner you can have tom'splanner to start with free of cost.
  • If you are working in group in different remote location, you might need to share the screen and have group meetings in that case you can use following service free of cost for screen share.
    • You can use skype screen share
    • You have join.me but its not supported to share screen from Linux machine.
    • One recent tool I found was Google Chrome Remote Desktop you will require to use Google Chrome browser. With this you can actually control remote desktop.
  • If yours is the web application which you want to host some where on internet. Either you can host it in house our use hosting service. Hosting in house requires lot of investment and its time and money consuming from the point of maintaining it initially. It may be worth having it if you are big enough to handle it. Following are few services which I found which can be cost effective.
    • To start with you can think of using Google App Engine but it depends what kind of storage you are going to use. If your application requires to use Mysql then I find Google service to be costly but you can use App Engine Data Store using either JDO or JPA with some some limitation. Like it doesn't support Many to Many relationships, Join Queries etc.
    • If you are looking for some VPS service then I was able to locate few cost effective good Services 
      • One is intovps http://www.intovps.com/ starts with 512 MB RAM, 30 GB RAID 10 Storage, 500 GB Monthly traffic, 1 Static IP and root SSH access 10$ / Month. Available to host in Fremont CA, Dallas TX, London UK, Amsterdam NL and Bucharest RO.
      • Other one is yardvps http://www.yardvps.com/ starts 512 RAM, 1 CPU core, 20 GB RAID 10 storage, 1500 GB bandwidth, 1 static IP etc with around 8$ / Month.
  • If you are looking for CDN then https://www.cloudflare.com/ is free and very good service you can use as a CDN.
       I will update it as and when I get some more information. It would be also helpful for me if any body who is reading this blog has some more information or other feedback about above mentioned service it would be helpful for me to know it.