2011-08-18

Create a simple web application with Seam Forge Beta1, JBoss AS 7, Metawidget und RichFaces 4

The way to create a new project had changed since the last Seam Forge alpha-releases. The syntax is a bit differnt now. I use this configuration:


Open a command shell and start the JBoss AS with ..\jboss-as-7.0.0.Final\bin\standalone.bat (or standalone.sh within linux)
Start SeamForge with 'forge' in the forge-1.0.0.Beta1\bin-directory and let's begin.

The short story:

[no project] bin $ cd C:\Users\myuser\workspace
[MyApp] workspace $ new-project --named TestForge --topLevelPackage de.test
 ? Use [C:\/Users/myuser/workspace/TestForge] as project directory? [Y/n] Y
[TestForge] TestForge $ persistence setup --container JBOSS_AS7 --provider HIBERNATE
  ...
  34 - [org.hibernate:hibernate-entitymanager:::3.6.6.Final]
  ...
 ? Choose an option by typing the number of the selection [*-default]:  34
[TestForge] TestForge $ entity --named Person --package de.test.entity
[TestForge] Person.java $ field string --named firstname
[TestForge] Person.java $ field string --named surname
[TestForge] Person.java $ field int --named age
[TestForge] TestForge $ cd ..
[TestForge] TestForge $ scaffold setup --scaffoldType metawidget
 ? Scaffold provider [metawidget] is not installed. Install it? [Y/n] Y
 ? Facet [forge.maven.WebResourceFacet] requires packaging type(s) [war], but is currently [jar]. Update packaging? (Note: this could deactivate other plugins in your project.) [Y/n] Y
Install which version of Metawidget?
  ...
  4 - [org.metawidget.modules.faces:metawidget-faces:::1.25]

  ? Choose an option by typing the number of the selection: 4
Install which version of Seam Persistence?

  1 - [org.jboss.seam.persistence:seam-persistence:::3.0.0.Final]
  ...
 ? Choose an option by typing the number of the selection: 1
 ? [C:\/Users/myuser/workspace/TestForge/src/main/webapp/index.html] File exists, overwrite? [Y/n] Y
[TestForge] TestForge $ forge git-plugin git://github.com/forge/plugin-richfaces.git
[TestForge] TestForge $ exit
Start Forge again and go into the project directory...
[TestForge] TestForge $ cd C:\Users\myuser\workspace\TestForge
[TestForge] TestForge $ setup richfaces
  Which version of RichFaces?

  1 - [RichFaces 4.0.0.Final]

  ? Choose an option by typing the number of the selection: 1    
 ***ERROR*** [richfaces setup] null
 

Oops there was an error(maybe a bug), let's do manually.
We need:
  • in the file ...\workspace\TestForge\src\main\webapp\WEB-INF\metawidget.xml look for 'Only if using RichFaces' and uncomment the lines behind that
  • in ...\workspace\TestForge\pom.xml add the dependency:
  • 	
          org.metawidget.modules.faces
          metawidget-richfaces
          1.25
    	
    	
  • in ...\workspace\TestForge\src\main\webapp\WEB-INF\web.xml paste:
  • 
        Faces Servlet
        javax.faces.webapp.FacesServlet
        1
      
     
      
        Faces Servlet
        *.jsf
      
    
In SeamForge again:
[TestForge] TestForge $ cd C:\Users\myuser\workspace\TestForge
[TestForge] TestForge $ scaffold from-entity  de.test.entity.Person.java
 ? No scaffold type was selected, use default (Metawidget & JSF)? [Y/n]Y
[TestForge] TestForge $ build
[TestForge] TestForge $ as7 deploy

With the m2eclipse-Plugin you can import the project in Eclipse by selecting File->Import...->Maven->Existing Maven Project

Open http://localhost:8080/TestForge/scaffold/person/create.jsf in your browser and enjoy.




I found a documentation @jboss.org, take a look for more information: Generating a basic Java EE web-application

2011-08-16

Faster coding with content assist in eclipse

Sometimes writing common Java-Statements is a bit boring. I couldn't find a list with common statements, so I decided to create my own.


NameExample
arrayadd
/* add element to an array */
String[] myStringArray = 
    new String[5];
   
/* type 'arrayadd' and press ctrl + space 
* the new myStringArray2 will have length + 1
*/    
String[] myStringArray2 = 
    new String[myStringArray.length + 1];
System.arraycopy(
  myStringArray, 
  0, 
  myStringArray2, 
  0, 
  myStringArray.length);
myStringArray2[myStringArray.length] = 
  new String("newElement");
arraymerge
/* merge two arrays into one */
String[] myStringArray = new String[5];
String[] arrayToAdd = new String[5];
  
/* type 'arraymerge' and press ctrl + space */
String[] myStringArray2 = 
  new String[myStringArray.length
             + arrayToAdd.length];
System.arraycopy(
  myStringArray, 
  0, 
  myStringArray2, 
  0,
  myStringArray.length);
System.arraycopy(
  arrayToAdd, 
  0, 
  myStringArray2, 
  myStringArray.length,
  arrayToAdd.length);
cast
/* cast an object
* type 'cast' and press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/ 
  type new_name = (type) name;
catch
/* create a catch block
* type 'catch' and press ctrl + space 
*/ 
  catch (Exception e) {
  // TODO: handle exception
}
do
/* create a do-while loop
* type 'do' and press ctrl + space 
*/ 
  do {

  } while (condition);
