JavaInvokeConfig.java
/*
** Module : JavaInvokeConfig.java
** Abstract : Defines a builder configuration with the details required to execute a Java method.
**
** Copyright (c) 2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA 20220323 Created initial version.
** CA 20220405 Added authentication and authorization for web requests. When this is enabled, the target
** API call will be executed under the authenticated FWD context, and not the agent's context.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program 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 Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.util;
import java.io.*;
import java.lang.reflect.*;
/**
* Defines a configuration which can be used to invoke Java methods on the appserver.
*/
public class JavaInvokeConfig
implements Externalizable
{
/** The Java {@link Class} defining this method. */
private Class<?> clazz;
/** The target method. */
private Method method;
/** The arguments for this call. */
private Object[] arguments = new Object[0];
/** A token sent via a web service request, to execute the target service in the specified FWD context. */
private String webServiceToken;
/**
* Create a new invocation for the given target.
*
* @param cls
* The Java {@link Class} defining this method.
* @param method
* The target method.
*/
public JavaInvokeConfig(Class<?> cls, Method method)
{
this.clazz = cls;
this.method = method;
}
/**
* Get the target {@link #clazz}.
*
* @return See above.
*/
public Class<?> getClazz()
{
return clazz;
}
/**
* Get the target {@link #method}.
*
* @return See above.
*/
public Method getMethod()
{
return method;
}
/**
* Get the caller's {@link #arguments}.
*
* @return the {@link #arguments}
*/
public Object[] getArguments()
{
return arguments;
}
/**
* Set this invocation's {@link #arguments}.
*
* @param args
* A invocation's arguments.
*
* @return This instance.
*/
public JavaInvokeConfig setArguments(Object... args)
{
this.arguments = args;
return this;
}
/**
* Get a string representation of the target.
*
* @return See above.
*/
public String getTarget()
{
return method.toString();
}
/**
* Set the {@link #webServiceToken}.
*
* @param webServiceToken
* The web service token.
*
* @return This instance.
*/
public JavaInvokeConfig setWebServiceToken(String webServiceToken)
{
this.webServiceToken = webServiceToken;
return this;
}
/**
* Get the web service token.
*
* @return The {@link #webServiceToken}.
*/
public String getWebServiceToken()
{
return webServiceToken;
}
/**
* Serialize this invoke configuration.
*
* @param out
* The output stream.
*/
@Override
public void writeExternal(ObjectOutput out)
throws IOException
{
out.writeUTF(clazz.getName());
out.writeUTF(method.getName());
out.writeInt(method.getParameterCount());
for (int i = 0; i < method.getParameterCount(); i++)
{
out.writeUTF(method.getParameterTypes()[i].getName());
}
out.writeObject(arguments);
out.writeObject(webServiceToken);
}
/**
* Read this invoke configuration from a stream.
* @param in
* The input stream.
*/
@Override
public void readExternal(ObjectInput in)
throws IOException,
ClassNotFoundException
{
clazz = Class.forName(in.readUTF());
String mname = in.readUTF();
int pcount = in.readInt();
Class<?>[] ptypes = new Class[pcount];
for (int i = 0; i < pcount; i++)
{
ptypes[i] = Class.forName(in.readUTF());
}
try
{
method = clazz.getMethod(mname, ptypes);
}
catch (NoSuchMethodException |
SecurityException e)
{
// should never happen
throw new RuntimeException(e);
}
arguments = (Object[]) in.readObject();
webServiceToken = (String) in.readObject();
}
}