Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking. I'm 24, born in Romania, studied at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff.

Thursday, April 21, 2011

Define a producer field or method


A producer method or field is annotated with the javax.enterprise.inject.Produces annotation.

Define a producer field

A producer field acts as a getter method and it is a simple alternative to a producer method. A producer field is declared by annotating a field of a bean class with the @Produces annotation. Usually, a producer field is also annotated with a CDI qualifier. Per example, a producer field can be used to inject a random number into a bean or non-Java context.

Note: Producer fields may have a primitive bean type - primitive types are considered to be like their corresponding wrapper types in java.lang (Integer, Float, Byte etc).

First, you write a qualifier (this is the recommended technique). Let us name it RandomQualifier (since you have a field qualifier, the target contains only the FIELD value):

@Qualifier
@Retention(RUNTIME)
@Target({FIELD})
public @interface RandomQualifier {
}

Moreover, the producer field that will inject a random int number is defined in the class RandomGenerator:

public class RandomGenerator {
@Produces @RandomQualifier public int nr = new Random().nextInt(4);
}

Now, you can get a random number anywhere. You can have an injection point into a bean, like below:

@Inject @RandomQualifier int nr;

On the other hand, if you want a random number into a non-Java context, like a JSF page, then you should add the @Named annotation into the producer field annotations suite:

@Produces @Named @RandomQualifier public int nr = new Random().nextInt(4);

Now, the EL can sustain the injection of a random number into a JSF page:

<h:outputText value="${nr}" />

Define a producer method

A producer method is a method that provides bean instances when no instance exists in the specified context – the container will invoke this method for getting an instance, while the application is responsible to take care of bean instantiation process. Per example, let us write a producer method that randomly returns a command strategy based on your producer field. You can start with the producer method qualifier, named CommandQualifier:

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD})
public @interface CommandQualifier {
}

Next, the producer method itself is declared as below (named, generateCommandStrategy):

@Produces @CommandQualifier public CommandStrategy generateCommandStrategy() {
switch (nr) {
case 1:
return new SingleCommandStrategy();
case 2:
return new MultipleCommandStrategy();
case 3:
return new CustomCommandStrategy();
default:
return new BasicCommandStrategy();
}
}

Above, you have isolated the generateCommandStrategy producer method. For the complete picture, here it is the entire CommandStrategy bean – notice how the injected producer field (nr) was used just any other Java field.

public class CommandStrategy {

@Inject @RandomQualifier int nr;

@Produces @CommandQualifier public CommandStrategy generateCommandStrategy() {
switch (nr) {
case 1:
return new SingleCommandStrategy();
case 2:
return new MultipleCommandStrategy();
case 3:
return new CustomCommandStrategy();
default:
return new BasicCommandStrategy();
}
}
}

Now, the producer method result can be injected. The injection point will have the same type and qualifier annotations as the producer method. The producer method will be called by the container to obtain an instance to service the injection point.

@Inject @CommandQualifier CommandStrategy injectedStrategy;

Saturday, April 16, 2011

Working with multiple CDI qualifiers


An injection point may specify multiple qualifiers. When you specify multiple qualifiers, only the beans that annotated with those qualifiers will be injected - a bean can have multiple qualifiers and injection points only need to specify enough qualifiers to uniquely match a bean. Per example, let us suppose that you have the below two qualifiers – each qualifier represent a tennis player:

package com.racquets;

//imports here

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface JoWilfriedTsonga {
}

package com.racquets;

//imports here

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface RafaelNadal {
}

Both top players, Rafael Nadal and Jo-Wilfred Tonga play with the same racquet, a Babolat AeroPro Drive GT. Therefore, a possible implementation of RacquetType type can be annotated with both qualifiers from above:

package com.racquets;

@RafaelNadal @JoWilfriedTsonga
public class AeroProDriveGTRacquet implements RacquetType {

@Override
public String getRacquetType() {
return ("Babolat AeroPro Drive GT");
}
}

