Taming the beast: Binding imqbrokerd / OpenMQ to fixed ports

If you want to tunnel OpenMQ through SSH or any firewall, you’ll have to make sure to have fixed ports you can open.
This took me ages, so maybe it helps someone:

I’m starting my imqbrokerd with this command

/home/domdorn/gf/glassfishv3/mq/bin/imqbrokerd \
-javahome /home/domdorn/jdk1.6.0_21/ \
-port 7676 \
-startRmiRegistry \
-rmiRegistryPort 34000 \
-Dimq.jmx.connector.jmxrmi.port=31000 \
-Dimq.jmx.connector.ssljmxrmi.port=32000 \
-Dimq.jmx.rmiregistry.port=34000 \
-Dimq.portmapper.port=7676 \
-Dimq.admin.tcp.port=36000 \
-Dimq.cluster.port=37000 \
-Dimq.cluster_discovery.port=38000 \
-Dimq.cluster.heartbeat.port=39000 \
-Dimq.httpjms.http.servletPort=40000 \
-Dimq.httpsjms.https.servletPort=41000 \
-Dimq.jms.tcp.port=43000

which effectively binds imqbrokerd to these ports:

tcp6       0      0 :::34000                :::*                    LISTEN      22171/java      
tcp6       0      0 :::43000                :::*                    LISTEN      22171/java      
tcp6       0      0 :::31000                :::*                    LISTEN      22171/java      
tcp6       0      0 :::7676                 :::*                    LISTEN      22171/java      
tcp6       0      0 :::36000                :::*                    LISTEN      22171/java

Now simply connect to the remote host with

ssh REMOTEHOST -L7676:127.0.0.1:7676 -L31000:127.0.0.1:31000 -L34000:127.0.0.1:34000 -L36000:127.0.0.1:36000 -L43000:127.0.0.1:43000

Good luck!

CDI/Weld beans.xml XSD/DTD

As I’m often looking for the correct header of the beans.xml file required for Web Beans / Context and Dependency Injection (CDI) to work, I decided to share this simple header here with you.

If you don’t have anything to declare, create an empty beans.xml like this one

<beans
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" />
 
If you have to declare alternatives or interceptors, do it like this
<pre lang='xml'>
<beans
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
    <alternatives>
        <stereotype>
            com.dominikdorn.dc.passwordReset.PasswordResetService
        </stereotype>
        <class>com.dominikdorn.dc.passwordReset.StudyGuruPasswordReset</class>
    </alternatives>
</beans>

A good IDE will help you with creating a proper beans.xml as soon as you specify the xml namespace.

Popular implementations of CDI are

  1. CanDI
  2. Weld
  3. OpenWebBeans

StarCraft 2 (DVD) with Linux + Wine

I just got my StarCraft 2 Collectors Edition.

Right after unpacking it, I inserted the DVD and was wondering “They ship a whole DVD with just 2 Files, not more than 3 MB of size?”

The thing is: The DVD has a file system called UDF which supports hidden files and directories. Unlike with normal filesystems in linux, even a ls -lah does not show these files.

1. Unmount the DVD
Make sure to unmount the DVD first.
Close every filemanager and console that has the DVD folder open.
In my Ubuntu installation, the DVD is mounted to /media/cdrom0

Do the following in a console

sudo umount -f /media/cdrom0

If it does not work, look which processes still have a lock on the directory using

sudo lsof /media/cdrom0

and kill those.

2. Mount the DVD correctly
First, get your own user Id. Most of the times its just 1000.

id

should return something like this

uid=1000(domdorn) gid=1000(domdorn) groups=4(adm),20(dialout),24(cdrom),46(plugdev),103(fuse),104(lpadmin),114(admin),118(sambashare),1000(domdorn)

Note the values of uid=… and gid=…. (here both are 1000)

Next, mount the DVD the following way:

mount /dev/cdrom /media/cdrom0 -o uid=1000,gid=1000,unhide,umask=0000

unhide makes linux show the hidden files on the dvd, uid/gid makes sure you’re allowed to read the files.

3. Start the Installer
Now try to start the installer: Open a console, change to /media/cdrom0 and start it.

cd /media/cdrom0
wine Installer.exe

If you’re lucky, it now works out of the box and you are finished.

If not (like me), it simply does nothing and we have to do the following.

4. Copy the DVD
If the Installer does not work out of the box, create a folder on your filesystem, e.g.
~/.wine/drive_c/sc2install
and copy the whole DVDs contents to this directory. After this is finished, try to start the Installer from there.


