The best place to *find* answers to programming/development questions, imo, however it's 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 again, a great reference for *finding* answers.

My Music (Nickleus)

20121231

eclipse svn cant commit file - at least one resource should be selected

i came across something weird when i was manually merging 2 projects (compare with > each other). i had copied a folder from Project A to Project B, then tried to commit the folder in Project B, but got the following message:

at least one resource should be selected



then when i deleted the folder and tried to commit the deletion instead (not knowing it wasn't in svn at all) i got the following message:

Some of selected resources were not added to version control.
Some of selected resources were not committed.
svn: Can't add '/home/me/workspaceJuno/projectB/testfolder/testfile.txt' to a parent directory scheduled for deletion

svn: Working copy '/home/me/workspaceJuno/
projectB/testfolder' locked



i finally figured out that the problem was that when i copy/pasted the testfolder inside eclipse, from Project A to Project B, it had copied the .svn folder inside testfolder as well, so eclipse thought it was already in svn in Project B. so when you copy folders inside eclipse from one project to another, you need to manually delete the .svn folder in a normal file browser, e.g. nautilus, then you will be able to commit the folder.

20121219

eclipse f3 stateless bean interface class name opening binary class file instead of java source file

i was editing a bean and when i clicked on the class name it implemented, eclipse opened a binary class file instead of the java source.

the problem was the order of my classpath entries. to fix this i right clicked on my project > properties > java build path > order and export

then i moved all the source folders to the top.

this page helped me out:
http://www.herikstad.net/2011/04/eclipse-opens-class-instead-of-java.html


apache directory studio not showing userPassword field

in an earlier post, i wrote about how to find ldap user passwords:
http://nickhumphreyit.blogspot.no/2012/01/how-to-list-openldap-ldap-user-info-in.html

but yesterday when i tried to view a user's password in apache directory studio, the record for a particular user wasn't showing any userPassword field. i finally figured out that my java code was accidentally setting the user's password to null, thus removing the userPassword field from the record, so it wasn't showing because it didn't exist! (facepalm) ;)

javax.persistence.PersistenceException: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance

i was trying to perform a simple delete operation in our ejb3/hibernate application and was getting the following error:
javax.persistence.PersistenceException: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.myapp.domainentitiesEJB3.party.Party.references
Transaction failed


the problem was in our Party entity class:
public void setReferences(List<Reference> references_) {
    this.references = references_;
}


it had to be changed to:
public void setReferences(List<Reference> references_) {
    if(this.references != null && references_ != null && !references_.isEmpty()){
        this.references.clear();
        for(int i = 0; i < references_.size(); i++){
            Reference lp = references_.get(i);
                this.references.add(lp);
        }
        this.references.addAll(references_);
    }
    else this.references.clear();
}


the problem was that we were trying to overwrite the collection, but by doing that, hibernate lost "ownership" of the collection and threw an error. it's best explained in the article Let's Play "Who Owns That Collection?" With Hibernate

20121218

sahi sah test script - ERROR missing ( before function parameters

i tried running a seemingly well-coded sahi test script, but i only got FAILURE every time i ran it. the log output was simply that my script was missing a left parenthesis somewhere (which wasn't the case i found out):
ERROR
missing ( before function parameters.


here's the sahi code that was failing:
...
view_invoice_specification_list();
...

and here's that function code:
function view_invoice_specification_list(){
    _click(_span("Fakturaspesifikasjon"));
    _assert(_isVisible(_tableHeader("Fakturaspesifikasjon")));
}



in the sahi dashboard i clicked "view parsed script" and saw the following weird output:
function view_invoice_specification_sahi._list(){
...
}

...
view_invoice_specification_sahi._list();
...


the problem turned out to be the use of the word "list" in the function name. i renamed the function name to view_invoice_specification_lst(), and then it worked. it also works if i put an underscore after "list", e.g. view_invoice_specification_list_()

20121216

java webservices basic auth username password WebServiceException: Failed to access the WSDL Server returned HTTP response code 401

in my post java webservices wsimport - parsing WSDL [ERROR] Server returned HTTP response code: 401 for URL: http://?wsdl needs authorization

i explained how i got an authentication error when trying to create webservices client code using wsimport and how i fixed the problem.

when i tried using the code to test my webservice i got a similar error when running my test code:

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:9001/wservice3/services/AdminWebService?wsdl. It failed with:
    Server returned HTTP response code: 401 for URL: http://localhost:9001/wservice3/services/AdminWebService?wsdl.
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:151)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
    at javax.xml.ws.Service.<init>(Service.java:76)
    at com.myapp.test.wservice3.server3.AdminWebServiceService.<init>(AdminWebServiceService.java:46)
    at com.myapp.test.wservice3.server3.Test.main(Test.java:8)
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:9001/wservice3/services/AdminWebService?wsdl
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
    at java.net.URL.openStream(URL.java:1031)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:793)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:251)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:118)
    ... 7 more



