2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.shale.clay.utils;
22
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25
26 import org.apache.commons.chain.Catalog;
27 import org.apache.commons.chain.CatalogFactory;
28 import org.apache.commons.chain.Command;
29 import org.apache.commons.chain.Context;
30 import org.apache.commons.chain.generic.LookupCommand;
31
32 /***
33 * <p>This chains class performs a check to verify the catalog exists.
34 * If the catalog doesn't exist, it returns <code>true</code> if the
35 * <code>optional</code> property in the super class is <code>true</code>.
36 * If the catalog is not found and the command is not optional, it returns a
37 * <code>false</code> value. Otherwise, if the catalog exists, the super
38 * implementation is invoked. The name of the command can be a value
39 * binding expression. The value is evaluated if it contains an expression
40 * and the resolved command is invoked.</p>
41 *
42 */
43 public class PluggableLookupCommand extends LookupCommand {
44
45
46 /***
47 * <p>Checks to see if the catalog exists.</p>
48 *
49 * @return <code>true</code> if the catalog exists
50 */
51 private boolean catalogExists() {
52 String catalogName = getCatalogName();
53 if (catalogName == null) {
54 return false;
55 }
56 CatalogFactory catalogFactory = CatalogFactory.getInstance();
57
58 return (catalogFactory.getCatalog(catalogName) != null);
59 }
60
61 /***
62 * @return <code>Catalog</code> for the <code>catalogName</code>
63 */
64 private Catalog getCatalog() {
65 CatalogFactory catalogFactory = CatalogFactory.getInstance();
66 return catalogFactory.getCatalog(getCatalogName());
67 }
68
69 /***
70 * <p>Adds an additional check to determine if the catalog name is loaded.
71 * If loaded, the super implementation is invoked. Otherwise, the chain
72 * continues if the command is optional.</p>
73 *
74 * @param context chains context
75 * @return <code>true</code> if the chain is done
76 * @exception Exception up the calling chain
77 */
78 public boolean execute(Context context) throws Exception {
79
80 if (!catalogExists()) {
81 return !isOptional();
82 }
83
84 if (isValueReference(getName())) {
85
86 FacesContext facesContext = FacesContext.getCurrentInstance();
87 ValueBinding vb = facesContext.getApplication().createValueBinding(getName());
88 String targetCommand = (String) vb.getValue(facesContext);
89
90 if (targetCommand != null) {
91 Command command = getCatalog().getCommand(targetCommand);
92 if (command != null) {
93 return command.execute(context);
94 }
95 }
96 }
97 return super.execute(context);
98 }
99
100 /***
101 * @param value the command name to test for a binding expression
102 * @return <code>true</code> if the <code>value</code> is a binding expression
103 */
104 private boolean isValueReference(String value) {
105 if (value == null) {
106 return false;
107 }
108
109 if ((value.indexOf("#{") > -1)
110 && (value.indexOf("}") > -1)) {
111 return true;
112 }
113
114 return false;
115 }
116
117 }