Skip to content

Commit

Permalink
Deprecate BeanManager#getReference() in favor of Beans#getReference()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauke Scholtz committed Feb 6, 2016
1 parent 9a0c9b7 commit 214d189
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/omnifaces/application/OmniApplication.java
Expand Up @@ -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;
Expand All @@ -30,7 +31,6 @@

import org.omnifaces.cdi.converter.ConverterManager;
import org.omnifaces.cdi.validator.ValidatorManager;
import org.omnifaces.config.BeanManager;

/**
* <p>
Expand Down Expand Up @@ -67,8 +67,8 @@ public class OmniApplication extends ApplicationWrapper {
*/
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()
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}

Expand Down
Expand Up @@ -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;

/**
Expand All @@ -42,7 +42,7 @@ public class EagerBeansPhaseListener extends DefaultPhaseListener {

public EagerBeansPhaseListener() {
super(RESTORE_VIEW);
eagerBeansRepository = BeanManager.INSTANCE.getReference(EagerBeansRepository.class);
eagerBeansRepository = getReference(EagerBeansRepository.class);
}

@Override
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -58,7 +59,7 @@ public class EagerBeansRepository {

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;
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/org/omnifaces/config/BeanManager.java
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -137,7 +136,9 @@ public <T> T get() {
* @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));
Expand Down
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 214d189

Please sign in to comment.