Now, at the injection point you must specify both qualifiers, like below:

package com.racquets;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@RequestScoped
@Named
public class AeroProDriveGTBean {

@Inject @RafaelNadal @JoWilfriedTsonga RacquetType aeroprodrivegtRacquet;
private String aeroprodrivegtRacquetName;

public String getAeroprodrivegtRacquetName() {
aeroprodrivegtRacquetName = aeroprodrivegtRacquet.getRacquetType();
return aeroprodrivegtRacquetName;
}
}

From a JSF page, you can test multiple qualifiers like this:

<br/><h3><b>Multiple qualifiers</b></h3>
<b>Rafael Nadal and Jo-Wilfried Tsonga racquet:</b>
<h:outputText value="#{aeroProDriveGTBean.aeroprodrivegtRacquetName}" /><br/>

The output is in figure below:

When to use a CDI producer field or method


Conforming to the official specification, a producer field can be used:

• instead of a simple getter method
• for declaring Java EE resources

Moreover, a producer method can be used – producer methods acts as sources of bean instances:

• to inject non-beans objects (expose any JDK class as a bean)
• to inject objects that have type that vary at runtime (vary the implementation of a bean type at runtime)
• to inject objects that requires some custom initialization that the bean constructor does not perform (define multiple beans, with different scopes or initialization, for the same implementation class).

Thursday, April 14, 2011

The built-in qualifiers @Default and @Any


• @Default - Whenever a bean or injection point does not explicitly declare a qualifier, the container assumes the qualifier @Default.

• @Any – When you need to declare an injection point without specifying a qualifier, is useful to know that all beans have the @Any qualifier. This qualifier “belongs” to all beans and injection points (not applicable when @New is present) and when is explicitly specified you suppress the @Default qualifier. This is useful if you want to iterate over all beans with a certain bean type.

The example presented in this post will prove how @Any qualifier works in a useful example. You will try to iterate over all beans with a certain bean type. For start, you define a new Java type, like below – a simple interface representing a Wilson tennis racquet type:

package com.racquets;

public interface WilsonType {
public String getWilsonRacquetType();
}

Now, three Wilson racquets will implement this interface – notice that no qualifier was specified:

package com.racquets;

public class Wilson339Racquet implements WilsonType{

@Override
public String getWilsonRacquetType() {
return ("Wilson BLX Six.One Tour 90 (339 gr)");
}

}

package com.racquets;

public class Wilson319Racquet implements WilsonType {

@Override
public String getWilsonRacquetType() {
return ("Wilson BLX Six.One Tour 90 (319 gr)");
}
}

package com.racquets;

public class Wilson96Racquet implements WilsonType {

@Override
public String getWilsonRacquetType() {
return ("Wilson BLX Pro Tour 96");
}
}

Next, you can use the @Any qualifier at injection point to iterate over all beans of type WilsonRacquet. An instance of each implementation is injected and the result of getWilsonRacquetType method is stored in an ArrayList:

package com.racquets;

import java.util.ArrayList;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

@RequestScoped
public class WilsonRacquetBean {

private @Named @Produces ArrayList wilsons = new ArrayList();

@Inject
void initWilsonRacquets(@Any Instance racquets) {
for (WilsonType racquet : racquets) {
wilsons.add(racquet.getWilsonRacquetType());
}
}
}

From a JSF page, you can easily iterate over the ArrayList (notice here that the wilsons collection was annotated with @Named (allows JSF to have access to this field) and @Produces (as a simple explanation, a producer field suppress the getter method)):

<br/><h3><b>The built-in qualifiers @Default and @Any</b></h3>
<h:form>
<h:dataTable value="#{wilsons}" var="item" border="1">
<h:column>
<f:facet name="header" >
<h:outputText value="Racquets"/>
</f:facet>
<h:outputText value="#{item}"/>
</h:column>
</h:dataTable>
</h:form>

