Tag Archives: java

JBoss remote debug

jboss_logo
To connect to JBoss application server remotely you should perform next action:

open run.conf.bat (for windows) or run.conf (for linux) file in JBoss bin folder
and add the following java argument:

JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

If you miss -Xdebug parameter you can enter to debug mode but after timeout (a few minutes) you’ll be disconnected and JBoss restart occurred!
Read more at http://stackoverflow.com/questions/18182666/eclipse-remote-debugging-with-jboss.

Thanx to http://stackoverflow.com/a/18211332

Could not transfer artifact with Maven

mvn
You may see the error when try to make maven project:
Could not transfer artifact Connection refused: connect

The problem may be with your internet connection and can be fixed by adding the following settings in maven settings.xml file (for windows: %USERPROFILE%\.m2 folder):


        
            true
            http
            your_proxy_host
            your_proxy_port
            
        
    

Thanx to http://stackoverflow.com/a/16729906.

Trim string in Java

char
Don’t like string frameworks? Use POJO java!

static String trim(String value, char character) {
    if (value == null || value.isEmpty()) {
        return value;
    }
    String normalized = value.trim();

    int startIndex = 0;
    while (normalized.charAt(startIndex) == character) {
        startIndex++;
    }

    int endIndex = value.length() - 1;
    while (normalized.charAt(endIndex) == character) {
        endIndex--;
    }

    return normalized.substring(startIndex, endIndex + 1).trim();
}

Simple!

Why JavaEE ?

java_ee
Why JavaEE is so popular? Not because it is the super useful, just this is implementation of all incoming business requirements for a time. This is so called template patterns for business, nothing more: not technical brilliance, not ideological concern.

Here is high availability, high scalability, high reliability platform for building business infrastructure.
The time from first release is a proof.
Use java!

Floating point operations in XPath

xpath
You may need to perform arithmetic operation with XPath-expression like sum, etc. But Saxon parser cast all floating point values to DOUBLE by default (as Java).
If you have next xml:

<root>
    <value>1.555</value>
    <value>1</value>
    <value>-1</value>
</root>

So, for sum you use next XPath expression:

sum(/root/value)

And in result you’ll have NOT 1.555, but 1.4999999754 for example.
This is normal java behavior! Don’t panic!
Just use some trick:

sum(/root/value/xs:decimal(.))

And it gives you 1.555 in result.

As you see, adding /xs:decimal(.) solves the problem with data losses.

Capitalize word algorithm in Java

capit
Here is simple snippet to capitalize string:

private static String capitalize(String value) {
    return value == null ? null : 
        value.substring(0, 1).toUpperCase() + value.substring(1);
}

Simple!

Thanx to
http://stackoverflow.com/questions/5725892/how-to-capitalize-the-first-letter-of-word-in-a-string-using-java

Using binding in JSF

jsflife
You may heard about binding term in JSF. But it can be used?
Binding can be involved in validation procedure.

1. Add form to JSF page:

<h:form>
    <h:inputText id="email" 
        value="#{SomeBaen.emailValue}" 
        binding="#{SomeBaen.emailComponent}" />
    <h:message for="email" />

    <h:commandButton value="Check" action="#{SomeBaen.doCheck}" />
</h:form>

2. Create bean:

public class SomeBean implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private String emailValue;
    private UIComponent emailComponent;
    
    public String doCheck() {
        if (isValid(emailValue)) {
            return "ok";
        }
        
        // set invalid message
        FacesMessage message = new FacesMessage("Invalid email");
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(emailComponent.getClientId(context), message);
        
        return null;
    }
    
    public void setEmailValue(String emailValue) {
        this.emailValue = emailValue;
    }
    
    public String getEmailValue() {
        return emailValue;
    }
    
    public void setEmailComponent(UIComponent emailComponent) {
        this.emailComponent = emailComponent;
    }
    
    public UIComponent getEmailComponent() {
        return emailComponent;
    }
}

So, if validation failed you’ll see a message near inputText control.
Also example http://stackoverflow.com/a/321722