Skip to content

Commit

Permalink
One time only runtime check if MyFaces is in use
Browse files Browse the repository at this point in the history
Issue: SWF-1698
  • Loading branch information
rstoyanchev committed May 25, 2017
1 parent 380de15 commit 5a86845
Showing 1 changed file with 8 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2016 the original author or authors.
* Copyright 2004-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,6 @@

package org.springframework.faces.webflow;

import javax.faces.FacesWrapper;
import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;

import org.springframework.util.Assert;
Expand Down Expand Up @@ -77,7 +75,7 @@ public class JsfRuntimeInformation {
private static final boolean myFacesPresent =
ClassUtils.isPresent("org.apache.myfaces.webapp.MyFacesServlet", CLASSLOADER);

private static boolean myFacesInUse = isMyFacesContextFactoryInUse();
private static Boolean myFacesInUse;


private static boolean portletPresent = ClassUtils.isPresent("javax.portlet.Portlet", CLASSLOADER);
Expand Down Expand Up @@ -121,26 +119,14 @@ public static boolean isMyFacesPresent() {
}

public static boolean isMyFacesInUse() {
if (myFacesInUse) {
return true;
}
// On WebSphere MyFaces may have loaded after this class...
myFacesInUse = isMyFacesContextFactoryInUse();
return myFacesInUse;
}

private static boolean isMyFacesContextFactoryInUse() {
try {
Class<?> clazz = CLASSLOADER.loadClass("org.apache.myfaces.context.FacesContextFactoryImpl");
Object factory = FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
while (!clazz.isInstance(factory) && factory instanceof FacesWrapper) {
factory = ((FacesWrapper) factory).getWrapped();
if (myFacesInUse == null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext == null) {
return false;
}
return (factory != null && clazz.isInstance(factory));
}
catch (Throwable ex) {
return false;
myFacesInUse = facesContext.getClass().getPackage().getName().startsWith("org.apache.myfaces.");
}
return myFacesInUse;
}


Expand Down

0 comments on commit 5a86845

Please sign in to comment.