Permalink
Browse files

Deprecate BeanManager#getReference() in favor of Beans#getReference()

  • Loading branch information...
1 parent 9a0c9b7 commit 214d189bdf373a80754970df63d0779e4c181300 @BalusC BalusC committed Feb 6, 2016
@@ -17,6 +17,7 @@
import static java.lang.Boolean.parseBoolean;
import static javax.faces.convert.Converter.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME;
+import static org.omnifaces.util.Beans.getReference;
import static org.omnifaces.util.Faces.getInitParameter;
import java.util.TimeZone;
@@ -30,7 +31,6 @@
import org.omnifaces.cdi.converter.ConverterManager;
import org.omnifaces.cdi.validator.ValidatorManager;
-import org.omnifaces.config.BeanManager;
/**
* <p>
@@ -67,8 +67,8 @@
*/
public OmniApplication(Application wrapped) {
this.wrapped = wrapped;
- converterManager = BeanManager.INSTANCE.getReference(ConverterManager.class);
- validatorManager = BeanManager.INSTANCE.getReference(ValidatorManager.class);
+ converterManager = getReference(ConverterManager.class);
+ validatorManager = getReference(ValidatorManager.class);
dateTimeConverterDefaultTimeZone =
parseBoolean(getInitParameter(DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME))
? TimeZone.getDefault()
@@ -15,14 +15,15 @@
*/
package org.omnifaces.application;
+import static org.omnifaces.util.Beans.getReference;
+
import javax.faces.component.UIViewRoot;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PreDestroyViewMapEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.ViewMapListener;
import org.omnifaces.cdi.viewscope.ViewScopeManager;
-import org.omnifaces.config.BeanManager;
/**
* Listener for JSF view scope destroy events so that view scope manager can be notified.
@@ -51,7 +52,7 @@ public boolean isListenerForSource(Object source) {
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
if (event instanceof PreDestroyViewMapEvent) {
- BeanManager.INSTANCE.getReference(ViewScopeManager.class).preDestroyView();
+ getReference(ViewScopeManager.class).preDestroyView();
}
}
@@ -16,11 +16,11 @@
package org.omnifaces.cdi.eager;
import static javax.faces.event.PhaseId.RESTORE_VIEW;
+import static org.omnifaces.util.Beans.getReference;
import static org.omnifaces.util.FacesLocal.getViewId;
import javax.faces.event.PhaseEvent;
-import org.omnifaces.config.BeanManager;
import org.omnifaces.eventlistener.DefaultPhaseListener;
/**
@@ -42,7 +42,7 @@
public EagerBeansPhaseListener() {
super(RESTORE_VIEW);
- eagerBeansRepository = BeanManager.INSTANCE.getReference(EagerBeansRepository.class);
+ eagerBeansRepository = getReference(EagerBeansRepository.class);
}
@Override
@@ -17,6 +17,7 @@
import static java.lang.String.format;
import static java.util.logging.Level.WARNING;
+import static org.omnifaces.util.Beans.getReference;
import static org.omnifaces.util.Utils.isAnyEmpty;
import static org.omnifaces.util.Utils.isEmpty;
@@ -58,7 +59,7 @@
public static EagerBeansRepository getInstance() { // Awkward workaround for it being unavailable via @Inject in listeners in Tomcat+OWB and Jetty.
if (instance == null) {
- instance = org.omnifaces.config.BeanManager.INSTANCE.getReference(EagerBeansRepository.class);
+ instance = getReference(EagerBeansRepository.class);
}
return instance;
@@ -17,23 +17,21 @@
import java.lang.reflect.Type;
import java.util.Set;
+import org.omnifaces.ApplicationListener;
+import org.omnifaces.util.Beans;
import org.omnifaces.util.JNDI;
/**
* <p>
- * This configuration enum allows you to get a reference to CDI managed beans without having any direct CDI dependency.
- * It will during initialization grab the CDI bean manager instance from JNDI and if it's not <code>null</code>, then
- * it's using reflection to get and store the necessary methods in the enum instance which are then invoked on instance
- * methods such as <code>getReference()</code>.
- *
- * <h3>Usage</h3>
- * <pre>
- * // Get the CDI managed bean instance of the given bean class.
- * SomeBean someBean = BeanManager.INSTANCE.getReference(SomeBean.class);
- * </pre>
+ * This configuration enum allows you to get the CDI <code>BeanManager</code> anyway in cases where
+ * <code>&#64;Inject</code> and/or <code>CDI#current()</code> may not work, or when you'd like to test availability of
+ * CDI without having any direct CDI dependency (as done in {@link ApplicationListener}). It will during initialization
+ * grab the CDI bean manager instance as generic object from JNDI.
* <p>
- * If you however already have a CDI bean manager instance at hands via <code>@Inject</code>, use
- * {@link org.omnifaces.util.BeansLocal#getReference(javax.enterprise.inject.spi.BeanManager, Class)} instead.
+ * <strong>Do not use it directly.</strong> Use {@link Beans} utility class instead. It will under the covers use this
+ * configuration enum. This configuration enum is basically a leftover from OmniFaces 1.x where the CDI dependency was
+ * optional. The {@link #getReference(Class)} method is deprecated since OmniFaces 2.3 and will be removed in OmniFaces
+ * 3.0.
*
* @author Bauke Scholtz
* @since 1.6.1
@@ -121,6 +119,7 @@ private BeanManager() {
/**
* Returns the CDI bean manager.
+ * <strong>It's preferred that you use {@link Beans#getManager()} for this.</strong>
* @param <T> The <code>javax.enterprise.inject.spi.BeanManager</code>.
* @return The CDI bean manager.
* @throws ClassCastException When you assign it to a variable which is not declared as CDI BeanManager.
@@ -137,7 +136,9 @@ private BeanManager() {
* @param beanClass The CDI managed bean class.
* @return The CDI managed bean reference (proxy) of the given class, or <code>null</code> if there is none.
* @throws UnsupportedOperationException When obtaining the CDI managed bean reference failed with an exception.
+ * @deprecated Use {@link Beans#getReference(Class)} instead.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public <T> T getReference(Class<T> beanClass) {
try {
Object bean = resolve.invoke(beanManager, getBeans.invoke(beanManager, beanClass, NO_ANNOTATIONS));
@@ -14,6 +14,7 @@
import static java.lang.Boolean.TRUE;
import static org.omnifaces.cdi.viewscope.ViewScopeManager.isUnloadRequest;
+import static org.omnifaces.util.Beans.getReference;
import static org.omnifaces.util.Components.buildView;
import static org.omnifaces.util.Faces.responseComplete;
import static org.omnifaces.util.FacesLocal.getApplicationAttribute;
@@ -30,7 +31,6 @@
import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.cdi.viewscope.ViewScopeManager;
-import org.omnifaces.config.BeanManager;
import org.omnifaces.taghandler.EnableRestorableView;
/**
@@ -78,7 +78,7 @@ public UIViewRoot restoreView(FacesContext context, String viewId) {
if (isUnloadRequest(context)) {
UIViewRoot createdView = createView(context, viewId);
createdView.restoreViewScopeState(context, getRenderKit(context).getResponseStateManager().getState(context, viewId));
- BeanManager.INSTANCE.getReference(ViewScopeManager.class).preDestroyView();
+ getReference(ViewScopeManager.class).preDestroyView();
responseComplete();
return createdView;
}

0 comments on commit 214d189

Please sign in to comment.