/**
 * Licensed to Apereo under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright ownership. Apereo
 * 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 the
 * following location:
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>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.apereo.portal.xml.xpath;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathVariableResolver;
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Creates new {@link XPathExpression} instances */
class XPathExpressionFactory extends BaseKeyedPoolableObjectFactory {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private final XPathFactory xPathFactory = XPathFactory.newInstance();
    private final NamespaceContext namespaceContext;
    private final XPathVariableResolver variableResolver;

    public XPathExpressionFactory(
            NamespaceContext namespaceContext, XPathVariableResolver variableResolver) {
        this.namespaceContext = namespaceContext;
        this.variableResolver = variableResolver;
    }

    @Override
    public synchronized Object makeObject(Object key) throws Exception {
        final String expression = (String) key;

        final XPath xPath = xPathFactory.newXPath();
        if (this.namespaceContext != null) {
            xPath.setNamespaceContext(this.namespaceContext);
        }
        if (this.variableResolver != null) {
            xPath.setXPathVariableResolver(this.variableResolver);
        }

        logger.debug("Compiling XPathExpression from: {}", expression);

        try {
            return xPath.compile(expression);
        } catch (XPathExpressionException e) {
            throw new RuntimeException(
                    "Failed to compile XPath expression '" + expression + "'", e);
        }
    }

    @Override
    public void destroyObject(Object key, Object obj) throws Exception {
        final String expression = (String) key;
        logger.debug("Destroying XPathExpression: {}", expression);
    }
}

@@ -1098,16 +1098,17 @@
 ons.pool
+2
 .BaseKey
@@ -1105,36 +1105,34 @@
 l2.BaseKeyedPool
-abl
 e
+d
 ObjectFactory;%0Ai
@@ -1126,24 +1126,128 @@
 ectFactory;%0A
+import org.apache.commons.pool2.PooledObject;%0Aimport org.apache.commons.pool2.impl.DefaultPooledObject;%0A
 import org.s
@@ -1395,20 +1395,18 @@
 eyedPool
-abl
 e
+d
 ObjectFa
@@ -1410,16 +1410,41 @@
 tFactory
+%3CString, XPathExpression%3E
  %7B%0A    p
@@ -1958,45 +1958,345 @@
 lic 
-synchronized Object makeObject(Object
+PooledObject%3CXPathExpression%3E wrap(XPathExpression obj) %7B%0A        return new DefaultPooledObject%3C%3E(obj);%0A    %7D%0A%0A    @Override%0A    public synchronized PooledObject%3CXPathExpression%3E makeObject(String key) throws RuntimeException %7B%0A        return wrap(create(key));%0A    %7D%0A%0A    @Override%0A    public synchronized XPathExpression create(String
  key
@@ -2296,32 +2296,39 @@
 ing key) throws 
+Runtime
 Exception %7B%0A    
@@ -3033,26 +3033,49 @@
 ect(
-Object key, Object
+String key, PooledObject%3CXPathExpression%3E
  obj
@@ -3083,16 +3083,23 @@
  throws 
+Runtime
 Exceptio
@@ -3131,33 +3131,24 @@
 expression =
- (String)
  key;%0A      

update XPathExpressionFactory to implement new factory