SetParameterResourceCustomiser.java
01 /*
02  *  SetParameterResourceCustomiser.java
03  *
04  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
05  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
06  *
07  *  This file is part of GATE (see http://gate.ac.uk/), and is free
08  *  software, licenced under the GNU Library General Public License,
09  *  Version 2, June 1991 (in the distribution as file licence.html,
10  *  and also available at http://gate.ac.uk/gate/licence.html).
11  *
12  *  Ian Roberts, 22/Jan/2008
13  *
14  *  $Id: SetParameterResourceCustomiser.java 12006 2009-12-01 17:24:28Z thomas_heitz $
15  */
16 
17 package gate.util.spring;
18 
19 import gate.Controller;
20 import gate.Resource;
21 
22 /**
23  * ResourceCustomiser that sets a parameter on the resource being
24  * customised. When used to customise Controllers, it can optionally
25  * take a "prName" property. In this case it will set the parameter on
26  * the first PR with that name in the controller, rather than the
27  * controller itself
28  */
29 public class SetParameterResourceCustomiser implements ResourceCustomiser {
30 
31   private String paramName;
32 
33   private Object value;
34 
35   private String prName = null;
36 
37   public void customiseResource(Resource resthrows Exception {
38     if(prName == null) {
39       res.setParameterValue(paramName, value);
40     }
41     else {
42       if(res instanceof Controller) {
43         for(Object pr : ((Controller)res).getPRs()) {
44           if(prName.equals(((Resource)pr).getName())) {
45             ((Resource)pr).setParameterValue(paramName, value);
46             break;
47           }
48         }
49       }
50       else {
51         throw new IllegalArgumentException("prName was specified, so we can "
52                 "only customise Controllers.  Supplied resource was a "
53                 + res.getClass().getName());
54       }
55     }
56   }
57 
58   public void setParamName(String paramName) {
59     this.paramName = paramName;
60   }
61 
62   public void setValue(Object value) {
63     this.value = value;
64   }
65 
66   public void setPrName(String prName) {
67     this.prName = prName;
68   }
69 
70 }