The output is in figure below:

Qualifiers with members


As you will see in this post, qualifiers can have members (more generally, annotations may have members). Per example, you can replace several qualifiers representing multiple racquets by writing a single qualifier with a member. This approach will avoid writing dozens of qualifiers in a single application and will provide a clear code.

The below code is a qualifier with a single member:

package com.racquets;

//imports here

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface PFHRacquets {
EnumPFHRacquets value();
}

Where the EnumPFHRacquets is a Java enum:

package com.racquets;

public enum EnumPFHRacquets {
PRINCE, FISCHER, HEAD
}

At this point, you can start annotating beans with our PFHRacquets qualifier. As you can see this is very simple and intuitive – below are three implementation of RacquetType, annotated by PFHRacquets qualifier:

package com.racquets;

@PFHRacquets(EnumPFHRacquets.PRINCE)
public class PRacquet implements RacquetType{

@Override
public String getRacquetType() {
return("Prince O3 Hybrid Tour");
}
}

package com.racquets;

@PFHRacquets(EnumPFHRacquets.FISCHER)
public class FRacquet implements RacquetType{

@Override
public String getRacquetType() {
return("Fischer Black Granite Tour 100");
}
}

package com.racquets;

@PFHRacquets(EnumPFHRacquets.HEAD)
public class HRaquet implements RacquetType{

@Override
public String getRacquetType() {
return("Head MicroGEL Radical");
}

}

Wednesday, April 13, 2011

Turn a Java annotation into a CDI qualifier


A CDI qualifier is an annotation that itself is annotated with the @javax.inject.Qualifier meta-annotation. Per example, if you add the @Qualifier to the MyAnnotation (http://e-blog-java.blogspot.com/2011/04/what-is-java-annotation.html) you obtain a CDI qualifier:

@Qualifier
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
// Property Definitions here.
}

CDI provide a set of built-in qualifiers:

Qualifier Description
@javax.enterprise.inject.Named - This qualifier is used to un-typed access from
non-Java code. Commonly this qualifier serves the
JSF pages that access beans through EL.

@javax.enterprise.inject.New - This qualifier forces the creation of a new
instance, instead of using the contextual
instance. It allows us to obtain a dependent
object of a specified class.

@javax.enterprise.inject.Any - This qualifier “belongs” to all beans and
injection points (not applicable when @New is
present). This is useful if you want to iterate
over all beans with a certain bean type.

@javax.enterprise.inject.Default - As the qualifier name suggests, whenever a bean
or injection point does not explicitly declare a
qualifier, the container assumes the qualifier
@Default.

Define a CDI Qualifier


I suppose a classic scenario – a set of beans that implements the same bean type, and injection points for those beans. Usually, when bean type has multiple implementations, you deal with a Java interface type, like this one – a simple interface representing a tennis racquet type:

package com.racquets;

public interface RacquetType {
public String getRacquetType();
}

More, let us develop two simple implementations of this interface. You have a Head power racquet:

package com.racquets;

public class HeadRacquet implements RacquetType {

@Override
public String getRacquetType() {
return("Head YouTek Speed Pro");
}

}

Moreover, a Babolat spin racquet:

package com.racquets;

public class BabolatRacquet implements RacquetType{

@Override
public String getRacquetType() {
return ("Babolat AeroPro Drive GT");
}

}

At this moment, you can inject these implementations into a bean (@Inject), but an ambiguous injection error will occur, because the container will not know which implementation must be injected. Qualifiers will save us, because they will uniquely identify the implementations, therefore the container will know exactly what to inject. For non-tennis fans, the Head racquet is a power racquet, while the Babolat racquet is a spin racquet, therefore you can use this to define the below two qualifiers:

package com.racquets;

//imports here

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface PowerRacquet {
}

package com.racquets;

//imports here

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface SpinRacquet {
}

