View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.ogn;
14  
15  import java.util.Map;
16  
17  import net.sf.oval.Validator;
18  import net.sf.oval.exception.ObjectGraphNavigatorNotAvailableException;
19  import net.sf.oval.internal.Log;
20  import net.sf.oval.internal.util.Assert;
21  import net.sf.oval.internal.util.ReflectionUtils;
22  
23  /**
24   * @author Sebastian Thomschke
25   *
26   */
27  public class ObjectGraphNavigatorRegistry
28  {
29  	private static final Log LOG = Log.getLog(ObjectGraphNavigatorRegistry.class);
30  
31  	private final Map<String, ObjectGraphNavigator> cache = Validator.getCollectionFactory().createMap(2);
32  
33  	private ObjectGraphNavigator _initializeDefaultOGN(final String id)
34  	{
35  		// JXPath support
36  		if ("jxpath".equals(id) && ReflectionUtils.isClassPresent("org.apache.commons.jxpath.JXPathContext"))
37  			return registerObjectGraphNavigator("jxpath", new ObjectGraphNavigatorJXPathImpl());
38  
39  		if ("".equals(id)) return registerObjectGraphNavigator("", new ObjectGraphNavigatorDefaultImpl());
40  		return null;
41  	}
42  
43  	public ObjectGraphNavigator getObjectGraphNavigator(final String id)
44  	{
45  		Assert.argumentNotNull("id", id);
46  
47  		ObjectGraphNavigator ogn = cache.get(id);
48  
49  		if (ogn == null) ogn = _initializeDefaultOGN(id);
50  
51  		if (ogn == null) throw new ObjectGraphNavigatorNotAvailableException(id);
52  
53  		return ogn;
54  	}
55  
56  	public ObjectGraphNavigator registerObjectGraphNavigator(final String id, final ObjectGraphNavigator ogn)
57  			throws IllegalArgumentException
58  	{
59  		Assert.argumentNotNull("id", id);
60  		Assert.argumentNotNull("ogn", ogn);
61  
62  		LOG.info("Object Graph Navigator '{1}' registered: {2}", id, ogn);
63  
64  		cache.put(id, ogn);
65  		return ogn;
66  	}
67  }