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.apache.cxf.frontend;
19:
20: import java.lang.reflect.InvocationHandler;
21: import java.lang.reflect.Method;
22: import java.lang.reflect.Proxy;
23: import java.util.Map;
24: import java.util.logging.Logger;
25:
26: import org.apache.cxf.common.i18n.Message;
27: import org.apache.cxf.common.logging.LogUtils;
28: import org.apache.cxf.endpoint.Client;
29: import org.apache.cxf.endpoint.Endpoint;
30: import org.apache.cxf.interceptor.Fault;
31: import org.apache.cxf.service.model.BindingOperationInfo;
32:
33: public class ClientProxy implements InvocationHandler {
34:
35: private static final Logger LOG = LogUtils
36: .getL7dLogger(ClientProxy.class);
37:
38: private Endpoint endpoint;
39:
40: private Client client;
41:
42: public ClientProxy(Client c) {
43: endpoint = c.getEndpoint();
44: client = c;
45: }
46:
47: public Object invoke(Object proxy, Method method, Object[] args)
48: throws Throwable {
49:
50: MethodDispatcher dispatcher = (MethodDispatcher) endpoint
51: .getService().get(MethodDispatcher.class.getName());
52: BindingOperationInfo oi = dispatcher.getBindingOperation(
53: method, endpoint);
54: if (oi == null) {
55: // check for method on BindingProvider and Object
56: if (method.getDeclaringClass().equals(Object.class)) {
57: return method.invoke(this );
58: }
59:
60: throw new Fault(new Message("NO_OPERATION_INFO", LOG,
61: method.getName()));
62: }
63:
64: Object[] params = args;
65: if (null == params) {
66: params = new Object[0];
67: }
68:
69: return invokeSync(method, oi, params, null);
70: }
71:
72: public Object invokeSync(Method method, BindingOperationInfo oi,
73: Object[] params, Map<String, Object> context)
74: throws Exception {
75: Object rawRet[] = client.invoke(oi, params, context);
76:
77: if (rawRet != null && rawRet.length > 0) {
78: return rawRet[0];
79: } else {
80: return null;
81: }
82: }
83:
84: public Client getClient() {
85: return client;
86: }
87:
88: public static Client getClient(Object o) {
89: return ((ClientProxy) Proxy.getInvocationHandler(o))
90: .getClient();
91: }
92: }
|