Log into your Battle.net Account.
http://www.battle.net
and Add your CD-Key to your Account. If you don’t have an Battle.net Account yet, create one, you’ll need it anyway.

After you’ve added the Game to your account, download the Windows Installer.
Start up the downloaded installer and select a folder in your wines Drive C. Let it download a few bytes and then quit the installer.
Now copy the files

Installer Tome 1.MPQE.part
Installer UI 1.MPQ.part
Installer UI 2.MPQE.part

from /media/cdrom0 to the created folder. In my case its ~/.wine/drive_c/sc2download/SC2-WingsOfLiberty-enGB-Installer

Now startup the downloaded installer again.
It should start checking the downloaded files (you might not see a difference in the progress bar, but the CPU goes up, watch with “top”).
After the file check is finished, the installer should start.

JPA2 Abstract DAO, Criteria Query & the “like” Operator

For a project at the university, I had to implement an abstract search in an abstract JPA Dao.

Maybe this class comes handy for some of you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.dominikdorn.rest.dao;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @author Dominik Dorn
 */
public class AbstractJpaDao<TYPE> {
    @PersistenceContext
    protected EntityManager em;
 
    protected Class entityClass;
 
    public Class getEntityClass() {
        return entityClass;
    }
 
    public void setEntityClass(Class entityClass) {
        this.entityClass = entityClass;
    }
 
    public AbstractJpaDao() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.entityClass = (Class<TYPE>) genericSuperclass.getActualTypeArguments()[0];
    }
 
    public AbstractJpaDao(Class clazz) {
        this.entityClass = clazz;
    }
 
    public EntityManager getEm() {
        return em;
    }
 
    public AbstractJpaDao setEm(EntityManager em) {
        this.em = em;
        return this;
    }
 
 
    @Override
    public TYPE persist(TYPE item) {
        if (item == null)
            throw new PersistenceException("Item may not be null");
        em.persist(item);
        return item;
    }
 
    @Override
    public TYPE update(TYPE item) {
        if (item == null)
            throw new PersistenceException("Item may not be null");
 
        em.merge(item);
        return item;
    }
 
    @Override
    public List<TYPE> getAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return em.createQuery(cq).getResultList();
    }
 
    @Override
    public TYPE getById(Long id) {
        if (id == null || id < 1)
            throw new PersistenceException("Id may not be null or negative");
 
        return (TYPE) em.find(entityClass, id);
    }
 
    @Override
    public void delete(TYPE item) {
        if (item == null)
            throw new PersistenceException("Item may not be null");
 
        em.remove(em.merge(item));
    }
 
    @Override
    public List<TYPE> findByAttributes(Map<String, String> attributes) {
        List<TYPE> results;
        //set up the Criteria query
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<TYPE> cq = cb.createQuery(getEntityClass());
        Root<TYPE> foo = cq.from(getEntityClass());
 
        List<Predicate> predicates = new ArrayList<Predicate>();
        for(String s : attributes.keySet())
        {
            if(foo.get(s) != null){
                predicates.add(cb.like((Expression) foo.get(s), "%" + attributes.get(s) + "%" ));
            }
        }
        cq.where(predicates.toArray(new Predicate[]{}));
        TypedQuery<TYPE> q = em.createQuery(cq);
 
        results = q.getResultList();
        return results;
    }
}

To instantiate this for an Entity, e.g. “Item”, simply do this

1
2
3
  // get an entityManager somewhere here 
  AbstractJpaDao<Item> dao = new AbstractJpaDao<Item>();
dao.setEm(em);

now you can use this Data Access Object for your persistence stuff.

To now search for entities in your DB, you can use the method findByAttributes which takes a map<String,String> and searches for appropriate items.
If your Entity looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Entity
public class Item {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "ITEM_GEN")
    @SequenceGenerator(name="ITEM_GEN", allocationSize=25, sequenceName = "item_seq")
    private long id;
 
    @Basic
    private String name;
 
    @Basic
    private String description;
 
    @Basic
    private Integer size;
// constructors, getters, setters

you could search for an item which names contain “test” like this

1
2
3
4
 
Map<String,String> attr = new Hashmap<String,String>();
attr.put("name", "test");
List<Item> results = dao.findByAttributes(attr);

which comes quite handy in my opinion. Also note, that you don’t have to pre-generate your JPA2 Model classes.

