Archive for the ‘Java’ Category

IP to country/location libraries

Saturday, July 21st, 2007

Save here for future use.

PHP & Java version

Link: http://firestats.cc/wiki/ip2c

IP2C

IP2C is a small library that provides IP to country resolution using a binary file.
The binary file is compiled f2rom ip-to-country free database and takes only about 500kb (the CSV takes about 3.5mb).

License

IP2C is released under the terms GPL v2.

Supports

  • Command line
  • Java
  • PHP

Performance

All tests done on an Athlon XP 3200+.
PHP:

  • No cache: About 1200 searches in a second.
  • Cache: not implemented (Want to help?)

Java:

  • No cache: About 8000 searches/second.
  • Memory cache: About 180,000 searches/second.

Download

ip2c 1.4.3 (Database version : 2007.3.5)

C# version

http://www.extremeoptimization.com/resources/Concept/

Source download: http://www.codeproject.com/csharp/IPLookupOptimise.asp

Popularity: 9% [?]

IoC/DI framework evaluation notes

Monday, July 16th, 2007

(originally written on Apr 15 when I am evaluation some IoC framework)

Just some basic ideas: Some IoC/DI framework can be introduced into tako framework and related projects to make the code cleaner and easier to be maintained. Since there are so many IoC frameworks now day, I spend sometime on evaluation them and reading the tako codes to see if there are any synergy of adopt some IoC inside.

Spring framework is very popular and is the only IoC framework I worked on before, however Google’s Guice became very hot these days.

IoC: Invert Of Control DI: Dependency Injection. Means the same.
Spring framework

Pros:

* Matured, well documented
* Not just an IoC, many other useful modules

Cons:

* XML hells ? (but can use Spring Java config or Spring Annotation to override this problem)
* Slow ? (Juice anounced that it’s 100x faster than Spring)

Spring has JavaConfig sub project which enable using Java code instead of XML file to config the modules. Spring Annotation is a 3rd party project which use a lot of annotation to reduce the requirement of XML.

Guice

http://code.google.com/p/google-guice/

Pros:

* lightweight
* no XML, simpler, less lines of codes
* 100X times faster than Spring IoC? (That compare may not really valuable for a real product after all)

Cons:

* intrusive design ( at least I think it is… bad smell of code… xxx.google.xxx everywhere. )
* new, less document, no successful project other than google’s

Tapestry IoC

http://tapestry.apache.org/tapestry5/tapestry-ioc/

Tapestry is something I think could be introduced to the view layer of Tako to replace JSP/Velocity. Tapestry have so many advantages esp. in make web development easier.

Tapestry 5 is still under construction, the latest matured version is 4. Tapestry includes an IoC framework inside.

Tapestry’s author said he was impressed by Guice, and had learn some from Guice and adopted into Tapestry IoC.

Hivemind

Tapestry 4’s IoC. Even Tapstry 5 giveup hivemind, so we don’t need to consider after all?

Pico

Too lightweight to useful? Not at all, I feel it’s lightweight but yet powerful. Need more time to test and play around with it.

Spring VS Guice

Compare: http://code.google.com/p/google-guice/wiki/SpringComparison
Guice better?: http://stuffthathappens.com/blog/2007/03/09/guicy-good/

Popularity: 7% [?]

CoC learning notes

Monday, July 16th, 2007

CoC(Convention over Configuration) is an mantra in modern software development.  CoC rescued programmer by making life easier.

CoC is a term often bandied around by Ruby on Rails:

    “Convention over Configuration” means a developer only needs to specify unconventional aspects of their application. For example, if there’s a class Sale in the model, the corresponding table in the database is called sales by default. It is only if someone deviates from this convention, such as calling the table “products_sold”, that they need to write code regarding these names.
from: http://en.wikipedia.org/wiki/Ruby_on_rails

