Archive

Archive for the ‘Java’ Category

Practical API Design

May 31st, 2010 jmguilla No comments

I just finished reading “Practical API Design“, a really good book that every API maintainer should have. I suggest to every developer to have it as fellow for your daily programs…

Categories: Java, Programming Tags: ,

Only expose API Methods to users

May 21st, 2010 jmguilla 1 comment

Accessor Pattern

Everybody has always been considered by separation of code i.e, related functionalities written in the same packages for instance. The issue that comes up when writing an API is that you likely need to call some of that methods in other packages but you still don’t want to expose them to users. In Java language you can use 4 differents modifiers: Public, Protected, Package and Private. To answer our problem it lacks a modifier, let’s name it “Friendly”, that would allow to call methods in other packages under friendship. That would solve both both code separation concerns and non exposable methods. Here is an example of the Accessor Pattern:

package jean.michel.guillaume.api;
 
/**
 * class part of the public API, every one can use it
 */
public final class RMCore {
    static {
        Accessor.setDefault(new AccessorImpl());
    }
 
    /** Anyone can change value of the item. */
    public void addNode(String url) {
        //impl
    }
 
    /** Only friends can notify node changes.  */
    void notifyNodeUpdate(String url) {
        //impl
    }
}
package jean.michel.guillaume;
 
/**
 * We'll use this class to expose methods in separate packages
 */
public abstract class Accessor {
    private static volatile Accessor DEFAULT;
    public static Accessor getDefault(){
        Accessor a = DEFAULT;
        if( a != null){
            return a;
        }
        try{
            Class.forName(RMCore.class.getName(), true, RMCore.class.getClassLoader());
        }catch(Exception e){
            e.printStackTrace();
        }
        return DEFAULT;
    }
 
    public static void setDefault(Accessor a){
        if( DEFAULT != null){
            throw new IllegalStateException();
        }
        DEFAULT = a;
    }
 
    protected abstract void void notifyNodeUpdate(String url);
}
package jean.michel.guillaume.api;
 
import jean.michel.guillaume.Accessor;
 
/**
 * Now, we can just implement our own Accessor in a separate package
 * to gain access to friendly methods!! Don't forget to declare AccessorImpl
 * package private!!
 */
final class AccessorImpl extends Accessor {
 
    protected void notifyNodeUpate(String url) {
 
    }
}
Categories: Java, Programming Tags: ,

Java 5 and dependency injection

May 11th, 2010 jmguilla 1 comment

How to dynamically load an implementation using Java 5?

You cannot use Java 6? One of the coolest features brought by this last release is the capability of loading an implementation just using:

ServiceLoader.load(MyInterface.class)

And you were looking for that… I know how boring it is!! It is not public in the API but I found it there. That means that to use ServiceLoader with Java 5 is straight forward:

   CharEncoder getEncoder(String encodingName) { 
       Iterator ps = Service.providers(MyInterface.class); 
       while (ps.hasNext()) { 
           MyInterface myInterface = (MyInterface)ps.next(); 
           MyImpl myImpl = MyInterface.getImpl(implName); 
           if (myImpl != null) 
               return myImpl; 
       } 
       return null; 
   }

That’s it

Categories: Java, Programming Tags: ,

Locate your files in Java

April 30th, 2010 jmguilla No comments

I am sure you have already tried to locate files when running methods in a framework or in a web container… If so, you have experimented how tough it is… Forget the MyClass.class.getResourceAsStream() method, the solution rely on the following:

import java.net.URL;
import java.security.ProtectionDomain;
 
public class TestPath {
 
	/** @param args */
	public static void main(String[] args) {
	    ProtectionDomain protectionDomain = TestPath.class.getProtectionDomain();
	    URL location = protectionDomain.getCodeSource().getLocation();
	    System.out.println(location.toExternalForm());
	}
}

Now I use that two simple lines of code, my life is different ;)

Categories: Java, Programming Tags: ,

What a Java Engineer should do…

April 30th, 2010 jmguilla No comments

How to improve your skills?

A short story about a guy who absolutely wants to maintain his level in programming Java

If you are like me, you are really concerned about what you should do daily to keep on being at top level in programming Java. The recipe is simple: READ and PRACTICE. For everyone sake, I am sharing my favorite feeds ;)

  • Pure Danger Tech: Alex Miller’s Technical Blog. In my opinion, the most interesting guy in the Web Java Ecosystem. He gives many advices regarding concurrency, is really implied in Java SDK development, and founder of the Lambda Lounge, which is a Lamda Language User Group. Be sure not to miss anything written by this amazing Java developer.
  • DZone: Java Lobby. A Java Database filled in by many users. A big source of information, in every Java fields.
  • Java Black Belt: Building Better Developers. The best way to practice online. You have access to a big exam database and prepare yourself to graduate. Your  programming level is determined by your belt color, from white to brown. So far, black belt exam is not available. Several exams for almost all important Java frameworks are free. This is a very attractive website as anybody can contribute and the top contributors are all part of the best known Java developers. This ensure a fast pace improvement for your level.
  • API Design Blog. The blog to promote the well known book: “API Design Book“.
  • Java.net: The source for Java Technology Collaboration. Tips and experience shared by top developers. Many advices about frameworks, IDE, API…
Categories: Java, Programming Tags: ,