Essential Glassfish Performance Tuning Blogs

In my time on the Glassfish users mailinglist, I came across some important links for everyone setting up a Glassfish server for production.

This is my personal bookmarks list, that may also serve its purpose to others.

  1. Putting Glassfish v3 in Production – Essential surviving Guide: This post describes basic performance enhancements you can get with changing settings in your domain.xml configuration and adjusting jvm settings
  2. Monitoring in Glassfish: This post shows how to further enhance the thread pool settings for your Glassfish domain and how to determine the correct settings for your system
  3. Tomcat/Glassfish/Jetty on Port 80 with IPTables + NAT: This post describes, how you can run your Glassfish hosted webapps on Port 80 without running a Apache or other web server in front of it, thus you are able to fully utilize every aspect of Glassfishs new asynchronous architecture without always have to think of that old Indian making problems (Comet etc.)
  4. Blog Post on Java.net: This post by user "radix_zero" also suggests a few other tricks, like creating multiple connection pools to the same data source or running multiple real-domains, each in a own JVM.
  5. Another Post on Java.net: The thread points out that one should use distinct thread pools for each http-listener to prevent locking between the listeners

This post will be updated with new additional & helpful links, as soon as I get aware of them.

Do you have interesting links? Post them in the comments!

Access Quercus PHP-Session from Java Servlets/Filters/Beans – Part 1

If you are integrating a PHP Application in your Java Environment, you’ll probably came across Quercus, a Java Implementation of PHP provided by Caucho Technologies.

As you are moving more and more into the Servlet world, you’ll probably will be using Servlet-Filters and normal Servlets todo some tasks.

If you then want to access Variables in a PHP Session, here’s how to do it:

The Quercus PHP-Session is not stored the same way as the normal Servlet HTTP-Session is stored. Quercus uses an own Session Manager that is capable of doing all this stuff that you know from PHP, like e.g. migrating Session-IDs (to prevent session-hijacking etc.).

To access this Session Manager, you first have to get a hand on the QuercusContext. In the current release (4.0.7) there is no easy way to get a hand on it if you are not inside a PHPModule or a PHP-Page. Thus I simply copied com.caucho.quercus.servlet.QuercusServlet to com.caucho.quercus.servlet.DCQuercusServlet and added this line to the end of the method:

1
2
3
4
5
6
  private void initImpl(ServletConfig config)
    throws ServletException
  {
....
      config.getServletContext().setAttribute("quercusContext", quercus);
 }

which exposes the QuercusContext as an Attribute in the ServletContext.

(Note: If you are running multiple QuercusServlets in your WebApp – which you probably shouldn’t – this might will be a problem)
(Note2: The package name com.caucho.quercus.servlet must be used, or else you will not have access to some package scoped properties used in the QuercusServlet)
Ok, so now we have access to the QuercusContext from every Servlet, Filter or JSF-Bean, horray 🙂

But how to use it?

Lets take a look at the following filter and its methods helping me to work with the PHP-Session:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class LoginOutFilter implements Filter {
 
    QuercusContext context; // we store the reference to the quercusContext in the filter
    FilterConfig config; // the reference to the config 
 
... 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        config = filterConfig;
    }
 
    // looks up the QuercusContext the first time it is accessed
    // Beware: will return null if no PHP page has been rendered yet and the quercusContext is not already set. 
    private QuercusContext getQuercusContext()
    {
        if(context == null)
        {
            context = (QuercusContext) config.getServletContext().getAttribute("quercusContext");
        }
        return context;
 
    }
 
    // find the PHP-Session Cookie in the array of Cookies provided by the browser
    public Cookie findPHPSessionCookie(Cookie[] cookies)
    {
        for(Cookie c : cookies)
        {
            if(c.getName().equals("PHPSESSID")){
                return c;
            }
        }
        return null;
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
     if(someCondition)
     {
                // clear a PHP-Session, e.g. when logging out a user
                Cookie c = findPHPSessionCookie(((HttpServletRequest) servletRequest).getCookies());
                if(c != null){
                    QuercusSessionManager mng = getQuercusContext().getQuercusSessionManager();
                    String phpSessionId = c.getValue();
                    mng.removeSession(phpSessionId);
                }
     }
 
     if(someOtherCondition) {
                        QuercusSessionManager mng = getQuercusContext().getQuercusSessionManager();
 
                        SessionArrayValue val;
                        Cookie c = findPHPSessionCookie(((HttpServletRequest) servletRequest).getCookies());
                        String phpSessionId;
                        if(c != null)
                        {
        // we found a session cookie
                            phpSessionId = c.getValue();
                            val = mng.getSession(null, phpSessionId, new Date().getTime()  );
                            if(val == null)
                            {
        // but the session does not exist on the server, so create it
                                val = mng.createSession(null, phpSessionId, new Date().getTime());
                            }
 
                        }else
                        {
          // we haven't found a php-session cookie, so create a new session
                            val = mng.createSession(null,null,new Date().getTime());
                            phpSessionId = val.getId();
          // and store the appropriate cookie
                            Cookie newCookie = new Cookie("PHPSESSID", phpSessionId);
                            ((HttpServletResponse)servletResponse).addCookie(newCookie);
                        }
     // storing data in the session
                      String username = "myUsername";
                        val.put("ludata_suserid", username);
                        val.put("ludata_nusernr", someService.getUserId(username));
                        val.put("ludata_nstatusnr", someService.getUserStatus(username));
                        val.addUse(); // This is required for saving, else an IllegalStateException will be thrown
                        mng.saveSession(null, val);
       }
     }

