blob: cb36287e8006c9c0281a06b3f6bfd72fadf427cf [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.myfaces.extensions.validator.core.renderkit;
import org.apache.myfaces.extensions.validator.internal.UsageCategory;
import org.apache.myfaces.extensions.validator.internal.UsageInformation;
import org.apache.myfaces.extensions.validator.core.ExtValContext;
import org.apache.myfaces.extensions.validator.util.ClassUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.faces.context.FacesContext;
import javax.faces.render.Renderer;
import javax.faces.component.UIComponent;
import javax.faces.convert.ConverterException;
import java.io.IOException;
import java.lang.reflect.Constructor;
/**
* to support a custom proxy
*
* @author Gerhard Petracek
* @since 1.x.1
*/
@UsageInformation(UsageCategory.INTERNAL)
class ExtValLazyRendererProxy extends Renderer
{
protected final Log logger = LogFactory.getLog(getClass());
private Renderer wrapped;
public ExtValLazyRendererProxy(Renderer renderer)
{
this.wrapped = renderer;
if(logger.isTraceEnabled())
{
logger.trace("simple proxy created for " + renderer.getClass().getName());
}
}
@Override
public void decode(FacesContext facesContext, UIComponent uiComponent)
{
getLazyRenderer().decode(facesContext, uiComponent);
}
@Override
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
throws IOException
{
getLazyRenderer().encodeBegin(facesContext, uiComponent);
}
@Override
public void encodeChildren(FacesContext facesContext, UIComponent uiComponent)
throws IOException
{
getLazyRenderer().encodeChildren(facesContext, uiComponent);
}
@Override
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
throws IOException
{
getLazyRenderer().encodeEnd(facesContext, uiComponent);
}
@Override
public String convertClientId(FacesContext facesContext, String s)
{
return getLazyRenderer().convertClientId(facesContext, s);
}
@Override
public boolean getRendersChildren()
{
return getLazyRenderer().getRendersChildren();
}
@Override
public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object o)
throws ConverterException
{
return getLazyRenderer().getConvertedValue(facesContext, uiComponent, o);
}
private Renderer getLazyRenderer()
{
String proxyClassName = (String) ExtValContext.getContext().getGlobalProperty(ExtValRendererProxy.KEY);
if(proxyClassName != null && !proxyClassName.endsWith(getClass().getName()))
{
Class targetClass = ClassUtils.tryToLoadClassForName(proxyClassName);
if(targetClass == null)
{
throw new IllegalStateException("a custom invalid renderer proxy is configured: " + proxyClassName);
}
Class[] argClasses = new Class[1];
argClasses[0] = Renderer.class;
try
{
Constructor constructor = targetClass.getConstructor(argClasses);
return (Renderer)constructor.newInstance(this.wrapped);
}
catch (Throwable t)
{
if(logger.isWarnEnabled())
{
logger.warn("couldn't create: " + targetClass.getName());
}
return this.wrapped;
}
}
else
{
return this.wrapped;
}
}
}