01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.plugin;
18:
19: import java.lang.reflect.Method;
20:
21: import javax.xml.namespace.QName;
22:
23: import org.kuali.rice.proxy.BaseInvocationHandler;
24: import org.kuali.rice.proxy.TargetedInvocationHandler;
25: import org.kuali.rice.resourceloader.GlobalResourceLoader;
26:
27: /**
28: * A proxy for a service which can be overridden in the Institutional Plugin.
29: *
30: * @author ewestfal
31: */
32: public class ServiceOverrideProxy extends BaseInvocationHandler
33: implements OverridableService, TargetedInvocationHandler {
34:
35: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
36: .getLogger(ServiceOverrideProxy.class);
37:
38: private static final String GET_DEFAULT_SERVICE_METHOD = "getDefaultService";
39:
40: private final QName serviceName;
41: private final Object defaultService;
42: private final Method defaultServiceMethod;
43:
44: private Object actualService = null;
45:
46: public ServiceOverrideProxy(QName serviceName, Object defaultService)
47: throws NoSuchMethodException {
48: this .serviceName = serviceName;
49: this .defaultService = defaultService;
50: this .defaultServiceMethod = OverridableService.class.getMethod(
51: GET_DEFAULT_SERVICE_METHOD, new Class[0]);
52: }
53:
54: protected Object invokeInternal(Object proxy, Method m,
55: Object[] args) throws Throwable {
56: if (m.equals(defaultServiceMethod)) {
57: return m.invoke(this , args);
58: }
59: synchronized (this ) {
60: if (actualService == null) {
61: actualService = GlobalResourceLoader
62: .getService(serviceName);
63: // if we get an OverridableService instance back then we know that we are using the default service implementation
64: if (actualService instanceof OverridableService) {
65: actualService = ((OverridableService) actualService)
66: .getDefaultService();
67: LOG
68: .info("Could not locate service override for service '"
69: + serviceName
70: + "', falling back to default implementation.");
71: } else {
72: LOG.info("Found service override for service '"
73: + serviceName
74: + "' in the institutional plugin.");
75: }
76: }
77: }
78: return m.invoke(actualService, args);
79: }
80:
81: public Object getDefaultService() {
82: return defaultService;
83: }
84:
85: public Object getTarget() {
86: return actualService;
87: }
88:
89: }
|