Τρίτη, 2 Δεκεμβρίου 2008

Οι καθηγητές Πληροφορικής αγωνίζονται ενάντια στην απαξίωση - υποβάθμιση του επιπέδου σπουδών Πληροφορικής στο Δημόσιο Σχολείο όπου πρόσφατα μεθόδευσε το ΥΠΕΠΘ και απαιτούν:
  1. Οι αναθέσεις μαθημάτων ΚΑΙ για τα μαθήματα τα σχετικά με την Πληροφορική να γίνονται με βάση το γνωστικό αντικείμενο του πτυχίου και μόνο.
  2. Ο αριθμός μαθητών ανά καθηγητή στα εργαστηριακά μαθήματα να γίνει δώδεκα (12) ΚΑΙ για τα μαθήματα Πληροφορικής και όχι δεκαεπτά (17) όπως τώρα κατ’ εξαίρεση ορίζει η νέα εγκύκλιος.
  3. Ισονομία για τους υπεύθυνους εργαστηρίων Πληροφορικής με τρίωρη μείωση ωραρίου ΚΑΙ για το ΣΕΚ.
ΒΟΗΘΕΙΣΤΕ ΣΤΟΝ ΑΓΩΝΑ ΤΩΝ ΠΛΗΡΟΦΟΡΙΚΩΝ ΓΙΑ ΤΗΝ ΕΚΠΑΙΔΕΥΣΗ ΠΛΗΡΟΦΟΡΙΚΗΣ ΚΑΙ ΜΕΤΑΔΩΣΤΕ ΤΟ ΜΥΝΗΜΑ. Διαβάστε το αυτονόητο.

Δευτέρα, 29 Σεπτεμβρίου 2008

Tips and tricks

  1. Tricks on immutability
a. Always make a copy of mutable objects inside your methods, e.g.

void myMethod(Date date) {
Date myCopy = new Date(date.getTime());
}
b. How do I create an immutable Java Bean or POJO? Make it read-only, and pass any parameters during initialisation (in the constructor):

public class MyClass {
private int param1;
private String param2;
private Date date;

public MyClass (int param1, String param2, Date d) {
this.param1 = param1;
this.param2 = param2;
this.date = new Date(d.getTime());
}

public int getParam1() {
return param1;
}

public String getParam2() {
return param2;
}

public Date getDate() {
return new Date(date.getTime());
}
}

c. Immutable objects and Hibernate
  • set the access type to “field” in order to use reflection to set the fields instead of property access, which uses setters.
  • set mutable to false (Hibernate will perform small optimizations, too).

Κυριακή, 7 Σεπτεμβρίου 2008

Compare and sort Greek Strings

Inspired from the article Compare and sort Strings, I hereby present an example of Collators and Comparators with Strings that contain Greek letters.

import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class GreekCollator {
private static final Locale GREEK_LOCALE = new Locale("el", "GR");
private static final int NO_COLLATOR = -1;

private enum CollatorEnum {
NoCollator(NO_COLLATOR),
Primary(Collator.PRIMARY),
Secondary(Collator.SECONDARY),
Tertiary(Collator.TERTIARY),
Identical(Collator.IDENTICAL);

private int collatorStrength;
CollatorEnum(int collatorStrength) {
this.collatorStrength = collatorStrength;
}
}

/**
* @param args
*/
public static void main(String[] args) {
List<String> words = Arrays.asList("άλλος", "αλάνι", "βασικός", "ελεύθερος", "Ελεύθερος", "Άλλος", "Αλάνι");

System.out.println(words + " - Original Data\n");
sort(words, CollatorEnum.NoCollator); // don't use a collator
sort(words, CollatorEnum.Primary);
sort(words, CollatorEnum.Secondary);
sort(words, CollatorEnum.Tertiary);
sort(words, CollatorEnum.Identical);

System.out.println();
compare("αβγ", "ΆΒΓ", CollatorEnum.NoCollator);
compare("αβγ", "ΆΒΓ", CollatorEnum.Primary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Secondary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Tertiary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Identical);

System.out.println();
compare("αβγ", "ΑΒΓ", CollatorEnum.NoCollator);
compare("αβγ", "ΑΒΓ", CollatorEnum.Primary);
compare("αβγ", "ΑΒΓ", CollatorEnum.Secondary);
compare("αβγ", "ΑΒΓ", CollatorEnum.Tertiary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Identical);
}

private static void sort(List<String> aWords, CollatorEnum collatorEnum) {
if (collatorEnum.collatorStrength < 0) {
Collections.sort(aWords);
} else {
Collator collator = Collator.getInstance(GREEK_LOCALE);
collator.setStrength(collatorEnum.collatorStrength);
Collections.sort(aWords, collator);
}
System.out.println(aWords.toString() + " " + collatorEnum);
}

private static void compare(String aThis, String aThat, CollatorEnum collatorEnum) {
int comparison = 999;
if (collatorEnum.collatorStrength < 0) {
comparison = aThis.compareTo(aThat);
} else {
Collator collator = Collator.getInstance(GREEK_LOCALE);
collator.setStrength(collatorEnum.collatorStrength);
comparison = collator.compare(aThis, aThat);
}
if (comparison == 0) {
System.out.println("Collator sees them as the same : " + aThis
+ ", " + aThat + " - " + collatorEnum);
} else {
System.out.println("Collator sees them as DIFFERENT : " + aThis
+ ", " + aThat + " - " + collatorEnum);
}
}

}

