View Javadoc

1   /**
2    * Copyright 2005-2013 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.resourceloader;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.rice.core.api.config.CoreConfigHelper;
22  import org.kuali.rice.core.api.config.property.ConfigContext;
23  import org.kuali.rice.core.api.reflect.ObjectDefinition;
24  import org.kuali.rice.core.api.resourceloader.ServiceLocator;
25  import org.kuali.rice.core.impl.resourceloader.BaseWrappingResourceLoader;
26  import org.kuali.rice.kew.api.KewApiServiceLocator;
27  import org.kuali.rice.kew.plugin.PluginRegistry;
28  import org.kuali.rice.kew.api.KewApiConstants;
29  
30  
31  /**
32   * A resource loader which is responsible for loading resources from the Workflow ConfigContext.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class CoreResourceLoader extends BaseWrappingResourceLoader {
37  
38  	public static final QName NAME = new QName(CoreConfigHelper.getApplicationId(), "KEW_SPRING+PLUGIN_REGISTRY_CONTAINER_RESOURCE_LOADER");
39  
40  	private final PluginRegistry registry;
41  
42  	public CoreResourceLoader(ServiceLocator serviceLocator, PluginRegistry registry) {
43  		super(CoreResourceLoader.NAME, serviceLocator);
44  		this.registry = registry;
45  	}
46  
47  	/**
48  	 * Overrides the standard getService method to looks for the service in the plugin if it can't find it in the core.
49  	 */
50  	public Object getService(QName serviceName) {
51  		if (isRemoteService(serviceName)) {
52  			return null;
53  		}
54  		Object service = super.getService(serviceName);
55  		if (service == null && getRegistry() != null) {
56  		    service = getRegistry().getService(serviceName);
57  		}
58  		return service;
59  	}
60  
61  
62  
63  	@Override
64  	public Object getObject(ObjectDefinition objectDefinition) {
65  	    Object object = super.getObject(objectDefinition);
66  	    if (object == null && getRegistry() != null) {
67  		object = getRegistry().getObject(objectDefinition);
68  	    }
69  	    return object;
70  	}
71  
72  	@Override
73  	protected boolean shouldWrapService(QName serviceName, Object service) {
74  		// transaction template is not wrappable because it does not implement an interface
75  		if (serviceName.getLocalPart().equals("transactionTemplate")) {
76  			return false;
77  		}
78  		return super.shouldWrapService(serviceName, service);
79  	}
80  	
81  	
82  	
83  	@Override
84  	public void stop() throws Exception {
85  		if (getRegistry() != null) {
86  			registry.stop();
87  		}
88  		super.stop();
89  	}
90  
91  	/**
92  	 * Returns true if the given service name is one which should be loaded from the service bus.  This is used
93  	 * primarily for embedded clients that want to reference the workgroup and user services on a standalone
94  	 * server.
95  	 */
96  	protected boolean isRemoteService(QName serviceName) {
97  	    return (useRemoteEmailServices() &&
98  			    serviceName.getLocalPart().equals(KewApiServiceLocator.IMMEDIATE_EMAIL_REMINDER_QUEUE.getLocalPart()));
99  	}
100 		
101 	protected boolean useRemoteEmailServices() {
102 	    String useRemoteEmailServicesValue = ConfigContext.getCurrentContextConfig().getProperty(KewApiConstants.USE_REMOTE_EMAIL_SERVICES);
103 	    if (!StringUtils.isBlank(useRemoteEmailServicesValue)) {
104 	        return new Boolean(useRemoteEmailServicesValue.trim());
105 	    }
106 	    return false;
107 	}
108 
109 	public PluginRegistry getRegistry() {
110 		return registry;
111 	}
112 	
113 }