10 Ocak 2011 Pazartesi

changing netbeans locale

i am trying to reengineer my master thesis work in order to prsesent it as a set of web services instead of a web application.. well, it is not as easy as it seems, since i am also migrating the development environment from eclipse + tomcat + mysql to netbeans 6.8 + glassfish v3 + derby coming with netbeans ide.. while glassfish v3 is started, it comes up with this error:
Server cannot operate in current Locale. Locale is switched to en_US for the process.

actually, after clicking on the dialog box with the error, the server starts and the web service is deployed successfully, i can see the wsdl.. however, when i try to generate client stubs for a java client, stubs seems problematic.. Its the - big i - issue, if you see: (it says illegal character \65533)
private final static URL ONTOLOGYMNGWSSERV�CE_WSDL_LOCATION;
private final static WebServiceException ONTOLOGYMNGWSSERV�CE_EXCEPTION;
private final static QName ONTOLOGYMNGWSSERV�CE_QNAME = new QName("http://ws.moass.com/", "OntologyMngWSService");
therefore, i want to change my Netbeans Locale to en_US in order to generate client stubs currectly..

unfortunately, changing the Locale by adding -locale en_US to netbeans_default_options= in NETBEANS_HOME/etc/netbeans.conf file did not help my cause..

i still get the error :(

Edit on 11/1/2011, 13:23:
i tried the same thing on my office computer which is an OpenSuse environment.. i checked the locale with env command, it is set to en_US.. luckily, the above error did not show up this time.. however, it tries to generate constructors such as
public CalculatorWSService(WebServiceFeature... features) {
super(__getWsdlLocation(), CALCULATORWSSERVICE_QNAME, features);
}
and the super method results in error this time..
by the way, i made the test with Netbeans 6.9.1 and glassfish v3.1..

on the contrary, my home computer is windows vista and here is how to change windows locale: http://windows.microsoft.com/en-US/windows7/Change-the-system-locale

2 Ocak 2011 Pazar

printing an object with reflection api (for debug purpose)

I am at work on a very very cold sunday morning.. then lets learn something in companionship of a cup of hot coffee in this empty office..
my aim is to print an objects attributes regardless of its type.. in other words, i do not want to write seperate debug statements for all my object types.. i want a static method written in java reflection api and use it everywhere.. by the way, i am only interested in printing methods that start with get...()..

good code samples here: http://java.sun.com/developer/technicalArticles/ALT/Reflection/

here is the code to do that:
public static void printObject(String className, Object classItself) {
try {
System.out.println("PRINTING-OBJ " + className);
Class c = Class.forName(className);
Field[] f = c.getFields();
for (int i = 0; i <>
System.out.println(f[i]);
}
Method[] m = c.getDeclaredMethods();
for (int i = 0; i <>
// System.out.println(m[i].getName());
if (m[i].getName().startsWith("get")) {
try {
System.out.println("METHOD: " + m[i].getName() + " : " + m[i].invoke(classItself, null));
} catch (Exception ex) {
// do nothing
}
}
}
System.out.println("END OF PRINTING-OBJ " + className);
} catch (Exception ex) {
// do nothing
}
}
a sample call:

Helper.printObject(Parameter.class.getCanonicalName(), parameter);

output:

PRINTING-OBJ .admin.Parameter
METHOD: getName : name
METHOD: getDescription : description
METHOD: getCode : -10117439
END OF PRINTING-OBJ admin.Parameter

28 Aralık 2010 Salı

revisited: migration from open esb + glassfish to apache tomcat + ode

please see http://hilaltarakci.blogspot.com/2010/03/migration-from-open-esb-glassfish-to.html for prior knowledge about the topic..
i have to migrate our processes from glassfish+open esb to apache tomcat+apache ode..
in this post, i simply note down differences or additions to the above prior post.
first of all, this time, i used latest versions:
  • apache ode : 1.3.4 instead of 1.3.3
  • apache tomcat: 7.0.5 instead of 6.0.24
  • metro: 2.0.1 instead of 2.0
CATALINA_HOME, JAVA_HOME and JRE_HOME environment variables are defined properly.

While installing metro on tomcat, i used the command
ant -f metro-on-tomcat.xml install
and there is luckily no problems due to version changes..

the correct arrangement of web.xml and creation of sun-jaxws.xml under project-home/web/WEB-INF is not straightforward this time, since there is a number of web services deployed in the same .war file. so, i want to explain the details..
here some links:

a small tip: after rewriting web.xml and sun-jaxws.xml, in order to prepare the .war file under dist folder, right click and build the project in netbeans ide.. choosing clean and build results in wiping out sun-jaxws.xml and overwriting web.xml..

a sample web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<description>Balistika2010Ws</description>
<display-name>Balistika2010Ws</display-name>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint - Service1</description>
<display-name>Service1</display-name>
<servlet-name>Service1</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Service1</servlet-name>
<url-pattern>/ode/processes/Service1</url-pattern>
</servlet-mapping>
<servlet>
<description>JAX-WS endpoint - Service2</description>
<display-name>Service2</display-name>
<servlet-name>Service2</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Service2</servlet-name>
<url-pattern>/ode/processes/Service2</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

a sample corresponding sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>

<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint
name='Service1'
implementation='tr.gov.tubitak.service.Service1'
url-pattern='/ode/processes/Service1'/>
<endpoint
name='Service2'
implementation='tr.gov.tubitak.service.Service2'
url-pattern='/ode/processes/Service2'/>
</endpoints>

The url-pattern attributes in web.xml and sun-jaxws.xml should match.

The implementation attribute is the web service itself..

However, tomcat throws the following error:
SEVERE: WSSERVLET11: failed to parse runtime descriptor: javax.xml.ws.WebServiceException: Unable to create JAXBContext
javax.xml.ws.WebServiceException: Unable to create JAXBContext
at com.sun.xml.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:166)
...
Caused by: java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
javax.persistence.EntityManager is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at javax.persistence.EntityManager
at public javax.persistence.EntityManager tr.gov.tubitak.uzay.balistika2010.sas.service.series.jaxws.MergeAbsEvidGrpsInCase.arg1
at tr.gov.tubitak.uzay.balistika2010.sas.service.series.jaxws.MergeAbsEvidGrpsInCase
javax.persistence.EntityManager does not have a no-arg default constructor.

