The best place to ask programming/development questions, imo. UPDATE: stackoverflow is the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but a great reference for finding answers.

My Music (Nickleus)

20121130

TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple

our application was throwing the following error:
//////////////////////////////////////////////////////////
>30 Nov 2012 11:19:46,671  WARN     com.arjuna.ats.arjuna.logging.arjLoggerI18N                                                    [com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator_2] TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple@2f66531d
javax.persistence.PersistenceException: java.lang.NullPointerException
    at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:527)
...
Caused by: java.lang.NullPointerException
    at org.hibernate.type.IntegerType.next(IntegerType.java:59)
    at org.hibernate.engine.Versioning.increment(Versioning.java:108)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.getNextVersion(DefaultFlushEntityEventListener.java:365)

...
com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] Can't commit because the transaction is in aborted state
//////////////////////////////////////////////////////////



which was caused after running the following code:
em.merge(anObject);
em.flush(); // this one caused the problem



to better be able to find a clue about the error we had to enable some hibernate logging.

persistence.xml:
####################
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="EJBRelationshipsPU"
        transaction-type="JTA">

        ...
        <properties>
            ...
            <property name="hibernate.show_sql" value="true" />

            ...
        </properties>
    </persistence-unit>
</persistence>

####################


/usr/jboss/server/tcdomain/conf/myApp-jboss-log4j.xml:
####################
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

...
    <category name="org.hibernate.event.def" additivity="false">
        <priority value="trace"/>
        <appender-ref ref="stdout"/>
    </category>
    <category name="org.hibernate.loader" additivity="false">
        <priority value="trace"/>
        <appender-ref ref="stdout"/>
    </category>
    <category name="org.hibernate" additivity="false">
        <priority value="trace"/>
        <appender-ref ref="stdout"/>
    </category>

...
</log4j:configuration>
####################

we rebuilt the code and restarted the server in debugging. the configuration above will generate TONS of logging output, so to narrow it down we set a breakpoint on the following line:
em.merge(anObject);

then we cleared the console and clicked "Resume". now we could focus on just the relevant error code.