Here is the output of the program, for your comments.

[άλλος, αλάνι, βασικός, ελεύθερος, Ελεύθερος, Άλλος, Αλάνι] - Original Data

[Άλλος, Αλάνι, Ελεύθερος, άλλος, αλάνι, βασικός, ελεύθερος] NoCollator
[Αλάνι, αλάνι, Άλλος, άλλος, βασικός, Ελεύθερος, ελεύθερος] Primary
[Αλάνι, αλάνι, Άλλος, άλλος, βασικός, Ελεύθερος, ελεύθερος] Secondary
[αλάνι, Αλάνι, άλλος, Άλλος, βασικός, ελεύθερος, Ελεύθερος] Tertiary
[αλάνι, Αλάνι, άλλος, Άλλος, βασικός, ελεύθερος, Ελεύθερος] Identical

Collator sees them as DIFFERENT : αβγ, ΆΒΓ - NoCollator
Collator sees them as the same : αβγ, ΆΒΓ - Primary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Secondary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Tertiary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Identical

Collator sees them as DIFFERENT : αβγ, ΑΒΓ - NoCollator
Collator sees them as the same : αβγ, ΑΒΓ - Primary
Collator sees them as the same : αβγ, ΑΒΓ - Secondary
Collator sees them as DIFFERENT : αβγ, ΑΒΓ - Tertiary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Identical

References
  1. Java Practices, Compare and sort Strings.

Τετάρτη, 16 Ιουλίου 2008

Collections.copy

Well, it looks like this method contains a "bug"?!?!?

Simply do this test:

public void testCollectionsCopy () {
ArrayList list1 = new ArrayList(3);
list1.add("one");
list1.add("two");
list1.add("three");
ArrayList list2 = new ArrayList(3);
Collections.copy(list2, list1); // throws IndexOutOfBoundsException!!!!!
}
Well, the solution? Pass the collection to be copied as a parameter to the constructor.
ArrayList list1 = new ArrayList(list1);


Σάββατο, 28 Ιουνίου 2008

JavaOne Afterglow 2008

On Friday, 27th of June 2008, I attended the JavaOne Afterglow 2008 in De Montil, Affligem, a few km outside Brussels. It was a well organised day event, that gave us useful information about the things to come, a summary of what was presented in JavaOne conference. Here is the agenda:

Agenda:
09:15 - 09:45 Registration
09:45 - 10:45 "JavaOne Afterglow keynote"
David Delabassée - Sun Microsystems Belgium
10:45 - 11:30 "Modularity in Java : OSGi and/or JSR 277 ?"
Stijn Van Den Enden - ACA IT-Solutions
11:30 - 12:30 "Project Fuji, an Integration Profile"
Andreas Egloff - Sun Microsystems Inc.
12:30 - 13:30 Lunch
13:30 - 14:30 "Java EE 6"
Alexis Moussine - Pouchkine - Sun Microsystems Inc.
14:30 - 15:30 "Boldly Go Where the Java Programming Language Has Never Gone Before"
Geert Bevin - Terracotta
15:30 - 15:45 Coffee break
15:45 - 16:15 "JavaFX overview"
Jo Voordeckers - RealDolmen
16:15 - 17:00 "Parleys.com: A Flex/AIR, JavaFX and GWT Case Study"
Stephan Janssen - RealDolmen
17:00 - 17:15 Wrap-up and prize raffle


The conference started with David Delabasée, the Java ambassador in Belgium-Luxembourgh, who gave an overview of the new things we are to expect regarding Java 7 as well as other Java technologies such as Java Card. A Java Card is a smart card that contains an application server in it! Hence, you can upload a servlet to itconnect it to various devices and call it from various programs and devices as David demonstrated. David also shown a roadmap of Java through expected version 7, mentioning OpenJDK/IcedTea project, the new Nimbus look & feel in JDK 6 update 10 (consumer JRE). The "Java Virutal Machine" becomes simply "Virtual Machine", as it now hosts not only Java but a large number of other languages such as Ruby, Groovy, Grails, Pascal etc. Characteristics of the new JDK 7 are:
  • hosting of many languages, as already mentioned
  • Profiles
  • JMX 2.0
  • Visual JVM (a nice tool that will graphically displays the internal of the VM); and
  • closures
