01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.entity.jdbc;
19:
20: import java.lang.reflect.Constructor;
21: import java.lang.reflect.InvocationHandler;
22: import java.lang.reflect.InvocationTargetException;
23: import java.lang.reflect.Method;
24: import java.lang.reflect.Proxy;
25:
26: public abstract class AbstractCursorHandler implements
27: InvocationHandler {
28:
29: protected String cursorName;
30: protected int fetchSize;
31:
32: protected AbstractCursorHandler(String cursorName, int fetchSize) {
33: this .cursorName = cursorName;
34: this .fetchSize = fetchSize;
35: }
36:
37: public void setCursorName(String cursorName) {
38: this .cursorName = cursorName;
39: }
40:
41: public String getCursorName() {
42: return cursorName;
43: }
44:
45: public void setFetchSize(int fetchSize) {
46: this .fetchSize = fetchSize;
47: }
48:
49: public int getFetchSize() {
50: return fetchSize;
51: }
52:
53: protected Object invoke(Object obj, Object proxy, Method method,
54: Object[] args) throws Throwable {
55: if ("toString".equals(method.getName())) {
56: String str = obj.toString();
57: return getClass().getName() + "{" + str + "}";
58: }
59: return method.invoke(obj, args);
60: }
61:
62: protected static Object newHandler(InvocationHandler handler,
63: Class implClass) throws IllegalAccessException,
64: IllegalArgumentException, InstantiationException,
65: InvocationTargetException, NoSuchMethodException,
66: SecurityException {
67: ClassLoader loader = implClass.getClassLoader();
68: if (loader == null)
69: loader = ClassLoader.getSystemClassLoader();
70: Class proxyClass = Proxy.getProxyClass(loader,
71: new Class[] { implClass });
72: Constructor constructor = proxyClass
73: .getConstructor(new Class[] { InvocationHandler.class });
74: return constructor.newInstance(new Object[] { handler });
75: }
76: }
|