it's basically the same error i got when running wsimport because the service requires authentication to fix the problem i had to insert authorization code into the service.

here's a sample of the webservice code generated by wsimport in AdminWebServiceService:
static {
    URL url = null;
    try {
        URL baseUrl;
        baseUrl = com.myapp.test.wservice3.server3.AdminWebServiceService.class.getResource(".");
        url = new URL(baseUrl, "http://localhost:9001/wservice3/services/AdminWebService?wsdl");
    } catch (MalformedURLException e) {
        logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9001/wservice3/services/AdminWebService?wsdl', retrying as a local file");
        logger.warning(e.getMessage());
    }
    ADMINWEBSERVICESERVICE_WSDL_LOCATION = url;
}

 


to get things rolling i had to make the code look like this:
static {
    Authenticator.setDefault(new Authenticator() {
     @Override
     protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication(
         "myusername",
         "mypassword".toCharArray());
     }
    });

    URL url = null;
    try {
        URL baseUrl;
        baseUrl = com.myapp.test.wservice3.server3.AdminWebServiceService.class.getResource(".");
        url = new URL(baseUrl, "http://localhost:9001/wservice3/services/AdminWebService?wsdl");
    } catch (MalformedURLException e) {
        logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9001/wservice3/services/AdminWebService?wsdl', retrying as a local file");
        logger.warning(e.getMessage());
    }
    ADMINWEBSERVICESERVICE_WSDL_LOCATION = url;
}


posts that helped me figure this out:
http://www.xinotes.org/notes/note/1081/
http://etfdevlab.blogspot.no/2009/12/http-basic-authentication-with-jax-ws.html

in the last post it mentions putting the Authenticator code in the constructor, but that wasn't the case for my code generated by wsimport. i had to put the Authenticator code into the static block at the top of the webservice service class.






 

here's what the full webservice service class code looks like:

package com.myapp.test.wservice3.server3;

import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.logging.Logger;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;


@WebServiceClient(name = "AdminWebServiceService", targetNamespace = "http://server3.wservice3", wsdlLocation = "http://localhost:9001/wservice3/services/AdminWebService?wsdl")
public class AdminWebServiceService
    extends Service
{

    private final static URL ADMINWEBSERVICESERVICE_WSDL_LOCATION;
    private final static Logger logger = Logger.getLogger(com.myapp.test.wservice3.server3.AdminWebServiceService.class.getName());

    static {
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(
                "myusername",
                "mypassword".toCharArray());
            }
           });
        URL url = null;
        try {
            URL baseUrl;
            baseUrl = com.myapp.test.wservice3.server3.AdminWebServiceService.class.getResource(".");
            url = new URL(baseUrl, "http://localhost:9001/wservice3/services/AdminWebService?wsdl");
        } catch (MalformedURLException e) {
            logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9001/wservice3/services/AdminWebService?wsdl', retrying as a local file");
            logger.warning(e.getMessage());
        }
        ADMINWEBSERVICESERVICE_WSDL_LOCATION = url;
    }

    public AdminWebServiceService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public AdminWebServiceService() {
        super(ADMINWEBSERVICESERVICE_WSDL_LOCATION, new QName("http://server3.wservice3", "AdminWebServiceService"));
    }
 

    @WebEndpoint(name = "AdminWebService")
    public AdminWebService getAdminWebService() {
        return super.getPort(new QName("http://server3.wservice3", "AdminWebService"), AdminWebService.class);
    }


    @WebEndpoint(name = "AdminWebService")
    public AdminWebService getAdminWebService(WebServiceFeature... features) {
        return super.getPort(new QName("http://server3.wservice3", "AdminWebService"), AdminWebService.class, features);
    }
}



and here's what my test class looks like that calls the webservice:

package com.myapp.test.wservice3.server3;

public class Test {
    public static void main(String[] args) {
        AdminWebServiceService awss = new AdminWebServiceService();
        AdminWebService aws = awss.getAdminWebService();
        System.out.println(aws.maxIdFromMessageCounter());
    }
}

three dots (ellipsis)/periods/full stops in java method parameter: "WebServiceFeature... features"

in my last post:
java webservices wsimport - parsing WSDL [ERROR] Server returned HTTP response code: 401 for URL: http://?wsdl needs authorization

i created a webservice client by using the commandline tool wsimport. in the generated code i noticed that one of the methods looked like this:

@WebEndpoint(name = "AdminWebService")
public AdminWebService getAdminWebService(WebServiceFeature... features) {
    return super.getPort(new QName("http://server3.wservice3", "AdminWebService"), AdminWebService.class, features);
}


notice the three dots (ellipsis) in the method parameters:
getAdminWebService(WebServiceFeature... features)

i wondered if this was an error, but it's also in the api:
http://docs.oracle.com/javase/6/docs/api/javax/xml/ws/class-use/WebServiceFeature.html