The error is due to the fact that i pass EntityManager between (public) method calls in order to preserve my transactions.. see this for details: http://hilaltarakci.blogspot.com/2010/12/persistence-using-jpa.html

so, how to fix this? for now, i redesigned the code in such a way that EntityManager passing methods are all defined as private..

in fact, defining problematic methods as private solved some other weird errors as well.. i do not record them, since i did not understand their nature!!

at this point, web services are all deployed successfully and i can see wsdl s..

now the bpel part.. if you do not bind all partner links that is used in mentioned process in deploy.xml, you get the following error:
16:52:58,729 ERROR [NStateLatch] Latch error, was releasing for state 1 but actually in -1
16:52:58,731 WARN [ProcessStoreImpl] Deployment failed within the engine, store undeploying process.
java.lang.IllegalArgumentException: SecurityPL must be bound to an endpoint in deploy.xml
at org.apache.ode.bpel.engine.BpelProcess.setRoles(BpelProcess.java:519)

deploy.xml of ode-home/examples/DynPartner is a good sample..

now, it is time to test deployed processes from a java client.. i created client side artifacts from manually written wsdl in the deployed bpel module, not from the endpoint itself..

when i called the process from the client, i got the following error:

SEVERE: org.objectweb.asm.ClassWriter.(I)V
java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(I)V
at net.sf.cglib.core.DebuggingClassWriter.(DebuggingClassWriter.java:47)
...
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at tr.gov.tubitak.uzay.balistika2010.sas.dao.base.BlsEntityManager.getEm(BlsEntityManager.java:20)
at tr.gov.tubitak.service.Service1.method1(Service1.java:116)

no need to say, the process calls a web service which persists data inside a transaction.. according to http://www.myeclipseide.com/PNphpBB2-viewtopic-t-24287.html, the error was due to conflicting asm.jar s around.. actually, i faced the same problem before in a different environment: http://hilaltarakci.blogspot.com/2010/02/javalangnosuchmethoderror.html
in deployed web services library, asm.jar was of version 1.5.3; upgrading it to version 3.3.1 solved the mentioned error..

last update on 29.12.2010, 11:30..

22 Aralık 2010 Çarşamba

using blob field with jpa and web services

In the project, client side has to store local machine specific data in the cenral database and this field has to be binary large object (blob).. Jpa with hibernate is used as orm tool..Unfortunately, according to http://opensource.atlassian.com/projects/hibernate/browse/JPA-8 jpa does not support java.sql.Blob.. Indeed, the following error shows up when i tried to do so:
// bean
import java.sql.Blob;
import javax.persistence.Lob;

class MyClass{
@Lob
@Column(name = "FLD_BLOB")
private Blob myBlob;
...
}

// the error when deployed on Glassfish (jaxws is used for web services)
javax.xml.ws.WebServiceException: Unable to create JAXBContext ...
Caused by: java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
java.sql.Blob is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at java.sql.Blob
at public java.sql.Blob
...
java.sql.Blob does not have a no-arg default constructor.
...
java.sql.Blob is an interface, and JAXB can't handle interfaces.
...

So, use byte [] instead of java.sql.Blob

Web service using the bean with blob field is succesfully deployed and in the wsdl, the blob field is like this:
< xs:element name= " myBlob " type= " xs:base64Binary " minoccurs= " 0 " >
Moreover, when client side stubs are generated, the field is again generated as byte []..

Testing the blob field:
I used the code at http://www.java-tips.org/java-se-tips/java.io/reading-a-file-into-a-byte-array.html in order to prepare test data from a file and the following lines to write back the read bytes into a file to test correctness:

FileOutputStream fos = new FileOutputStream( "blobDataReadFromDB.png");
fos.write(myBean.getMyBlob());

The test data is a 5.4 MB-mp3.

Two test cases are considered.
In the first test case, the web services are treated as applications and tested without deployment. The blob field is successfully persisted and retrieved from database in this case..
In the second test case, web services are deployed and then tested.. The blob data is persisted successfully, but, the following error shows up when trying to retreive the data: (the underlying database is postgresql..)
ERROR org.hibernate.util.JDBCExceptionReporter - Large Objects may not be used in auto-commit mode.
...
sun-org.hibernate.exception.GenericJDBCException: could not execute query at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
....
Caused by: org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode.
at org.postgresql.largeobject.LargeObjectManager.open(LargeObjectManager.java:200)

Here is someone in a similar situation: https://forum.hibernate.org/viewtopic.php?f=1&t=994742&start=0 The following answer in this link, worked for me:

You might need to put your DB operations into a transaction:
session.beginTransaction();
...
session.getTransaction().commit();

Normally, retrieve operations do not require a transaction since they leave the database unchanged.. You only use transactions for create, update and delete operations since they change database state.. However, retrieval of blob field require transaction as well, maybe due to paging..

I also used and tested the blob field through a bpel process.. Again, persistence and retrieval is successful..

21 Aralık 2010 Salı

parsing an xml portion (without dom)

My problem is trying to parse an xml portion stored as a string. In trivial case, this is pretty easy by using indexOf() and lastIndexOf() methods, however, when the namespace is dynamic, the problem becomes a little tricky. For instance, when looking for foo, the data may come between
<foo>foo data < /foo> or
<ns1:foo>foo data< /ns1:foo> or
<ns2:foo>foo data< /ns2:foo> and so on..

In my case, i used such a simple solution:

String upToFooPre = fullMessage.substring(0,fullMessage.lastIndexOf("foo&gt ;"));
String upToFoo = upToFooPre.substring(0, upToFooPre.lastIndexOf("&lt ;"));
String foo = upToFoo.substring(upToFoo.lastIndexOf(("&gt ;")) + 4 );