📄 jaxrpcportclientinterceptor.java
字号:
* @see #postProcessJaxRpcService
* @see #postProcessPortStub
* @see #postProcessJaxRpcCall
*/
public void prepare() throws ServiceException {
if (this.portName == null) {
throw new IllegalArgumentException("portName is required");
}
if (this.jaxRpcService == null) {
this.jaxRpcService = createJaxRpcService();
}
// Cache the QName for the port.
this.portQName = getQName(this.portName);
// Determine interface to use at the JAX-RPC port level:
// Use portInterface if specified, else fall back to serviceInterface.
Class actualInterface = (this.portInterface != null ? this.portInterface : this.serviceInterface);
if (actualInterface != null && Remote.class.isAssignableFrom(actualInterface) &&
!alwaysUseJaxRpcCall()) {
// JAX-RPC-compliant port interface -> using JAX-RPC stub for port.
if (logger.isInfoEnabled()) {
logger.info("Creating JAX-RPC proxy for JAX-RPC port [" + this.portQName +
"], using port interface [" + actualInterface.getName() + "]");
}
Remote remoteObj = this.jaxRpcService.getPort(this.portQName, actualInterface);
if (logger.isInfoEnabled()) {
if (this.serviceInterface != null) {
boolean isImpl = this.serviceInterface.isInstance(remoteObj);
logger.info("Using service interface [" + this.serviceInterface.getName() + "] for JAX-RPC port [" +
this.portQName + "] - " + (!isImpl ? "not" : "") + " directly implemented");
}
}
if (!(remoteObj instanceof Stub)) {
throw new ServiceException("Port stub of class [" + remoteObj.getClass().getName() +
"] is not a valid JAX-RPC stub: it does not implement interface [javax.xml.rpc.Stub]");
}
Stub stub = (Stub) remoteObj;
// Apply properties to JAX-RPC stub.
preparePortStub(stub);
// Allow for custom post-processing in subclasses.
postProcessPortStub(stub);
this.portStub = remoteObj;
}
else {
// No JAX-RPC-compliant port interface -> using JAX-RPC dynamic calls.
if (logger.isInfoEnabled()) {
logger.info("Using JAX-RPC dynamic calls for JAX-RPC port [" + this.portQName + "]");
}
}
}
/**
* Return the prepared QName for the port.
* @see #setPortName
* @see #getQName
*/
protected QName getPortQName() {
return portQName;
}
/**
* Return whether to always use JAX-RPC dynamic calls.
* Called by <code>afterPropertiesSet</code>.
* <p>Default is false; if an RMI interface is specified as "portInterface"
* or "serviceInterface", it will be used to create a JAX-RPC port stub.
* <p>Can be overridden to enforce the use of the JAX-RPC Call API,
* for example if there is a need to customize at the Call level.
* This just necessary if you you want to use an RMI interface as
* "serviceInterface", though; in case of only a non-RMI interface being
* available, this interceptor will fall back to the Call API anyway.
* @see #postProcessJaxRpcCall
*/
protected boolean alwaysUseJaxRpcCall() {
return false;
}
/**
* Prepare the given JAX-RPC port stub, applying properties to it.
* Called by <code>afterPropertiesSet</code>.
* <p>Just applied when actually creating a JAX-RPC port stub,
* in case of a specified JAX-RPC-compliant port interface.
* Else, JAX-RPC dynamic calls will be used.
* @param stub the current JAX-RPC port stub
* @see #afterPropertiesSet
* @see #setUsername
* @see #setPassword
* @see #setEndpointAddress
* @see #setMaintainSession
* @see #setCustomProperties
* @see #setPortInterface
* @see #prepareJaxRpcCall
*/
protected void preparePortStub(Stub stub) {
if (this.username != null) {
stub._setProperty(Stub.USERNAME_PROPERTY, this.username);
}
if (this.password != null) {
stub._setProperty(Stub.PASSWORD_PROPERTY, this.password);
}
if (this.endpointAddress != null) {
stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, this.endpointAddress);
}
if (this.maintainSession) {
stub._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, new Boolean(this.maintainSession));
}
if (this.customProperties != null) {
Enumeration en = this.customProperties.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
stub._setProperty(key, this.customProperties.getProperty(key));
}
}
}
/**
* Post-process the given JAX-RPC port stub.
* Default implementation is empty. Called by <code>prepare</code>.
* <p>Just applied when actually creating a JAX-RPC port stub,
* in case of a specified JAX-RPC-compliant port interface.
* Else, JAX-RPC dynamic calls will be used.
* @param stub the current JAX-RPC port stub
* (can be cast to an implementation-specific class if necessary)
* @see #prepare
* @see #setPortInterface
* @see #postProcessJaxRpcCall
*/
protected void postProcessPortStub(Stub stub) {
}
/**
* Return the underlying JAX-RPC port stub that this interceptor delegates to
* for each method invocation on the proxy.
*/
protected Remote getPortStub() {
return portStub;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
if (AopUtils.isToStringMethod(invocation.getMethod())) {
return "JAX-RPC proxy for port [" + getPortName() + "] of service [" +
getJaxRpcService().getServiceName() + "]";
}
Remote stub = getPortStub();
if (stub != null) {
// JAX-RPC stub available -> traditional RMI stub invocation.
if (logger.isDebugEnabled()) {
logger.debug("Invoking operation '" + invocation.getMethod().getName() +
"' on JAX-RPC port stub");
}
return RmiClientInterceptorUtils.invoke(invocation, stub, getPortQName().toString());
}
else {
// No JAX-RPC stub -> using JAX-RPC dynamic calls.
if (logger.isDebugEnabled()) {
logger.debug("Invoking operation '" + invocation.getMethod().getName() +
"' as JAX-RPC dynamic call");
}
return performJaxRpcCall(invocation);
}
}
/**
* Perform a JAX-RPC dynamic call for the given AOP method invocation.
* Delegates to <code>prepareJaxRpcCall</code> and
* <code>postProcessJaxRpcCall</code> for setting up the call object.
* <p>Default implementation uses method name as JAX-RPC operation name
* and method arguments as arguments for the JAX-RPC call. Can be
* overridden in subclasses for custom operation names and/or arguments.
* @param invocation the current AOP MethodInvocation that should
* be converted to a JAX-RPC call
* @return the return value of the invocation, if any
* @throws Throwable the exception thrown by the invocation, if any
* @see #getJaxRpcService
* @see #getPortQName
* @see #prepareJaxRpcCall
* @see #postProcessJaxRpcCall
*/
protected Object performJaxRpcCall(MethodInvocation invocation) throws Throwable {
Service service = getJaxRpcService();
if (service == null) {
throw new IllegalStateException("JaxRpcClientInterceptor is not properly initialized - " +
"invoke 'prepare' before attempting any operations");
}
QName portQName = getPortQName();
// Create JAX-RPC call object, using the method name as operation name.
Call call = service.createCall(portQName, invocation.getMethod().getName());
// Apply properties to JAX-RPC stub.
prepareJaxRpcCall(call);
// Allow for custom post-processing in subclasses.
postProcessJaxRpcCall(call, invocation);
// Perform actual invocation.
try {
return call.invoke(invocation.getArguments());
}
catch (RemoteException ex) {
throw RmiClientInterceptorUtils.convertRmiAccessException(
invocation.getMethod(), ex, portQName.toString());
}
}
/**
* Prepare the given JAX-RPC call, applying properties to it.
* Called by <code>invoke</code>.
* <p>Just applied when actually using JAX-RPC dynamic calls,
* i.e. if no JAX-RPC-compliant port interface was specified.
* Else, a JAX-RPC port stub will be used.
* @param call the current JAX-RPC call object
* @see #invoke
* @see #setUsername
* @see #setPassword
* @see #setEndpointAddress
* @see #setMaintainSession
* @see #setCustomProperties
* @see #setPortInterface
* @see #preparePortStub
*/
protected void prepareJaxRpcCall(Call call) {
if (this.username != null) {
call.setProperty(Call.USERNAME_PROPERTY, this.username);
}
if (this.password != null) {
call.setProperty(Call.PASSWORD_PROPERTY, this.password);
}
if (this.endpointAddress != null) {
call.setTargetEndpointAddress(this.endpointAddress);
}
if (this.maintainSession) {
call.setProperty(Call.SESSION_MAINTAIN_PROPERTY, new Boolean(this.maintainSession));
}
if (this.customProperties != null) {
Enumeration en = this.customProperties.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
call.setProperty(key, this.customProperties.getProperty(key));
}
}
}
/**
* Post-process the given JAX-RPC call.
* Default implementation is empty. Called by <code>invoke</code>.
* <p>Just applied when actually using JAX-RPC dynamic calls,
* i.e. if no JAX-RPC-compliant port interface was specified.
* Else, a JAX-RPC port stub will be used.
* @param call the current JAX-RPC call object
* (can be cast to an implementation-specific class if necessary)
* @param invocation the current AOP MethodInvocation that the call was
* created for (can be used to check method name, method parameters
* and/or passed-in arguments)
* @see #invoke
* @see #setPortInterface
* @see #postProcessPortStub
*/
protected void postProcessJaxRpcCall(Call call, MethodInvocation invocation) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -