Samstag, 27. Juni 2009

Liferay get all Roles

If you need to get all roles from your liferay portal you can achieve it in this way:



public void listAllRoles()
{
try {
List companies=CompanyLocalServiceUtil.getCompanies();
for(Company company:companies)
{
List roles=RoleLocalServiceUtil.getRoles(company.getCompanyId());
for(Role role:roles)
{
System.out.println(role.getRoleId()+" "+role.getName());
}
}
} catch (SystemException e) {
e.printStackTrace();
}
}

Dienstag, 15. Juli 2008

Spring Dynamic Modules

The Equinox servlet bridge is a nice thing. If you only need JSP / JSTL it's a super base to build your OSGi-Webapplication. The problem is there are not yet JSF Plugins to get running JSF.
So there are some ways to achieve this. One of them is to use Spring Dynamic Modules. But the begin is never easy. But I was lucky and found a great tutorial on springosgi.googlepages.com. Because I had many questions in the Google Group Spring Osgi Oleg was so friendly and extended his tutorial on JSF. Great!

Donnerstag, 17. April 2008

Equinox in a Servlet Container Step by Step

If you want to run OSGi with Servlets on an Apache Tomcat for example a very good start is Equinox in a Servlet Container
First do the things with the number 1,2 and 3 you can find in the section Quickstart.
And now it is time to install and get to run the sample.http and sample.http.registry examples.
Download these both examples and extract them anywhere on your harddisk.
Now start Eclipse and choose File>Import>General>Existing Projects into Workspace and import these both projects.Then these both projects can be seen in your eclipse project explorer. Now export them both as jar on your harddisk and please not in a directory of your tomcat. Make a rightklick on the sample.http project and choose export>jar.

In the next step choose a place where the jar should be exported. It is not in your tomcat directory but anywhere on your harddisk:

In the next window click on next and be careful with the next window:

Here you have to choose the manifest file of the sample.http project. Click then on finish and do these same steps with the sample.http.registry.
At the end you should have two jars.

Tomcat should run now, so please start it if it not does.
You should see the console now:

Now write in this window -console and hit the Enter-button to get the osgi console. The result should be that:

Now you have to install these two jars you exported before. Enter in the console
install file:/pathofthejar/jarname for the samplehttp.jar and the samplehttpregistry.jar.
In my case it is this:

The result are two ids for every jar.
Now start this jars with start id. In my case it is start 14 and start 15 as you see here:

Now with the command ss you can see the status and these both plugin should run and have the status ACTIVE.

Now your plugins are running and correctly installed.
You can see them in action if you type hostOfTomcat/directory where bridge is installed/helloworld for sample.http and hostOfTomcat/directory where bridge is installed/ext/helloworld for sample.http.registry.
In my case http://localhost:8080/bridge/helloworld and http://localhost:8080/bridge/ext/helloworld
Have fun!

Montag, 6. August 2007

Java Strings vergleichen

Im JavaForum taucht mindestens zweimal in der Woche die Frage auf, warum denn in Java
ein String Vergleich mit einem == nicht zum gewünschten Ergebnis führt


public class FalscherStringVergleich
{
String a="Hallo";
String b="Hallo";

public static void main(String[] args)
{
//Die Ausgabe ist false
System.out.println(a==b);

}

}


Die Ausgabe ist false obwohl beide Strings den gleichen Inhalt haben und eigentlich true sein sollte. Warum ist das so?
Der Vergleich mit a==b prüft nicht auf den Inhalt der Stringobjekte sondern auf den Speicherplatz wo diese sich im Ram befinden.
Da der String a und der String b verschiedene Objekte der Klasse String sind, befinden sie sich natürlich nicht auf der selben Speicherstelle im Ram und deshalb liefert der Vergleich false was soviel aussagt, dass sich beide Objekte nicht auf dem selben Platz befinden.

Doch wie prüft man jetzt ob zwei Strings denselben Inhalt haben?

Mit equals() oder mit equalsIgnoreCase()
Equals() liefert true wenn der Inhalt zweier Strings gleich ist, also beispielsweise wenn a="Hallo" und b="Hallo" ist.
Spielt die Großschreibung bzw. Kleinschreibung keine Rolle so wählt man equalsIgnoreCase() die true liefert beispielsweise für a="Hallo" und b="hallo". Die Methode equals() würde in diesem Fall false liefern.


equals()

public class RichtigerStringVergleichEquals
{
String a="Hallo";
String b="Hallo";

public static void main(String[] args)
{
//Die Ausgabe ist true
System.out.println(a.equals(b));

}

}



equalsIgnoreCase()

public class RichtigerStringVergleichEqualsIgnoreCase
{
String a="Hallo";
String b="hallo";

public static void main(String[] args)
{
//Die Ausgabe ist true
System.out.println(a.equalsIgnoreCase(b));

}

}

Freitag, 4. Mai 2007

Hello World in Java

Hallo Welt in Java


Hallo,

ihr möchtet Java lernen aber wißt nicht wie ihr anfangen sollt?
Nun nunmal haben alle Programmierer mit einem einfachen Beispiel angefangen,
den sogenanntent HelloWorld Programm. Wie der Name schon sagt, handelt es sich um ein Programm das nix anderes tut als "Hello World" auf der Konsole auszugeben.
Aber Zuerst müssen wir Java installieren. Dazu gehen wir zuerst auf die Webseite
Sun.de um das Java JDK zu beziehen.
Wichtig: Ihr müßt dass Java JDK installieren Version 1.5 oder 1.6 nicht das JRE!
Das JDK ist das Java Development Kit, das gebraucht wird um Javaprogrammer schreiben zu können. Es beinhaltet auch die Java JRE. Das Java JRE ist das Java Runtime Environment, dass genügt wenn man nur Javaprogramme laufen lassen möchte, aber falls man selbst Code schreiben will, so ist das nicht ausreichend.

Wir wählen den Menüpunkt Downloads und Java 2 Standard Edition.





Auf der nächsten Seit wählt man die gewünschte Javaversion aus. Es empfielht sich immer die aktuellste Version zu wählen. Mit einem Klick auf Download geht es weiter.





Nun muß man Accept wählen um den Lizenzbedingungen zustimmen zu können.




Nachdem man Accept gewählt hat, wählt man die gewünschte Javaversion für das jeweilige Betriebssystem aus. Es gibt die Auswahl zwischen eine Online- und einer Offlineinstallation. Dazu sei gesagt, dass jeweils die gleiche Javaversion installiert wird. Der Unterschied ist der, dass bei einer Offlineinstallation das ganze JDK zuerst heruntergeladen wird, bevor die Installation losgeht. Die Onlineversion, lädt das JDK während der Installation sukzessiv herunter. Das Ergebnis bei beiden Installationsarten ist jedoch dasselbe: Man hat am Ende das JDK auf seinem Rechner.




Tippt den folgenden Code ab und speichert in einem Editor eurer Wahl unter
ErsteKlasse.java ab.
Der Dateiname-also ErsteKlasse.java muß dem Klassennamen aus dem Code -in der Farbe orange-gleichen.
Compiliert diese Datei mit javac ErsteKlasse.java und startet anschließend das Programm mit java ErsteKlasse

public class ErsteKlasse
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}