View Javadoc

1   /*
2    Copyright (C) 2008 Grid Systems, S.A.
3   
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8   
9    This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13  
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18  package com.gridsystems.nextgrid.api.pom;
19  
20  import java.beans.PropertyDescriptor;
21  import java.net.URI;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.xml.namespace.QName;
26  
27  import nextgrid.api.pom.AbstractProcess;
28  import nextgrid.api.pom.ControlProcess;
29  import nextgrid.api.pom.Grounding;
30  import nextgrid.api.pom.Process;
31  import nextgrid.api.pom.Service;
32  
33  import org.apache.commons.jxpath.DynamicPropertyHandler;
34  import org.apache.commons.jxpath.JXPathBeanInfo;
35  
36  /**
37   * Extended BeanInfo meta-data class for JXPath access.
38   *
39   * @author Rodrigo Ruiz
40   */
41  public class ProcessImplXBeanInfo implements JXPathBeanInfo,
42                                               DynamicPropertyHandler {
43  
44    /**
45     * Process property names.
46     */
47    private static final String[] PROPERTY_NAMES = {
48      "attributes",
49      "children",
50      "description",
51      "grounding",
52      "id",
53      "inputs",
54      "name",
55      "outputs",
56      "parent",
57      "root",
58      "selected",
59    };
60  
61    /**
62     * Empty array.
63     */
64    private static final Object[] EMPTY_ARRAY = new Object[0];
65  
66    /**
67     * {@inheritDoc}
68     */
69    public Class<?> getDynamicPropertyHandlerClass() {
70      return ProcessImplXBeanInfo.class;
71    }
72  
73    /**
74     * {@inheritDoc}
75     */
76    public PropertyDescriptor getPropertyDescriptor(String arg0) {
77      return null;
78    }
79  
80    /**
81     * {@inheritDoc}
82     */
83    public PropertyDescriptor[] getPropertyDescriptors() {
84      return new PropertyDescriptor[0];
85    }
86  
87    /**
88     * {@inheritDoc}
89     */
90    public boolean isAtomic() {
91      return false;
92    }
93  
94    /**
95     * {@inheritDoc}
96     */
97    public boolean isDynamic() {
98      return true;
99    }
100 
101   /**
102    * {@inheritDoc}
103    */
104   public Object getProperty(Object obj, String propertyName) {
105     if (obj instanceof Process) {
106       Process p = (Process)obj;
107       if ("attributes".equals(propertyName)) {
108         Map<QName, String> attribs = p.getAttributes();
109         Map<String, String> attributes = new HashMap<String, String>();
110         for (Map.Entry<QName, String> entry : attribs.entrySet()) {
111           QName key = entry.getKey();
112           attributes.put(toString(key), entry.getValue());
113         }
114         return attributes;
115       } else if ("children".equals(propertyName)) {
116         return getChildrenOf(p);
117       } else if ("description".equals(propertyName)) {
118         return p.getDescription();
119       } else if ("id".equals(propertyName)) {
120         URI id = p.getId();
121         return (id == null) ? null : id.toString();
122       } else if ("inputs".equals(propertyName)) {
123         return p.getInputs();
124       } else if ("name".equals(propertyName)) {
125         return p.getName();
126       } else if ("outputs".equals(propertyName)) {
127         return p.getOutputs();
128       } else if ("parent".equals(propertyName)) {
129         return p.getParent();
130       } else if ("root".equals(propertyName)) {
131         return p.findRoot();
132       }
133 
134       if (p instanceof AbstractProcess) {
135         AbstractProcess ap = (AbstractProcess)p;
136 
137         if ("candidates".equals(propertyName)) {
138           return ap.getCandidates();
139         } else if ("selected".equals(propertyName)) {
140           return ap.getSelected();
141         }
142       }
143 
144       if (p instanceof Service) {
145         Service s = (Service)p;
146 
147         if ("grounding".equals(propertyName)) {
148           return s.getGrounding();
149         }
150       }
151     }
152 
153     return null;
154   }
155 
156   /**
157    * {@inheritDoc}
158    */
159   public String[] getPropertyNames(Object obj) {
160     if (obj instanceof Process) {
161       String[] copy = new String[PROPERTY_NAMES.length];
162       System.arraycopy(PROPERTY_NAMES, 0, copy, 0, PROPERTY_NAMES.length);
163       return copy;
164     } else {
165       return new String[0];
166     }
167   }
168 
169   /**
170    * {@inheritDoc}
171    */
172   public void setProperty(Object arg0, String arg1, Object arg2) {
173     // For now, this is left empty. XPathExpressions should not be used
174     // for modifying the process tree
175   }
176 
177   /**
178    * Gets an array containing all children objects of the given process.
179    *
180    * @param p The process
181    * @return Its children
182    */
183   private Object[] getChildrenOf(Process p) {
184     if (p instanceof AbstractProcess) {
185       Process selected = ((AbstractProcess)p).getSelected();
186       return (selected == null) ? EMPTY_ARRAY : new Object[] { selected };
187     } else if (p instanceof ControlProcess) {
188       return ((ControlProcess)p).getChildren();
189     } else if (p instanceof Service) {
190       Grounding g = ((Service)p).getGrounding();
191       return (g == null) ? EMPTY_ARRAY : new Object[] { g };
192     } else {
193       return EMPTY_ARRAY;
194     }
195   }
196 
197   /**
198    * Converts a QName to a string that can be used in an XPath expression.
199    *
200    * @param qname The QName to convert
201    * @return String representation
202    */
203   private String toString(QName qname) {
204     String ns = qname.getNamespaceURI();
205     String local = qname.getLocalPart();
206     String s = ((ns == null) ? "" : ns) + "_" + local;
207     return s.replaceAll("[\\:\\/\\%\\$\\&\\#\\@\\;]", "_");
208   }
209 
210 }