@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
throws Exception
{
// get hold of EJBContext
EJBContext currentEJBContext = CurrentInvocationContext.get().getEJBContext();
// now get hold of the BeanManager from the EJBContext.
// Note that we could have directly used EJBContext.getTimerService(), but that
// won't work in all cases, because the EJBContext.getTimerService() is an user API
// and the spec mandates that this API throw an exception when invoked during injection.
// Internal implementation (like injectors) of EJB3 will require to get hold of the
// TimerService during injection (to actually do the injection). Hence we bypass the user
// API and instead use the BeanManager.getTimerService() which doesn't have a restriction
// on using it during injection.
BeanManager beanManager = currentEJBContext.getManager();
// now finally get hold of the timerservice
return beanManager.getTimerService();
}