001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.ogn;
014    
015    import java.util.Map;
016    
017    import net.sf.oval.Validator;
018    import net.sf.oval.exception.ObjectGraphNavigatorNotAvailableException;
019    import net.sf.oval.internal.Log;
020    import net.sf.oval.internal.util.Assert;
021    import net.sf.oval.internal.util.ReflectionUtils;
022    
023    /**
024     * @author Sebastian Thomschke
025     *
026     */
027    public class ObjectGraphNavigatorRegistry
028    {
029            private static final Log LOG = Log.getLog(ObjectGraphNavigatorRegistry.class);
030    
031            private final Map<String, ObjectGraphNavigator> cache = Validator.getCollectionFactory().createMap(2);
032    
033            private ObjectGraphNavigator _initializeDefaultOGN(final String id)
034            {
035                    // JXPath support
036                    if ("jxpath".equals(id) && ReflectionUtils.isClassPresent("org.apache.commons.jxpath.JXPathContext"))
037                            return registerObjectGraphNavigator("jxpath", new ObjectGraphNavigatorJXPathImpl());
038    
039                    if ("".equals(id)) return registerObjectGraphNavigator("", new ObjectGraphNavigatorDefaultImpl());
040                    return null;
041            }
042    
043            public ObjectGraphNavigator getObjectGraphNavigator(final String id)
044            {
045                    Assert.argumentNotNull("id", id);
046    
047                    ObjectGraphNavigator ogn = cache.get(id);
048    
049                    if (ogn == null) ogn = _initializeDefaultOGN(id);
050    
051                    if (ogn == null) throw new ObjectGraphNavigatorNotAvailableException(id);
052    
053                    return ogn;
054            }
055    
056            public ObjectGraphNavigator registerObjectGraphNavigator(final String id, final ObjectGraphNavigator ogn)
057                            throws IllegalArgumentException
058            {
059                    Assert.argumentNotNull("id", id);
060                    Assert.argumentNotNull("ogn", ogn);
061    
062                    LOG.info("Object Graph Navigator '{1}' registered: {2}", id, ogn);
063    
064                    cache.put(id, ogn);
065                    return ogn;
066            }
067    }