SF 1702604 - alpha prototype for activeXInvocationProxy event callbacks

This commit is contained in:
clay_shooter
2007-04-18 02:59:38 +00:00
parent 536fc35171
commit 7d7ed62066
13 changed files with 747 additions and 534 deletions

View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 1999-2004 Sourceforge JACOB Project.
* All rights reserved. Originator: Dan Adler (http://danadler.com).
* Get more information about JACOB at http://sourceforge.net/projects/jacob-project
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.jacob.activeX;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.InvocationProxy;
/**
* RELEASE 1.12 EXPERIMENTAL.
* <p>
* Use this exactly like the DispatchEvents class. This class plugs in
* an ActiveXInvocationProxy instead of an InvocationProxy. It is the
* ActiveXInvocationProxy that implements the reflection calls and invoke
* the found java event callbacks. See ActiveXInvocationProxy for details.
*
*
*/
public class ActiveXDispatchEvents extends DispatchEvents {
/**
* This is the most commonly used constructor.
* <p>
* Creates the event callback linkage between the the
* MS program represented by the Dispatch object and the
* Java object that will receive the callback.
* @param sourceOfEvent Dispatch object who's MS app will generate callbacks
* @param eventSink Java object that wants to receive the events
*/
public ActiveXDispatchEvents(Dispatch sourceOfEvent, Object eventSink) {
super(sourceOfEvent, eventSink, null );
}
/**
* None of the samples use this constructor.
* <p>
* Creates the event callback linkage between the the
* MS program represented by the Dispatch object and the
* Java object that will receive the callback.
* @param sourceOfEvent Dispatch object who's MS app will generate callbacks
* @param eventSink Java object that wants to receive the events
* @param progId ???
*/
public ActiveXDispatchEvents(Dispatch sourceOfEvent, Object eventSink, String progId) {
super(sourceOfEvent, eventSink, progId, null );
}
/**
* Creates the event callback linkage between the the
* MS program represented by the Dispatch object and the
* Java object that will receive the callback.
* <pre>
* >ActiveXDispatchEvents de =
* new ActiveXDispatchEvents(someDispatch,someEventHAndler,
* "Excel.Application",
* "C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE");
*
* @param sourceOfEvent Dispatch object who's MS app will generate callbacks
* @param eventSink Java object that wants to receive the events
* @param progId , mandatory if the typelib is specified
* @param typeLib The location of the typelib to use
*/
public ActiveXDispatchEvents(Dispatch sourceOfEvent, Object eventSink, String progId, String typeLib)
{
super(sourceOfEvent, eventSink, progId, typeLib);
}
/*
* (non-Javadoc)
* @see com.jacob.com.DispatchEvents#getInvocationProxy(java.lang.Object)
*/
protected InvocationProxy getInvocationProxy(Object pTargetObject){
InvocationProxy newProxy = new ActiveXInvocationProxy();
newProxy.setTarget(pTargetObject);
return newProxy;
}
}

View File

@@ -0,0 +1,167 @@
/*
* Copyright (c) 1999-2004 Sourceforge JACOB Project.
* All rights reserved. Originator: Dan Adler (http://danadler.com).
* Get more information about JACOB at http://sourceforge.net/projects/jacob-project
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.jacob.activeX;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.jacob.com.InvocationProxy;
import com.jacob.com.NotImplementedException;
import com.jacob.com.Variant;
/**
* RELEASE 1.12 EXPERIMENTAL.
* <p>
* This class that lets event handlers receive events with all java
* objects as parameters. The standard Jacob event methods all accept an array of
* Variant objects. When using this class, you can set up your event methods
* as regular java methods with the correct number of parameters of the correct
* java type. This does NOT work for any event that wishes to accept a call
* back and modify the calling parameters to tell windows what to do. An example
* is when an event lets the receiver cancel the action by setting a boolean flag
* to false. The java objects cannot be modified and their values will not be passed
* back into the originating Variants even if they could be modified.
* <p>
* This class acts as a proxy between the windows event callback mechanism and
* the Java classes that are looking for events. It assumes that
* all of the Java classes that are looking for events implement methods with the
* same names as the windows events and that the implemented methods native
* java objects of the type and order that match the windows documentation.
* The methods can return void or a Variant that
* will be returned to the calling layer. All Event methods that will be
* recognized by InvocationProxyAllEvents have the signature
*
* <code> void eventMethodName(Object,Object...)</code>
* or
* <code> Object eventMethodName(Object,Object...)</code>
*/
public class ActiveXInvocationProxy extends InvocationProxy {
/*
* (non-Javadoc)
* @see com.jacob.com.InvocationProxy#invoke(java.lang.String, com.jacob.com.Variant[])
*/
public Variant invoke(String methodName, Variant targetParameters[]) {
Variant mVariantToBeReturned = null;
if (mTargetObject == null){
// structured programming guidlines say this return should not be up here
return null;
}
Class targetClass = mTargetObject.getClass();
if (methodName == null){
throw new IllegalArgumentException("InvocationProxy: missing method name");
}
if (targetParameters == null){
throw new IllegalArgumentException("InvocationProxy: missing Variant parameters");
}
try {
Method targetMethod;
Object parametersAsJavaObjects[] = getParametersAsJavaObjects(targetParameters);
Class parametersAsJavaClasses[] = getParametersAsJavaClasses(parametersAsJavaObjects);
targetMethod = targetClass.getMethod(methodName, parametersAsJavaClasses);
if (targetMethod != null){
// protected classes can't be invoked against even if they
// let you grab the method. you could do targetMethod.setAccessible(true);
// but that should be stopped by the security manager
Object mReturnedByInvocation = null;
mReturnedByInvocation =
targetMethod.invoke(mTargetObject,parametersAsJavaObjects);
if (mReturnedByInvocation == null){
mVariantToBeReturned = null;
} else if (!(mReturnedByInvocation instanceof Variant)){
mVariantToBeReturned = new Variant(mReturnedByInvocation);
} else {
mVariantToBeReturned = (Variant) mReturnedByInvocation;
}
}
} catch (SecurityException e) {
// what causes this exception?
e.printStackTrace();
} catch (NoSuchMethodException e) {
// this happens whenever the listener doesn't implement all the methods
} catch (IllegalArgumentException e) {
// we can throw these inside the catch block so need to re-throw it
Exception oneWeShouldToss = new IllegalArgumentException("Unable to map parameters for method "+methodName
+ ": "+e.toString());
oneWeShouldToss.printStackTrace();
} catch (IllegalAccessException e) {
// can't access the method on the target instance for some reason
e.printStackTrace();
} catch (InvocationTargetException e) {
// invocation of target method failed
e.printStackTrace();
}
return mVariantToBeReturned;
}
/**
* creates a method signature compatible array of classes from an array of parameters
* @param parametersAsJavaObjects
* @return
*/
private Class[] getParametersAsJavaClasses(Object[] parametersAsJavaObjects) {
if (parametersAsJavaObjects == null){
throw new IllegalArgumentException("This only works with an array of parameters");
}
int numParameters = parametersAsJavaObjects.length;
Class parametersAsJavaClasses[] = new Class[numParameters];
for ( int parameterIndex = 0 ; parameterIndex < numParameters; parameterIndex++){
Object oneParameterObject = parametersAsJavaObjects[parameterIndex];
if (oneParameterObject == null){
parametersAsJavaClasses[parameterIndex] = null;
} else {
Class oneParameterClass = oneParameterObject.getClass();
parametersAsJavaClasses[parameterIndex] = oneParameterClass;
}
}
return parametersAsJavaClasses;
}
/**
* converts an array of Variants to their associated Java types
* @param targetParameters
* @return
*/
private Object[] getParametersAsJavaObjects(Variant[] targetParameters) {
if (targetParameters == null){
throw new IllegalArgumentException("This only works with an array of parameters");
}
int numParameters = targetParameters.length;
Object parametersAsJavaObjects[] = new Object[numParameters];
for ( int parameterIndex = 0 ; parameterIndex < numParameters; parameterIndex++){
Variant oneParameterObject = targetParameters[parameterIndex];
if (oneParameterObject == null){
parametersAsJavaObjects[parameterIndex] = null;
} else {
try {
parametersAsJavaObjects[parameterIndex] = oneParameterObject.toJavaObject();
} catch (NotImplementedException nie){
throw new IllegalArgumentException("Can't convert parameter "+parameterIndex+
" type "+oneParameterObject.getvt()
+" to java object: "+nie.getMessage());
}
}
}
return parametersAsJavaObjects;
}
}

View File

@@ -108,7 +108,7 @@ public class DispatchEvents extends JacobObject {
if (eventSink instanceof InvocationProxy) {
mInvocationProxy = (InvocationProxy) eventSink;
} else {
mInvocationProxy = new InvocationProxy(eventSink);
mInvocationProxy = getInvocationProxy(eventSink);
}
if (mInvocationProxy != null) {
init3(sourceOfEvent, mInvocationProxy, progId, typeLib);
@@ -121,6 +121,17 @@ public class DispatchEvents extends JacobObject {
}
/**
* returns an instance of the proxy configured with pTargetObject as its target
* @param pTargetObject
* @return InvocationProxy an instance of the proxy this DispatchEvents will send to the COM layer
*/
protected InvocationProxy getInvocationProxy(Object pTargetObject){
InvocationProxy newProxy = new InvocationProxyAllVariants();
newProxy.setTarget(pTargetObject);
return newProxy;
}
/**
* hooks up a connection point proxy by progId
* event methods on the sink object will be called
* by name with a signature of <name>(Variant[] args)
@@ -155,7 +166,7 @@ public class DispatchEvents extends JacobObject {
*/
public void safeRelease(){
if (mInvocationProxy!=null){
mInvocationProxy.clearTarget();
mInvocationProxy.setTarget(null);
}
mInvocationProxy = null;
super.safeRelease();

View File

@@ -19,9 +19,6 @@
*/
package com.jacob.com;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @version $Id$
* @author joe
@@ -31,136 +28,44 @@ import java.lang.reflect.Method;
* This means that EventProxy.cpp just calls invoke(String,Variant[])
* against the instance of this class. Then this class does
* reflection against the event listener to call the actual event methods.
* All Event methods have the signature
*
* <code> void eventMethodName(Variant[])</code>
* or
* <code> Variant eventMethodName(Variant[])</code>
* The event methods can return void or return a Variant. Any
* value returned will be passed back to the calling windows module
* by the Jacob JNI layer.
* <p>
*
* The void returning signature is the standard legacy signature.
* The Variant returning signature was added in 1.10 to support event handlers
* returning values.
*
*/
public class InvocationProxy {
public abstract class InvocationProxy {
/**
* the object we will try and forward to.
*/
Object mTargetObject = null;
protected Object mTargetObject = null;
/**
* dummy constructor for subclasses that don't actually wrap
* anything and just want to override the invoke() method
*/
protected InvocationProxy(){
}
/**
* Constructs an invocation proxy that fronts for an event listener.
* The InvocationProxy will wrap the target object and forward
* any received messages to the wrapped object
* @param pTargetObject
*/
protected InvocationProxy(Object pTargetObject){
super();
if (JacobObject.isDebugEnabled()){
JacobObject.debug(
"InvocationProxy: created for object "+pTargetObject);
}
mTargetObject = pTargetObject;
if (mTargetObject == null){
throw new IllegalArgumentException("InvocationProxy requires a target");
}
// JNI code apparently bypasses this check and could operate against
// protected classes. This seems like a security issue...
// maybe it was because JNI code isn't in a package?
if (!java.lang.reflect.Modifier.isPublic(
pTargetObject.getClass().getModifiers())){
throw new IllegalArgumentException(
"InvocationProxy only public classes can receive event notifications");
}
}
/**
* The method actually invoked by EventProxy.cpp.
* The method name is calculated by the underlying JNI code from the MS windows
* Callback function name. The method is assumed to take an array of Variant
* Callback function name. The method is assumed to take an array of Variant
* objects. The method may return a Variant or be a void. Those are the only
* two options that will not blow up.
* <p>
* Subclasses that override this should make sure mTargetObject is not null before processing.
*
* @param methodName name of method in mTargetObject we will invoke
* @param targetParameter Variant[] that is the single parameter to the method
* @param targetParameters Variant[] that is the single parameter to the method
*/
public Variant invoke(String methodName, Variant targetParameter[]){
Variant mVariantToBeReturned = null;
if (mTargetObject == null){
if (JacobObject.isDebugEnabled()){
JacobObject.debug(
"InvocationProxy: received notification ("+methodName+") with no target set");
}
// structured programming guidlines say this return should not be up here
return null;
}
Class targetClass = mTargetObject.getClass();
if (methodName == null){
throw new IllegalArgumentException("InvocationProxy: missing method name");
}
if (targetParameter == null){
throw new IllegalArgumentException("InvocationProxy: missing Variant parameters");
}
try {
if (JacobObject.isDebugEnabled()){
JacobObject.debug("InvocationProxy: trying to invoke "+methodName
+" on "+mTargetObject);
}
Method targetMethod;
targetMethod = targetClass.getMethod(methodName,
new Class[] {Variant[].class});
if (targetMethod != null){
// protected classes can't be invoked against even if they
// let you grab the method. you could do targetMethod.setAccessible(true);
// but that should be stopped by the security manager
Object mReturnedByInvocation = null;
mReturnedByInvocation =
targetMethod.invoke(mTargetObject,new Object[] {targetParameter});
if (mReturnedByInvocation == null){
// so we do something in this block
mVariantToBeReturned = null;
} else if (!(mReturnedByInvocation instanceof Variant)){
throw new IllegalArgumentException(
"InvocationProxy: invokation of target method returned "
+"non-null non-variant object: "+mReturnedByInvocation);
} else {
mVariantToBeReturned = (Variant) mReturnedByInvocation;
}
}
} catch (SecurityException e) {
// what causes this exception?
e.printStackTrace();
} catch (NoSuchMethodException e) {
// this happens whenever the listener doesn't implement all the methods
if (JacobObject.isDebugEnabled()){
JacobObject.debug("InvocationProxy: listener ("+mTargetObject+") doesn't implement "
+ methodName);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
// we can throw these inside the catch block so need to re-throw it
throw e;
} catch (IllegalAccessException e) {
// can't access the method on the target instance for some reason
if (JacobObject.isDebugEnabled()){
JacobObject.debug("InvocationProxy: probably tried to access public method on non public class"
+ methodName);
}
e.printStackTrace();
} catch (InvocationTargetException e) {
// invocation of target method failed
e.printStackTrace();
}
return mVariantToBeReturned;
}
public abstract Variant invoke(String methodName, Variant targetParameters[]);
/**
* used by EventProxy.cpp to create variant objects in the right thread
@@ -171,10 +76,26 @@ public class InvocationProxy {
}
/**
* helper method used by DispatchEvents to help clean up and reduce references
*
*/
protected void clearTarget(){
mTargetObject = null;
}
* Sets the target for this InvocationProxy.
* @param pTargetObject
* @throws IllegalArgumentException if target is not publicly accessable
*/
public void setTarget(Object pTargetObject){
if (JacobObject.isDebugEnabled()){
JacobObject.debug(
"InvocationProxy: setting target "+pTargetObject);
}
if (pTargetObject != null){
// JNI code apparently bypasses this check and could operate against
// protected classes. This seems like a security issue...
// maybe it was because JNI code isn't in a package?
if (!java.lang.reflect.Modifier.isPublic(
pTargetObject.getClass().getModifiers())){
throw new IllegalArgumentException(
"InvocationProxy only public classes can receive event notifications");
}
}
mTargetObject = pTargetObject;
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright (c) 1999-2004 Sourceforge JACOB Project.
* All rights reserved. Originator: Dan Adler (http://danadler.com).
* Get more information about JACOB at http://sourceforge.net/projects/jacob-project
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.jacob.com;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* This class acts as a proxy between the windows event callback mechanism and
* the Java classes that are looking for events. It assumes that
* all of the Java classes that are looking for events implement methods with the
* same names as the windows events and that the implemented methods accept an
* array of variant objects. The methods can return void or a Variant that
* will be returned to the calling layer. All Event methods that will be
* recognized by InvocationProxyAllEvents have the signature
*
* <code> void eventMethodName(Variant[])</code>
* or
* <code> Variant eventMethodName(Variant[])</code>
*/
public class InvocationProxyAllVariants extends InvocationProxy {
/*
* (non-Javadoc)
* @see com.jacob.com.InvocationProxy#invoke(java.lang.String, com.jacob.com.Variant[])
*/
public Variant invoke(String methodName, Variant targetParameters[]) {
Variant mVariantToBeReturned = null;
if (mTargetObject == null){
if (JacobObject.isDebugEnabled()){
JacobObject.debug(
"InvocationProxy: received notification ("+methodName+") with no target set");
}
// structured programming guidlines say this return should not be up here
return null;
}
Class targetClass = mTargetObject.getClass();
if (methodName == null){
throw new IllegalArgumentException("InvocationProxy: missing method name");
}
if (targetParameters == null){
throw new IllegalArgumentException("InvocationProxy: missing Variant parameters");
}
try {
if (JacobObject.isDebugEnabled()){
JacobObject.debug("InvocationProxy: trying to invoke "+methodName
+" on "+mTargetObject);
}
Method targetMethod;
targetMethod = targetClass.getMethod(methodName,
new Class[] {Variant[].class});
if (targetMethod != null){
// protected classes can't be invoked against even if they
// let you grab the method. you could do targetMethod.setAccessible(true);
// but that should be stopped by the security manager
Object mReturnedByInvocation = null;
mReturnedByInvocation =
targetMethod.invoke(mTargetObject,new Object[] {targetParameters});
if (mReturnedByInvocation == null){
mVariantToBeReturned = null;
} else if (!(mReturnedByInvocation instanceof Variant)){
// could try and convert to Variant here.
throw new IllegalArgumentException(
"InvocationProxy: invokation of target method returned "
+"non-null non-variant object: "+mReturnedByInvocation);
} else {
mVariantToBeReturned = (Variant) mReturnedByInvocation;
}
}
} catch (SecurityException e) {
// what causes this exception?
e.printStackTrace();
} catch (NoSuchMethodException e) {
// this happens whenever the listener doesn't implement all the methods
if (JacobObject.isDebugEnabled()){
JacobObject.debug("InvocationProxy: listener ("+mTargetObject+") doesn't implement "
+ methodName);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
// we can throw these inside the catch block so need to re-throw it
throw e;
} catch (IllegalAccessException e) {
// can't access the method on the target instance for some reason
if (JacobObject.isDebugEnabled()){
JacobObject.debug("InvocationProxy: probably tried to access public method on non public class"
+ methodName);
}
e.printStackTrace();
} catch (InvocationTargetException e) {
// invocation of target method failed
e.printStackTrace();
}
return mVariantToBeReturned;
}
}

View File

@@ -361,8 +361,7 @@ public class Variant extends JacobObject {
*
* @throws IllegalArgumentException
* if inVariant = null
* @param in
* a variant that is to be referenced by this variant
* @param inVariant A variant that is to be referenced by this variant
*/
public void putVariant(Variant inVariant) {
if (inVariant == null) {
@@ -387,7 +386,7 @@ public class Variant extends JacobObject {
* Used to get the value from a windows type of VT_VARIANT
* or a jacob Variant type of VariantVariant.
* Added 1.12 pre 6 - VT_VARIANT support is at an alpha level
* @returns Object a java Object that represents the content of the enclosed Variant
* @return Object a java Object that represents the content of the enclosed Variant
*/
public Object getVariant() {
if ((this.getvt() & VariantVariant) == VariantVariant