Next, Stijn Van Den Enden, spoke about modularity and compared the current two frameworks, OSGi and JSR 277. As you already know, currently modularity in Java is addressed by packages and interfaces and there is no versioning or access control. If a class in one package needs to access a class in another package, the only way is to declare the class in the other package public, hoping that no one will find out that an internal class is exposed as public.
OSGi solves these problems with bundles. A bundle is a package with some useful metadata about the package in MANIFEST.MF. It separates private from public resources; determines package resolution; and allows versioning.
JSR 277, or Java Module System, is a new specification that addresses the same problem. The equivalent to OSGi bundle is called Java Module and it is packaged as a special kind of JAR, called JAM (JAva Module). Metadata is not in the manifest file, but as annotations.

Andreas Engloff gave a presentation about project Fuji, an integration profile project or standard plugin model for modules. It has three layers, Modularity, Service Orientation and application interface. It is based on OSGi, Open ESB and provides for Enterprise Integration. Andreas presented a demo of the simple workflow language one can use to easily get RSS feeds, filter them by JRuby and feed them to an XMPP (instant messaging) application. He wrote less than 10 lines of simple commands to accomplish this!

The session continued after the lunch break with Geert Bevin from Terracotta who presented 4 hot Java technologies:
  • Terracotta, an open source clustering framework which allows you to run your code to multiple JVMs seamlessly
  • RIFE, a full-stack component framework for web applications, which with the principle of continuation, allows you to stop, save and reload a web app state like one does with computer games
  • Google Web Toolkit; and
  • Google Android
Alexis Moussine-Pouchkine gave a presentation about upcoming Glassfish v.3 application server as well as about the new features of Java EE 6:
  • Extensibility
  • Profiles
  • Pruning; and
  • ease of development
After the coffee break, Jo Voordeckers gave a short presentation about JavaFX, or the return of the applet. JavaFX is the new scripting language provided by SUN, that hopes to boost and simplify both desktop and web development. The new "Java 6 update N" is less than 3 Mb, compared to ~12 Mb of current JRE 6, which can be quickly downloaded by clients' machines. However, JavaFX has a number of competitors, like Adobe Flex, MS Silverlight but also JRuby and others.

The conference ended with Stephan Janssens' presentation of parleys.com, and a comparison of 3 hot competitors in the RIA arena, namely, Adobe Flex, JavaFX and GWT.
Adobe provides AIR for desktop and Flex for Web applications; JavaFX is for both the desktop and the web; while GWT is only for developing Web applications. A thing to note is that Flex is only compatible with Flash version 9 or later, so he is developing a version of parleys.com with GWT which is compatible with Flash version 8 for clients that run this version of Flash.

The conference finished with some draws, and the winners won books and a playstation 3.

A well organised conference as I said, but in the middle of nowhere, with no public transport available, and difficult to go there if you don't have a car! :-)

Τρίτη, 24 Ιουνίου 2008

Hashtable vs. HashMap

HashMaps accept null values, while Hashtables don't.

import java.util.HashMap;
import java.util.Hashtable;

import junit.framework.TestCase;

public class HashMapTest extends TestCase {
private HashMap map = new HashMap();
private Hashtable hashtable = new Hashtable();

public void testHashMap() {
map.put("name", null);
assertNotNull(map);
assertEquals(1,map.size());
}

public void testHashtable() {
hashtable.put("name", null);
assertNotNull(hashtable);
assertEquals(1,hashtable.size());
}
}

The 2nd test simply throws a NullPointerException.

Hashtable is historical, according to JavaPractices.

Παρασκευή, 1 Φεβρουαρίου 2008

How to debug your web application in JBoss with Eclipse

For many developers, debugging a web application simply means looking at the logs and trying to figure out what went wrong. Fortunately, there is an easier way. You can debug a web/JEE application the same way you debug a JSE or JME application.
With today's IDEs you can also debug web/jee applications step by step, while the application is deployed to an application server like JBoss. The following trick works with JBoss application server, please contribute for other application servers.
OK, here is the trick. Open %JBOSS_HOME%/bin/run.bat (Windows) or $JBOSS_HOME/bin/run.sh (Linux) and uncomment the line that contains the port 8787, i.e. this line (by simply deleting the rem word in front):

rem set JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y %JAVA_OPTS%

If you cannot find the line in run.sh, try $JBOSS_HOME/bin/run.conf.

In your eclipse environment, click on the arrow next to the Debug button and select Debug... Select Remote Java Application and press the New button to create a new configuration of this type.
In the Connect tab, browse your web / jee eclipse project, leave the connection type to Standard (Socket Attach) and enter the host name (localhost if it is local) but modify the port to 8787 (this is JBoss' debugging port). In the Source tab you may wish to add any dependent projects. Click on Apply. That's it.

Now all you need to do is start your JBoss application server, deploy before or after your war or ear file. The application server stops waiting for someone to listen to port 8787. Click on Debug or select the above configuration in the Debug window, and voila, JBoss continues starting up.

Set your breakpoints etc. enjoy!

P.S. Netbeans has very good debugging integration with most of the application servers. If your application server is not listed in its list of available application servers, simply define it under Tools --> Server.

References
  1. Monitoring a remote JBoss Server from Eclipse