The above code shows how to erase a session and how to add values to a session. To get values from a session, simply use val.get(..).

Attention: This currently only works if the page after the filter is not a Quercus Page itself (e.g. a Servlet or a JSF Page). I’m working on a solution for this

Maven + JUnit + ClassFormatError: Absent Code attribute in method that is not native or abstract in class file

Once in a while one stumbles upon an error message like this

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Session

This happens if your code compiles against incomplete classes, like the JavaEE6 Api and your Unit tests try to access code thats not there. JUnit will simply fail and mark the test as error, printing something like this

Tests in error: 
  initializationError(com.dominikdorn.dc.passwordReset.SimplePasswordResetServiceTest)

and the corresponding surefire text file starts like this:

-------------------------------------------------------------------------------
Test set: com.dominikdorn.dc.passwordReset.SimplePasswordResetServiceTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.022 sec <<< FAILURE!
initializationError(com.dominikdorn.dc.passwordReset.SimplePasswordResetServiceTest)  Time elapsed: 0.005 sec  <<< ERROR!
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Session
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
....

The Solution
You have to compile against real-implementations of the classes. You do that by adding those dependencies before the most generic dependency in your pom.xml

As example, we add javax.mail BEFORE javaee6-api like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<project ...>
...
    <dependencies>
 
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
</project>

If you are deploying your App on an Appserver like Glassfish or JBoss AS, leave the scope of javax.mail:mail as provided to prevent the inclusion of the jar in the final webapp.

I now list required dependencies for various Absent Code Errors I've encountered and still encountering. This post will be updated every time I solve another of these problems.

JavaMail: javax/mail/Session

1
2
3
4
5
6
7
8
9
10
11
<project ...>
...
    <dependencies>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

Servlet 3.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<project ...>
...
    <dependencies>
               <dependency>
                       <groupId>org.glassfish</groupId>
                       <artifactId>javax.servlet</artifactId>
                       <version>3.0</version>
                       <scope>provided</scope>
               </dependency>
...
    </dependencies>
....   
    <repositories>
               <!-- Required until the Servlet 3.0 API can be resolved in Central -->     
               <repository>
                       <id>Glassfish</id>
                       <name>Glassfish Maven2 Repository</name>
                       <url>http://download.java.net/maven/glassfish/</url>
               </repository>
    </repositories>
 </project>

JPA2: javax/persistence/PersistenceException

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>...
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
            <scope>provided</scope>
        </dependency>
...</dependencies>
..
<repositories>
...
    <repositories>
...
        <repository>
            <id>eclipse</id>
            <name>Eclipse Maven Repository</name>
            <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
        </repository>
    </repositories>

JAX-RS: javax/ws/rs/core/UriBuilder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.1.5</version>
            <scope>provided</scope>
        </dependency>
<!-- for json support -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.1.5</version>
            <scope>provided</scope>
        </dependency>
<!-- for testing --> 
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-test-framework</artifactId>
            <version>1.1.5.1</version>
            <scope>test</scope>
        </dependency>

more to come soon!

XHTML strict & OL start=”xx”

In XHTML strict, the ol-tag does not have the attribute start.

To get this working again, simply create a css-class

1
2
.counterList li {display: block }
.counterList li:before {content: counter(start) ". "; counter-increment: start}