Even frameworks like Spring, Struts are begin to adopt CoC ideas inside. From Struts 2, it will support a “Zero Configuration” mode, which is a CoC design.(http://struts.apache.org/2.x/index.html) In Spring 2, we can also see many CoC improvements. (http://www.memestorm.com/blog/convention-over-configuration-in-springs-mvc/)

Popularity: 5% [?]

XML or Annotation, that’s a problem

Monday, July 16th, 2007

This is from: http://evolutionarygoo.com/blog/?p=54

    When to use annotations:

1. You despise XML and are looking for new ways to express your metadata.
2. Your metadata is related to a single class or method.
3. Your metadata is fairly static and will not need to change at runtime.
4. You want to see your metadata encapsulated inside of your objects.
5. You are looking to take advantage of special annotation features supported by the JVM.
6. You are looking for ways to access metadata within your Java code using native language commands.
7. For specifying default metadata.
8. Several of the popular frameworks are using annotations and you want try them out yourself.

When to use XML:

1. You are happy with XML and are not looking for a new way to express metadata.
2. You think that the addition of intelligent defaults to many of the most popular frameworks is good enough to warrant the continued use of XML.
3. Your metadata is related to multiple classes.
4. Your metadata is not related to a class or a method at all, but to a higher level of the application and therefore aren’t a good solution for what you are doing.
5. Your metadata will change often. Configuration data is a good example of this.
6. Your metadata needs multiple implementations.
7. You want to see all of your metadata defined in one location.
8. Your metadata needs to change at runtime.

A comment for that post is also vauleable:

    one of the key things is how frequently you change the metadata and/or how flexible you need it to be at runtime. Since annotations are compiled into the classes, it cannot be changed at run time without recompilation. So, annotations are best used for things that are declarative and describe unchanging (or very infrequently changing) properties of the software. For example, I’ve used them for describing constraints or adapters that should be used with a particular property within a framework. That’s how the software is constructed, not a runtime modification attribute.

If you want to change the software at runtime, you shouldn’t put it in annotations, since it won’t work. So, that could drive you towards XML. Although given the enormous uptake in scripting language support in the JDK, it might even better drive you towards adding scripting support via JRuby, Rhino, Jython, Groovy, Beanshell, etc.

Some other good articles talking about this:
Annotation, friend or foo? :http://java.sys-con.com/read/163245.htm
Notes on XML vs annotation: http://linuxintegrators.com/acoliver/code/2006/02/26/x-0195.html

Popularity: 14% [?]

One more lesson learned on “volatile varible”

Monday, July 16th, 2007

Rockch left a comment on “lesson learned on synchronized method“:

rockch | July 15th, 2007 at 8:16 pm e
synchronized is so heavy for your code,you can use volatile.
http://www.ibm.com/developerworks/cn/java/j-jtp06197.html?S_TACT=105AGX52&S_CMP=techcsdn

The article is a Chinese translation, the original English version is here. After I read the article, though i though volatile variable actually can’t help me in my code (cause the init() did quite a few dirty and time costing works, so I still need synchronized method lock on other object to provide thread safe), I still learned more on volatile variables.

Frankly speaking, I never really care about “volatile variables” before, all I know before is, “volatile” tell compiler DO NOT optimized on it and it related code, and generally will be used in threads shared variables.

I copied some from original articles for my own reference.

Patterns to use volatile variables:

1. Using a volatile variable as a status flag

volatile boolean shutdownRequested; ...public void shutdown() { shutdownRequested = true; }public void doWork() {

while (!shutdownRequested) {

// do stuff

}

}

2.Using a volatile variable for safe one-time publication

public class BackgroundFloobleLoader {

    public volatile Flooble theFlooble;    public void initInBackground() {

        // do lots of stuff

        theFlooble = new Flooble();  // this is the only write to theFlooble

    }

}

public class SomeOtherClass {

    public void doWork() {

        while (true) {

            // do some stuff...

            // use the Flooble, but only if it is ready

            if (floobleLoader.theFlooble != null)

                doSomething(floobleLoader.theFlooble);

        }

    }

}

3.Using a volatile variable for multiple publications of independent observations

public class UserManager {

    public volatile String lastUser;    public boolean authenticate(String user, String password) {

        boolean valid = passwordIsValid(user, password);

        if (valid) {

            User u = new User();

            activeUsers.add(u);

            lastUser = user;

        }

        return valid;

    }

}

4.volatile bean pattern

@ThreadSafe

public class Person {

    private volatile String firstName;

    private volatile String lastName;

    private volatile int age;    public String getFirstName() { return firstName; }

    public String getLastName() { return lastName; }

    public int getAge() { return age; }

public void setFirstName(String firstName) {

        this.firstName = firstName;

    }

public void setLastName(String lastName) {

        this.lastName = lastName;

    }

public void setAge(int age) {

        this.age = age;

    }

}

5. “cheap read-write lock”

@ThreadSafe

public class CheesyCounter {

    // Employs the cheap read-write lock trick

    // All mutative operations MUST be done with the 'this' lock held

    @GuardedBy("this") private volatile int value;    public int getValue() { return value; }

public synchronized int increment() {

        return value++;

    }

}

The reason this technique is called the “cheap read-write lock” is that you are using different synchronization mechanisms for reads and writes. Because the writes in this case violate the first condition for using volatile, you cannot use volatile to safely implement the counter — you must use locking

That article also gave some valuable links, which worth to read on…

  • Java Concurrency in Practice : The how-to manual for developing concurrent programs in Java code, including constructing and composing thread-safe classes and programs, avoiding liveness hazards, managing performance, and testing concurrent applications.
  • Going Atomic: Describes the atomic variable classes added in Java 5.0, which extend the concept of volatile variables to support atomic state transitions.
  • An introduction to nonblocking algorithms: Describes how concurrent algorithms can be implemented without locks, using atomic variables.
  • Volatiles: More about volatile variables from Wikipedia.
  • The Java technology zone: Hundreds of articles about every aspect of Java programming.

Popularity: 8% [?]

More lesson learned on synchronized method

Thursday, July 12th, 2007

I made a mistake first in my last post:
Some lessons learned on synchronized method

Use a simple object as the synchronized object could be dangerous and create hard to find hidden problems.

e.g.

   String loocker = “lockme”;

synchronized (locker) { …}

Because:

String locker = “lockme”;

String PROMPT_LOCK = “lockme”;   // some where else use same constant value

“locker”, “PROMPT_LOCK” could reference the same object in the memory!  So in some worst case, it could cause dead lock, hard to debug.

Here is a sample buggy program for test, in some case, Foo may never be able print any number to screen:

class Foo extends Thread
{
private Boolean b= false;
private int val;
public Foo(int v)
{
val = v;
}

public  void printVal(int v)
{
synchronized(b) {
while(true)
System.out.println(v);
}
}

public void run()
{
printVal(val);
}
}

class Bar extends Thread
{
private Boolean b = false;
public  void printVal(int v)
{
System.out.println(”lalal”);
synchronized(b) {
while(true);  // just lock forever and do nothing
}
}
public void run()
{
printVal(1);
}
}
class Test
{
public static void main(String args[]) throws IOException {
new Bar().start();
new Foo(1).start();
}
}

Popularity: 5% [?]

Some lessons learned on synchronized method

Thursday, July 12th, 2007

Found a multi-threading synchronization problem, my code look like this:

class ASimpleTagClass extends SimpleTagSupport {

static class SingletonHolder {
static SomeClass instance = new SomeClass();
}

static boolean inited = false;

synchronized init() {

if (!inited) {

//… blah blah init codes

}
}

}

Because SimpleTagSupport will create an Instance for each, the ASimpleTagClass’s init() is not thread safe, because e synchronized method will prevent two invocations of the methods on the same object, not two different objects. Here obviously they are different instances, so code inside init() will run concurrently and will cause bug.

An article ” Understand that for instance methods, synchronized locks objects, not methods or code” is exactly talking about this issue:

The synchronized keyword is used as either a method modifier or as a statement inside of a method. This dual usage causes some confusion about exactly what the synchronized keyword does. It is often described in terms of mutual exclusion (mutex) or a critical section. This causes many programmers to incorrectly think that because code is protected by synchronized , it can be accessed by only one thread at a time.

For instance methods, the synchronized keyword does not lock a method or code; it locks objects. Remember that there is only one lock associated with each object.

When synchronized is used as a method modifier, the lock obtained is that for the object on which the method was invoked.When it is used on an object refer-ence, the lock obtained is that for the object referred to. For example, consider the following code:

class Test
{
public synchronized void method1()
{
//…
}

public void method2()
{
synchronized(this) {
//…
}
}

public void method3(SomeObject someObj)
{
//…
synchronized(someObj) {
//…
}
}

}

The first two methods, method1 and method2 , are functionally identical regarding the object being locked. (They differ in the amount of code generated and in how they perform.) Both methods are synchronized on this . In other words, a lock is obtained for the object on which the method was invoked. Because both methods are of the class Test , the lock is obtained for an object of class Test . The method3 method is synchronized on the object referenced by someObj .

What exactly does it mean to synchronize on an object? It means that the thread that invoked the method has that object s lock. To hold an object s lock means another thread requesting a lock for the same object through a synchronized method or a synchronized statement cannot obtain that lock until it is released. However, another thread executing the same synchronized method or block on a different instance of the same class can obtain that instance s lock.

Change my code to:

class ASimpleTagClass extends SimpleTagSupport {

static class SingletonHolder {
static SomeClass instance = new SomeClass();
}

static Boolean inited = false;

void init() {

synchronized(inited) {

if (!inited) {

//… blah blah init codes

}
}

}

}

Then the problem should gone.

Lesson learned:

Remember that synchronization locks objects, not methods or code. Simply because a method, or section of code, is declared synchronized does not necessarily mean it is executed by only one thread at a time. This is important if the code in the synchronized method or block alters a mutually shared resource.

Another interesting thing is, if init() in my sample could be a static method, then it will not have thread safe problem. Because:

class MyClass  {public synchronized static someMethod() {}}

Is it the equivalent to:
synchronized ( MyClass.class ) {

}

from: 

The JVM creates a Class object when the class is loaded (When it is used for the first time) the
JVM creates one instance of Class for each class that is loaded, thus a class itself has a moniter
which a thread can gain ownership of.


—-

 UPDATED : the above changed code is still buggy!!! And may contain hidden problem:

 static Boolean inited = false;

“inited” may reference to some Boolean object which may be used else where! (That’s Java’s nature, it pool some objects for reuse. ) So if there are some where else have similar code, and lock on this object, it may cause dead lock! And such problem may be hard to find out.

Change the code to:

 …

synchronized(ASimpleTag.class) {

Popularity: 6% [?]

Problem u& solution of using org.apache.AnnotationProcessor in Tomcat 6

Thursday, July 12th, 2007

When I run a project which was worked fine in Tomcat 5.5 on Tomcat 6.0, some error bumped out:

 Jul 12, 2007 3:01:11 PM com.xxxx.xxx.xxx.RendererException <init>
SEVERE: Couldn’t include the view: ‘/WEB-INF/templates/jsp/xxx.jsp’. java.lang.ClassCastException: org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apache.AnnotationProcessor

Found the reason & solution here:

This was due to efforts from MYFACES-1246, proposal of annotation
processing here in list (adding method), but this of course breaks
compatibility of the same named interfaces.

If you have older code using org.apache.AnnotationProcessor you can make
it work on Tomcat:

Add

<Loader delegate="true"/>

into context.xml file (to Context element) in tomcat/conf directory.
This makes classloading in Tomcat sticking to J2EE spec.
(I couldn't make it work with suggested approach for only 1 webapp)

More info on classloader:
http://tomcat.apache.org/tomcat-6.0-doc/config/loader.html

Popularity: 11% [?]

Begin to evaluate some automatic unit test tools

Friday, July 6th, 2007

Unit test is great for developing solid codes, however, unfortunately there are some legacy projects with bunch of workable codes but no single line unit tests after all, when we try to refactor those legacy codes, we will face the problem that we don’t have unit tests and the refactoring works may depends on. Of course we can create some unit test for those legacy codes, but that’s really boring, I am looking for some tools which can generate some unit test codes for me automatically.

AgitarOne

Looks very promising , I applied for a trail version and still wait for their response. So I can say nothing more on it…

AgitarOne provides powerful automation capabilities that can help you detect regressions, avoid preventable defects, and ensure quality as you modify your code. With AgitarOne, you can:

  • Quickly and easily generate JUnit tests with AgitarOne’s automated JUnit generation capabilities
  • Create a change-detection “safety net” of JUnit regression tests for Java applications that lack tests, so that you can change them with confidence
  • Agitate your software to interactively explore the behavior of your new code and find unexpected behavior
  • Use code-rule enforcement to help you find and remove bug-prone anti-patterns
  • Get meaningful JUnit tests and agitation results for an entire team in a matter of minutes, through AgitarOne’s server-based deployment model
  • Get full visibility into the status and trends of your unit-testing activities through AgitarOne’s comprehensive dashboard

AgitarOne provides support for J2EE, Struts, Spring, Hibernate, and other common Java frameworks, and is compatible with any Eclipse-based IDE.

Fortunately the do provide a free online service, JUnitFactory seemed use the same engine as AgitarOne.

JUnitFactory

This is an online service by Agitar. As it said “You send us code. We send you tests”. There is a web page to submit code and then get the generated unit test. But this web interface is just for simple demo, to use it create unit tests for comprehensive projects will need to install eclipse plugin.

With an Eclipse plugin, it’s very easy to use it generate unit tests for very large projects. ( I tried an open source project with over 2000 source files)

JunitFactory will upload all of your project codes to their web site, include source code and all dependencies…which seemed not really useful for most of company.

The generated code is not quite readable, and it’s heavily depends on some bases classes of Agitar, and can’t run directly with JUnit.

TestGen
TestGen is a collection of open-source tools that automatically generates unit test cases. The first released component of TestGen is TestGen4J. TestGen4J automatically generates JUnit test cases from your own Java class files, or source files. Its primary focus is to exercise boundary value testing of the arguments passed to the method. It uses rules, written in a user-configurable XML file, that defines boundary conditions for the data types. The test code is separated from test data with the help of JTestCase.

Flowchart.gif

Any more? 

Will wrote a detail report later on those tools….

Popularity: 7% [?]

Playing around Terracotta

Wednesday, July 4th, 2007

Click this logo to return to the home page

Installed Terracotta (windows version) in two PCs of a local area network. For single machine, just launch a demo multiple times to see it work, for two machines, it seemed need to change the configure file by hand, I changed the tc-config.xml files inside each demo’s directory in one PC(remain another unchanged) and the launch them, they worked fine exactly as what they run in one computer. There is some latency in different computer, the network traffic seemed not too much for those simple tests.

Tomorrow I will try a much bigger test to deploy a *BIG* web project I am working on to see how easy it a web app can be deployed on Terracotta and at least see if it work correctly.

During I evaluate Terracotta, I began remember last year I ever ask one member in one of my project team to evaluate Terracotta (because I do remember the screen of that shared image editor!) but at last he suggest not to use it, I didn’t do the evaluation myself, so just let it be forgotten. I will try to search inside my email to see if I could find that report and find out why we gave it up. Also I wish Terracotta have been improved a lot during this year.

From their blog, it seemed really sexy:

Tomcat’s build in session replication performance was poor as we tested. In my last project, we didn’t use session replication and use a “share nothing” approach which store key information in a cookie.

I also saw some solution(in Chinese) to use memcached as a centralized session store, which also seemed to be very promissing.

Popularity: 8% [?]

Close
E-mail It
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.