1 package org.andromda.cartridges.spring.metafacades;
2
3 import java.text.MessageFormat;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.List;
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
21
22
23
24
25
26
27 public class SpringServiceLogicImpl
28 extends SpringServiceLogic
29 {
30 private static final long serialVersionUID = 34L;
31
32
33
34
35
36
37 public SpringServiceLogicImpl(Object metaObject, String context)
38 {
39 super(metaObject, context);
40 }
41
42
43
44
45
46 protected String handleGetEjbJndiName()
47 {
48 StringBuilder jndiName = new StringBuilder();
49 String jndiNamePrefix = StringUtils.trimToEmpty(this.getEjbJndiNamePrefix());
50 if (StringUtils.isNotBlank(jndiNamePrefix))
51 {
52 jndiName.append(jndiNamePrefix);
53 jndiName.append('/');
54 }
55 jndiName.append("ejb/");
56 jndiName.append(this.getFullyQualifiedName());
57 return jndiName.toString();
58 }
59
60
61
62
63
64 protected String handleGetEjbLocalJndiName()
65 {
66 StringBuilder jndiName = new StringBuilder();
67 String jndiNamePrefix = StringUtils.trimToEmpty(this.getEjbJndiNamePrefix());
68 if (StringUtils.isNotBlank(jndiNamePrefix))
69 {
70 jndiName.append(jndiNamePrefix);
71 jndiName.append('/');
72 }
73 jndiName.append("ejb/");
74 jndiName.append(SpringMetafacadeUtils.getFullyQualifiedName(
75 this.getPackageName(),
76 this.getName(),
77 (EJB_BOTH_VIEW.equalsIgnoreCase(this.getEjbViewType()) ? "Local" : null)));
78 return jndiName.toString();
79 }
80
81
82
83
84
85 protected String handleGetEjbImplementationName()
86 {
87 return this.getName() + SpringGlobals.EJB_IMPLEMENTATION_SUFFIX;
88 }
89
90
91
92
93
94 protected String handleGetImplementationName()
95 {
96 return this.getName() + SpringGlobals.IMPLEMENTATION_SUFFIX;
97 }
98
99
100
101
102
103 protected String handleGetFullyQualifiedEjbImplementationName()
104 {
105 return SpringMetafacadeUtils.getFullyQualifiedName(
106 this.getEjbPackageName(),
107 this.getName(),
108 SpringGlobals.EJB_IMPLEMENTATION_SUFFIX);
109 }
110
111
112
113
114
115 protected String handleGetFullyQualifiedEjbName()
116 {
117 return SpringMetafacadeUtils.getFullyQualifiedName(
118 this.getEjbPackageName(),
119 this.getName(),
120 null);
121 }
122
123
124
125
126
127
128 protected String handleGetFullyQualifiedLocalEjbName()
129 {
130
131
132 return SpringMetafacadeUtils.getFullyQualifiedName(
133 this.getEjbPackageName(),
134 this.getName(),
135 (EJB_BOTH_VIEW.equalsIgnoreCase(this.getEjbViewType()) ? "Local" : null));
136 }
137
138
139
140
141
142 protected String handleGetFullyQualifiedImplementationName()
143 {
144 return SpringMetafacadeUtils.getFullyQualifiedName(
145 this.getImplementationPackageName(),
146 this.getName(),
147 SpringGlobals.IMPLEMENTATION_SUFFIX);
148 }
149
150
151
152
153
154 protected String handleGetImplementationPackageName() {
155 String implementationPackageName =
156 MessageFormat.format(
157 this.getImplemenationPackageNamePattern(),
158 StringUtils.trimToEmpty(this.getPackageName()));
159 if (StringUtils.isBlank(this.getPackageName()))
160 {
161 implementationPackageName = implementationPackageName.replaceAll(
162 "^\\.",
163 "");
164 }
165 return implementationPackageName;
166 }
167
168
169
170
171
172 protected String handleGetImplementationPackagePath()
173 {
174 return this.getImplementationPackageName().replace(
175 '.',
176 '/');
177 }
178
179
180
181
182
183
184 protected String getImplemenationPackageNamePattern()
185 {
186 return (String)this.getConfiguredProperty(SpringGlobals.IMPLEMENTATION_PACKAGE_NAME_PATTERN);
187 }
188
189
190
191
192
193 protected String handleGetBaseName()
194 {
195 return this.getName() + SpringGlobals.SERVICE_BASE_SUFFIX;
196 }
197
198
199
200
201
202 protected String handleGetFullyQualifiedBaseName()
203 {
204 return SpringMetafacadeUtils.getFullyQualifiedName(
205 this.getImplementationPackageName(),
206 this.getName(),
207 SpringGlobals.SERVICE_BASE_SUFFIX);
208 }
209
210
211
212
213
214 protected String handleGetEjbPackageName()
215 {
216 String ejbPackageName =
217 MessageFormat.format(
218 this.getEjbPackageNamePattern(),
219 StringUtils.trimToEmpty(this.getPackageName()));
220 if (StringUtils.isBlank(this.getPackageName()))
221 {
222 ejbPackageName = ejbPackageName.replaceAll(
223 "^\\.",
224 "");
225 }
226 return ejbPackageName;
227 }
228
229
230
231
232
233 protected String handleGetEjbPackageNamePath()
234 {
235 return this.getEjbPackageName().replace(
236 '.',
237 '/');
238 }
239
240
241
242
243
244 protected String handleGetBeanName()
245 {
246 return this.getBeanName(false);
247 }
248
249
250
251
252
253
254 protected String handleGetBeanName(boolean targetSuffix)
255 {
256 StringBuilder beanName = new StringBuilder(String.valueOf(this.getConfiguredProperty(SpringGlobals.BEAN_NAME_PREFIX)));
257 beanName.append(StringUtils.uncapitalize(StringUtils.trimToEmpty(this.getName())));
258 if (targetSuffix)
259 {
260 beanName.append(SpringGlobals.BEAN_NAME_TARGET_SUFFIX);
261 }
262 return beanName.toString();
263 }
264
265
266
267
268
269
270 protected String getEjbPackageNamePattern()
271 {
272 return (String)this.getConfiguredProperty(SpringGlobals.EJB_PACKAGE_NAME_PATTERN);
273 }
274
275
276
277
278
279
280 protected String getEjbJndiNamePrefix()
281 {
282 return this.isConfiguredProperty(SpringGlobals.EJB_JNDI_NAME_PREFIX)
283 ? ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.EJB_JNDI_NAME_PREFIX)) : null;
284 }
285
286
287
288
289
290 protected String handleGetWebServiceDelegatorName()
291 {
292 return this.getName() + SpringGlobals.WEB_SERVICE_DELEGATOR_SUFFIX;
293 }
294
295
296
297
298
299 protected String handleGetFullyQualifiedWebServiceDelegatorName()
300 {
301 return SpringMetafacadeUtils.getFullyQualifiedName(
302 this.getImplementationPackageName(),
303 this.getName(),
304 SpringGlobals.WEB_SERVICE_DELEGATOR_SUFFIX);
305 }
306
307
308
309
310
311 protected boolean handleIsWebService()
312 {
313 boolean webService = this.hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE);
314 if (!webService)
315 {
316 webService = !this.getWebServiceOperations().isEmpty();
317 }
318 return webService;
319 }
320
321
322
323
324 protected Collection<String> handleGetInterceptors()
325 {
326 String serviceInterceptorString =
327 this.isConfiguredProperty(SpringGlobals.SERVICE_INTERCEPTORS) ? ObjectUtils.toString(this
328 .getConfiguredProperty(SpringGlobals.SERVICE_INTERCEPTORS)) : null;
329 Collection<String> interceptors = null;
330 if (StringUtils.isNotBlank(serviceInterceptorString) && serviceInterceptorString != null)
331 {
332 interceptors = Arrays.asList(serviceInterceptorString.split(","));
333 }
334 return SpringMetafacadeUtils.getServiceInterceptors(this, interceptors);
335 }
336
337
338
339
340
341 protected boolean handleIsRemotable()
342 {
343 return !this.isPrivate() && !this.isRemotingTypeNone();
344 }
345
346
347
348
349 private String getRemotingType()
350 {
351 final String serviceRemotingType =
352 StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTING_TYPE)));
353 return SpringMetafacadeUtils.getServiceRemotingType(this, serviceRemotingType);
354 }
355
356
357
358
359 protected String handleGetRemoteServer()
360 {
361 return StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_SERVER)));
362 }
363
364
365
366
367
368 protected String handleGetRemotePort()
369 {
370 String serviceRemotePort =
371 StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_PORT)));
372 return SpringMetafacadeUtils.getServiceRemotePort(this, serviceRemotePort);
373 }
374
375
376
377
378 protected String handleGetRemoteContext()
379 {
380 return this.isConfiguredProperty(SpringGlobals.SERVICE_REMOTE_CONTEXT)
381 ? ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_CONTEXT)) : "";
382 }
383
384
385
386
387
388
389 private boolean hasServiceRemotePort()
390 {
391 final String serviceRemotePort = this.getRemotePort();
392 return StringUtils.isNotBlank(serviceRemotePort);
393 }
394
395
396
397
398
399
400 private boolean hasServiceRemoteContext()
401 {
402 final String serviceRemoteContext = this.getRemoteContext();
403 return StringUtils.isNotBlank(serviceRemoteContext);
404 }
405
406
407
408
409
410 protected String handleGetRemoteUrl()
411 {
412 String result = "";
413
414 String propertyPrefix = ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.CONFIG_PROPERTY_PREFIX));
415
416 if (this.isRemotingTypeNone())
417 {
418
419 }
420 else if (this.isRemotingTypeHttpInvoker() || this.isRemotingTypeHessian() || this.isRemotingTypeBurlap())
421 {
422
423 result = "${" + propertyPrefix + "remoteHttpScheme}://${" + propertyPrefix + "remoteServer}";
424
425
426 if (hasServiceRemotePort())
427 {
428 result += ":${" + propertyPrefix + "remotePort}";
429 }
430
431
432 if (hasServiceRemoteContext())
433 {
434 result += "/${" + propertyPrefix + "remoteContext}";
435 }
436
437
438 result += '/' + getName();
439 }
440 else if (this.isRemotingTypeLingo())
441 {
442 result = "${" + propertyPrefix + "remoteTcpScheme}://${" + propertyPrefix + "remoteServer}";
443 result += "/${" + propertyPrefix + "remotePortJMS}";
444 }
445 else if (this.isRemotingTypeRmi())
446 {
447
448 result = "${" + propertyPrefix + "remoteRmiScheme}://${" + propertyPrefix + "remoteServer}";
449
450
451 if (hasServiceRemotePort())
452 {
453 result += ":${" + propertyPrefix + "remotePort}";
454 }
455
456
457 result += '/' + getName();
458 }
459 return result;
460 }
461
462
463
464
465
466 protected Collection handleGetWebServiceOperations()
467 {
468 Collection<OperationFacade> operations = this.getOperations();
469 if (!this.hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE))
470 {
471 operations = new FilteredCollection<OperationFacade>(operations)
472 {
473 private static final long serialVersionUID = 34L;
474 public boolean evaluate(Object object)
475 {
476 return object instanceof SpringServiceOperation && ((SpringServiceOperation)object).isWebserviceExposed();
477 }
478 };
479 }
480 return operations;
481 }
482
483
484
485
486
487
488
489 public List<OperationFacade> getOperations()
490 {
491 final List<OperationFacade> operations = super.getOperations();
492 if (!this.isAbstract())
493 {
494 for (ClassifierFacade generalization = (ClassifierFacade)this.getGeneralization(); generalization != null;
495 generalization = (ClassifierFacade)generalization.getGeneralization())
496 {
497 if (generalization.isAbstract())
498 {
499 CollectionUtils.forAllDo(
500 generalization.getOperations(),
501 new Closure()
502 {
503 public void execute(Object object)
504 {
505 if (((OperationFacade)object).isAbstract())
506 {
507 operations.add((OperationFacade)object);
508 }
509 }
510 });
511 }
512 }
513 }
514 return operations;
515 }
516
517
518
519
520
521 protected String handleGetDefaultExceptionName()
522 {
523 String name =
524 StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.DEFAULT_SERVICE_EXCEPTION_NAME_PATTERN)));
525 return name.replaceAll("\\{0\\}", this.getName());
526 }
527
528
529
530
531
532 protected String handleGetFullyQualifiedDefaultExceptionName()
533 {
534 StringBuilder fullyQualifiedName = new StringBuilder("RuntimeException");
535 if (this.isAllowDefaultServiceException())
536 {
537 fullyQualifiedName = new StringBuilder();
538 if (StringUtils.isNotBlank(this.getPackageName()))
539 {
540 fullyQualifiedName.append(this.getPackageName());
541 fullyQualifiedName.append('.');
542 }
543 fullyQualifiedName.append(this.getDefaultExceptionName());
544 }
545 return fullyQualifiedName.toString();
546 }
547
548
549
550
551
552 protected boolean handleIsAllowDefaultServiceException()
553 {
554 return Boolean.valueOf(String.valueOf(this.getConfiguredProperty(SpringGlobals.DEFAULT_SERVICE_EXCEPTIONS))).booleanValue();
555 }
556
557
558
559
560
561 protected boolean handleIsRemotingTypeRmi()
562 {
563 return SpringGlobals.REMOTING_PROTOCOL_RMI.equalsIgnoreCase(this.getRemotingType());
564 }
565
566
567
568
569
570 protected boolean handleIsRemotingTypeBurlap()
571 {
572 return SpringGlobals.REMOTING_PROTOCOL_BURLAP.equalsIgnoreCase(this.getRemotingType());
573 }
574
575
576
577
578
579 protected boolean handleIsRemotingTypeHessian()
580 {
581 return SpringGlobals.REMOTING_PROTOCOL_HESSIAN.equalsIgnoreCase(this.getRemotingType());
582 }
583
584
585
586
587
588 protected boolean handleIsRemotingTypeHttpInvoker()
589 {
590 return SpringGlobals.REMOTING_PROTOCOL_HTTPINVOKER.equalsIgnoreCase(this.getRemotingType());
591 }
592
593
594
595
596
597 protected boolean handleIsRemotingTypeLingo()
598 {
599 return SpringGlobals.REMOTING_PROTOCOL_LINGO.equalsIgnoreCase(this.getRemotingType());
600 }
601
602
603
604
605
606 protected boolean handleIsRemotingTypeNone()
607 {
608 return SpringGlobals.REMOTING_PROTOCOL_NONE.equalsIgnoreCase(this.getRemotingType());
609 }
610
611
612
613
614
615 private static final String HIBERNATE_INTERCEPTOR_ENABLED = "serviceHibernateInterceptorEnabled";
616
617
618
619
620
621 protected boolean handleIsHibernateInterceptorEnabled()
622 {
623 return Boolean.valueOf(String.valueOf(this.getConfiguredProperty(HIBERNATE_INTERCEPTOR_ENABLED))).booleanValue();
624 }
625
626
627
628
629 private static final String EJB_VIEW_TYPE = "ejbViewType";
630
631
632
633
634 private String getEjbViewType()
635 {
636 Object value = this.findTaggedValue(SpringProfile.TAGGEDVALUE_EJB_VIEW_TYPE);
637 if (value == null)
638 {
639 value = this.getConfiguredProperty(EJB_VIEW_TYPE);
640 }
641 return ObjectUtils.toString(value);
642 }
643
644
645
646
647 private static final String EJB_REMOTE_VIEW = "remote";
648 private static final String EJB_LOCAL_VIEW = "local";
649 private static final String EJB_BOTH_VIEW = "both";
650
651
652
653
654
655 protected boolean handleIsEjbRemoteView()
656 {
657 return (EJB_REMOTE_VIEW.equalsIgnoreCase(this.getEjbViewType())
658 || EJB_BOTH_VIEW.equalsIgnoreCase(this.getEjbViewType()));
659 }
660
661
662
663
664
665 protected boolean handleIsEjbLocalView()
666 {
667 return (EJB_LOCAL_VIEW.equalsIgnoreCase(this.getEjbViewType())
668 || EJB_BOTH_VIEW.equalsIgnoreCase(this.getEjbViewType()));
669 }
670
671
672
673
674
675 protected String handleGetEjbTransactionType()
676 {
677 String transactionType = null;
678 final boolean ejbTransactionsEnabled =
679 BooleanUtils.toBoolean(
680 ObjectUtils.toString(this.getConfiguredProperty(SpringGlobals.EJB_TRANSACTIONS_ENABLED)));
681 if (ejbTransactionsEnabled)
682 {
683 transactionType = "Container";
684 }
685 else
686 {
687 transactionType = "Bean";
688 }
689 return transactionType;
690 }
691
692
693
694
695 protected boolean handleIsConfigonly()
696 {
697 String value = (String)this.findTaggedValue(SpringProfile.TAGGEDVALUE_SERVICE_CONFIG_ONLY);
698 return BooleanUtils.toBoolean(StringUtils.trimToEmpty(value));
699 }
700
701
702
703
704 protected boolean handleIsPrivate()
705 {
706 String value = (String)this.findTaggedValue(SpringProfile.TAGGEDVALUE_SERVICE_PRIVATE);
707 return BooleanUtils.toBoolean(StringUtils.trimToEmpty(value));
708 }
709
710
711
712
713
714 protected String handleGetWebServiceOutgoingAttachmentHandlerCall()
715 {
716 return this.getWebServiceAttachmentHandlerCall(SpringGlobals.WEBSERVICE_OUTGOING_ATTACHMENT_HANDLER_CALL_PATTERN);
717 }
718
719
720
721
722
723 protected String handleGetWebServiceIncomingAttachmentHandlerCall()
724 {
725 return this.getWebServiceAttachmentHandlerCall(SpringGlobals.WEBSERVICE_INCOMING_ATTACHMENT_HANDLER_CALL_PATTERN);
726 }
727
728
729
730
731
732
733
734
735
736 private String getWebServiceAttachmentHandlerCall(final String patternProperty)
737 {
738 String call = null;
739 if (this.isWebService())
740 {
741 final String value = ObjectUtils.toString(this.getConfiguredProperty(patternProperty));
742 if (StringUtils.isNotBlank(value))
743 {
744 call = value;
745 }
746 }
747 return call;
748 }
749
750
751
752
753
754 protected boolean handleIsRichClient()
755 {
756 final String richClient = StringUtils.trimToEmpty(String.valueOf(this.getConfiguredProperty(SpringGlobals.RICH_CLIENT)));
757 return "true".equalsIgnoreCase(richClient);
758 }
759
760
761
762
763 protected String handleGetInitMethodName()
764 {
765 String result = "";
766 for (OperationFacade operation : getOperations())
767 {
768 if(((SpringServiceOperation)operation).isInitMethod())
769 {
770 result = operation.getName();
771 break;
772 }
773 }
774 return result;
775 }
776
777
778
779
780 protected String handleGetDestroyMethodName()
781 {
782 String result = "";
783 for (OperationFacade operation : getOperations())
784 {
785 if(((SpringServiceOperation)operation).isDestroyMethod())
786 {
787 result = operation.getName();
788 break;
789 }
790 }
791 return result;
792 }
793 }