and surround your list with a tag like this

1
2
3
4
5
6
7
<div class='counterList>
<ol style="counter-reset: start 4">
<li>this will be item 5</li>
<li>this will be item 6</li>
...
</ol>
</div>

I got this idea from http://www.timrivera.com/tests/ol-start.html

CDI/Weld manual lookup

So, you’re ended up in a situation, where you are somewhere (e.g. a javax.faces.Converter) where you are unable to simple @Inject SomeClass ?

I had the problem, that I had a FacesConverter like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
 
 
@FacesConverter(forClass = AvailableCountry.class)
@ManagedBean // does not help :(
@RequestScoped // does not help :(
public class AvailableCountryConverter implements Converter
{
	@Inject
	AvailableCountryDao dao;
 
	@PostConstruct
	public void postConstruct()
	{
		System.out.println("calling postConstruct");
	}
 
	public Object getAsObject(FacesContext facesContext, UIComponent
			component, String value) {
		if (value == null || value.length() == 0) {
			return null;
		}
		return dao.find(getKey(value));
	}
 
	Long getKey(String value) {
		Long key;
		key = Long.valueOf(value);
		return key;
	}
 
	String getStringKey(long value) {
		StringBuffer sb = new StringBuffer();
		sb.append(value);
		return sb.toString();
	}
 
	public String getAsString(FacesContext facesContext, UIComponent
			component, Object object) {
		if (object == null) {
			return null;
		}
		if (object instanceof AvailableCountry) {
			AvailableCountry o = (AvailableCountry) object;
			return getStringKey(o.getCountry().getId());
		} else {
			throw new IllegalArgumentException("object " + object + "
					is of type " + object.getClass().getName() + "; expected type: " +
					AvailableCountry.class.getName());
		}
	}
}

But my DAO was not injected, nor was the postConstruct method triggered by CDI.

Why?
Because the bean is not managed by CDI, not even when annotating it with @ManagedBean because it gets created by the JSF-Lifecycle and not by CDI.

Well.. but how to manually lookup a Bean with CDI / Weld?

First, you need to get the BeanManager. When you have a FacesContext (like in the converter above), you can get it like this:

1
2
3
4
5
6
    public BeanManager getBeanManager()
    {
        return (BeanManager) 
              ((ServletContext) facesContext.getExternalContext().getContext())
                   .getAttribute("javax.enterprise.inject.spi.BeanManager"); 
    }

If you don’t have access to a FacesContext, ServletContext or similar, you can lookup the BeanManager through JNDI

1
2
3
4
5
6
7
8
9
10
    public BeanManager getBeanManager()
    {
        try{
            InitialContext initialContext = new InitialContext();
            return (BeanManager) initialContext.lookup("java:comp/BeanManager");
        catch (NamingException e) {
            log.error("Couldn't get BeanManager through JNDI");
            return null;
        }
    }

After you’ve got your BeanManager, simply lookup your Bean like this:
(In my case, I wanted to lookup a bean with the type AvailableCountryDao)
Type-based CDI manual lookup

1
2
3
4
5
6
7
8
    public AvailableCountryDao getFacade()
    {
        BeanManager bm = getBeanManager();
        Bean<AvailableCountryDao> bean = (Bean<AvailableCountryDao>) bm.getBeans(AvailableCountryDao.class).iterator().next();
        CreationalContext<AvailableCountryDao> ctx = bm.createCreationalContext(bean);
        AvailableCountryDao dao = (AvailableCountryDao) bm.getReference(bean, AvailableCountryDao.class, ctx); // this could be inlined, but intentionally left this way
        return dao;
    }

Thanks to my friend Lincoln Baxter, III for the snipped.

Name-based CDI manual lookup

1
2
3
4
5
6
7
8
    public Object getBeanByName(String name) // eg. name=availableCountryDao
    {
        BeanManager bm = getBeanManager();
        Bean bean = bm.getBeans(name).iterator().next();
        CreationalContext ctx = bm.createCreationalContext(bean); // could be inlined below
        Object o = bm.getReference(bean, bean.getClass(), ctx); // could be inlined with return
        return o;
    }

So, now you’re able to manually lookup beans with CDI.
In case you have the same problem (with Converters/Validators) like I had above, checkout Seam Faces, where this problem already is fixed,
meaning your @ManagedBean annotated Converter/Validator is working as expected with @Inject, @PostConstruct & @PreDestroy 🙂