Now, you should annotate the two implementations accordingly, as below – at this point, the container is able to distinguish between the two implementations:

package com.racquets;

@PowerRacquet
public class HeadRacquet implements RacquetType {

@Override
public String getRacquetType() {
return("Head YouTek Speed Pro");
}

}

package com.racquets;

@SpinRacquet
public class BabolatRacquet implements RacquetType{

@Override
public String getRacquetType() {
return ("Babolat AeroPro Drive GT");
}

}

Next, you can give a test to the container, by injecting the two implementations into another bean, like this – if everything works fine, you will not get errors, and you will inject the correct information – this is the prove that the container is capable to distinguish between the Head and Babolat racquets:

package com.racquets;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class RacquetBean {

private String racquetSpin;
private String racquetPower;
private RacquetType racquetSpinType;
private RacquetType racquetPowerType;

@Inject
public RacquetBean(@SpinRacquet RacquetType racquetSpinType,
@PowerRacquet RacquetType racquetPowerType) {
this.racquetSpinType = racquetSpinType;
this.racquetPowerType = racquetPowerType;
}

public String getRacquetPower() {
racquetPower = racquetPowerType.getRacquetType();
return racquetPower;
}

public String getRacquetSpin() {
racquetSpin = racquetSpinType.getRacquetType();
return racquetSpin;
}
}

Call the racquetSpin and racquetPower property from a JSF page through EL (notice that the @Named built-in qualifier let JSF to do this):

<h3><b>Qualifier annotations</b></h3>
Selected Racquets:<br/>
<b>Power Racquet:</b><h:outputText value="#{racquetBean.racquetPower}" /><br/>
<b>Spin Racquet:</b><h:outputText value="#{racquetBean.racquetSpin}" /><br/>

Moreover, the output is in figure below:

Note: Qualifier annotations can also qualify method arguments of producer, disposer and observer methods.

Tuesday, April 12, 2011

What is the @Target annotation


The target annotation (@Target) indicates for which element type this annotation is applicable. This task is accomplished by the argument of the @Target annotations, which can be:

• ElementType.TYPE – applicable only to type (Java class, interface, Enum or even an annotation)
• ElementType.FIELD – applicable only to Java fields
• ElementType.METHOD – applicable only to methods
• ElementType.PARAMETER – applicable only to method parameters in a method definition
• ElementType.CONSTRUCTOR – applicable only to a constructor of a class
• ElementType.LOCAL_VARIABLE – applicable only to local variables
• ElementType.ANNOTATION_TYPE – applicable only to annotation types
• ElementType.PACKAGE – applicable only to a package

A simple test case example:

@Target(ElementType.FIELD)
public @interface MyAnnotation
{
// Property Definitions here.
}

What is the @Retention annotation


Known as retention policy, this a mechanism through which we can say that to what extent the annotations should be retained. The annotation is named @Retention and its argument accept three values:

• RetentionPolicy.SOURCE - retain the annotation in the source code
• RetentionPolicy.CLASS - retain the annotation in the class file
• RetentionPolicy.RUNTIME - retain the annotation during runtime so that JVM can make use of it

A simple test case example:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
// Property Definitions here.
}

What is a Java annotation


Probably you know the answer to this question, but we take the risk of boring you with a little description of a Java annotation. Take it as a short remember!

Annotations were introduced in Java starting with version 5.0 and they are commonly used to add meta-data information to classes, methods, fields and so on. Annotations are easy to recognize because they are marked by the “@” symbol, like you can see in the annotation definition, below:

public @interface MyAnnotation
{
// Property Definition here.
}

Note: Do not confuse the @interface with the Java reserved keyword interface.

Whether annotations can be applied to class, method or field is specified in the declaration of the annotation itself - this is known as meta-annotations (meta-data about meta-data).

Sunday, April 10, 2011

How to upload with SWF Upload and Struts 1.x (Apache FileUpload on server)



Download a complete example tested under Tomcat 6 from here