then i found the java documentation about Arbitrary Number of Arguments:

You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually...

To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.

20121214

java webservices wsimport - parsing WSDL [ERROR] Server returned HTTP response code: 401 for URL: http://?wsdl needs authorization

i tried automatically creating a java webservices client for the following wsdl:
http://localhost:9001/wservice3/services/AdminWebService?wsdl

but running the following command in a terminal:
wsimport -keep -verbose http://localhost:9001/wservice3/services/AdminWebService?wsdl

gave me the following error:
parsing WSDL...

[ERROR] Server returned HTTP response code: 401 for URL: http://localhost:9001/wservice3/services/AdminWebService?wsdl,  "http://localhost:9001/wservice3/services/AdminWebService?wsdl" needs authorization, please provide authorization file with read access at /home/me/.metro/auth or use -Xauthfile to give the authorization file and on each line provide authorization information using this format : http[s]://user:password@host:port//<url-path>


so to make it work i had to first create an authfile composed of the wsdl URI plus my username and password:
echo http://myUsername:myPassword@localhost:9001/wservice3/services/AdminWebService?wsdl > wsxauthfile

then i ran the wsimport command again, this time with the flag -Xauthfile:
wsimport -keep -verbose -Xauthfile wsxauthfile http://localhost:9001/wservice3/services/AdminWebService?wsdl

and it generated the code for me in the current directory (i ran it from ~/Downloads/)

:)

-------------------FYI----------------------
here's the output i saw:

parsing WSDL...
[INFO] Trying to read authorization file : "wsxauthfile"...
generating code...
wservice3/server3/AdminWebService.java
wservice3/server3/AdminWebServiceService.java
wservice3/server3/GetNextThousandNewMessageCounter.java
wservice3/server3/GetNextThousandNewMessageCounterResponse.java
wservice3/server3/GetNextThousandNewTransportRequest.java
wservice3/server3/GetNextThousandNewTransportRequestResponse.java
wservice3/server3/MaxIdFromMessageCounter.java
wservice3/server3/MaxIdFromMessageCounterResponse.java
wservice3/server3/MaxIdFromTransportRequest.java
wservice3/server3/MaxIdFromTransportRequestResponse.java
wservice3/server3/MinIdFromMessageCounter.java
wservice3/server3/MinIdFromMessageCounterResponse.java
wservice3/server3/MinIdFromTransportRequest.java
wservice3/server3/MinIdFromTransportRequestResponse.java
wservice3/server3/ObjectFactory.java
wservice3/server3/package-info.java

compiling code...

javac -d /home/me/Downloads/. -classpath /usr/lib/jvm/java-6-openjdk-amd64/lib/tools.jar:/usr/lib/jvm/java-6-openjdk-amd64/classes -Xbootclasspath/p:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/rt.jar /home/me/Downloads/./wservice3/server3/AdminWebService.java /home/me/Downloads/./wservice3/server3/AdminWebServiceService.java /home/me/Downloads/./wservice3/server3/GetNextThousandNewMessageCounter.java /home/me/Downloads/./wservice3/server3/GetNextThousandNewMessageCounterResponse.java /home/me/Downloads/./wservice3/server3/GetNextThousandNewTransportRequest.java /home/me/Downloads/./wservice3/server3/GetNextThousandNewTransportRequestResponse.java /home/me/Downloads/./wservice3/server3/MaxIdFromMessageCounter.java /home/me/Downloads/./wservice3/server3/MaxIdFromMessageCounterResponse.java /home/me/Downloads/./wservice3/server3/MaxIdFromTransportRequest.java /home/me/Downloads/./wservice3/server3/MaxIdFromTransportRequestResponse.java /home/me/Downloads/./wservice3/server3/MinIdFromMessageCounter.java /home/me/Downloads/./wservice3/server3/MinIdFromMessageCounterResponse.java /home/me/Downloads/./wservice3/server3/MinIdFromTransportRequest.java /home/me/Downloads/./wservice3/server3/MinIdFromTransportRequestResponse.java /home/me/Downloads/./wservice3/server3/ObjectFactory.java /home/me/Downloads/./wservice3/server3/package-info.java

20121206

how to add jboss 7 server in eclipse juno on ubuntu 12.04 - Missing classpath entry ...jboss-as-7.1.1.Final/bin/run.jar, no new server runtime environments were found

i imported a project of ours that uses jboss 7, but i wasn't able to create a new jboss 7 server instance in eclipse (on ubuntu linux 12.04). i got the following errors:
"Missing classpath entry /home/me/workspaceJuno/myJBoss7Project/jboss-as-7.1.1.Final/bin/run.jar"
and when i tried to use eclipse's "search" function:
window > preferences > server > runtime environments > search > (select jboss 7 folder "jboss-as-7.1.1.Final")i got the following error:
"no new server runtime environments were found"

