Author: nbelaevski
Date: 2010-06-29 06:16:16 -0400 (Tue, 29 Jun 2010)
New Revision: 17680
Modified:
root/core/trunk/impl/src/main/java/org/richfaces/jsr330/BinderImpl.java
Log:
Small fixes & generics refactoring in BinderImpl
Modified: root/core/trunk/impl/src/main/java/org/richfaces/jsr330/BinderImpl.java
===================================================================
--- root/core/trunk/impl/src/main/java/org/richfaces/jsr330/BinderImpl.java 2010-06-28
23:35:15 UTC (rev 17679)
+++ root/core/trunk/impl/src/main/java/org/richfaces/jsr330/BinderImpl.java 2010-06-29
10:16:16 UTC (rev 17680)
@@ -28,6 +28,7 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.faces.component.UIComponent;
+import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.inject.Provider;
@@ -55,8 +56,12 @@
public T get() {
if (!instantiated) {
- value = getProvider().get();
- instantiated = true;
+ synchronized (this) {
+ if (!instantiated) {
+ value = getProvider().get();
+ instantiated = true;
+ }
+ }
}
return value;
}
@@ -77,7 +82,7 @@
T value;
FacesContext facesContext = FacesContext.getCurrentInstance();
if (null != facesContext) {
- Map map = getMap(facesContext);
+ Map<? super String, Object> map = getMap(facesContext);
String key = target.toKey();
value = (T) map.get(key);
if (null == value) {
@@ -90,8 +95,7 @@
return value;
}
- @SuppressWarnings("unchecked")
- protected abstract Map getMap(FacesContext facesContext);
+ protected abstract Map<? super String, Object> getMap(FacesContext
facesContext);
}
/**
@@ -121,8 +125,12 @@
rwLock.readLock().unlock();
rwLock.writeLock().lock();
try {
- value = getProvider().get();
- map.put(key, value);
+ //re-check the value
+ value = (T) map.get(key);
+ if (null == value) {
+ value = getProvider().get();
+ map.put(key, value);
+ }
} finally {
rwLock.readLock().lock();
rwLock.writeLock().unlock();
@@ -164,15 +172,14 @@
};
private final Scope<T> componentScope = new FacesScope() {
- @SuppressWarnings("unchecked")
@Override
protected Map<String, Object> getMap(FacesContext facesContext) {
- Map<String, Object> map =
- (Map<String, Object>)
facesContext.getAttributes().get(UIComponent.CURRENT_COMPONENT);
- if (null == map) {
+ UIComponent currentComponent =
UIComponentBase.getCurrentComponent(facesContext);
+ if (currentComponent == null) {
throw new DependencyException("Current component is not set for
dependency target " + target.toString());
}
- return map;
+
+ return currentComponent.getAttributes();
}
};
private final Scope<T> applicationScope = new ThreadSafeFacesScope() {
@@ -184,9 +191,9 @@
private final Target target;
- private Provider<T> provider;
+ private Provider<? extends T> provider;
- private Provider<Provider<T>> providerOfProvider;
+ private Provider<Provider<? extends T>> providerOfProvider;
private Scope<T> scope;
@@ -249,10 +256,9 @@
*
* @see org.richfaces.jsr330.Binder#toProvider(java.lang.Class)
*/
- @SuppressWarnings("unchecked")
public Binder<T> toProvider(Class<? extends Provider<T>>
providerImplementation) {
checkNotInitialized();
- this.providerOfProvider = new DynamicProvider(providerImplementation);
+ this.providerOfProvider = new DynamicProvider<Provider<? extends
T>>(providerImplementation);
return this;
}
@@ -361,7 +367,7 @@
this.initialized = false;
}
- Provider<T> getProvider() {
+ Provider<? extends T> getProvider() {
return null == this.provider ? this.providerOfProvider.get() : this.provider;
}