If you like, Struts 1.x, SWFUpload and Apache FileUpload and you need to put all of them together in a web application for accomplish upload tasks, then follow the above/below link.

For Struts 2.x, just point the SWF Upload to Struts fileUpload solution - http://struts.apache.org/2.0.14/docs/file-upload.html.


Download a complete example tested under Tomcat 6 from here

How to upload with SWF Upload and Struts 1.x (OReilly COS on server)


Download a complete example tested under Tomcat 6 from here


If you like, Struts 1.x, SWFUpload and OReilly COS (upload servlet) and you need to put all of them together in a web application for accomplish upload tasks, then follow the above/below link.

For Struts 2.x, just point the SWF Upload to Struts fileUpload solution - http://struts.apache.org/2.0.14/docs/file-upload.html.


Download a complete example tested under Tomcat 6 from here

Wednesday, April 6, 2011

How to call an Ant script (part 2 - with parameters)


The below method call an Ant script with parameters. All you need to pass to it is the name (and path) of the Ant script and a Map of parameters – the keys are the parameters names as in the Ant script, and the values are the parameters values:

private boolean runAntScript(String scriptName, Map params) {

File buildFile = new File(scriptName);
Project project = new Project();
DefaultLogger myLogger = new DefaultLogger();

project.setUserProperty("ant.file", buildFile.getAbsolutePath());

Set keys = params.keySet();
Iterator iteratorKeys = keys.iterator();
while (iteratorKeys.hasNext()) {
String key = (String) iteratorKeys.next();
project.setProperty(key, params.get(key));
}

myLogger.setErrorPrintStream(System.err);
myLogger.setOutputPrintStream(System.out);
myLogger.setMessageOutputLevel(Project.MSG_INFO);

project.addBuildListener(myLogger);

try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(e);
return false;
}

return true;
}

You will need the ant libraries in the application classpath. Notice that the above example sends the script output to default System.out and runs the default script start target.

Tuesday, April 5, 2011

How to show details on hover of a image


Images! Big, small, colored, beautiful images. Sometimes, you want to add a description to your images and also want to keep this look nice. There are many ways to do this. The below trick it’s just one way of doing this, on mouse over an image, details just pop-up.


