01: /*
02: * $Id: GetPersonDetailsImpl.java,v 1.4 2007/03/16 10:06:08 agoubard Exp $
03: */
04: package com.mycompany.toolbox.api;
05:
06: import groovy.lang.Binding;
07: import groovy.lang.GroovyShell;
08:
09: import java.io.IOException;
10: import java.io.InputStream;
11: import java.util.Map;
12:
13: import org.xins.common.BeanUtils;
14: import org.xins.common.collections.InvalidPropertyValueException;
15: import org.xins.common.collections.MissingRequiredPropertyException;
16: import org.xins.common.collections.PropertyReader;
17: import org.xins.common.io.IOReader;
18: import org.xins.common.manageable.InitializationException;
19:
20: /**
21: * Implementation of the <code>GetPersonDetails</code> function.
22: *
23: * <p>Description: Gets the detail of some people.
24: *
25: * @version $Revision: 1.4 $ $Date: 2007/03/16 10:06:08 $
26: * @author TODO
27: */
28: public final class GetPersonDetailsImpl extends GetPersonDetails {
29:
30: /**
31: * The script.
32: */
33: private String _matchScript;
34:
35: /**
36: * Constructs a new <code>GetPersonDetailsImpl</code> instance.
37: *
38: * @param api
39: * the API to which this function belongs, guaranteed to be not
40: * <code>null</code>.
41: */
42: public GetPersonDetailsImpl(APIImpl api) {
43: super (api);
44: }
45:
46: protected void initImpl(PropertyReader properties)
47: throws MissingRequiredPropertyException,
48: InvalidPropertyValueException, InitializationException {
49: try {
50: InputStream matchStream = getAPI()
51: .getResourceAsStream(
52: "/WEB-INF/classes/com/mycompany/toolbox/api/personsStub.groovy");
53: if (matchStream == null) {
54: throw new InitializationException(
55: "Cannot find script at /WEB-INF/classes/com/mycompany/toolbox/api/personsStub.groovy");
56: }
57: _matchScript = IOReader.readFully(matchStream);
58: } catch (IOException ioe) {
59: throw new InitializationException(ioe);
60: }
61: }
62:
63: /**
64: * Calls this function. If the function fails, it may throw any kind of
65: * exception. All exceptions will be handled by the caller.
66: *
67: * @param request
68: * the request, never <code>null</code>.
69: *
70: * @return
71: * the result of the function call, should never be <code>null</code>.
72: *
73: * @throws Throwable
74: * if anything went wrong.
75: */
76: public Result call(Request request) throws Throwable {
77: SuccessfulResult result = new SuccessfulResult();
78:
79: // Put the values of the request in the script
80: Binding binding = new Binding(BeanUtils.getParameters(request));
81: GroovyShell shell = new GroovyShell(binding);
82:
83: Object value = shell.evaluate(_matchScript);
84: BeanUtils.setParameters((Map) value, result);
85:
86: return result;
87: }
88: }
|