1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.server.monitoring;
36
37 import java.text.MessageFormat;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.Map;
41
42 import org.apache.commons.beanutils.ConvertUtilsBean;
43 import org.apache.commons.beanutils.converters.BooleanConverter;
44 import org.apache.commons.beanutils.converters.DoubleConverter;
45 import org.apache.commons.beanutils.converters.FloatConverter;
46 import org.apache.commons.beanutils.converters.IntegerConverter;
47 import org.apache.commons.beanutils.converters.LongConverter;
48 import org.apache.commons.beanutils.converters.ShortConverter;
49 import org.apache.log4j.Logger;
50 import org.apache.xmlbeans.XmlDateTime;
51 import org.apache.xmlbeans.XmlDouble;
52 import org.apache.xmlbeans.XmlObject;
53 import org.apache.xmlbeans.XmlString;
54 import org.ogf.graap.wsag.api.logging.LogMessage;
55 import org.ogf.schemas.graap.wsAgreement.AgreementPropertiesType;
56 import org.ogf.schemas.graap.wsAgreement.ServicePropertiesType;
57 import org.ogf.schemas.graap.wsAgreement.VariableType;
58
59
60
61
62
63
64
65 public class ServicePropertyResolver
66 {
67 private static final Logger LOG = Logger.getLogger( ServicePropertyResolver.class );
68
69 private final ServicePropertiesType properties;
70
71 private final AgreementPropertiesType agreementProperties;
72
73 private final ConvertUtilsBean convertor = new ConvertUtilsBean();
74
75 private final Map<String, Class<?>> metrics = new HashMap<String, Class<?>>();
76
77
78
79
80
81
82
83
84
85
86 public ServicePropertyResolver( ServicePropertiesType properties,
87 AgreementPropertiesType agreementProperties )
88 {
89 this.properties = properties;
90 this.agreementProperties = agreementProperties;
91
92 metrics.put( "string", String.class );
93 metrics.put( "integer", Integer.class );
94 metrics.put( "short", Short.class );
95 metrics.put( "long", Long.class );
96 metrics.put( "double", Double.class );
97 metrics.put( "float", Float.class );
98 metrics.put( "date", Date.class );
99 metrics.put( "boolean", Boolean.class );
100
101 convertor.register( new IntegerConverter(), Integer.class );
102 convertor.register( new IntegerConverter(), Integer.TYPE );
103 convertor.register( new ShortConverter(), Short.class );
104 convertor.register( new ShortConverter(), Short.TYPE );
105 convertor.register( new LongConverter(), Long.class );
106 convertor.register( new LongConverter(), Long.TYPE );
107 convertor.register( new DoubleConverter(), Double.class );
108 convertor.register( new DoubleConverter(), Double.TYPE );
109 convertor.register( new FloatConverter(), Float.class );
110 convertor.register( new FloatConverter(), Float.TYPE );
111 convertor.register( new BooleanConverter(), Boolean.class );
112 convertor.register( new BooleanConverter(), Boolean.TYPE );
113 }
114
115
116
117
118
119
120 public Map<String, Object> resolveServiceProperties()
121 {
122
123 Map<String, Object> variableMap = new HashMap<String, Object>();
124
125 VariableType[] variables = properties.getVariableSet().getVariableArray();
126 for ( int j = 0; j < variables.length; j++ )
127 {
128
129 String xpath = variables[j].getLocation();
130 String variableName = variables[j].getName();
131 String metric = variables[j].getMetric();
132 Object variableValue = "";
133
134 try
135 {
136 XmlObject[] values = agreementProperties.selectPath( xpath );
137 XmlObject selected = values[0];
138 variableValue = convertValue( selected, metric );
139 }
140 catch ( Exception ex )
141 {
142 variableValue = null;
143
144 String msgResolveError =
145 "Could not resolve variable {0} for [{1}:{2}]. Variable will not be used for guarantee evaluation.";
146
147 String error =
148 MessageFormat.format( msgResolveError,
149 new Object[] { variableName, properties.getServiceName(), properties.getName() } );
150 LOG.warn( error );
151 continue;
152 }
153
154
155 if ( !variableMap.containsKey( variableName ) )
156 {
157
158 variableMap.put( variableName, variableValue );
159
160 if ( LOG.isDebugEnabled() )
161 {
162 LOG.debug( LogMessage.getMessage( "Added variable [Name: {0}][Value: {1}]", variableName,
163 variableValue ) );
164 }
165 }
166 else
167 {
168 if ( LOG.isDebugEnabled() )
169 {
170 LOG.debug( LogMessage.getMessage(
171 "Variable [Name: {0}] already in variable map. Ignore this instance.", variableName ) );
172 }
173 }
174 }
175 return variableMap;
176 }
177
178 private Object convertValue( XmlObject in, String metric ) throws Exception
179 {
180
181
182
183 Class<?> target = String.class;
184 if ( metrics.containsKey( metric ) )
185 {
186 target = metrics.get( metric );
187 }
188 else
189 {
190 LOG.warn( MessageFormat.format(
191 "No convertor configured for metric ''{0}''. Value is treated as string.",
192 new Object[] { metric } ) );
193 }
194
195 Object result = null;
196 String strValue = XmlString.Factory.parse( in.getDomNode() ).getStringValue();
197
198 if ( target == Date.class )
199 {
200 XmlDateTime dt = XmlDateTime.Factory.parse( in.getDomNode() );
201 result = dt.getCalendarValue().getTime();
202 }
203 else
204 {
205 try
206 {
207 result = convertor.convert( strValue, target );
208 }
209 catch ( Exception e )
210 {
211 String msgConversionError =
212 "Failed to convert metric with commons beanutils. Start conversion based on schema definition.";
213 LOG.debug( msgConversionError, e );
214 }
215 }
216
217 if ( result != null )
218 {
219 return result;
220 }
221
222
223
224
225 if ( in instanceof XmlDateTime )
226 {
227 return ( (XmlDateTime) in ).getCalendarValue().getTime();
228 }
229
230 if ( in instanceof XmlDouble )
231 {
232 return new Double( ( (XmlDouble) in ).getDoubleValue() );
233 }
234
235
236
237
238 LOG.warn( MessageFormat.format(
239 "Failed to convert value {0} into metric {1}. Value will be treated as string.", new Object[] {
240 strValue, metric } ) );
241 return strValue;
242 }
243 }