View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Resolves a set of service properties from a given agreement properties document and converts the resolved
61   * properties in the correct type using the specified service property metric.
62   * 
63   * @author owaeld
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       * Constructs a new resolver.
79       * 
80       * @param properties
81       *            the service properties to resolve
82       * 
83       * @param agreementProperties
84       *            the agreement properties document to resolve the service properties from
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      * Resolves the service properties from the agreement properties document.
117      * 
118      * @return the resolved service properties
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             // and put data in the variable map
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         // first convert the value according to the metric specification
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         // now try to convert values based on the schema definition
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         // if still not succeeded return the string
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 }