else
/* create an else block
* type 'else' and press ctrl + space 
*/ 
  else {
  
  }
elseif
/* create an else-if block
* type 'elseif' and press ctrl + space 
*/ 
  else if (condition) {
   
  }
for - iterate over array
/* iterate over an array */
String string[] = new String[5];
  
/* type 'for', press ctrl + space and 
* select 'for - iterate over array'
*/ 
for (int i = 0; i < string.length; i++) {

}
for - iterate over array with temporary variable
/* iterate over array using a temporary variable */

String string[] = new String[5];
/* type 'for', press ctrl + space and 
* select 'for - iterate over array 
* with temporary variable'
*/ 
for (int i = 0; i < string.length; i++) {
  String string2 = string[i];
}
for - iterate over collection
/* a for-loop iterating over collection */

Set s = new HashSet();
  
/* type 'for', press ctrl + space and 
* select 'for iterate over collection'
*/  
for (Iterator iterator = s.iterator(); 
     iterator.hasNext();) {
  String string = (String) iterator.next();   
}
foreach
/* iterate over an array or Iterable */

Set s = new HashSet();
  
/* type 'foreach', press ctrl + space and 
* select 'for iterate over an array or
* Iterable
*/   
for (String string : s) {
 
}
if
/* if block
* type 'if', press ctrl + space  
*/ 
if (condition) {

}
ifelse
/* dynamic type test and cast */
String string = new String();
  
/* type 'instanceof', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/ 
  
if (string instanceof type) {
  type new_name = (type) string;
}
lazy
/* init a new object */
String string;

/* type 'lazy', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/ 
if (string == null) {
  string = new type(arguments);
}
return string;
main
/* create a main-method
* type 'main', press ctrl + space  
*/ 
public static void main(String[] args) {
 
}
new
/* create a new object
* type 'new', press ctrl + space  
*/ 
type name = new type(arguments);
private_method
/* create a private method
* type 'private_method', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/
private return_type name() {
  
}
private_static_method
/* create a private static method
* type 'private_static...', press ctrl + space  
* same as before, only static 
*/
protected_method
/* create a protected method
* type 'protected', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/
protected return_type name() {
  
}
public_method
/* create a public method
* type 'public_method', press ctrl + space  
* switch with tab between the fields and 
* fill in the values
*/
public return_type name() {
  
}
runnable
/* create a new runnable 
* type 'runnable', press ctrl + space 
*/   
new Runnable() {
  public void run() {
    
  }
}
static_final
/* create a new constant
* type 'static_final', press ctrl + space 
*/
public static final String NAME;
switch
/* create a switch block
* type 'switch', press ctrl + space 
*/
switch (key) {
case value:
 
  break;

default:
  break;
}
synchronized
/* create a synchronized block
* type 'synchronized', press ctrl + space 
*/
synchronized (args) {
   
}
syserr
/* print to standard error
* type 'syserr', press ctrl + space 
*/
System.err.println();
sysout
/* print to standard out
* type 'sysout', press ctrl + space 
*/
System.out.println();
systrace
/* print current method to standard out
* type 'systrace', press ctrl + space 
*/
System.out.println("MyClass.main()");
test
/* test method
* type 'test', press ctrl + space 
*/
public void testname() throws Exception {
  
}
toarray
/* convert collection to array */
Set hashSet = new HashSet();
  
/* type 'toarray', press ctrl + space */
(String[]) hashSet.toArray(
                   new String[hashSet.size()])
try
/* create try block
*/ type 'try', press ctrl + space 
*/
try {
   
} catch (Exception e) {
  // TODO: handle exception
}
while - iterate with enumeration
/* create a while loop(enumeration)
* type 'while', press ctrl + space
*/
while (en.hasMoreElements()) {
  type type = (type) en.nextElement();
   
}
while - iterate with iterator
/* create a while loop(iterator) */
Collection stringSet = new HashSet();
Iterator iterator = stringSet.iterator();

*/ type 'while', press ctrl + space */
while (iterator.hasNext()) {
 String string = (String) iterator.next();
  
}
while - while loop with condition
/* create a while loop(condition)
* type 'while', press ctrl + space
*/
while (condition) {
   
}
There are a lot more possibilities, take a closer look at Eclipse for additional information.

2011-08-05

Using a custom local IP for SFTP with JSch

I had to send a file using sftp to a remote host. Because there is more than one IP on my local host I had to look for a possibility setting the address. You can set the SocketFactory of the session you use. So writing your own SocketFactory allows you to use a custom IP.

Class MySocketFactory.java


In your class where sending the file, you can set the factory as follow:

2011-08-04

Fixing problem in hibernate "unknown Oracle major version [11]"

Using the datasource of JBoss 5.0.1 with Oracle 11g always causes the error

org.hibernate.HibernateException: unknown Oracle major 
version [11]

I wrote an emty class extending from org.hibernate.dialect.Oracle10gDialect

import org.hibernate.dialect.Oracle10gDialect;
package my.util;

public class My_Oracle_Dialect extends Oracle10gDialect{
 
 /* Workaround for bug "missing Oracle 11g dialect"*/
}

In the persistence.xml set the dialect:

...

org.hibernate.ejb.HibernatePersistence
    java:/MY_DS
    
      
                 
    
  
...

Maybe it is not the smartest solution, but it works...