
org.xlcloud.console.extensions.scope.ViewScopedContext Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2012 AMG.lab, a Bull Group Company
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xlcloud.console.extensions.scope;
import java.util.Map;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
/**
* Class intended to work with ViewScope annotation.
* It stores instantiated beans for the whole cycle of view contextm
* even if there is ajax data exchange.
*
* @author "Konrad Król", AMG.net
*/
public class ViewScopedContext implements Context {
/** {@inheritDoc} */
@Override
@SuppressWarnings( { "rawtypes", "unchecked" } )
public Class getScope() {
return ViewScoped.class;
}
/** {@inheritDoc} */
@Override
@SuppressWarnings( { "unchecked", "rawtypes" } )
public T get(Contextual contextual, CreationalContext creationalContext) {
Bean beanFromContextual = (Bean) contextual;
Map viewScopeMap = getViewScopeMap();
if(viewScopeMap.containsKey(beanFromContextual.getName())) {
return (T) viewScopeMap.get(beanFromContextual.getName());
} else {
T resultBean = (T) beanFromContextual.create(creationalContext);
viewScopeMap.put(beanFromContextual.getName(), resultBean);
return resultBean;
}
}
/** {@inheritDoc} */
@Override
@SuppressWarnings( { "unchecked", "rawtypes" } )
public T get(Contextual contextual) {
Bean bean = (Bean) contextual;
return (T) getViewScopeMap().get(bean.getName());
}
/** {@inheritDoc} */
@Override
public boolean isActive() {
return true;
}
@SuppressWarnings( "rawtypes" )
private Map getViewScopeMap() {
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
return viewRoot.getViewMap(true);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy