1 /**
2 * Copyright 2005-2016 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.kew.api.extension;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.reflect.ObjectDefinition;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21
22 import javax.xml.namespace.QName;
23
24 /**
25 * Contains utilities related to the loading of extension resources.
26 *
27 * @author Kuali Rice Team (rice.collab@kuali.org)
28 */
29 public final class ExtensionUtils {
30
31 /**
32 * Loads the extension object for the given extension definition.
33 *
34 * @param extensionDefinition the definition of the extension to load
35 * @param <T> the type of the extension object which is to be loaded
36 *
37 * @return the loaded extension, or null if no extension was loaded for the given definition
38 */
39 public static <T> T loadExtension(ExtensionDefinitionContract extensionDefinition) {
40 return ExtensionUtils.<T>loadExtension(extensionDefinition, null);
41 }
42
43 /**
44 * Loads the extension object for the given extension definition, using the default applicationId if the given
45 * extension definition has a null applicationId.
46 *
47 * @param extensionDefinition the definition of the extension to load
48 * @param defaultApplicationId the application id to use when attempting to loading the extension if the
49 * application id on the given definition is null
50 * @param <T> the type of the extension object which is to be loaded
51 *
52 * @return the loaded extension, or null if no extension was loaded for the given definition
53 */
54 public static <T> T loadExtension(ExtensionDefinitionContract extensionDefinition, String defaultApplicationId) {
55 T extensionService = null;
56 // first check if the class name is a valid and available java class
57 String resourceDescriptor = extensionDefinition.getResourceDescriptor();
58 ObjectDefinition extensionObjectDefinition = getExtensionObjectDefinition(extensionDefinition,
59 defaultApplicationId);
60 extensionService = GlobalResourceLoader.<T>getObject(extensionObjectDefinition);
61 if (extensionService == null) {
62 // if we can't find a class, try a service
63 extensionService = GlobalResourceLoader.<T>getService(QName.valueOf(resourceDescriptor));
64 }
65 return extensionService;
66 }
67
68 private static ObjectDefinition getExtensionObjectDefinition(ExtensionDefinitionContract extensionDefinition, String defaultApplicationId) {
69 if (StringUtils.isBlank(extensionDefinition.getApplicationId()) && StringUtils.isNotBlank(defaultApplicationId)) {
70 return new ObjectDefinition(extensionDefinition.getResourceDescriptor(), defaultApplicationId);
71 } else {
72 return new ObjectDefinition(extensionDefinition.getResourceDescriptor(), extensionDefinition.getApplicationId());
73 }
74 }
75
76 private ExtensionUtils() {
77 throw new UnsupportedOperationException();
78 }
79
80 }