the following log output gave us a better clue about the problem:
//////////////////////////////////////////////////////////
>30 Nov 2012 11:18:47,175  DEBUG    org.hibernate.event.def.AbstractFlushingEventListener                                          dirty checking collections
>30 Nov 2012 11:18:47,176  DEBUG    org.hibernate.engine.CollectionEntry                                                           Collection dirty: [com.myapp.domainentitiesEJB3.transportrequest.TransportRequest.quantitiesAndStatusLogs#153570]
>30 Nov 2012 11:18:47,176  DEBUG    org.hibernate.engine.CollectionEntry                                                           Collection dirty: [com.myapp.domainentitiesEJB3.transportrequest.TransportRequest.trackAndTraceEvents#153570]
>30 Nov 2012 11:18:47,176  TRACE    org.hibernate.event.def.AbstractFlushingEventListener                                          Flushing entities and processing referenced collections
>30 Nov 2012 11:18:47,176  DEBUG    org.hibernate.engine.Collections                                                               Collection found: [com.myapp.domainentitiesEJB3.trackandtrace.TrackAndTraceEvent.action#39023], was: [com.myapp.domainentitiesEJB3.trackandtrace.TrackAndTraceEvent.action#39023] (uninitialized)
>30 Nov 2012 11:18:47,176  DEBUG    org.hibernate.engine.Collections                                                               Collection found: [com.myapp.domainentitiesEJB3.trackandtrace.TrackAndTraceEvent.freeText#39023], was: [com.myapp.domainentitiesEJB3.trackandtrace.TrackAndTraceEvent.freeText#39023] (uninitialized)
>30 Nov 2012 11:18:47,176  TRACE    org.hibernate.persister.entity.AbstractEntityPersister                                         com.myapp.domainentitiesEJB3.transportrequest.TransportRequest.version is dirty
>30 Nov 2012 11:18:47,176  TRACE    org.hibernate.persister.entity.AbstractEntityPersister                                         com.myapp.domainentitiesEJB3.transportrequest.TransportRequest.versionnumber is dirty

//////////////////////////////////////////////////////////


we checked out everything mentioned here and it seemed fine, but when we checked the versionnumber field in the database table for TRANSPORT_REQUEST it was null. versionnumber is what hibernate uses for versioning in the entity. so we set it to 1 and then everything worked fine!

what happened was that row had been created a long time ago before we switched to ejb3 so it didn't have a value, it was null.

thanks to my colleague Dan for much help in resolving this issue! :)

20121128

richfaces "white screen" (blank white page) bug after saving. code injected into header title tag

in our app we experienced some very mysterious behavior. when we submitted a form and redirected back to a list page, the operation would complete and the data would get saved, but the page we redirected back to (a list of price agreements) would render just a blank white page.

when we looked at the source code of that result page, we could see that the content got "injected" into the html head > title tag, therefore the page was blank.

the reason, we found out, was a mail-sending exception that wasn't getting properly handled, so it had nothing to do with richfaces. additionally, a mail template had a hardcoded method call that contained a spelling error so that was failing as well.

<ui:repeat value="#{myBean.someClass.despathParties}" var="despathParty">#{despathParty.name}, #{despathParty.city}<br /></ui:repeat>

despath should have been despatch

jboss seam components.xml mail-session jndi path ejb2. content "injected" into html head title tag

we were trying configure jboss seam's mail session in components.xml to use an ejb2 bean's local interface and finally found out that the jndi path was simply the local interface's full qualified package name:
<components xmlns="http://jboss.com/products/seam/components"
            xmlns:core="http://jboss.com/products/seam/core"
            xmlns:mail="http://jboss.com/products/seam/mail">
      <mail:mail-session session-jndi-name="com.myapp.x.MyBeanLocal" />
</components>


but after further configuration we settled on the following configuration:
<components xmlns="http://jboss.com/products/seam/components"
            xmlns:core="http://jboss.com/products/seam/core"
            xmlns:mail="http://jboss.com/products/seam/mail">
      <mail:mail-session host="smtp.mailserver.no" port="25" username="myUser" password="myPassword" />
</components>

20121121

richfaces 3, rich:modalPanel, google chrome bug, Uncaught TypeError: Cannot read property 'action' of null framework.pack.js:2797

i was testing our application and it worked fine in firefox, but when i tested it inside the chrome web browser i got the following error (in the chrome debug console) when i clicked on a form button inside a rich:modalPanel:

Uncaught TypeError:
Cannot read property 'action' of null framework.pack.js:2797
A4J.Query framework.
pack.js:2797
A4J.AJAX.P
repareQuery framework.pack.js:2566
A4J.AJAX.S
ubmit framework.pack.js:2596
onclick
here are the contents of framework.pack.js, line 2797 (irrelevant to the solution, but just for info):
this._acti
onUrl=(this._form.action)?this._form.action:this._form


my original code looked like this:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j" markupType="xhtml"
    contentType="text/html" lang="no">
    ...
    <a4j:outputPanel id="detailPanelHolder">
        <a4j:outputPanel>
            <h:form id="detailsForm">
                ...
                <h:panelGrid>
                    <a4j:commandButton ajaxSingle="true"
                        value="#{msg['label.new.priceset']}"
                        reRender="addPanel"
                        oncomplete="#{rich:component('addPanel')}.show();"
                        action="#{...}"/>
                </h:panelGrid>
                ...
            </h:form>

            <rich:modalPanel id="addPanel">
                ...
                <h:form id="add">
                    ...
                    <h:panelGrid columns="3" styleClass="dataTable"
                        footerClass="centered">
                        ...
                        <f:facet name="footer">
                            <h:panelGroup>
                                <a4j:commandButton ajaxSingle="true"
                                    value="#{msg['label.new.step']}"
                                    reRender="addPanel" id="addPanelNStepBtn"
                                    action="#{...}"/>
                            </h:panelGroup>
                        </f:facet>
                    </h:panelGrid>
                </h:form>
            </rich:modalPanel>
            ...
        </a4j:outputPanel>
    </a4j:outputPanel>
</ui:composition>



i didn't write the code myself and maybe the problem was partially due to the particular tag nesting, idk.


the solution that worked for me:
create an h:panelGroup inside the modalpanel h:form and rerender that panelGroup instead of the whole modalpanel, like this (changes in bold):

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j" markupType="xhtml"
    contentType="text/html" lang="no">
    ...
    <a4j:outputPanel id="detailPanelHolder">
        <a4j:outputPanel>
            <h:form id="detailsForm">
                ...
                <h:panelGrid>
                    <a4j:commandButton ajaxSingle="true"
                        value="#{msg['label.new.priceset']}"
                        reRender="addPricesetPanel"
                        oncomplete="#{rich:component('addPanel')}.show();"
                        action="#{...}"/>
                </h:panelGrid>
                ...
            </h:form>

            <rich:modalPanel id="addPanel">
                ...
                <h:form id="add">
                    <h:panelGrid id="addPricesetPanel">
                        ...
                        <h:panelGrid columns="3" styleClass="dataTable"
                            footerClass="centered">
                            ...
                            <f:facet name="footer">
                                <h:panelGroup>
                                    <a4j:commandButton ajaxSingle="true"
                                        value="#{msg['label.new.step']}"
                                        reRender="addPricesetPanel" id="addPanelNStepBtn"
                                        action="#{...}"/>
                                </h:panelGroup>
                            </f:facet>
                        </h:panelGrid>
                    </h:panelGrid>
                </h:form>
            </rich:modalPanel>
            ...
        </a4j:outputPanel>
    </a4j:outputPanel>
</ui:composition>

20121119

phantomjs with sahi OS on ubuntu 8.10 -- /lib/libc.so.6: version `GLIBC_2.9' not found (required by ./phantomjs)

it looks like you can't run phantomjs with sahi on ubuntu 8.10 because the glibc/libc version is 2.8 and from this output, it looks like it needs 2.9+:
./phantomjs: /lib/libc.so.6: version `GLIBC_2.9' not found (required by ./phantomjs)
./phantomjs: /lib/libc.so.6: version `GLIBC_2.10' not found (required by ./phantomjs)
./phantomjs: /lib/libc.so.6: version `GLIBC_2.11' not found (required by ./phantomjs)
./phantomjs: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by ./phantomjs


i got this output by manually running phantomjs like this:
./phantomjs --proxy=localhost:9999 /home/me/myProject/testing/sahi/userdata/phantomjs-1.7.0-linux-x86_64/nick/sahi.js http://localhost:9999/_s_/dyn/Player_auto?startUrl=http%3A%2F%2Fmy.server.no%3A9001__SahiAmpersandSahi__sahisid=sahi_a4363cfb01c9904f8109e4702ddc52cbdb40sahix14e58ffa0de95046b12b2da051862a21417ex__SahiAmpersandSahi__isSingleSession=true

20121114

regex to find nested/embedded for loops in java

most say it is impossible to make a failsafe regexp for finding nested for loops, but here's something i came up with that is somewhat close:
for\s*\((?:[\s\S](?!private|public))+?for\s*\(

this will find nested for loops when they occur, but the drawback is that it will also match multiple instances of for loops within methods defined as either private or public (if you have methods that begin with other modifiers [e.g. protected], you can add those too). e.g. it will match code like this:
############
for(List<String> c2 : c){
    int counter = 0;
    for(
String str : c2){
        s += buildFragment(counter, (str));
    }
}
############
for(int i = 0; i < x.size(); i++){
...
}

if(i2 != null){
    for(
int y = 0; y < z.size(); y++){
############

i'll try to explain the regex for you:
for\s*\(

start matching when you find the beginning of a for loop.
\s* means zero or more whitespace between for and (
to match a literal left parenthesis you need to escape it with a backslash: \(

(?:[\s\S](?!private|public))+

match 1 or more ( + ) of any character ( [\s\S] ) that isn't ( ?! ) followed directly by the words private OR ( | ) public,

?for\s*\(

and stop after the first instance ( ? ) of a match for the beginning of another for loop ( for\s*\( ).

20121113

[SOLVED] sahi ant task - java.net.ConnectException: Connection refused

i created a sahi ant task to run a test suite:
<target name="runSahiTests">
    <antcall target="start-web" />
    <antcall target="runSahi" />
    <antcall target="stop-web" />
</target>

<target name="start-web" description="starts web">
    <exec executable="../testing/sahi/userdata/bin/start_sahi.sh" osfamily="unix" spawn="true"/>
</target>

<target name="stop-web" description="stop web server">
    <get dest="stopserver.htm" src="http://localhost:9999/dyn/stopserver" ignoreerrors="true" />
    <delete file="stopserver.htm"/>
</target>

<taskdef name="sahi" classname="net.sf.sahi.ant.RunSahiTask" classpath="../testing/sahi/lib/ant-sahi.jar" />
<target name="failsahi" if="sahi.failed">
    <fail message="Sahi tests failed!" />
</target>
<target name="runSahi">
    <sahi suite="/home/me/workspace/myApp/testing/functional_testing/stage_1/sahi/smoketest.suite"
        browserType="firefox"
        baseurl="http://myserver.example.com:9001"
        sahihost="localhost"
        sahiport="9999"
        failureproperty="sahi.failed"
        haltonfailure="false"
        threads="1">
        <report type="html" />
        <report type="junit" logdir="/home/me/workspace/myApp/testing/sahi/userdata/temp/junit" />
    </sahi>
    <antcall target="failsahi" />
</target>


but when i ran it i would get the following error:
[sahi] java.net.ConnectException: Connection refused
[sahi]     at java.net.PlainSocketImpl.socketConnect(Native Method)


to fix this i inserted a sleep call in the start-web target:
<target name="start-web" description="starts web">
    <exec executable="../testing/sahi/userdata/bin/start_sahi.sh" osfamily="unix" spawn="true"/>

    <sleep seconds="5"/>
</target>


to give the sahi proxy server enough time to start up beforing running the sahi task.

i also noticed that red paths above had to be fully qualified paths instead of relative paths, otherwise the sahi task would start, but it would never do anything and i would have to kill it in order to stop it.

see also how to run sahi tests in ant build file using testrunner

20121112

[SOLVED] sahi - firefox closed unexpectedly while starting, running sahi with ant, NoClassDefFoundError

i'm trying to run a sahi test suite on my laptop and getting some weird behavior (open source [OS] version install_sahi_v35_20110719.jar).

here's my suite file, smoketest.suite:

// smoketest tests.  Run a few simple tests
test1.sah

test2.sah
test3.sah
test4.sah
test5.sah
test6.sah
test7.sah
test8.sah


i run this using ant (run target runSingleTestSahi):
    <target name="runSingleTestSahi">
        <antcall target="start-web" />
        <antcall target="sahiTest" />
        <antcall target="stop-web" />
    </target>


    <target name="sahiTest">
        <echo>testrunner start</echo>
        <exec executable="../testing/sahi/userdata/bin/testrunner.sh" failonerror="true" osfamily="unix">
            <arg value="smoketest.suite" />
            <arg value="http://a.localtestserver.no:8080" />
            <arg value="firefox" />
        </exec>
        <echo>testrunner end</echo>
    </target>

    <target name="start-web" description="starts web">
        <echo>start web start</echo>
        <exec executable="../testing/sahi/userdata/bin/start_sahi.sh" osfamily="unix" spawn="true"/>
        <echo>start web end</echo>
    </target>

    <target name="stop-web" description="stop web server">
        <echo>stop-web start</echo>
        <get dest="stopserver.htm" src="http://localhost:9999/dyn/stopserver" ignoreerrors="true" />
        <delete file="stopserver.htm"/>
        <echo>stop-web end</echo>
    </target>


when i run this smoketest.suite, firefox windows popup and i can see some of the scripts get run. after 150 seconds the script stops/times out and the log shows that some of the scripts are red. if i click on one it shows the following error:
ERROR
Script did not start within 150 seconds.


when i tried running the scripts that failed, individually, they would always give me the error:
firefox closed unexpectedly while starting...

but the second time i would run them, they would complete successfully.

SOLUTION:
i found out that it was the number of threads (default 5) defined in testrunner.sh that was causing the problem. when i changed it from 5 to 1 (cue doors music ;), then the whole suite ran without problems--it just takes more time to complete the test, but at least it works. :)

change:
export THREADS=5
to:
export THREADS=1


FULLY QUALIFIED PATHS IN SH SCRIPTS
another thing i had to do was convert all PATH values to fully qualified paths, instead of relative paths, otherwise, if i tried running the scripts with ant, it would give me the following error:
     [exec] Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/sahi/test/TestRunner
     [exec] Caused by: java.lang.ClassNotFoundException: net.sf.sahi.test.TestRunner
     [exec]     at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
     [exec]     at java.security.AccessController.doPrivileged(Native Method)
     [exec]     at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
     [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
     [exec]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
     [exec]     at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
     [exec] Could not find the main class: net.sf.sahi.test.TestRunner. Program will exit.



files with PATHs that needed to be changed (change the red parts of the paths to where you installed sahi):
sahi/userdata/bin/start_sahi.sh
#################

#!/bin/bash
export SAHI_HOME=/home/me/eclipseWorkspace/myApp/testing/sahi
export SAHI_USERDATA_DIR=$SAHI_HOME/userdata
export SAHI_EXT_CLASS_PATH=
. $SAHI_HOME/bin/sahi.sh
 
#################


sahi/userdata/bin/testrunner.sh
#################
#!/bin/bash
if [ $# -ne 3 ]
then
echo "Usage: testrunner.sh <sah file|suite file> <startURL> <browserType>"
echo "File path is relative to userdata/scripts"
echo "Example:"
echo "testrunner.sh demo/demo.suite http://sahi.co.in/demo/ <browserType>"
echo "testrunner.sh demo/sahi_demo.sah http://sahi.co.in/demo/ <browserType>"
else

export SAHI_HOME=
/home/me/eclipseWorkspace/myApp/testing/sahi
export USERDATA_DIR=$SAHI_HOME/userdata
export SCRIPTS_PATH=
$USERDATA_DIR/scripts/$1
export BROWSER=$3
export START_URL=$2
export THREADS=1
export SINGLE_SESSION=true
java -cp $SAHI_HOME/lib/ant-sahi.jar net.sf.sahi.test.TestRunner -test $SCRIPTS_PATH -browserType "$BROWSER" -baseURL $START_URL -host localhost -port 9999 -threads $THREADS -useSingleSession $SINGLE_SESSION
fi

#################


sahi/bin/sahi.sh
#################
if [ ! $SAHI_HOME ]
then
    export SAHI_HOME=/
home/me/eclipseWorkspace/myApp/testing/sahi
fi
if [ ! $SAHI_USERDATA_DIR ]
then
    export SAHI_USERDATA_DIR_TMP=$SAHI_HOME/userdata
else   
    export SAHI_USERDATA_DIR_TMP=$SAHI_USERDATA_DIR
fi   

echo --------
echo SAHI_HOME: $SAHI_HOME
echo SAHI_USERDATA_DIR: $SAHI_USERDATA_DIR_TMP
echo SAHI_EXT_CLASS_PATH: $SAHI_EXT_CLASS_PATH
echo --------

SAHI_CLASS_PATH=$SAHI_HOME/lib/sahi.jar:$SAHI_HOME/extlib/rhino/js.jar:$SAHI_HOME/extlib/apc/commons-codec-1.3.jar
java -classpath $SAHI_EXT_CLASS_PATH:$SAHI_CLASS_PATH net.sf.sahi.Proxy "$SAHI_HOME" "$SAHI_USERDATA_DIR_TMP"

#################

see also how to run sahi tests as a sahi ant task