The CSS code is :
body
{
background-color: #000;
margin: 0 auto;
padding: 0;
position: relative;
}
img {
width: 450px;
height: 320px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
div.photo_album { width: 90%; padding: 20px;}

ul.album { list-style: none; }

ul.album li
{
width: 450px;
float: left; display: inline;
margin: 0; padding: 0;
position: relative;
}
ul.album li:hover {z-index: 99;}
/*--Thumbnail Styles--*/
ul.album li img
{
position: relative;
filter: alpha(opacity=50);
opacity: 0.5;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; /*--IE8
Specific--*/
}
ul.album li:hover img
{
z-index: 999;
filter: alpha(opacity=100);
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
}
/*--Details Style--*/
ul.album li .info
{
position: absolute;
left: -17px; top: -10px;
padding: 310px 10px 5px;
width: 465px;
display: none;
background: #fff;
-webkit-border-radius: 7px;
-moz-border-radius: 7px;
border-radius: 7px;
}
ul.album li:hover .info {display: block;}

ul.album li h2
{
font-weight: bold;
text-align: center;
}
ul.album li p
{
padding: 0; margin: -20px 0 0 10px;
font: 20px "Comic Sans MS", cursive, sans-serif;
}

In addition, you can test it from HTML, like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Show image details</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="./css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="photo_album">
<ul class="album">
<li>
<a href="#"><img src="./rafael/1.jpg" alt="" /></a>
<div class="info">
<h2>Rafael Nadal</h2>
<p>Miami Masters Series 2011<br />
Photos by Ella Ling</p>
</div>
</li>
<li>
<a href="#"><img src="./rafael/2.jpg" alt="" /></a>
<div class="info">
<h2>Rafael Nadal</h2>
<p>Miami Masters Series 2011<br />
Photos by Ella Ling</p>
</div>
</li>
</ul>
</div>
</body>
</html>

Sunday, April 3, 2011

Dynamic images with PrimeFaces


You can find this recipe in JSF 2.0 Cookbook from Packt




PrimeFaces is a lightweight library for JSF with regard to its functionality, simplicity, and support. Its power consists in AJAX support, providing more than 70 AJAX-based components. The additional TouchFaces module features a UI kit for developing mobile web applications. In this recipe, you will see how to use PrimeFaces to retrieve images from a database and to provide them dynamically to our JSF page.

Getting ready
We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library. In addition, we have used PrimeFaces 2.0, which provide support for JSF 2.0. You can download this distribution from http://www.primefaces.org/.

How to do it...
Our recipe will look very simple, thanks to PrimeFaces. Practically, all we do is to pick up the PrimeFaces fruits. The following code retrieves a BLOB from a JDBC ResultSet and provides its InputStream as a StreamedContent (the backing bean is listed next):

public class PictureBean {

private StreamedContent myImage;

public PictureBean() {
InputStream inputStream = //InputStream of a blob
myImage = new DefaultStreamedContent(inputStream, "image/png");
}

public StreamedContent getMyImage() {
return myImage;
}

public void setMyImage(StreamedContent myImage) {
this.myImage = myImage;
}
}

And the p:graphicImage tag can display any binary image, as shown next:

<p:graphicImage value="#{pictureBean.myImage}" />

How it works...
The entire solution is mapped in PrimeFaces; therefore you will need to go deeply into this framework to understand its secrets. Apparently, everything we have done relates to a simple JSF application with a simple conversational state between a JSF page and a backing bean.


You can find this recipe in JSF 2.0 Cookbook from Packt

Saturday, April 2, 2011

Populating a JS load function with JSF values


You can find this recipe in JSF 2.0 Cookbook from Packt




As you know, when a web page is loaded, the code on the page is generally processed from the top down. JS code can interfere in this top-down order in many ways, and the onload function (specified on the body tag) is one of these possibilities. When the page is loaded, the browser will stop at the onload function and will execute the indicated script. In this recipe,you will see how to populate that script with JSF values, provided by a managed bean.

Getting ready
We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...
The onload function calls our JS function, named calledOnLoad. Our function will retrieve some JSF values from a managed bean. Here it is how it will do this:

<?xml version='1.0' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Passing parameters on HTTP GET</title>
<script type="text/javascript" language="javascript">
function calledOnLoad(){
var p_1 = '<h:outputText value="#{bean.param_1}"/>';
var p_2 = '<h:outputText value="#{bean.param_2}"/>';
var ot = document.getElementById("formId:textId");
ot.textContent="Parameters from bean are:"+p_1+" and " + p_2;
}
</script>
</h:head>
<h:body onload="calledOnLoad();">
<h:form id="formId">
<h:outputText id="textId" value=""/>
</h:form>
</h:body>
</html>

The managed bean is:

package bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Bean {

private String param_1 = "Me";
private String param_2 = "You";

public String getParam_1() {
return param_1;
}

public void setParam_1(String param_1) {
this.param_1 = param_1;
}

public String getParam_2() {
return param_2;
}

public void setParam_2(String param_2) {
this.param_2 = param_2;
}
}

How it works...
The secret of this recipe is in this line:

var p_1 = '';

Notice that JS knows how to parse this line to extract the JSF value, instead of assigning a verbatim text to the p_1 variable.


You can find this recipe in JSF 2.0 Cookbook from Packt

How to redirect an Ant output to a JTextArea


If you need to capture an Ant script output and resend it to an JTextArea (or similar components), you can try a few solutions. For my purposes I solved this task by writing a new logger that extends the DefaultLogger and overwrite the buildFinished, buildStarted and messageLogged methods:

class MyLogger extends DefaultLogger {

@Override
public void buildFinished(BuildEvent be) {
outputTextArea.append("[BUILD FINISHED]\n");
}

@Override
public void buildStarted(BuildEvent be) {
outputTextArea.append("[BUILD STARTED]\n");
}

@Override
public void messageLogged(BuildEvent be) {
outputTextArea.append(be.getMessage() + "\n");
}
}

Next, MyLogger was set to the current Project:

private boolean runAntScript(String scriptName) {

File buildFile = new File(scriptName);
Project project = new Project();
MyLogger myLogger = new MyLogger();

project.setUserProperty("ant.file", buildFile.getAbsolutePath());
myLogger.setErrorPrintStream(System.err);
myLogger.setOutputPrintStream(System.out);
myLogger.setMessageOutputLevel(Project.MSG_INFO);

project.addBuildListener(myLogger);

try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(e);
return false;
}

return true;
}

