View Javadoc

1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.framework.services;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.rice.kim.api.type.KimType;
21  import org.kuali.rice.kim.api.type.KimTypeUtils;
22  import org.kuali.rice.kim.framework.type.KimTypeService;
23  
24  import javax.xml.namespace.QName;
25  
26  public class KimFrameworkServiceLocator {
27      private static final Logger LOG = Logger.getLogger(KimFrameworkServiceLocator.class);
28  
29      static <T> T getService(String serviceName) {
30          return GlobalResourceLoader.<T>getService(serviceName);
31      }
32  
33      	/**
34  	 * Fetches the KimTypeService for the given KimType  If the kimType passed in is null
35  	 * then this method will return null.  This method will resolve the kimTypeServiceName
36  	 * on the given KimType and then delegate to {@link #getKimTypeService(javax.xml.namespace.QName)}.
37  	 */
38  	public static KimTypeService getKimTypeService(KimType kimType) {
39  		if( kimType == null ) {
40              throw new IllegalArgumentException("Invalid service name passed, value was null.");
41  		}
42  		return getKimTypeService(KimTypeUtils.resolveKimTypeServiceName(kimType.getServiceName()));
43  	}
44  
45  	/**
46  	 * Fetches the KimTypeService for the given kim type service name.  If the given {@link javax.xml.namespace.QName}
47  	 * is null, then this method will throw an IllegalArgumentException.
48  	 *
49  	 * @throws IllegalArgumentException if the given kimTypeServiceName is null
50  	 */
51  	public static KimTypeService getKimTypeService(QName kimTypeServiceName) {
52  		if (kimTypeServiceName == null) {
53  			throw new IllegalArgumentException("Invalid service name passed, value was null.");
54  		}
55  		try {
56  			return (KimTypeService) GlobalResourceLoader.getService(kimTypeServiceName);
57  		} catch (Exception exception) {
58  
59  			// if we get an exception loading the remote KimTypeService, then instead of completly failing,
60  			// we'll handle the exception and return a null reference to the service
61  
62  			LOG.error("Unable to find KIM type service with name: " + kimTypeServiceName, exception);
63  			return null;
64  		}
65  	}
66  }