View Javadoc

1   package org.andromda.cartridges.spring.metafacades;
2   
3   import java.text.MessageFormat;
4   
5   import java.util.Collection;
6   
7   import org.andromda.cartridges.spring.SpringProfile;
8   import org.andromda.metafacades.uml.ClassifierFacade;
9   import org.andromda.metafacades.uml.FilteredCollection;
10  import org.andromda.metafacades.uml.OperationFacade;
11  import org.andromda.metafacades.uml.UMLProfile;
12  import org.apache.commons.collections.Closure;
13  import org.apache.commons.collections.CollectionUtils;
14  import org.apache.commons.lang.BooleanUtils;
15  import org.apache.commons.lang.ObjectUtils;
16  import org.apache.commons.lang.StringUtils;
17  
18  
19  /***
20   * MetafacadeLogic implementation for
21   * org.andromda.cartridges.spring.metafacades.SpringService.
22   *
23   * @author Chad Brandon
24   * @author Peter Friese
25   * @see org.andromda.cartridges.spring.metafacades.SpringService
26   */
27  public class SpringServiceLogicImpl
28      extends SpringServiceLogic
29  {
30      public SpringServiceLogicImpl(Object metaObject, String context)
31      {
32          super(metaObject, context);
33      }
34  
35      /***
36       * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbJndiName()
37       */
38      protected java.lang.String handleGetEjbJndiName()
39      {
40          StringBuffer jndiName = new StringBuffer();
41          String jndiNamePrefix = StringUtils.trimToEmpty(this.getEjbJndiNamePrefix());
42          if (StringUtils.isNotEmpty(jndiNamePrefix))
43          {
44              jndiName.append(jndiNamePrefix);
45              jndiName.append("/");
46          }
47          jndiName.append("ejb/");
48          jndiName.append(this.getFullyQualifiedName());
49          return jndiName.toString();
50      }
51  
52  	protected String handleGetEjbLocalJndiName() {
53          StringBuffer jndiName = new StringBuffer();
54          String jndiNamePrefix = StringUtils.trimToEmpty(this.getEjbJndiNamePrefix());
55          if (StringUtils.isNotEmpty(jndiNamePrefix))
56          {
57              jndiName.append(jndiNamePrefix);
58              jndiName.append("/");
59          }
60          jndiName.append("ejb/");
61          jndiName.append(SpringMetafacadeUtils.getFullyQualifiedName(
62                  this.getPackageName(),
63                  this.getName(),
64                  (this.getEjbViewType().equalsIgnoreCase(EJB_BOTH_VIEW) ? "Local" : null)));
65          return jndiName.toString();
66  	}
67  	
68      /***
69       * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbImplementationName()
70       */
71      protected java.lang.String handleGetEjbImplementationName()
72      {
73          return this.getName() + SpringGlobals.EJB_IMPLEMENTATION_SUFFIX;
74      }
75  
76      /***
77       * @see org.andromda.cartridges.spring.metafacades.SpringService#getImplementationName()
78       */
79      protected java.lang.String handleGetImplementationName()
80      {
81          return this.getName() + SpringGlobals.IMPLEMENTATION_SUFFIX;
82      }
83  
84      /***
85       * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedImplementationName()
86       */
87      protected java.lang.String handleGetFullyQualifiedEjbImplementationName()
88      {
89          return SpringMetafacadeUtils.getFullyQualifiedName(
90              this.getEjbPackageName(),
91              this.getName(),
92              SpringGlobals.EJB_IMPLEMENTATION_SUFFIX);
93      }
94  
95      /***
96       * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedEjbName()
97       */
98      protected java.lang.String handleGetFullyQualifiedEjbName()
99      {
100         return SpringMetafacadeUtils.getFullyQualifiedName(
101             this.getEjbPackageName(),
102             this.getName(),
103             null);
104     }
105     
106     /***
107      * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedLocalEjbName()
108      */
109 	protected String handleGetFullyQualifiedLocalEjbName() {
110 		//add "Local" to local ejb name when viewtype = "both",
111 		//to prevent name clashing with remote interface naming 
112         return SpringMetafacadeUtils.getFullyQualifiedName(
113                 this.getEjbPackageName(),
114                 this.getName(),
115                 (this.getEjbViewType().equalsIgnoreCase(EJB_BOTH_VIEW) ? "Local" : null));
116 	}
117 
118     /***
119      * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedImplementationName()
120      */
121     protected java.lang.String handleGetFullyQualifiedImplementationName()
122     {
123         return SpringMetafacadeUtils.getFullyQualifiedName(
124             this.getPackageName(),
125             this.getName(),
126             SpringGlobals.IMPLEMENTATION_SUFFIX);
127     }
128 
129     /***
130      * @see org.andromda.cartridges.spring.metafacades.SpringService#getBaseName()
131      */
132     protected java.lang.String handleGetBaseName()
133     {
134         return this.getName() + SpringGlobals.SERVICE_BASE_SUFFIX;
135     }
136 
137     /***
138      * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedBaseName()
139      */
140     protected java.lang.String handleGetFullyQualifiedBaseName()
141     {
142         return SpringMetafacadeUtils.getFullyQualifiedName(
143             this.getPackageName(),
144             this.getName(),
145             SpringGlobals.SERVICE_BASE_SUFFIX);
146     }
147 
148     /***
149      * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbPackageName()
150      */
151     protected java.lang.String handleGetEjbPackageName()
152     {
153         String ejbPackageName =
154             MessageFormat.format(
155                 this.getEjbPackageNamePattern(),
156                 new Object[] {StringUtils.trimToEmpty(this.getPackageName())});
157         if (StringUtils.isBlank(this.getPackageName()))
158         {
159             ejbPackageName = ejbPackageName.replaceAll(
160                     "^//.",
161                     "");
162         }
163         return ejbPackageName;
164     }
165 
166     /***
167      * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbPackageNamePath()
168      */
169     protected java.lang.String handleGetEjbPackageNamePath()
170     {
171         return this.getEjbPackageName().replace(
172             '.',
173             '/');
174     }
175 
176     /***
177      * @see org.andromda.cartridges.spring.metafacades.SpringService#getBeanName()
178      */
179     protected java.lang.String handleGetBeanName()
180     {
181         return this.getBeanName(false);
182     }
183 
184     /***
185      * @see org.andromda.cartridges.spring.metafacades.SpringService#getBeanName(boolean)
186      */
187     protected java.lang.String handleGetBeanName(boolean targetSuffix)
188     {
189         StringBuffer beanName = new StringBuffer(StringUtils.uncapitalize(StringUtils.trimToEmpty(this.getName())));
190         if (targetSuffix)
191         {
192             beanName.append(SpringGlobals.BEAN_NAME_TARGET_SUFFIX);
193         }
194         return beanName.toString();
195     }
196 
197     /***
198      * Gets the <code>ejbPackageNamePattern</code> for this EJB.
199      *
200      * @return the defined package pattern.
201      */
202     protected String getEjbPackageNamePattern()
203     {
204         return (String)this.getConfiguredProperty(SpringGlobals.EJB_PACKAGE_NAME_PATTERN);
205     }
206 
207     /***
208      * Gets the <code>ejbJndiNamePrefix</code> for this EJB.
209      *
210      * @return the EJB Jndi name prefix.
211      */
212     protected String getEjbJndiNamePrefix()
213     {
214         return this.isConfiguredProperty(SpringGlobals.EJB_JNDI_NAME_PREFIX)
215             ? ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.EJB_JNDI_NAME_PREFIX)) : null;
216     }
217 
218     /***
219      * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceDelegatorName()
220      */
221     protected String handleGetWebServiceDelegatorName()
222     {
223         return this.getName() + SpringGlobals.WEB_SERVICE_DELEGATOR_SUFFIX;
224     }
225 
226     /***
227      * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedWebServiceDelegatorName()
228      */
229     protected String handleGetFullyQualifiedWebServiceDelegatorName()
230     {
231         return this.getFullyQualifiedName() + SpringGlobals.WEB_SERVICE_DELEGATOR_SUFFIX;
232     }
233 
234     /***
235      * @see org.andromda.cartridges.spring.metafacades.SpringService#isWebService()
236      */
237     protected boolean handleIsWebService()
238     {
239         boolean webService = this.hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE);
240         if (!webService)
241         {
242             webService = !this.getWebServiceOperations().isEmpty();
243         }
244         return webService;
245     }
246 
247     /***
248      * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleGetInterceptors()
249      */
250     protected String[] handleGetInterceptors()
251     {
252         String serviceInterceptorString =
253             this.isConfiguredProperty(SpringGlobals.SERVICE_INTERCEPTORS) ? ObjectUtils.toString(this
254                 .getConfiguredProperty(SpringGlobals.SERVICE_INTERCEPTORS)) : null;
255         String[] interceptors = null;
256         if (StringUtils.isNotEmpty(serviceInterceptorString))
257         {
258             interceptors = serviceInterceptorString.split(",");
259         }
260         return SpringMetafacadeUtils.getServiceInterceptors(this, interceptors);
261     }
262 
263     /***
264      * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotable()
265      */
266     protected boolean handleIsRemotable()
267     {
268         return !this.isRemotingTypeNone();
269     }
270 
271     /***
272      * Gets the remoting type for this service.
273      */
274     private String getRemotingType()
275     {
276         final String serviceRemotingType =
277             StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTING_TYPE)));
278         return SpringMetafacadeUtils.getServiceRemotingType(this, serviceRemotingType);
279     }
280 
281     /***
282      * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleGetRemoteServer()
283      */
284     protected String handleGetRemoteServer()
285     {
286         return StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_SERVER)));
287     }
288 
289     /***
290      * @see org.andromda.cartridges.spring.metafacades.SpringService#getRemotePort()
291      */
292     protected String handleGetRemotePort()
293     {
294         String serviceRemotePort =
295             StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_PORT)));
296         return SpringMetafacadeUtils.getServiceRemotePort(this, serviceRemotePort);
297     }
298 
299     /***
300      * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleGetRemoteContext()
301      */
302     protected String handleGetRemoteContext()
303     {
304         return this.isConfiguredProperty(SpringGlobals.SERVICE_REMOTE_CONTEXT) 
305             ? ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_CONTEXT)) : "";
306     }
307 
308     /***
309      * Checks whether this service has a remote port assigned.
310      *
311      * @return <code>true</code> if the service has a remote port, <code>false</code> otherwise.
312      */
313     private boolean hasServiceRemotePort()
314     {
315         final String serviceRemotePort = this.getRemotePort();
316         return StringUtils.isNotEmpty(serviceRemotePort);
317     }
318 
319     /***
320      * Checks whether the service has a remote context assigned.
321      *
322      * @return <code>true</code> if the service has a remote context, <code>false</code> otherweise.
323      */
324     private boolean hasServiceRemoteContext()
325     {
326         final String serviceRemoteContext = this.getRemoteContext();
327         return StringUtils.isNotEmpty(serviceRemoteContext);
328     }
329 
330     /***
331      * @see org.andromda.cartridges.spring.metafacades.SpringService#getRemoteUrl()
332      */
333     protected String handleGetRemoteUrl()
334     {
335         String result = "";
336         
337         String propertyPrefix = ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.CONFIG_PROPERTY_PREFIX));
338 
339         if (this.isRemotingTypeNone())
340         {
341             // nothing
342         }
343         else if (this.isRemotingTypeHttpInvoker() || this.isRemotingTypeHessian() || this.isRemotingTypeBurlap())
344         {
345             // server
346             result = "http://${" + propertyPrefix + "remoteServer}";
347 
348             // port
349             if (hasServiceRemotePort())
350             {
351                 result += ":${" + propertyPrefix + "remotePort}";
352             }
353 
354             // context
355             if (hasServiceRemoteContext())
356             {
357                 result += "/${" + propertyPrefix + "remoteContext}";
358             }
359 
360             // service name
361             result += "/" + getName();
362         }
363         else if (this.isRemotingTypeRmi())
364         {
365             // server
366             result = "rmi://${" + propertyPrefix + "remoteServer}";
367 
368             // port
369             if (hasServiceRemotePort())
370             {
371                 result += ":${" + propertyPrefix + "remotePort}";
372             }
373 
374             // service name
375             result += "/" + getName();
376         }
377         return result;
378     }
379 
380     /***
381      * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceOperations()
382      */
383     protected Collection handleGetWebServiceOperations()
384     {
385         Collection operations = this.getOperations();
386         if (!this.hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE))
387         {
388             operations = new FilteredCollection(operations)
389                 {
390                     public boolean evaluate(Object object)
391                     {
392                         return object instanceof SpringServiceOperation && ((SpringServiceOperation)object).isWebserviceExposed();
393                     }
394                 };
395         }
396         return operations;
397     }
398 
399     /***
400      * Override to retrieve any abstract operations from an abstract
401      * generalization.
402      *
403      * @see org.andromda.metafacades.uml.ClassifierFacade#getOperations()
404      */
405     public Collection getOperations()
406     {
407         final Collection operations = super.getOperations();
408         if (!this.isAbstract())
409         {
410             for (ClassifierFacade generalization = (ClassifierFacade)this.getGeneralization(); generalization != null;
411                 generalization = (ClassifierFacade)generalization.getGeneralization())
412             {
413                 if (generalization.isAbstract())
414                 {
415                     CollectionUtils.forAllDo(
416                         generalization.getOperations(),
417                         new Closure()
418                         {
419                             public void execute(Object object)
420                             {
421                                 if (((OperationFacade)object).isAbstract())
422                                 {
423                                     operations.add(object);
424                                 }
425                             }
426                         });
427                 }
428             }
429         }
430         return operations;
431     }
432 
433     /***
434      * @see org.andromda.cartridges.spring.metafacades.SpringService#getDefaultExceptionName()
435      */
436     protected String handleGetDefaultExceptionName()
437     {
438         String name =
439             StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.DEFAULT_SERVICE_EXCEPTION_NAME_PATTERN)));
440         return name.replaceAll("//{0//}", this.getName());
441     }
442 
443     /***
444      * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedDefaultExceptionName()
445      */
446     protected String handleGetFullyQualifiedDefaultExceptionName()
447     {
448         StringBuffer fullyQualifiedName = new StringBuffer("java.lang.RuntimeException");
449         if (this.isAllowDefaultServiceException())
450         {
451             fullyQualifiedName = new StringBuffer();
452             if (StringUtils.isNotBlank(this.getPackageName()))
453             {
454                 fullyQualifiedName.append(this.getPackageName());
455                 fullyQualifiedName.append('.');
456             }
457             fullyQualifiedName.append(this.getDefaultExceptionName());
458         }
459         return fullyQualifiedName.toString();
460     }
461 
462     /***
463      * @see org.andromda.cartridges.spring.metafacades.SpringService#isAllowDefaultServiceException()
464      */
465     protected boolean handleIsAllowDefaultServiceException()
466     {
467         return Boolean.valueOf(String.valueOf(this.getConfiguredProperty(SpringGlobals.DEFAULT_SERVICE_EXCEPTIONS))).booleanValue();
468     }
469 
470     /***
471      * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeRmi()
472      */
473     protected boolean handleIsRemotingTypeRmi()
474     {
475         return this.getRemotingType().equalsIgnoreCase(SpringGlobals.REMOTING_PROTOCOL_RMI);
476     }
477 
478     /***
479      * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeBurlap()
480      */
481     protected boolean handleIsRemotingTypeBurlap()
482     {
483         return this.getRemotingType().equalsIgnoreCase(SpringGlobals.REMOTING_PROTOCOL_BURLAP);
484     }
485 
486     /***
487      * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeHessian()
488      */
489     protected boolean handleIsRemotingTypeHessian()
490     {
491         return this.getRemotingType().equalsIgnoreCase(SpringGlobals.REMOTING_PROTOCOL_HESSIAN);
492     }
493 
494     /***
495      * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeHttpInvoker()
496      */
497     protected boolean handleIsRemotingTypeHttpInvoker()
498     {
499         return this.getRemotingType().equalsIgnoreCase(SpringGlobals.REMOTING_PROTOCOL_HTTPINVOKER);
500     }
501 
502     /***
503      * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeNone()
504      */
505     protected boolean handleIsRemotingTypeNone()
506     {
507         return this.getRemotingType().equalsIgnoreCase(SpringGlobals.REMOTING_PROTOCOL_NONE);
508     }
509 
510     /***
511      * Stores the namespace property indicating whether or not the hibernate
512      * interceptor is enabled for this service.
513      */
514     private static final String HIBERNATE_INTERCEPTOR_ENABLED = "serviceHibernateInterceptorEnabled";
515 
516     /***
517      * @see org.andromda.cartridges.spring.metafacades.SpringService#isHibernateInterceptorEnabled()
518      */
519     protected boolean handleIsHibernateInterceptorEnabled()
520     {
521         return Boolean.valueOf(String.valueOf(this.getConfiguredProperty(HIBERNATE_INTERCEPTOR_ENABLED))).booleanValue();
522     }
523 
524     /***
525      * Stores the view type for an EJB service.
526      */
527     private static final String EJB_VIEW_TYPE = "ejbViewType";
528 
529     /***
530      * Gets the view for this service (if wrapped by an EJB).
531      */
532     private String getEjbViewType()
533     {
534         Object value = this.findTaggedValue(SpringProfile.TAGGEDVALUE_EJB_VIEW_TYPE);
535         if (value == null)
536         {
537             value = this.getConfiguredProperty(EJB_VIEW_TYPE);
538         }
539         return ObjectUtils.toString(value);
540     }
541 
542     /***
543      * The three EJB view type values.
544      */
545     private static final String EJB_REMOTE_VIEW = "remote";
546     private static final String EJB_LOCAL_VIEW = "local";
547     private static final String EJB_BOTH_VIEW = "both";
548 
549     /***
550      * @see org.andromda.cartridges.spring.metafacades.SpringService#isEjbRemoteView()
551      */
552     protected boolean handleIsEjbRemoteView()
553     {
554         return (this.getEjbViewType().equalsIgnoreCase(EJB_REMOTE_VIEW)
555         		|| this.getEjbViewType().equalsIgnoreCase(EJB_BOTH_VIEW));
556     }
557 
558     /***
559      * @see org.andromda.cartridges.spring.metafacades.SpringService#isEjbLocalView()
560      */
561 	protected boolean handleIsEjbLocalView()
562 	{
563         return (this.getEjbViewType().equalsIgnoreCase(EJB_LOCAL_VIEW)
564         		|| this.getEjbViewType().equalsIgnoreCase(EJB_BOTH_VIEW));
565 	}
566     
567     /***
568      * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbTransactionType()
569      */
570     protected String handleGetEjbTransactionType()
571     {
572         String transactionType;
573         final boolean ejbTransactionsEnabled =
574             BooleanUtils.toBoolean(
575                 ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.EJB_TRANSACTIONS_ENABLED)));
576         if (ejbTransactionsEnabled)
577         {
578             transactionType = "Container";
579         }
580         else
581         {
582             transactionType = "Bean";
583         }
584         return transactionType;
585     }
586 
587     /***
588      * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleIsConfigonly()
589      */
590     protected boolean handleIsConfigonly()
591     {
592         String value = (String)this.findTaggedValue(SpringProfile.TAGGEDVALUE_SERVICE_CONFIG_ONLY);
593         return BooleanUtils.toBoolean(StringUtils.trimToEmpty(value));
594     }
595 
596     /***
597      * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceOutgoingAttachmentHandlerCall()
598      */
599     protected String handleGetWebServiceOutgoingAttachmentHandlerCall()
600     {
601         return this.getWebServiceAttachmentHandlerCall(SpringGlobals.WEBSERVICE_OUTGOING_ATTACHMENT_HANDLER_CALL_PATTERN);
602     }
603 
604     /***
605      * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceIncomingAttachmentHandlerCall()
606      */
607     protected String handleGetWebServiceIncomingAttachmentHandlerCall()
608     {
609         return this.getWebServiceAttachmentHandlerCall(SpringGlobals.WEBSERVICE_INCOMING_ATTACHMENT_HANDLER_CALL_PATTERN);
610     }
611     
612     /***
613      * Constructs the webservice attachment handler call or returns null if
614      * one isn't found or is not appropriate it for the service (i.e. it isn't
615      * a webservice).
616      * 
617      * @param patternProperty the property defining the pattern type.
618      * @return the call value.
619      */
620     private String getWebServiceAttachmentHandlerCall(final String patternProperty)
621     {
622         String call = null;
623         if (this.isWebService())
624         {
625             final String value = ObjectUtils.toString(this.getConfiguredProperty(patternProperty));
626             if (StringUtils.isNotBlank(value))
627             {
628                 call = value;
629             }
630         }
631         return call;        
632     }
633 
634     /***
635      * @see org.andromda.cartridges.spring.metafacades.SpringService#isRichClient()
636      */
637     protected boolean handleIsRichClient() 
638     {
639         final String richClient = StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.RICH_CLIENT)));
640         return richClient.equalsIgnoreCase("true");
641     }
642 }