Another solution is to implement the org.apache.tools.ant.BuildListener interface. This interface will force you to overwrite more methods but provide more control, as you can see below:

class myBuildListener implements org.apache.tools.ant.BuildListener {

public void buildFinished(BuildEvent be) {
outputTextArea.append("[BUILD FINISHED]\n");
}

public void buildStarted(BuildEvent be) {
outputTextArea.append("[BUILD STARTED]\n");
}

public void messageLogged(BuildEvent be) {
outputTextArea.append(be.getMessage() + "\n");
}

public void targetFinished(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void targetStarted(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void taskFinished(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void taskStarted(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

}

Finally, a third solution is to send the Ant script output to a pipe:

private boolean runAntScript(String scriptName) {

PipedOutputStream pipeOut = null;
PipedInputStream pipeIn = null;
BufferedReader pipeReader = null;

try {
pipeOut = new PipedOutputStream();
pipeIn = new PipedInputStream(pipeOut);
pipeReader = new BufferedReader(new InputStreamReader(pipeIn));

System.setOut(new PrintStream(pipeOut));
System.setErr(new PrintStream(pipeOut));
} catch (IOException e) {
return false;
}

File buildFile = new File(scriptName);
Project project = new Project();
DefaultLogger consoleLogger = new DefaultLogger();

project.setUserProperty("ant.file", buildFile.getAbsolutePath());
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
project.addBuildListener(new myBuildListener());

try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(e);
return false;
}

return true;
}

Next, you will need a separate thread to read form pipe and put in the text area. This can be implemented through Java multi-threading capabilities in a few lines of code.


Well, that’s my solutions! Please fill free to bring other new and better ones.

How to call an Ant script (part 1 - without parameters)


The below method call an Ant script without parameters. All you need to pass to it is the name and path of the Ant script:

private boolean runAntScript(String scriptName) {

File buildFile = new File(scriptName);
Project project = new Project();
DefaultLogger myLogger = new DefaultLogger();

project.setUserProperty("ant.file", buildFile.getAbsolutePath());
myLogger.setErrorPrintStream(System.err);
myLogger.setOutputPrintStream(System.out);
myLogger.setMessageOutputLevel(Project.MSG_INFO);

project.addBuildListener(myLogger);

try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(e);
return false;
}

return true;
}

You will need the ant libraries in the application classpath. Notice that the above example sends the script output to default System.out and runs the default script start target.

java.lang.ClassNotFoundException org.apache.tools.ant.Main


Sometimes (rare, but occurs) when you install Ant out-of-the-box and run it you will get an error like this:

java.lang.ClassNotFoundException: org.apache.tools.ant.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:244)
at org.apache.tools.ant.launch.Launcher.main(Launcher.j

The solution is quite simple: It is usually due to a corrupted version of ant.jar in the Ant libs. Check the ant.jar and if it does not contain the org.apache.tools.ant.Main class try to grab a newer version of Ant. That should solve the problem!