tag:blogger.com,1999:blog-35314735477955439522024-03-06T01:18:27.334-08:00ProgrammingRobhttp://www.blogger.com/profile/16435838865749316772noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-3531473547795543952.post-65863118115647662562010-10-20T07:58:00.000-07:002010-10-20T08:09:15.633-07:00Java signal handler implementationHere is a Java signal handler implementation. The signal will be handled even though Thread.sleep is running.
<div style="overflow: auto; line-height: 1em;">
<pre><span style="font-size:-1;">import sun.misc.Signal;
import sun.misc.SignalHandler;
public class Test
{
public static void main(String[] args)
{
SignalHandler signalHandler = new SignalHandler() {
public void handle(Signal signal) {
System.err.println("Exiting because of signal: " + signal);
System.exit(1);
}
});
Signal.handle(new Signal("TERM"), signalHandler);
Signal.handle(new Signal("INT"), signalHandler);
try {
Thread.sleep(50000);
}
catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
}</span></pre></div>Robhttp://www.blogger.com/profile/16435838865749316772noreply@blogger.com0tag:blogger.com,1999:blog-3531473547795543952.post-23154662252409997692010-06-29T14:08:00.000-07:002011-08-23T12:45:53.838-07:00Java Comparable and equals() implementationIt is strongly recommended (though not required) that the natural ordering of a class (compareTo() method) be consistent with the equals() method (see <a HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html">java.lang.Comparable</A>). The best way to keep them consistent, is to implement the equals() method as a call to the compareTo() method. This will increase the maintainability of the code: if any new fields are added to the class in the future, they will only have to be added to the compareTo() method, the equals() method will not have to be changed. When calling compareTo() from equals(), we need to be careful, because e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false. <div style="overflow:auto; line-height: 1em;"><pre><font size="-1">public class ComparableExample implements Comparable<ComparableExample>
{
private int number;
private boolean bool;
private String str;
private Object obj;
@Override
public boolean equals(Object o)
{
if (null == o) { return false; }
if (this == o) { return true; }
if ( ! (o instanceof ComparableExample) ) { return false; }
return 0 == compareTo((ComparableExample)o);
}
public int compareTo(ComparableExample that)
{
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;
if (null == that) { throw new NullPointerException(getClass().getSimpleName() + ".compareTo() called with a null parameter"); }
if (this == that) { return EQUAL; }
int result = EQUAL;
if (EQUAL == result) { result = (this.number < that.number ? BEFORE : (this.number > that.number ? AFTER : EQUAL)); }
if (EQUAL == result) { result = (this.bool ? (that.bool ? EQUAL : AFTER) : (that.bool ? BEFORE : EQUAL)); }
if (EQUAL == result) { result = (null == this.str ? (null == that.str ? EQUAL : BEFORE) : this.str.compareTo(that.str)); }
if (EQUAL == result) { result = (null == this.obj ? (null == that.obj ? EQUAL : BEFORE) : this.obj.compareTo(that.obj)); }
return result;
}
}</font></pre></div>Robhttp://www.blogger.com/profile/16435838865749316772noreply@blogger.com0tag:blogger.com,1999:blog-3531473547795543952.post-70758483003687053322009-01-12T16:20:00.001-08:002009-01-12T16:21:07.185-08:00Why Singletons Are ControversialI found a good article about why Singletons should not be used:
<A HREF="http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial">Why Singletons Are Controversial</A>Robhttp://www.blogger.com/profile/16435838865749316772noreply@blogger.com0tag:blogger.com,1999:blog-3531473547795543952.post-36703086798814513672008-06-27T09:26:00.000-07:002010-06-30T06:46:41.336-07:00jBoss deployment error messageWhen you deploy, jBoss automatically checks the structural integrity and Specification compliance of the JAR file. If this check fails, the JAR has <i>not</i> been deployed. The error messages are relatively helpful; for example:
<div style="overflow:auto; line-height: 1em;">
<pre><font size="-1">Bean : SomeEJB
Method : public abstract returnType methodName(parameters)
throws RemoteException,...
Section: 7.10.5
Warning: The methods defined in the remote interface must
have a matching method in the bean's class with
the same name and same number and types of
arguments.
</font></pre></div>
Not only has jBoss detected a violation of the EJB Specification, it has indicated which section of the Specification to look at (7.10.5 in this case). If you see messages like this, you need to correct the code.
However, jBoss doesn't say what is the exact difference between the remote interface (usually called SomeService.java) and the bean definition (usually called SomeBean.java). Check all of the following: the method name, the type of its parameters, the list of exceptions it throws and its scope. Here is how it should be usually:
<br/>
<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" target="_blank" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiC5IzcOXGzuqkJgwBtJrvnGxCKPJR-cnya3PTBeUufu8tZpyr68VGjx70RNVvqqiNbg1OL06M_MmIxpt21_1C17nufCcIQfOWlCGWlsW2EPOibUHLB1HVtGvuaWcFQ5YA_43V2jxqG-mC-/s1600-h/jBoss.JPG"><img style="cursor:pointer; cursor:hand;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiC5IzcOXGzuqkJgwBtJrvnGxCKPJR-cnya3PTBeUufu8tZpyr68VGjx70RNVvqqiNbg1OL06M_MmIxpt21_1C17nufCcIQfOWlCGWlsW2EPOibUHLB1HVtGvuaWcFQ5YA_43V2jxqG-mC-/s400/jBoss.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5216609142535890386" /></a>Robhttp://www.blogger.com/profile/16435838865749316772noreply@blogger.com0tag:blogger.com,1999:blog-3531473547795543952.post-33456307459766980602008-01-03T10:36:00.000-08:002010-06-30T06:47:12.048-07:00C++ operator< implementation<div style="overflow:auto; line-height: 1em;">
<pre><font size="-1">class Foo
{
int a;
float b;
std::string c;
/* ... */
bool operator<(const Foo& foo) const
{
if (a != foo.a) return a < foo.a;
if (b != foo.b) return b < foo.b;
if (c != foo.c) return c < foo.c;
return false;
}
}</font></pre></div>Robhttp://www.blogger.com/profile/16435838865749316772noreply@blogger.com0