in order to fix this i followed the suggestion here:
http://stackoverflow.com/questions/6802754/how-to-run-jboss-as-7-with-eclipse-3-6helios

and did this:
eclipse > help > install new software > work with: http://download.jboss.org/jbosstools/updates/development/juno/ > (hit enter) > JBoss Web and Java EE Development > (check) JBossAS Tools > next > next > (accept) > finish

you need to restart eclipse before you can proceed.

window > preferences > server > runtime environments > add > jboss community > (select) jboss 7.1 runtime > next > (set the home directory for your jboss 7 installation, e.g. for me it is /home/me/workspaceJuno/myJBoss7Project/jboss-as-7.1.1.Final) > (set your JRE, e.g. for me, i am using java-6-openjdk-amd64) > finish

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

20121031

enhanced for loop (for each) for java.util.collection - Type mismatch: cannot convert from element type Object to String

i had the following code that i wanted to convert to the new enhanced for loop:
private String myMethod(Collection c){
    for(Iterator<String> it = c.iterator(); it.hasNext();){
        String s = it.next();
        ...
    }
}





so i rewrote it like this:
private String myMethod(Collection c){
    for(String s : c){
        ...
    }
}

but that code gave me the following "error" in eclipse:
Type mismatch: cannot convert from element type Object to String



here's the final, error-free solution:
private String myMethod(Collection<String> c){
    for(String s : c){
        ...
    }
}

20121025

java ejb ql: SELECT count(*) from X where x IN (SELECT DISTINCT x FROM Y WHERE z)

when a user with the buyer role logs into the system, we want to count the distinct number of tours that have trqs (transport requests) registered for that buyer/user.

a Tour is composed of 0 to many Trqs. Trqs can only be associated with 1 Tour.

Tour table:

CREATE TABLE TOUR
(
   ID decimal(20) PRIMARY KEY NOT NULL,

...
);

Trq table:

CREATE TABLE TRQ
(
...

   BUYERID varchar2(100) NOT NULL,
   TOURID decimal(20),

...
);



the relevant TourServicesBean java code will look like this:

public int findCountByMemberId(String memberId) {
  String s = "SELECT count(t) from Tour t where t.id IN (SELECT DISTINCT tr.tourId FROM Trq tr WHERE tr.buyerId = '"+memberId+"' and tr.tourId = t.id)";
  Query q = em.createQuery(s);
  Long l = (Long) q.getSingleResult();
  return l.intValue();
}


the inner SELECT can return multiple, identical tourIds so we use DISTINCT to only get unique tourIds. then the main SELECT counts the number of unique tourids.

20121024

how to output an array of strings to a comma separated list of strings

import java.util.Arrays;

public class StringTest {
    private static String[] colIds = {"abc","def","ghi"};
 

    public static void main(String[] args) {
        String s = Arrays.toString(colIds);
         // remove leading and trailing brackets, i.e. "[" and "]"
        System.out.println(s.substring(1, s.length()-1));
    }
}



the output will look like this:
abc, def, ghi

if you take away the substring code, then the output will look like this:
[abc, def, ghi]

sucks that geany doesn't appear to have negative lookahead regex capabilities :(

as of version 0.21 in ubuntu 12.04

e.g.
(?!rich)faces
doesn't work (i.e. match strings ending in faces that aren't preceded by rich)

whereas this works:
(?:rich)faces
doesn't work (i.e. match strings ending in faces that are preceded by rich)


20121015

rich:calendar button icon not rendering or getting rerendered

i have a rich:calendar inside a rich:column in a rich:dataTable. when the table gets rendered i only see an input field for the rich:calendar, no calendar icon. when i sort the column, then the icon becomes visible.

the workaround is to define buttonIcon, e.g.:

    <rich:column sortable="true" sortBy="#{trip.startDate}"
        filterBy="#{trip.startDate}" filterEvent="onkeyup">
        <f:facet name="header">
            <h:outputText value="#{msg['label.startDate']}" />
        </f:facet>
            <h:outputText value="#{trip.getStartDateDate()}"
                style="width: 80%;text-align: center" rendered="#{!tripMgrBean.isEditable(trip.id)}">
                <f:convertDateTime type="date" dateStyle="short" timeZone="Europe/Oslo" pattern="#{msg['calendar.datePattern']}"/>
            </h:outputText>
            <rich:calendar value="#{trip.startDate}" buttonIcon="/images/icons/calendar.gif"
                enableManualInput="true"
                converterMessage="#{msg['label.wrong']} #{msg['label.dateformat']}"
                locale="#{userMenuBean.userLocale}"
                direction="auto"
                popup="true"
                showInput="true"
                showApplyButton="false"
                datePattern="#{msg['calendar.datePattern']}"
                inputSize="13"
                cellWidth="24px"
                cellHeight="22px"
                rendered="#{tripMgrBean.isEditable(trip.id)}"/>
    </rich:column>

