View Javadoc
1   package org.andromda.cartridges.webservice.metafacades;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import org.andromda.cartridges.webservice.WebServiceGlobals;
6   import org.andromda.cartridges.webservice.WebServiceUtils;
7   import org.andromda.metafacades.uml.ClassifierFacade;
8   import org.andromda.metafacades.uml.ModelElementFacade;
9   import org.andromda.metafacades.uml.OperationFacade;
10  import org.andromda.metafacades.uml.TypeMappings;
11  import org.andromda.metafacades.uml.UMLMetafacadeProperties;
12  import org.andromda.metafacades.uml.UMLProfile;
13  import org.apache.commons.lang.StringUtils;
14  import org.apache.log4j.Logger;
15  
16  /**
17   * MetafacadeLogic implementation for org.andromda.cartridges.webservice.metafacades.WSDLType.
18   *
19   * @see org.andromda.cartridges.webservice.metafacades.WSDLType
20   * @author Bob Fields
21   */
22  public class WSDLTypeLogicImpl
23          extends WSDLTypeLogic
24  {
25      private static final long serialVersionUID = 34L;
26      // ---------------- constructor -------------------------------
27  
28      /**
29       * @param metaObject
30       * @param context
31       */
32      public WSDLTypeLogicImpl(Object metaObject, String context)
33      {
34          super(metaObject, context);
35      }
36  
37      /**
38       * The logger instance.
39       */
40      private static final Logger logger = Logger.getLogger(WSDLTypeLogicImpl.class);
41  
42      /**
43       * @return String this.getSchemaType(true, true)
44       * @see org.andromda.cartridges.webservice.metafacades.WSDLType#getSchemaType()
45       */
46      public String handleGetSchemaType()
47      {
48          return this.getSchemaType(true, true);
49      }
50  
51      /**
52       * @param withPrefix
53       * @param preserveArray
54       * @return String WebServiceUtils.getSchemaType
55       * @see org.andromda.cartridges.webservice.metafacades.WSDLType#getSchemaType(boolean, boolean)
56       */
57      public String handleGetSchemaType(boolean withPrefix, boolean preserveArray)
58      {
59          return WebServiceUtils.getSchemaType(this, this.getSchemaTypeMappings(), this.getNamespacePrefix(),
60                  this.getName(), this.getWsdlArrayNamePrefix(), withPrefix, preserveArray);
61      }
62  
63      /**
64       * @return getName().insert(0, this.getWsdlArrayNamePrefix())
65       * @see org.andromda.cartridges.webservice.metafacades.WSDLType#getWsdlArrayName()
66       */
67      protected String handleGetWsdlArrayName()
68      {
69          StringBuilder name = new StringBuilder(StringUtils.trimToEmpty(this.getName()).replaceAll("\\[\\]", ""));
70          name.insert(0, this.getWsdlArrayNamePrefix());
71          return name.toString();
72      }
73  
74      /**
75       * @return String getConfiguredProperty(WebServiceGlobals.ARRAY_NAME_PREFIX)
76       * @see org.andromda.cartridges.webservice.metafacades.WSDLType#getWsdlArrayNamePrefix()
77       */
78      protected String handleGetWsdlArrayNamePrefix()
79      {
80          return String.valueOf(this.getConfiguredProperty(WebServiceGlobals.ARRAY_NAME_PREFIX));
81      }
82  
83      /**
84       * @return String this.getQualfiedNameLocalPartPattern().replaceAll("\\{0\\}")
85       * @see org.andromda.cartridges.webservice.metafacades.WSDLType#getQName()
86       */
87      protected String handleGetQName()
88      {
89          return this.getQualfiedNameLocalPartPattern().replaceAll("\\{0\\}", StringUtils.trimToEmpty(this.getName()));
90      }
91  
92      /**
93       * @return String WebServiceUtils.reversePackage(packageName)
94       * @see org.andromda.cartridges.webservice.metafacades.WSDLType#getNamespace()
95       */
96      protected String handleGetNamespace()
97      {
98          String packageName = this.getPackageName();
99          if (this.isReverseNamespace())
100         {
101             packageName = WebServiceUtils.reversePackage(packageName);
102         }
103         return this.getNamespacePattern().replaceAll("\\{0\\}", StringUtils.trimToEmpty(packageName));
104     }
105 
106     /**
107      * Gets the array suffix from the configured metafacade properties.
108      *
109      * @return the array suffix.
110      */
111     private String getArraySuffix()
112     {
113         return String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.ARRAY_NAME_SUFFIX));
114     }
115 
116     /**
117      * Gets the schemaType mappings that have been set for this schema type.
118      *
119      * @return the TypeMappings instance.
120      */
121     private TypeMappings getSchemaTypeMappings()
122     {
123         final String propertyName = WebServiceGlobals.SCHEMA_TYPE_MAPPINGS_URI;
124         Object property = this.getConfiguredProperty(propertyName);
125         TypeMappings mappings = null;
126         String uri = null;
127         if (property instanceof String)
128         {
129             uri = (String)property;
130             try
131             {
132                 mappings = TypeMappings.getInstance(uri);
133                 mappings.setArraySuffix(this.getArraySuffix());
134                 this.setProperty(propertyName, mappings);
135             }
136             catch (Throwable th)
137             {
138                 String errMsg = "Error getting '" + propertyName + "' --> '" + uri + '\'';
139                 logger.error(errMsg, th);
140                 // don't throw the exception
141             }
142         }
143         else
144         {
145             mappings = (TypeMappings)property;
146         }
147         return mappings;
148     }
149 
150     /**
151      * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeLogic#handleGetNamespacePrefix()
152      */
153     protected String handleGetNamespacePrefix()
154     {
155         return (String)this.getConfiguredProperty(WebServiceLogicImpl.NAMESPACE_PREFIX);
156     }
157 
158     /**
159      * Gets the <code>qualifiedNameLocalPartPattern</code> for this WSDL type.
160      * @return String getConfiguredProperty(WebServiceLogicImpl.QNAME_LOCAL_PART_PATTERN)
161      */
162     protected String getQualfiedNameLocalPartPattern()
163     {
164         return (String)this.getConfiguredProperty(WebServiceLogicImpl.QNAME_LOCAL_PART_PATTERN);
165     }
166 
167     /**
168      * Gets the <code>namespacePattern</code> for this type.
169      * @return getConfiguredProperty(WebServiceLogicImpl.NAMESPACE_PATTERN)
170      */
171     protected String getNamespacePattern()
172     {
173         return (String)this.getConfiguredProperty(WebServiceLogicImpl.NAMESPACE_PATTERN);
174     }
175 
176     /**
177      * Gets whether or not <code>reverseNamespace</code> is true/false for this type.
178      *
179      * @return boolean true/false
180      */
181     private boolean isReverseNamespace()
182     {
183         return Boolean.valueOf(String.valueOf(this.getConfiguredProperty(WebServiceLogicImpl.REVERSE_NAMESPACE)))
184                 .booleanValue();
185     }
186 
187     /**
188      * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeLogic#handleIsWebFaultAnException()
189      */
190     @Override
191     protected boolean handleIsWebFaultAnException()
192     {
193         boolean result = true;
194         if (this.hasStereotype(UMLProfile.STEREOTYPE_WEB_FAULT))
195         {
196             if (!this.hasStereotype(UMLProfile.STEREOTYPE_APPLICATION_EXCEPTION) &&
197                 !this.hasStereotype(UMLProfile.STEREOTYPE_UNEXPECTED_EXCEPTION) &&
198                 !this.hasStereotype(UMLProfile.STEREOTYPE_EXCEPTION))
199             {
200                 result = false;
201             }
202         }
203         return result;
204     }
205 
206     private static final List<WebServiceOperation> weboperations = new ArrayList<WebServiceOperation>();
207     /**
208      * @see org.andromda.cartridges.webservice.metafacades.WSDLTypeLogic#handleIsWebFaultThrown()
209      */
210     @Override
211     protected boolean handleIsWebFaultThrown()
212     {
213         boolean result = true;
214         if (this.hasStereotype(UMLProfile.STEREOTYPE_WEB_FAULT))
215         {
216             result = false;
217             // collect all webservice operations for the entire model in a static list, only once for all wsdl types
218             if (weboperations.isEmpty())
219             {
220                 for (ClassifierFacade classifier : this.getModel().getAllClasses())
221                 {
222                     boolean isService = classifier.hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE);
223                     for (OperationFacade operation : classifier.getOperations())
224                     {
225                         boolean visibility = operation.getVisibility().equals("public") || operation.getVisibility().equals("protected");
226                         if (visibility && (isService || operation.hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE)))
227                         {
228                             weboperations.add((WebServiceOperation)operation);
229                         }
230                     }
231                 }
232             }
233             for (WebServiceOperation op : weboperations)
234             {
235                 for (Object exception : op.getExceptions())
236                 {
237                     if (((ModelElementFacade)exception).getFullyQualifiedName().equals(this.getFullyQualifiedName()))
238                     {
239                         // WebFault is actually thrown by a service - OK
240                         result = true;
241                         break;
242                     }
243                 }
244             }
245         }
246         return result;
247     }
248 }