Adding session bean to to requested bean using annotation JSF

One major part on JSF would separation of concerns even for beans. As a rule of thumb beans related to model are session beans and those which have actions to be taken care of are requested one.

So, In this particular scenario we would have two beans. Basically we don’t want to include any logic inside the session bean, rather we would add session bean as a member variable to request bean.

Lets take a simple registration process.

The requested bean which will be responsible for actions would look like

package com.enderase.beans;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Serializable;

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

import com.enderase.model.Contractor;

@ManagedBean
@RequestScoped
public class ActionListeners implements Serializable{
	
	@ManagedProperty(value="#{contractorBean.contractor}")
	private Contractor contractor;
	
	public void setContractor(Contractor contractor){
		this.contractor = contractor;
	}
	
	public Contractor getContractor() {
		return this.contractor;
	}
	
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * Action handler for Contractor save.
	 * @return String, next
	 */
	public String registerContractor() {
		Contractor contractor = this.contractor;
		if (contractor != null) {
			try {
				FileWriter fileWriter = new FileWriter("/tmp/name.note");
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write("Name "+contractor.getName()+" Email "+contractor.getEmail()
						+contractor.getState());
				bufferedWriter.close();
			} catch (Exception ex) {
				//log the exception here
			}
		}
		return "navigated";
	}
}

So the key thing here would be the @ManagedProperty part.
That would inject the session bean into the request bean without creating any instance of it.

*Don’t for get to add getter and setter for the session bean you are adding otherwise you would get an error.

The session bean would be a simple holder of model

package com.enderase.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.enderase.model.Contractor;

@ManagedBean
@SessionScoped
public class ContractorBean {
	
	private Contractor contractor;
	
	public ContractorBean() {
		this.contractor = new Contractor();
	}
	public Contractor getContractor(){
		return this.contractor;
	}
	
	public void setContractor(Contractor contractor){
		this.contractor = contractor;
	}
}

Where the contractor would be a simple POJO file

Adding user to sudoers in Mac

Working on mac, you might find something the sudo not working as expected or something.
Specially, if you are installing to ‘somehow’ restricted folders like /usr/local/bin ..
Here is what you can do to have an sudo access:
First check the group wheel (mostly) by running the following command

ls -la /private/etc/sudoers 

This shall produce something: -r–r—– 1 root wheel 1242 Jun 22 2009 /private/etc/sudoers
This will tell you the sudoers should be in the wheel group

Then check in what group the user currently is using

id 

From the listed groups, if wheel is not listed then add it

sudo dscl . -append /Groups/wheel GroupMembership 

-_-