then it gets rendered every time =)
set the value of buttonIcon to whatever you like.
note, you can also use a text if you like, instead, e.g.: buttonLabel="Choose"

20121014

jboss 4.2.2.GA how to inject ejb3 stateless session bean into jsf 1.2 managed backing bean

##########
// JSF 1.2 backing bean

@org.ajax4jsf.model.KeepAlive
public class TripManagerBean {
  @EJB(name="earName/TripBean/local")
  private TripLocal tripBean;

  public TripManagerBean() {
...
}
##########

earName is foo if the EAR is called foo.ear.

##########
// EJB3 bean

@Stateless
public class TripBean implements TripLocal {
  @PersistenceContext
  private EntityManager em;
...
}
##########

##########
// EJB3 bean local interface

public interface TripLocal {
...
}
##########

thanks to this page about jndi bindings in ejb3

and here's how to lookup an ejb3 stateless session bean using jndi from the backing bean.

jboss 4.2.2.GA how to get ejb3 stateless session bean using jndi in jsf 1.2 managed backing bean

##########
// JSF 1.2 backing bean

@org.ajax4jsf.model.KeepAlive
public class TripManagerBean {
  private TripLocal tripBean;
  public TripManagerBean() {
...
    initTripBean();
  }

  private void initTripBean() {
    if(tripBean == null) {
      try {
        tripBean = (TripLocal) InitialContext.doLookup("earName/TripBean/local");
      } catch (NamingException e) {
        e.printStackTrace();
      }          
    }
  }
...
}
##########

earName is foo if the EAR is called foo.ear.

##########
// EJB3 bean

@Stateless
public class TripBean implements TripLocal {
  @PersistenceContext
  private EntityManager em;
...
}
##########

##########
// EJB3 bean local interface

public interface TripLocal {
...
}
##########

thanks to this page about jndi bindings in ejb3

and here's how to inject an ejb3 stateless session bean into a jsf managed backing bean using the ejb annotation

eclipse not creating class files

when i save a change in a java file, its class file in the bin output folder wasn't getting updated with the change.

i found i had to enabled "build automatically" in preferences:
window > preferences > general > workspace > check build automatically

thanks to this thread:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=23076

20121008

eclipse keyboard shortcut: maximize/minimize/restore/hide/show visual/source view tab/panel: Shift+F6

if you're editing an xhtml/jsf file in eclipse you have views like:
visual/source
source
preview

well if you're in the visual/source view (tab) you can toggle the visual part of the view by doing the following keyboard shortcut:
Shift+F6

20120921

regexp examples for eclipse that match specific html/jsf tag and attribute names and also parsing java code

been working for hours on a regex to match all instances of a specific jsf tag name with a specific attribute name, practically getting an ulcer, and i finally figured out a simple solution:
TAG_NAME[^>]+ATTRIBUTE_NAME\s*=

if TAG_NAME == convertDateTime
if ATTRIBUTE_NAME == locale

then it will match e.g.:
<f:convertDateTime type="both" dateStyle="short"
locale=
"en" timeZone="Europe/Oslo" />

and

<f:convertDateTime locale="#{myBean.localeString}" type="date" dateStyle="medium" timeZone="Europe/Oslo" />



[^>]
this means no tag-close character, because we don't want matches like this:
<f:convertDateTime type="both" dateStyle="short" timeZone="Europe/Oslo" />
<rich:calendar locale="en" datePattern="dd.MM.yyyy"...

we want to stop the searching/matching at the end of the f:convertDateTime tag:
<f:convertDateTime type="both" dateStyle="short" timeZone="Europe/Oslo" />
<rich:calendar locale="en" datePattern="dd.MM.yyyy"...


\s*=
this means that 0 or more whitespace can be between the ATTRIBUTE_NAME and the equals sign. e.g. this would also get matched:
<f:convertDateTime locale   ="#{myBean.localeString}" type="date" dateStyle="medium" timeZone="Europe/Oslo" />


i couldn't find this solution anywhere and was surprised to find so few asking for it, but lots of people writing things like "you can't use regular expressions to parse html" (a post on Stack Overflow that got 4432 positive votes!).

here's a regexp that works in eclipse for finding a tag WITHOUT a specific attribute (i've tested it):
TAGNAME(?:\s+(?!ATTRIBUTENAME\b)[\w\-.:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[\w\-.:]+))?)*\s*/?>

#######

here's an example of regexp search and replace in eclipse that uses grouping and back references for rich:calendar tags with locale attribute that either have a normal hardcoded text string value (e.g. "en") OR a backing/managed bean expression value (e.g. "#{someBean.someValue}") OR empty value (""):
(rich:calendar[^>]*locale\s*=\s*")[^"]*
replace with:
\1#\{myBean\.usersLocale\}

this inserts the Locale value returned from MyBean.getUsersLocale()


