Package org.jpox.util

Source Code of org.jpox.util.ViewUtils

/**********************************************************************
Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
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.

Contributors:
    ...
**********************************************************************/
package org.jpox.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jpox.ObjectManagerFactoryImpl;
import org.jpox.exceptions.JPOXUserException;

/**
* Utilities for handling Views.
*
* @version $Revision: 1.4 $  
**/
public class ViewUtils
{
    protected static final Localiser LOCALISER=Localiser.getInstance("org.jpox.Localisation",
        ObjectManagerFactoryImpl.class.getClassLoader());

    /**
     * Check for any circular view references between referencer and referencee.
     * If one is found, throw a JPOXUserException with the chain of references.
     * @param viewReferences The Map of view references to check.
     * @param referencer_name Name of the class that has the reference.
     * @param referencee_name Name of the class that is being referenced.
     * @param referenceChain The List of class names that have been referenced
     * @throws JPOXUserException If a circular reference is found in the view definitions.
     */
    public static void checkForCircularViewReferences(Map viewReferences,
                                                      String referencer_name,
                                                      String referencee_name,
                                                      List referenceChain)
    {
        Set class_names = (Set)viewReferences.get(referencee_name);
        if (class_names != null)
        {
            // Initialize the chain of references if needed.  Add the referencee
            // to the chain.
            if (referenceChain == null)
            {
                referenceChain = new ArrayList();
                referenceChain.add(referencer_name);
            }
            referenceChain.add(referencee_name);

            // Iterate through all referenced classes from the referencee.  If
            // any reference the referencer, throw an exception.
            for (Iterator it=class_names.iterator(); it.hasNext(); )
            {
                String current_name=(String)it.next();
                if (current_name.equals(referencer_name))
                {
                    StringBuffer error=new StringBuffer(LOCALISER.msg("031003"));

                    for (Iterator chainIter=referenceChain.iterator(); chainIter.hasNext(); )
                    {
                        error.append(chainIter.next());
                        if (chainIter.hasNext())
                        {
                            error.append(" -> ");
                        }
                    }

                    throw new JPOXUserException(error.toString()).setFatal();
                }
                else
                {
                    // Make recursive call to check for any nested dependencies.
                    // e.g A references B, B references C, C references A.
                    checkForCircularViewReferences(viewReferences,
                                                   referencer_name,
                                                   current_name,
                                                   referenceChain);
                }
            }
        }
    }
}
TOP

Related Classes of org.jpox.util.ViewUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.