View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   */
16  package org.kuali.student.common.test.resourceloader;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.config.property.ConfigurationService;
21  import org.kuali.rice.core.api.resourceloader.ServiceLocator;
22  import org.kuali.rice.kim.api.identity.IdentityService;
23  import org.kuali.rice.kim.api.role.RoleService;
24  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25  import org.kuali.rice.krad.service.impl.KualiModuleServiceImpl;
26  import org.kuali.student.kim.permission.mock.IdentityServiceMockImpl;
27  import org.kuali.student.kim.permission.mock.RoleServiceMockImpl;
28  import org.springframework.context.ApplicationContext;
29  
30  import javax.xml.namespace.QName;
31  import java.util.Map;
32  
33  /**
34   * This is a simple service locator class which piggy backs on the application context for returning services.
35   * Certain special KRAD services are provided through static variables.
36   *
37   * @author haroon rafique
38   */
39  public class SimpleSpringResourceLoader implements ServiceLocator {
40  
41      private static final ConfigurationService configurationService = new ConfigurationService() {
42          @Override public String getPropertyValueAsString(String key) { return "{0} message"; }
43          @Override public boolean getPropertyValueAsBoolean(String key) { return false; }
44          @Override public Map<String, String> getAllProperties() { return null; }
45      };
46  
47      private static final KualiModuleServiceImpl kualiModuleService = new KualiModuleServiceImpl();
48      private static final IdentityService identityService = new IdentityServiceMockImpl();
49      private static final RoleService roleService = new RoleServiceMockImpl();
50  
51      private ApplicationContext applicationContext;
52  
53      public SimpleSpringResourceLoader(ApplicationContext applicationContext) {
54          this.applicationContext = applicationContext;
55          
56          kualiModuleService.setApplicationContext(applicationContext);
57      }
58  
59      public Object getService(QName qname) {
60          if (qname == null || StringUtils.isEmpty(qname.toString())) {
61              throw new IllegalArgumentException("The service name must be non-null.");
62          }
63  
64          String qualifiedServiceName = qname.toString();
65  
66          if (CoreApiServiceLocator.KUALI_CONFIGURATION_SERVICE.equals(qualifiedServiceName)) {
67              return configurationService;
68          } else if (KRADServiceLocatorWeb.KUALI_MODULE_SERVICE.equals(qualifiedServiceName)) {
69              return kualiModuleService;
70          } else if ("{http://rice.kuali.org/kim/v2_0}identityService".equals(qualifiedServiceName)) {
71              return identityService;
72          } else if ("{http://rice.kuali.org/kim/v2_0}roleService".equals(qualifiedServiceName)) {
73              return roleService;
74          } else {
75              
76              String localBeanName = qname.getLocalPart();
77              return applicationContext.getBean(localBeanName);
78          }
79      }
80  
81     
82  
83      @Override
84      public String getContents(String indent, boolean servicePerLine) {
85          return null;
86      }
87  
88      @Override
89      public void start() throws Exception {
90      }
91  
92      @Override
93      public void stop() throws Exception {
94      }
95  
96      @Override
97      public boolean isStarted() {
98          return false;
99      }
100 }