[^"]*
this means to match everything (0 matches to infinity) that isn't a ", i.e. stop matching once you reach the locale attribute's closing quote, ", e.g.:
<rich:calendar value="#{myBean.toDate}"  requiredMessage="required"
locale="no/NO"
datePattern="dd.MM.yyyy"
required="true"
/>
so the whole regex would match (everything in bold) e.g.:
<rich:calendar value="#{myBean.toDate}"  requiredMessage="required"
locale="
no/NO
"
datePattern="dd.MM.yyyy"
required="true"
/>
and the text with green background denotes the first back reference (\1) group, i.e.:
(rich:calendar[^>]*locale\s*=\s*")[^"]*
the enclosing parentheses ( and ) denote a grouping, so everything inside those parentheses that matches gets saved and pasted back into the result, using a backreference, e.g.: \1
for the first back reference, \2 for the second back reference/grouping, etc, e.g.:
(text-grouping-to-save-1)text-to-replace(text-grouping-to-save-2)
replace with:
\1new-text-to-replace-old-text\2

which would result in:
text-grouping-to-save-1new-text-to-replace-old-texttext-grouping-to-save-2

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

here's one that finds all elements with a locale attribute that doesn't have the value "#{myBean.usersLocale}":
locale\s*=\s*"(?!#\{myBean\.usersLocale\})[^"]*

(?!XXX)
means match text that ISN'T equal to XXX, and don't create a back reference for it--denoted by the question mark, ?

(?:XXX)
would mean: match text that IS equal to XXX, and don't create a back reference for it


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

someone had coded f:converter tags with a locale attribute, e.g.:
<f:converter locale="#{myBean.localeString}"
converterId="CustomIntConverter" />

so i needed a regex to delete all these locale attributes from all f:converter tags because locale isn't supported in this tag--not in jsf 1.2, 2.0 or 2.1! (however it is supported for related tags like f:convertDateTime and f:convertNumber)

here's the regex search:
(f:converter\s+[^>]*)locale\s*=\s*"[^"]*"

and replace with:
\1


i.e. keep everything that matched up until the locale attribute, and delete the locale attribute and its quoted value.



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

my next problem was finding all jsf tags that use some pageProps OR pageProperties class objects to get a property value and replace all references to such java classes with a standard resource bundle variable, msg, while at the same time keeping and reusing the properties label string value--yes, all in one search and replace!

so, i have code lines like this:
value="#{myBean.isBuyer ? myBean.pageProps.getPropertyValue('label.deviation.buyerdetails',  myBean.locale) : myBean.pageProps.getPropertyValue('label.deviation.transporterdetails',  myBean.locale)}"
...
value="#{myOtherBean.pageProperties.propertyValue('label.deviation.buyerdetails',  myBean.locale)}"

but i also have lines like this, that i don't want to match:
...
timeZone="#{myOtherBean.pageProperties.timeZone}" />

i want the resulting lines to look like this:
value="#{myBean.isBuyer ? msg['label.deviation.buyerdetails'] : msg['label.deviation.transporterdetails']}"
...
value="#{msg['label.deviation.transporterdetails']}"

