01: /*
02: * Copyright 2007 The Kuali Foundation
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.ojb;
17:
18: import java.util.Collection;
19:
20: import org.apache.ojb.broker.PBKey;
21: import org.apache.ojb.broker.PersistenceBrokerException;
22: import org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl;
23: import org.apache.ojb.broker.query.Query;
24: import org.kuali.rice.util.ClassLoaderUtils;
25: import org.kuali.rice.util.ContextClassLoaderBinder;
26:
27: /**
28: * Sets up the context classloader properly for OJB proxies. The sequence of events in the super class is as
29: * follows:
30: *
31: * <pre>
32: * public synchronized Collection getData()
33: * {
34: * if (!isLoaded())
35: * {
36: * beforeLoading();
37: * setData(loadData());
38: * afterLoading();
39: * }
40: * return _data;
41: * }
42: * </pre>
43: *
44: * Therefore, since there is no try-catch-finally in conjunction with this call, we need to handle exceptions thrown
45: * from loadData and unbind the classloader appropriately.
46: */
47: public class ContextClassLoaderListProxy extends ListProxyDefaultImpl {
48:
49: private static final long serialVersionUID = -6734848267763746202L;
50:
51: // this object is used by object that's are serialized and we don't want to take this reference
52: // across the wire. May need to acquire the CCL if this is null on beforeLoading() doing nothing for now...
53: private transient ClassLoader contextClassLoader;
54:
55: public ContextClassLoaderListProxy(PBKey aKey, Class aCollClass,
56: Query aQuery) {
57: super (aKey, aCollClass, aQuery);
58: this .contextClassLoader = ClassLoaderUtils
59: .getDefaultClassLoader();
60: }
61:
62: public ContextClassLoaderListProxy(PBKey aKey, Query aQuery) {
63: super (aKey, aQuery);
64: this .contextClassLoader = ClassLoaderUtils
65: .getDefaultClassLoader();
66: }
67:
68: @Override
69: protected void beforeLoading() {
70: ContextClassLoaderBinder.bind(this .contextClassLoader);
71: super .beforeLoading();
72: }
73:
74: @Override
75: protected Collection loadData() throws PersistenceBrokerException {
76: try {
77: return super .loadData();
78: } catch (Throwable t) {
79: ContextClassLoaderBinder.unbind();
80: if (t instanceof PersistenceBrokerException) {
81: throw (PersistenceBrokerException) t;
82: } else if (t instanceof Error) {
83: throw (Error) t;
84: } else if (t instanceof RuntimeException) {
85: throw (RuntimeException) t;
86: } else {
87: throw new PersistenceBrokerException(
88: "Invalid exception type thrown!", t);
89: }
90: }
91: }
92:
93: @Override
94: protected void afterLoading() {
95: super.afterLoading();
96: ContextClassLoaderBinder.unbind();
97: }
98:
99: }
|