001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kew.api.extension;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.rice.core.api.reflect.ObjectDefinition;
020 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
021
022 import javax.xml.namespace.QName;
023
024 /**
025 * Contains utilities related to the loading of extension resources.
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029 public final class ExtensionUtils {
030
031 /**
032 * Loads the extension object for the given extension definition.
033 *
034 * @param extensionDefinition the definition of the extension to load
035 * @param <T> the type of the extension object which is to be loaded
036 *
037 * @return the loaded extension, or null if no extension was loaded for the given definition
038 */
039 public static <T> T loadExtension(ExtensionDefinitionContract extensionDefinition) {
040 return ExtensionUtils.<T>loadExtension(extensionDefinition, null);
041 }
042
043 /**
044 * Loads the extension object for the given extension definition, using the default applicationId if the given
045 * extension definition has a null applicationId.
046 *
047 * @param extensionDefinition the definition of the extension to load
048 * @param defaultApplicationId the application id to use when attempting to loading the extension if the
049 * application id on the given definition is null
050 * @param <T> the type of the extension object which is to be loaded
051 *
052 * @return the loaded extension, or null if no extension was loaded for the given definition
053 */
054 public static <T> T loadExtension(ExtensionDefinitionContract extensionDefinition, String defaultApplicationId) {
055 T extensionService = null;
056 // first check if the class name is a valid and available java class
057 String resourceDescriptor = extensionDefinition.getResourceDescriptor();
058 ObjectDefinition extensionObjectDefinition = getExtensionObjectDefinition(extensionDefinition,
059 defaultApplicationId);
060 extensionService = GlobalResourceLoader.<T>getObject(extensionObjectDefinition);
061 if (extensionService == null) {
062 // if we can't find a class, try a service
063 extensionService = GlobalResourceLoader.<T>getService(QName.valueOf(resourceDescriptor));
064 }
065 return extensionService;
066 }
067
068 private static ObjectDefinition getExtensionObjectDefinition(ExtensionDefinitionContract extensionDefinition, String defaultApplicationId) {
069 if (StringUtils.isBlank(extensionDefinition.getApplicationId()) && StringUtils.isNotBlank(defaultApplicationId)) {
070 return new ObjectDefinition(extensionDefinition.getResourceDescriptor(), defaultApplicationId);
071 } else {
072 return new ObjectDefinition(extensionDefinition.getResourceDescriptor(), extensionDefinition.getApplicationId());
073 }
074 }
075
076 private ExtensionUtils() {
077 throw new UnsupportedOperationException();
078 }
079
080 }