to do this, i use a regexp search like this, in eclipse:
\w+Bean\.pageProp\w+\.\w+rop[^']+'([^']+)'[^\)]*\)
replace with:
msg\['\1'\]


##########SORT OF UNSOLVED MYSTERY ###############

here i have a java method:
    private String getPropertyValue(String property) {
        if(props == null)
            props = new RBUtils(Locale.ENGLISH, null);
        return RBUtils.getString(property, null, null);
    }


i want to find all java methods with that specific name and remove all lines before the return statement, like this:
    private String getPropertyValue(String property) {
        return RBUtils.getString(property, null, null);
    }


 
here's the regexp that took me about an hour to come up with:
search for:
(String\s+getPropertyValue\([^\{]+\{)[\s\S]+?(return\s+RBUtils)
replace with:
\1\n\2


explanation:
\s\S
matches any character

\n is just to place a newline in front of the return statement.

? is to stop greediness and force laziness.

UPDATE
this one will "fail" if you have the following code:
    private String getPropertyValue(String property){
        String s= RBUtils.getString(property, null, null);
        return s;
    }

    public String getPropertyLabelValue(String property){
        return RBUtils
.getString("label."+property, null, null);
    }


so i need to figure out how to stop the matching at the end of the method. one idea i have is to stop it at a public OR private, i.e. if it hasn't matched before it reaches a "public" or "private" string, then it's not a match.

let's try to put to words exactly what i want:
* i want to match within a method with the following signature:
String getPropertyValue(String name)

where name can be any string: \w+
and the preceding text is a string literal: (String\s+getPropertyValue\s*\(\s*String\s+)

so the whole first line is:
(String\s+getPropertyValue\s*\(\s*String\s+\w+)

the surrounding parentheses are a grouping, so we can save that text and reinsert it in the replace action by using what is called a backreference, like this:
\1

to end the matching at the end of the method is quite difficult though, if not impossible, illustrated by the following examples.

one way is to stop the matching at the first instance of a private or public:
    private String getPropertyValue(String property){
        String s= RBUtils.getString(property, null, null);
        return s;
    }

    public ...


(note: this doesn't match because we didn't find "return RBUtils")

but if you have the following code:
...
    private String getPropertyValue(String property){
        String s= RBUtils.getString(property, null, null);
        return s;
    }
} // end of the java class


there is no method after the method we're interested in--the class ends. one solution would be to somehow count the number of curly braces and stop at the last "}". i don't know how to do that or if that's possible, so someone please let me know =)

what i originally, specifically wanted was to match the following:
    private String getPropertyValue(String property) {
        if(props == null)
            props = new RBUtils(Locale.ENGLISH, null);
        return RBUtils.getString(property, null, null);
    }


so to solve this specific problem (instead of finding all methods with that signature and erasing any variation of code before the return statement--which currently seems impossible) i need to use the following regex:
(String\ +getPropertyValue\ *\(\ *String.+\s)(?:.+props.+\s){2}(.+\s.+\})
replace with:
\1\2

(?:          //group, but don't make a backreference
.+props.+\s          //match a whole line, including the newline (\s) that contains  "props"
)          //end grouping
{2}          //match that "props" line max 2 times

(.+\s.+\})          //match the whole "return" line, plus the line with the method-closing curly brace and put all this into a group, so we can backreference this second group with \2


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

today's fun example, 20120928, was about me figuring out that you can't use a "+" sign to dynamically concatenate label text and then get that label property value from a properties file. luckily i found this post that told me i could use EL 2.2's concat functionality.

so i need to convert all instances of this kind of JSF code:
#{msg['random-text-1'+random-text-2]}
to this:
#{msg['random-text-1-reinserted-here'.concat(random-text-2-reinserted-here)]}
search for:
(#\{\s*msg\[\s*'[^']+')\s*\+\s*(\w[^\]]+)
replace with:
\1\.concat\(\2\)


UPDATE 20131119

this regex searches for primefaces tags that have an update attribute, but are missing an update id for growl:
\bupdate\s*=[\s:a-zA-Z]*"(?!:growl)[^"]*


UPDATE 20131211

after migrating from richfaces to primefaces, i needed to remove all calendar attributes called "verticalOffset" (valid in rich:calendar, but not in p:calendar):

search/match:
verticalOffset\s*=\s*"-*[0-9]+"

replace with:
(nothing)


20120920

how to setup a syslog (rsyslog) server on an ubuntu machine to log d-link DIR-655 router logs

SETTING UP SYSLOG FOR ROUTER LOGS ON AN EXTERNAL SERVER

enable sending router syslogs to myserver (Tools > Syslog):
http://192.168.0.1/Tools/SysLog.shtml

put a checkmark next to Enable Logging To Syslog Server

Syslog Server IP Address is currently myserver: 192.168.0.19

// on the syslog server

install rsyslog:
sudo apt-get install rsyslog

edit the main rsyslog config file:
sudo nano -w /etc/rsyslog.conf
make sure the following lines are uncommented:
$ModLoad imudp
$UDPServerRun 514

$ModLoad imtcp
$InputTCPServerRun 514


in order to get dynamic log file naming to work, make sure these lines are commented out:
#$PrivDropToUser syslog
#$PrivDropToGroup syslog


before i figured out that last tip, dynamic file names using template was not working. if those 2 lines aren't commented out, then there becomes a permissions issue, and use of template won't work (see below)

edit the default rsyslog config file:
sudo nano -w /etc/rsyslog.d/50-default.conf

add the following lines at the very top:
$template DynFile,"/var/log/myrouter/%$year%%$month%%$day%.log"
:fromhost-ip, isequal, "192.168.0.1" ?DynFile
:fromhost-ip, isequal, "192.168.0.1" ~


192.168.0.1 is the router ip address

the bottom line means log nothing (~) after this line for any messages from host ip 192.168.0.1 (i.e. the logging rules specified after this line only apply to messages from the localhost)

then restart the rsyslog service:
sudo service rsyslog restart

you will shortly begin to see router log files appearing here:
/var/log/myrouter/

with filenames in the following format:
yyyymmdd.log

e.g.:
/var/log/myrouter/20120920.log

20120914

eclipse jboss launch configuration delete jboss tmp folders (data, log, work, tmp)

there doesn't seem to be any way to do this in eclipse's launch configuration for jboss, so i do this in my ant build.xml script instead:
<target name="jbossCleanup" description="remove jboss tmp folders">
    <property name="jbossServerDir" value="${jboss.server.dir}/${jboss.domain}"/>
    <delete dir="${jbossServerDir}/log" quiet="true"/>
    <delete dir="${jbossServerDir}/tmp" quiet="true"/>
    <delete dir="${jbossServerDir}/work" quiet="true"/>
    <delete dir="${jbossServerDir}/data" quiet="true"/>
</target>

then i include this target name as the first depends target in the main build target:
<target name="noCheckoutNoCompile" depends="jbossCleanup,clean,...

keep it simple stupid!

"just put some vinegar on it. why didn't you think of that?"
--dr. steve brule

20120913

bash regex/regexp how to remove all lines from file not matching specific text string

after struggling in the geany editor to get regex to work, i tried a different approach.

say you have a file, test.txt, with the following content:
menu2.getChildren().add(createMenuItem("none", userNorwegian ? ...
menu2.getChildren().add(createMenuItem("none", userEnglish ? ...
menu2.getChildren().add(createMenuItem("none", userNorwegian ? ...
menu2.getChildren().add(createMenuItem("none", userEnglish ? ...

but you only want a file with the lines containing the string userNorwegian, i.e. you want to remove the lines containing userEnglish.

open a terminal and run this:
sed -n 's/userNorwegian/userNorwegian/p' test.txt > out.txt

-n means:
suppress automatic printing of pattern space

p means:
Print the current pattern space.

keep it simple, stupid! ;)

thanks for the idea here:
http://stackoverflow.com/a/8255627/557194

MW3: which multiplayer playlist to use to get the Dome map

Team Deathmatch is at least one multiplayer playlist you can use to get the Dome map.

20120906

ubuntu: dont need to "exit" from terminal anymore to save bash history

when did ubuntu change things so that we don't need to type "exit" anymore in the terminal in order to save the most recent commands in bash history? quite useful, and bash_history isn't full of "exit" lines either now =)

20120904

toad sql extension eclipse plugin

in ubuntu linux, i've traditionally used squirrel as my sql client, but alternatively to installing squirrel, toad has a free plugin for eclipse that seems to work, called toad extension:
help > eclipse marketplace > toad

inline web statistics for blogger posts

one cool reason for using blogger is that if you go into your Posts view in your dashboard, you can see how many page views each of your posts has received =)

ubuntu linux bash command of the day

export your hardware configuration as a html file:
sudo lshw -html > hardware.html

OUTPUT:

id:
laptop
description: Notebook
product: VPCSE2V9E (N/A)
vendor: Sony Corporation
version: C60A6XQR
serial: 27553856-5000146
width: 64 bits
capabilities: smbios-2.6 dmi-2.6 vsyscall32
configuration:
boot=normal
chassis=notebook
family=VAIO
sku=N/A
uuid=E029B046-7565-E111-8895-211273E37426
...


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


display amount of free and used memory in the system, in gigabytes:
free -g

OUTPUT:

             total       used       free     shared    buffers     cached
Mem:             7          3          4          0          0          1
-/+ buffers/cache:          2          5
Swap:            7          0          7



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

20120903

how to do a clean install of eclipse

The following works for Eclipse Juno.

download the source

click Eclipse IDE for Java EE Developers > Linux 64-bitwe install eclipse manually to get the latest and best version. sometimes the ubuntu package manager isn't updated with the latest version.

unzip the file

configure eclipse.ini for svn

open a terminal and install the following (needed for the JavaHL plugin):
sudo apt-get install libsvn-java

find the path where the JavaHL library is installed (in your terminal window):
sudo find / -name libsvnjavahl-1.so

open eclipse.ini and set the java.library.path with the value from the above "find" command, e.g.:
-vmargs
-Djava.library.path=/usr/lib/x86_64-linux-gnu/jni
-Dosgi.requiredJavaVersion=1.5
-Dhelp.lucene.tokenizer=standard
-XX:MaxPermSize=256m
-Xms40m
-Xmx512m

reference:


set file encoding to UTF-8

window > preferences > general > content types > expand "Text" and everything under it.

In Eclipse Juno, the following file types need to be changed from ISO-8859-1 to UTF-8(Find the value, select it, then at the bottom, change Default Encoding, then click Update)
Java Properties
JSP
JSP > JSP Fragment
JSP > JSP Tag Definition
JSP > JSP Tag Definition > XML JSP Tag Definition



show line numbers
Windows > Preferences > General > Editors > Text Editors > checkmark the “Show line numbers” option




configure your package view
the default view is Package Explorer, but it shows you a lot of unecessary things like jars, etc. set your view to Navigator:
window > show view > navigator

click the "Link with editor" icon (2 vertical arrows pointing in opposite directions). this will highlight your file in the Navigator so you immediately see where it is.




configure Eclipse's Console
right click anywhere in the Console tab > Preferences > remove the checkmark by Limit console output


validation
disable unnecessary validators (sometimes they take a lot of time)
preferences > validation
click disable all
then enable the following:
classpath dependency validator


don't reuse editors
it's annoying when you want to open a file in search (or compare in team synchronizing view) and you lose the previous file you were looking at because eclipse, by default, will reuse editor windows. to fix this:
Windows > Preferences > General > Search
uncheck Reuse editors to show matches