Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PluggableLookupCommand |
|
| 4.0;4 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to you under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | /* | |
19 | * $Id: PluggableLookupCommand.java 473876 2006-11-12 04:50:13Z gvanmatre $ | |
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 | 250 | 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 | 832 | String catalogName = getCatalogName(); |
53 | 832 | if (catalogName == null) { |
54 | return false; | |
55 | } | |
56 | 832 | CatalogFactory catalogFactory = CatalogFactory.getInstance(); |
57 | ||
58 | 832 | return (catalogFactory.getCatalog(catalogName) != null); |
59 | } | |
60 | ||
61 | /** | |
62 | * @return <code>Catalog</code> for the <code>catalogName</code> | |
63 | */ | |
64 | private Catalog getCatalog() { | |
65 | 1 | CatalogFactory catalogFactory = CatalogFactory.getInstance(); |
66 | 1 | 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 | 832 | if (!catalogExists()) { |
81 | 831 | return !isOptional(); |
82 | } | |
83 | ||
84 | 1 | if (isValueReference(getName())) { |
85 | // command is a binding expression; evaluate and the find the target command | |
86 | 1 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
87 | 1 | ValueBinding vb = facesContext.getApplication().createValueBinding(getName()); |
88 | 1 | String targetCommand = (String) vb.getValue(facesContext); |
89 | ||
90 | 1 | if (targetCommand != null) { |
91 | 1 | Command command = getCatalog().getCommand(targetCommand); |
92 | 1 | if (command != null) { |
93 | 1 | 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 | 1 | if (value == null) { |
106 | return false; | |
107 | } | |
108 | ||
109 | 1 | if ((value.indexOf("#{") > -1) |
110 | && (value.indexOf("}") > -1)) { | |
111 | 1 | return true; |
112 | } | |
113 | ||
114 | return false; | |
115 | } | |
116 | ||
117 | } |