1 package org.andromda.cartridges.ejb3.metafacades;
2
3 import java.text.MessageFormat;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.LinkedHashSet;
7 import org.andromda.cartridges.ejb3.EJB3Globals;
8 import org.andromda.cartridges.ejb3.EJB3Profile;
9 import org.andromda.metafacades.uml.AttributeFacade;
10 import org.andromda.metafacades.uml.DependencyFacade;
11 import org.andromda.metafacades.uml.ModelElementFacade;
12 import org.andromda.metafacades.uml.Role;
13 import org.apache.commons.collections.Closure;
14 import org.apache.commons.collections.CollectionUtils;
15 import org.apache.commons.collections.Predicate;
16 import org.apache.commons.collections.Transformer;
17 import org.apache.commons.lang.BooleanUtils;
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.lang.math.NumberUtils;
20
21
22
23
24
25
26 public class EJB3MessageDrivenFacadeLogicImpl
27 extends EJB3MessageDrivenFacadeLogic
28 {
29 private static final long serialVersionUID = 34L;
30
31
32
33 public static final String MESSAGE_DRIVEN_DESTINATION_TYPE = "messageDrivenDestinationType";
34
35
36
37
38 public static final String MESSAGE_DRIVEN_TOPIC_SUBSCRIPTION_DURABILITY =
39 "messageDrivenTopicSubscriptionDurability";
40
41
42
43
44 public static final String MESSAGE_DRIVEN_NAME_PATTERN = "messageDrivenNamePattern";
45
46
47
48
49 private static final String MESSAGE_DRIVEN_LISTENER_NAME_PATTERN = "messageDrivenListenerNamePattern";
50
51
52
53
54 private static final String MESSAGE_DRIVEN_IMPLEMENTATION_NAME_PATTERN = "messageDrivenImplementationNamePattern";
55
56
57
58
59 private static final String MESSAGE_DRIVEN_TEST_NAME_PATTERN = "messageDrivenTestNamePattern";
60
61
62
63
64 private static final String MESSAGE_DRIVEN_TEST_PACKAGE_NAME_PATTERN = "messageDrivenTestPackageNamePattern";
65
66
67
68
69 private static final String MESSAGE_DRIVEN_DURABLE_SUBSCRIPTION_ID_PATTERN =
70 "messageDrivenDurableSubscriptionIdPattern";
71
72
73
74
75
76
77
78 public EJB3MessageDrivenFacadeLogicImpl(final Object metaObject, final String context)
79 {
80 super (metaObject, context);
81 }
82
83
84
85
86
87
88 @Override
89 protected String handleGetAcknowledgeMode()
90 {
91 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_ACKNOWLEDGE_MODE);
92 }
93
94
95
96
97 @Override
98 protected String handleGetDestination()
99 {
100 String destination = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DESTINATION);
101 if (StringUtils.isBlank(destination))
102 {
103 destination = (getDestinationType().equalsIgnoreCase(EJB3Globals.MDB_DESTINATION_TYPE_TOPIC) ?
104 "topic/" : "queue/") + getMessageDrivenName();
105 }
106 return destination;
107 }
108
109
110
111
112 @Override
113 protected String handleGetDestinationType()
114 {
115 String destinationType = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DESTINATION_TYPE);
116 if (StringUtils.isBlank(destinationType))
117 {
118 destinationType = (String)this.getConfiguredProperty(MESSAGE_DRIVEN_DESTINATION_TYPE);
119 }
120
121
122
123
124 destinationType = StringUtils.replaceChars(destinationType, '_', '.');
125 if (destinationType.equalsIgnoreCase(EJB3Globals.MDB_DESTINATION_TYPE_QUEUE))
126 {
127 destinationType = EJB3Globals.MDB_DESTINATION_TYPE_QUEUE;
128 }
129 else if (destinationType.equalsIgnoreCase(EJB3Globals.MDB_DESTINATION_TYPE_TOPIC))
130 {
131 destinationType = EJB3Globals.MDB_DESTINATION_TYPE_TOPIC;
132 }
133 return destinationType;
134 }
135
136
137
138
139 @Override
140 protected String handleGetTestPackageName()
141 {
142 String namespacePattern = String.valueOf(this.getConfiguredProperty(MESSAGE_DRIVEN_TEST_PACKAGE_NAME_PATTERN));
143 return MessageFormat.format(
144 namespacePattern,
145 this.getPackageName());
146 }
147
148
149
150
151 @Override
152 protected String handleGetFullyQualifiedMessageDrivenImplementationName()
153 {
154 return EJB3MetafacadeUtils.getFullyQualifiedName(
155 this.getPackageName(),
156 this.getMessageDrivenImplementationName(),
157 null);
158 }
159
160
161
162
163 @Override
164 protected String handleGetFullyQualifiedMessageDrivenListenerName()
165 {
166 return EJB3MetafacadeUtils.getFullyQualifiedName(
167 this.getPackageName(),
168 this.getMessageDrivenListenerName(),
169 null);
170 }
171
172
173
174
175 @Override
176 protected String handleGetFullyQualifiedMessageDrivenName()
177 {
178 return EJB3MetafacadeUtils.getFullyQualifiedName(
179 this.getPackageName(),
180 this.getMessageDrivenName(),
181 null);
182 }
183
184
185
186
187 @Override
188 protected String handleGetFullyQualifiedMessageDrivenTestName()
189 {
190 return EJB3MetafacadeUtils.getFullyQualifiedName(
191 this.getTestPackageName(),
192 this.getMessageDrivenTestName(),
193 null);
194 }
195
196
197
198
199 @Override
200 protected String handleGetMessageDrivenImplementationName()
201 {
202 String messageDrivenImplNamePattern =
203 (String)this.getConfiguredProperty(MESSAGE_DRIVEN_IMPLEMENTATION_NAME_PATTERN);
204
205 return MessageFormat.format(
206 messageDrivenImplNamePattern,
207 StringUtils.trimToEmpty(this.getName()));
208 }
209
210
211
212
213 @Override
214 protected String handleGetMessageDrivenListenerName()
215 {
216 String messageDrivenListenerNamePattern =
217 (String)this.getConfiguredProperty(MESSAGE_DRIVEN_LISTENER_NAME_PATTERN);
218
219 return MessageFormat.format(
220 messageDrivenListenerNamePattern,
221 StringUtils.trimToEmpty(this.getName()));
222 }
223
224
225
226
227 @Override
228 protected String handleGetMessageDrivenName()
229 {
230 String messageDrivenNamePattern =
231 (String)this.getConfiguredProperty(MESSAGE_DRIVEN_NAME_PATTERN);
232
233 return MessageFormat.format(
234 messageDrivenNamePattern,
235 StringUtils.trimToEmpty(this.getName()));
236 }
237
238
239
240
241 @Override
242 protected String handleGetMessageDrivenTestName()
243 {
244 String messageDrivenTestNamePattern =
245 (String)this.getConfiguredProperty(MESSAGE_DRIVEN_TEST_NAME_PATTERN);
246
247 return MessageFormat.format(
248 messageDrivenTestNamePattern,
249 StringUtils.trimToEmpty(this.getName()));
250 }
251
252
253
254
255 @Override
256 protected String handleGetMessageSelector()
257 {
258 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_SELECTOR);
259 }
260
261
262
263
264 @Override
265 protected String handleGetRunAs()
266 {
267 String runAsRole = null;
268 DependencyFacade dependency = (DependencyFacade)CollectionUtils.find(
269 this.getTargetDependencies(),
270 new Predicate()
271 {
272 public boolean evaluate(final Object object)
273 {
274 DependencyFacade dependency = (DependencyFacade)object;
275 return dependency != null
276 && dependency.getSourceElement() != null
277 && dependency.getSourceElement() instanceof Role
278 && dependency.hasStereotype(EJB3Profile.STEREOTYPE_SECURITY_RUNAS);
279 }
280 });
281 if (dependency != null)
282 {
283 Role role = (Role)dependency.getSourceElement();
284 runAsRole = role.getName();
285 }
286 return runAsRole;
287 }
288
289
290
291
292 @Override
293 protected String handleGetSubscriptionDurability()
294 {
295 String subscriptionDurability = null;
296 if (this.isDestinationTypeTopic())
297 {
298 subscriptionDurability = String.valueOf(
299 this.getConfiguredProperty(MESSAGE_DRIVEN_TOPIC_SUBSCRIPTION_DURABILITY));
300 if (this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DURABILITY) != null)
301 {
302 subscriptionDurability = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_DURABILITY);
303 }
304 }
305 return subscriptionDurability;
306 }
307
308
309
310
311 @Override
312 protected boolean handleIsSubscriptionDurable()
313 {
314 return StringUtils.equalsIgnoreCase(this.getSubscriptionDurability(), EJB3Globals.MDB_SUBSCRIPTION_DURABLE) ?
315 true : false;
316 }
317
318
319
320
321 @Override
322 protected boolean handleIsSubscriptionNonDurable()
323 {
324 return StringUtils.equalsIgnoreCase(this.getSubscriptionDurability(), EJB3Globals.MDB_SUBSCRIPTION_NONDURABLE) ?
325 true : false;
326 }
327
328
329
330
331 @Override
332 protected String handleGetDurableSubscriptionId()
333 {
334 String durableSubscriptionIdPattern =
335 (String)this.getConfiguredProperty(MESSAGE_DRIVEN_DURABLE_SUBSCRIPTION_ID_PATTERN);
336
337 return MessageFormat.format(
338 durableSubscriptionIdPattern,
339 StringUtils.trimToEmpty(this.getName()));
340 }
341
342
343
344
345 @Override
346 protected String handleGetTransactionManagement()
347 {
348 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_TRANSACTION_MANAGEMENT);
349 }
350
351
352
353
354 @Override
355 protected boolean handleIsTransactionManagementBean()
356 {
357 return StringUtils.equalsIgnoreCase(getTransactionManagement(), EJB3Globals.TRANSACTION_MANAGEMENT_BEAN);
358 }
359
360
361
362
363 @Override
364 protected String handleGetTransactionType()
365 {
366 return EJB3MetafacadeUtils.getTransactionType(this,
367 String.valueOf(this.getConfiguredProperty(EJB3Globals.TRANSACTION_TYPE)));
368 }
369
370
371
372
373 @Override
374 protected String handleGetAttributesAsList(
375 Collection attributes,
376 boolean includeTypes,
377 boolean includeNames)
378 {
379 if (!includeNames && !includeTypes || attributes == null)
380 {
381 return "";
382 }
383
384 StringBuilder sb = new StringBuilder();
385 String separator = "";
386
387 for (final Iterator it = attributes.iterator(); it.hasNext();)
388 {
389 AttributeFacade attr = (AttributeFacade)it.next();
390 sb.append(separator);
391 separator = ", ";
392 if (includeTypes)
393 {
394 sb.append(attr.getGetterSetterTypeName());
395 sb.append(" ");
396 }
397 if (includeNames)
398 {
399 sb.append(attr.getName());
400 }
401 }
402 return sb.toString();
403 }
404
405
406
407
408
409
410 protected Collection handleGetConstants(boolean follow)
411 {
412 return EJB3MetafacadeUtils.getConstants(this, follow);
413 }
414
415
416
417
418 public Collection<DependencyFacade> getServiceReferences()
419 {
420 Collection<DependencyFacade> references = super.getServiceReferences();
421 CollectionUtils.filter(
422 references,
423 new Predicate()
424 {
425 public boolean evaluate(Object object)
426 {
427 ModelElementFacade targetElement = ((DependencyFacade)object).getTargetElement();
428 return targetElement.hasStereotype(EJB3Profile.STEREOTYPE_SERVICE);
429 }
430 });
431 return references;
432 }
433
434
435
436
437 @Override
438 protected boolean handleIsDestinationTypeQueue()
439 {
440 boolean typeQueue = false;
441 if (StringUtils.equalsIgnoreCase(this.getDestinationType(), EJB3Globals.MDB_DESTINATION_TYPE_QUEUE))
442 {
443 typeQueue = true;
444 }
445 return typeQueue;
446 }
447
448
449
450
451 @Override
452 protected boolean handleIsDestinationTypeTopic()
453 {
454 boolean typeTopic = false;
455 if (StringUtils.equalsIgnoreCase(this.getDestinationType(), EJB3Globals.MDB_DESTINATION_TYPE_TOPIC))
456 {
457 typeTopic = true;
458 }
459 return typeTopic;
460 }
461
462
463
464
465 protected Collection handleGetEnvironmentEntries(boolean follow)
466 {
467 return EJB3MetafacadeUtils.getEnvironmentEntries(this, follow);
468 }
469
470
471
472
473 protected int handleGetMinimumPoolSize()
474 {
475 int minPoolSize = 0;
476 String minPoolSizeStr = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_MINIMUM_POOL_SIZE);
477 if (StringUtils.isNotBlank(minPoolSizeStr) && NumberUtils.isDigits(minPoolSizeStr))
478 {
479 minPoolSize = Integer.parseInt(minPoolSizeStr);
480 }
481 return minPoolSize;
482 }
483
484
485
486
487 protected int handleGetMaximumPoolSize()
488 {
489 int maxPoolSize = 0;
490 String maxPoolSizeStr = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_MDB_MAXIMUM_POOL_SIZE);
491 if (StringUtils.isNotBlank(maxPoolSizeStr) && NumberUtils.isDigits(maxPoolSizeStr))
492 {
493 maxPoolSize = Integer.parseInt(maxPoolSizeStr);
494 }
495 return maxPoolSize;
496 }
497
498
499
500
501 @Override
502 protected boolean handleIsListenerEnabled()
503 {
504 return this.hasStereotype(EJB3Profile.STEREOTYPE_LISTENER);
505 }
506
507
508
509
510 protected Collection<DependencyFacade> handleGetInterceptorReferences()
511 {
512 Collection<DependencyFacade> references = this.getSourceDependencies();
513 CollectionUtils.filter(
514 references,
515 new Predicate()
516 {
517 public boolean evaluate(Object object)
518 {
519 DependencyFacade dependency = (DependencyFacade)object;
520 ModelElementFacade targetElement = dependency.getTargetElement();
521 return (targetElement != null && targetElement.hasStereotype(EJB3Profile.STEREOTYPE_INTERCEPTOR));
522 }
523 });
524 CollectionUtils.transform(
525 references,
526 new Transformer()
527 {
528 public Object transform(final Object object)
529 {
530 return ((DependencyFacade)object).getTargetElement();
531 }
532 });
533 final Collection<DependencyFacade> interceptors = new LinkedHashSet<DependencyFacade>(references);
534 CollectionUtils.forAllDo(
535 references,
536 new Closure()
537 {
538 public void execute(Object object)
539 {
540 if (object instanceof EJB3InterceptorFacade)
541 {
542 interceptors.addAll(((EJB3InterceptorFacade)object).getInterceptorReferences());
543 }
544 }
545 });
546 return interceptors;
547 }
548
549
550
551
552 @Override
553 protected boolean handleIsExcludeDefaultInterceptors()
554 {
555 boolean excludeDefault = false;
556 String excludeDefaultStr =
557 (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_SERVICE_INTERCEPTOR_EXCLUDE_DEFAULT);
558 if (excludeDefaultStr != null)
559 {
560 excludeDefault = BooleanUtils.toBoolean(excludeDefaultStr);
561 }
562 return excludeDefault;
563 }
564 }