/contrib/initial/nof/framework/branches/osgi-conversion/nof-reflector-java/src/main/java/org/nakedobjects/nof/reflect/java/reflect/JavaAction.java
Java | 329 lines | 275 code | 39 blank | 15 comment | 46 complexity | 6940d7dc3a669673c6146d18dfb831b1 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0, GPL-2.0
- package org.nakedobjects.nof.reflect.java.reflect;
- import org.nakedobjects.noa.adapter.Naked;
- import org.nakedobjects.noa.adapter.NakedCollection;
- import org.nakedobjects.noa.adapter.NakedReference;
- import org.nakedobjects.noa.reflect.Consent;
- import org.nakedobjects.noa.reflect.NakedObjectAction;
- import org.nakedobjects.noa.security.Session;
- import org.nakedobjects.noa.spec.NakedObjectSpecification;
- import org.nakedobjects.nof.core.context.NakedObjectsContext;
- import org.nakedobjects.nof.core.persist.TransactionException;
- import org.nakedobjects.nof.core.reflect.Allow;
- import org.nakedobjects.nof.core.util.DebugString;
- import org.nakedobjects.nof.reflect.peer.ActionPeer;
- import org.nakedobjects.nof.reflect.peer.MemberIdentifier;
- import org.nakedobjects.nof.reflect.peer.ReflectionException;
- import org.nakedobjects.nof.reflect.peer.ReflectiveActionException;
- import org.nakedobjects.nof.reflect.spec.CollectionSpecification;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.util.Enumeration;
- import org.apache.log4j.Logger;
- /*
- * TODO (in all Java...Peer classes) make all methods throw ReflectiveActionException when
- * an exception occurs when calling a method reflectively (see execute method). Then instead of
- * calling invocationExcpetion() the exception will be passed though, and dealt with generally by
- * the reflection package (which will be the same for all reflectors and will allow the message to
- * be better passed back to the client).
- */
- public class JavaAction extends JavaMember implements ActionPeer {
- final static Logger LOG = Logger.getLogger(JavaAction.class);
- private final Method actionMethod;
- private final boolean isInstanceMethod;
- private final int paramCount;
- private final NakedObjectSpecification[] parameters;
- private final NakedObjectAction.Target target;
- private final NakedObjectAction.Type type;
- private final MemberHelper descriptionsMethod;
- private final MemberHelper namesMethod;
- private final MemberHelper optionalParatmetersMethod;
- private final Method validMethod;
- private final MemberHelper parameterOptions;
- private final MemberHelper defaultParametersMethod;
- private final Method sessionHideMethod;
- private final int[] maxLengths;
- private final int[] typicalLengths;
- private final int[] noLines;
- private final boolean[] canWrap;
- private final NakedObjectSpecification onType;
- private final NakedObjectSpecification returnType;
- public JavaAction(
- final MemberIdentifier identifier,
- final NakedObjectAction.Type type,
- final NakedObjectAction.Target target,
- final NakedObjectSpecification onType,
- final NakedObjectSpecification[] parameters,
- final Method action,
- final NakedObjectSpecification returnType,
- final ParameterMethods parameterMethods,
- final DescriptiveMethods descriptiveMethods,
- final GeneralControlMethods controlMethods,
- final Method sessionHideMethod) {
- super(identifier, descriptiveMethods, controlMethods);
- this.type = type;
- this.actionMethod = action;
- this.target = target;
- this.onType = onType;
- this.returnType = returnType;
- int parameterLength = action.getParameterTypes().length;
- isInstanceMethod = !Modifier.isStatic(actionMethod.getModifiers());
- validMethod = controlMethods.getValidMethod1();
- this.sessionHideMethod = sessionHideMethod;
- namesMethod = parameterMethods.getNamesMethod();
- descriptionsMethod = parameterMethods.getDescriptionsMethod();
- optionalParatmetersMethod = parameterMethods.getOptionalMethod();
- defaultParametersMethod = parameterMethods.getDefaultsMethod();
- parameterOptions = parameterMethods.getOptionsMethod();
- canWrap = parameterMethods.canWrap();
- maxLengths = parameterMethods.getMaxLengths();
- typicalLengths = parameterMethods.getTypicalLengths();
- noLines = parameterMethods.getNoLines();
- paramCount = parameterLength;
- this.parameters = new NakedObjectSpecification[paramCount];
- for (int i = 0; i < this.parameters.length; i++) {
- this.parameters[i] = parameters[i];
- }
- }
- public void debugData(final DebugString debug) {
- debug.appendln("Action", actionMethod);
- if (!(namesMethod instanceof NoMemberHelper)) {
- debug.appendln("Labels", namesMethod);
- }
- if (!(optionalParatmetersMethod instanceof NoMemberHelper)) {
- debug.appendln("Optional", optionalParatmetersMethod);
- }
- if (!(defaultParametersMethod instanceof NoMemberHelper)) {
- debug.appendln("Defaults", defaultParametersMethod);
- }
- if (!(parameterOptions instanceof NoMemberHelper)) {
- debug.appendln("Options", parameterOptions);
- }
- if (validMethod != null) {
- debug.appendln("Valid", validMethod);
- }
- if (sessionHideMethod != null) {
- debug.appendln("Authorised", sessionHideMethod);
- }
- super.debugData(debug);
- }
- public Naked execute(final NakedReference inObject, final Naked[] parameters) throws ReflectiveActionException {
- if (parameters.length != paramCount) {
- LOG.error(actionMethod + " requires " + paramCount + " parameters, not " + parameters.length);
- }
- try {
- Object[] executionParameters = new Object[parameters.length];
- for (int i = 0; i < parameters.length; i++) {
- executionParameters[i] = domainObject(parameters[i]);
- }
- Object object = domainObject(inObject);
- Object result = actionMethod.invoke(object, executionParameters);
- LOG.debug(" action result " + result);
- if (result != null) {
- Naked adapter;
- if (getReturnType().getType() == NakedObjectSpecification.COLLECTION) {
- NakedObjectSpecification elementSpec;
- if (result.getClass().isArray()) {
- elementSpec = NakedObjectsContext.getReflector().loadSpecification(result.getClass().getComponentType());
- adapter = NakedObjectsContext.getObjectLoader().createAdapterForCollection(result, elementSpec);
- } else {
- elementSpec = ((CollectionSpecification) getReturnType()).getElementType();
- adapter = NakedObjectsContext.getObjectLoader().createAdapterForCollection(result, elementSpec);
- }
- } else {
- adapter = NakedObjectsContext.getObjectLoader().getAdapterFor(result);
- if (adapter == null) {
- adapter = NakedObjectsContext.getObjectLoader().createAdapterForTransient(result);
- }
- }
- return adapter;
- } else {
- return null;
- }
- } catch (InvocationTargetException e) {
- if (e.getTargetException() instanceof TransactionException) {
- throw new ReflectiveActionException("TransactionException thrown while executing " + actionMethod + " "
- + e.getTargetException().getMessage(), e.getTargetException());
- } else {
- invocationException("Exception executing " + actionMethod, e);
- return null;
- }
- } catch (IllegalAccessException e) {
- throw new ReflectiveActionException("Illegal access of " + actionMethod, e);
- }
- }
- protected Consent executeConsent(final Method method, final NakedReference target, final Naked[] parameters) {
- if (method == null) {
- return Allow.DEFAULT;
- }
- Naked[] parameterSet = parameters == null ? new Naked[0] : parameters;
- if (parameterSet.length != paramCount) {
- LOG.error(actionMethod + " requires " + paramCount + " parameters, not " + parameterSet.length);
- }
- Object[] longParams;
- if (method.getName().startsWith("default")) {
- longParams = new Object[0];
- } else {
- longParams = new Object[parameterSet.length];
- for (int i = 0; i < longParams.length; i++) {
- longParams[i] = parameterSet[i] == null ? null : parameterSet[i].getObject();
- }
- }
- return executeConsent(method, target, longParams);
- }
- public Object getExtension(final Class cls) {
- return null;
- }
- public NakedObjectSpecification getOnType() {
- return onType;
- }
- public int getParameterCount() {
- return paramCount;
- }
- public Object[] getParameterDefaults(NakedReference target) {
- // TODO here and elsewhere: the target needs to be replaced by the resource where the action is for a
- // resource!
- // set a flag on entry if for a resource - or get from spec using isResource
- return (Object[]) defaultParametersMethod.execute(domainObject(target));
- }
- public String[] getParameterDescriptions() {
- try {
- String[] descriptions = (String[]) descriptionsMethod.execute(null);
- return descriptions;
- } catch (IllegalArgumentException e) {
- throw new ReflectionException("Failed to execute associated method for action " + getIdentifier() + ": "
- + namesMethod, e);
- }
- }
- public String[] getParameterNames() {
- try {
- String[] labels = (String[]) namesMethod.execute(null);
- return labels;
- } catch (IllegalArgumentException e) {
- throw new ReflectionException("Failed to execute associated method for action " + getIdentifier() + ": "
- + namesMethod, e);
- }
- }
- public Object[][] getParameterOptions(NakedReference target) {
- Object[] options = (Object[]) parameterOptions.execute(domainObject(target));
- if (options == null) {
- return new Object[getParameterCount()][];
- }
- Object[][] array = new Object[options.length][];
- for (int i = 0; i < options.length; i++) {
- if (options[i] == null) {
- continue;
- } else if (options[i].getClass().isArray()) {
- array[i] = (Object[]) options[i];
- } else {
- NakedCollection adapter = NakedObjectsContext.getObjectLoader().createAdapterForCollection(options[i],
- getParameterTypes()[i]);
- Enumeration e = adapter.elements();
- Object[] optionArray = new Object[adapter.size()];
- int j = 0;
- while (e.hasMoreElements()) {
- optionArray[j++] = ((Naked) e.nextElement()).getObject();
- }
- array[i] = optionArray;
- }
- }
- return array;
- }
- public NakedObjectSpecification[] getParameterTypes() {
- return parameters;
- }
- public boolean[] getOptionalParameters() {
- return (boolean[]) optionalParatmetersMethod.execute(null);
- }
- public NakedObjectSpecification getReturnType() {
- return returnType;
- /*
- * Class returnType = actionMethod.getReturnType(); boolean hasReturn = returnType != void.class;
- * return hasReturn ? specification(returnType) : null;
- */
- }
- public NakedObjectAction.Target getTarget() {
- return target;
- }
- public NakedObjectAction.Type getType() {
- return type;
- }
- public boolean isOnInstance() {
- return isInstanceMethod;
- }
- public Consent isParameterSetValid(final NakedReference object, final Naked[] parameters) {
- return executeConsent(validMethod, object, parameters);
- }
- public Consent isUsable(final NakedReference target) {
- return executeConsent(usableMethod, target);
- }
- public boolean isVisible(final Session session) {
- return !executeBoolean(sessionHideMethod, false, session);
- }
- public String toString() {
- StringBuffer parameters = new StringBuffer();
- Class[] types = actionMethod.getParameterTypes();
- if (types.length == 0) {
- parameters.append("none");
- }
- for (int i = 0; i < types.length; i++) {
- if (i > 0) {
- parameters.append("/");
- }
- parameters.append(types[i]);
- }
- return "JavaAction [method=" + actionMethod.getName() + ",type=" + type.getName() + ",parameters=" + parameters + "]";
- }
- public boolean[] canParametersWrap() {
- return canWrap;
- }
- public int[] getParameterMaxLengths() {
- return maxLengths;
- }
- public int[] getParameterNoLines() {
- return noLines;
- }
- public int[] getParameterTypicalLengths() {
- return typicalLengths;
- }
- }
- // Copyright (c) Naked Objects Group Ltd.