org.apache.myfaces.trinidadinternal.application.ViewDeclarationLanguageFactoryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trinidad-impl Show documentation
Show all versions of trinidad-impl Show documentation
Private implementation of the Apache MyFaces Trinidad project
The newest version!
/*
* 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.trinidadinternal.application;
import java.beans.BeanInfo;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import javax.faces.application.Resource;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.faces.view.StateManagementStrategy;
import javax.faces.view.ViewDeclarationLanguage;
import javax.faces.view.ViewDeclarationLanguageFactory;
import javax.faces.view.ViewMetadata;
import org.apache.myfaces.trinidad.change.ChangeManager;
import org.apache.myfaces.trinidad.context.RequestContext;
import org.apache.myfaces.trinidad.logging.TrinidadLogger;
import org.apache.myfaces.trinidad.render.InternalView;
public class ViewDeclarationLanguageFactoryImpl
extends ViewDeclarationLanguageFactory
{
public ViewDeclarationLanguageFactoryImpl(ViewDeclarationLanguageFactory delegate)
{
super();
_wrapped = delegate;
_loadInternalViews();
InternalViewFinder finder = new InternalViewFinder()
{
public InternalView getInternalView(FacesContext context, String viewId)
{
return _getInternalView(context, viewId);
}
};
_internalViewStrategy = new InternalViewHandlingStrategy(finder);
}
@Override
public ViewDeclarationLanguage getViewDeclarationLanguage(String viewId)
{
FacesContext context = FacesContext.getCurrentInstance();
if (_getInternalView(context, viewId) != null)
return _internalViewStrategy;
// TRINIDAD-1703 - use physical URI (retrieved from the PageResolver) before calling the delegate's
// implementation
viewId = _getPath(context, viewId);
ViewDeclarationLanguage vdl = getWrapped().getViewDeclarationLanguage(viewId);
// Possiblity of nested VDLs of the same kind
if (vdl instanceof ChangeApplyingVDLWrapper)
return vdl;
return new ChangeApplyingVDLWrapper(getWrapped().getViewDeclarationLanguage(viewId));
}
@Override
public ViewDeclarationLanguageFactory getWrapped()
{
return _wrapped;
}
private InternalView _getInternalView(
FacesContext context,
String viewId)
{
Object cached = _internalViewCache.get(viewId);
if (cached != null)
{
return ((cached == _NOT_FOUND) ? null : (InternalView)cached);
}
InternalView internal = _internalViews.get(viewId);
if (internal == null)
{
// If we're using suffix-mapping, then any internal viewId will
// get affixed with ".jsp" or ".jspx"; try trimming that off
// if present
ExternalContext external = context.getExternalContext();
// Only bother when using suffix-mapping (path info will always
// be non-null for prefix-mapping)
if (external.getRequestPathInfo() == null)
{
String suffix = external.getInitParameter("javax.faces.DEFAULT_SUFFIX");
if (suffix == null)
suffix = ".jspx";
if (viewId.endsWith(suffix))
{
String viewIdWithoutSuffix = viewId.substring(
0, viewId.length() - suffix.length());
internal = _internalViews.get(viewIdWithoutSuffix);
}
}
}
_internalViewCache.put(viewId, (internal == null) ? _NOT_FOUND : internal);
return internal;
}
//
// Load the META-INF/org.apache.myfaces.trinidad.render.InternalView.properties
// files.
//
private void _loadInternalViews()
{
_internalViews = new HashMap();
List list = new ArrayList();
ClassLoader loader = _getClassLoader();
try
{
Enumeration en = loader.getResources(
"META-INF/org.apache.myfaces.trinidad.render.InternalView.properties");
while (en.hasMoreElements())
{
list.add(en.nextElement());
}
// And, for some temporary backwards compatibility, also load
// the incorrect properties without "render"
en = loader.getResources(
"META-INF/org.apache.myfaces.trinidad.InternalView.properties");
while (en.hasMoreElements())
{
list.add(en.nextElement());
}
// Reverse the list so it is in the proper order (most local
// entry "wins")
Collections.reverse(list);
}
catch (IOException ioe)
{
_LOG.severe(ioe);
}
for (URL url : list)
{
try
{
Properties properties = new Properties();
_LOG.fine("Loading internal views from {0}", url);
InputStream is = url.openStream();
try
{
properties.load(is);
}
finally
{
is.close();
}
for (Map.Entry