001: package org.andromda.cartridges.spring.metafacades;
002:
003: import java.text.MessageFormat;
004:
005: import java.util.Collection;
006:
007: import org.andromda.cartridges.spring.SpringProfile;
008: import org.andromda.metafacades.uml.ClassifierFacade;
009: import org.andromda.metafacades.uml.FilteredCollection;
010: import org.andromda.metafacades.uml.OperationFacade;
011: import org.andromda.metafacades.uml.UMLProfile;
012: import org.apache.commons.collections.Closure;
013: import org.apache.commons.collections.CollectionUtils;
014: import org.apache.commons.lang.BooleanUtils;
015: import org.apache.commons.lang.ObjectUtils;
016: import org.apache.commons.lang.StringUtils;
017:
018: /**
019: * MetafacadeLogic implementation for
020: * org.andromda.cartridges.spring.metafacades.SpringService.
021: *
022: * @author Chad Brandon
023: * @author Peter Friese
024: * @see org.andromda.cartridges.spring.metafacades.SpringService
025: */
026: public class SpringServiceLogicImpl extends SpringServiceLogic {
027: public SpringServiceLogicImpl(Object metaObject, String context) {
028: super (metaObject, context);
029: }
030:
031: /**
032: * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbJndiName()
033: */
034: protected java.lang.String handleGetEjbJndiName() {
035: StringBuffer jndiName = new StringBuffer();
036: String jndiNamePrefix = StringUtils.trimToEmpty(this
037: .getEjbJndiNamePrefix());
038: if (StringUtils.isNotEmpty(jndiNamePrefix)) {
039: jndiName.append(jndiNamePrefix);
040: jndiName.append("/");
041: }
042: jndiName.append("ejb/");
043: jndiName.append(this .getFullyQualifiedName());
044: return jndiName.toString();
045: }
046:
047: protected String handleGetEjbLocalJndiName() {
048: StringBuffer jndiName = new StringBuffer();
049: String jndiNamePrefix = StringUtils.trimToEmpty(this
050: .getEjbJndiNamePrefix());
051: if (StringUtils.isNotEmpty(jndiNamePrefix)) {
052: jndiName.append(jndiNamePrefix);
053: jndiName.append("/");
054: }
055: jndiName.append("ejb/");
056: jndiName.append(SpringMetafacadeUtils.getFullyQualifiedName(
057: this .getPackageName(), this .getName(), (this
058: .getEjbViewType().equalsIgnoreCase(
059: EJB_BOTH_VIEW) ? "Local" : null)));
060: return jndiName.toString();
061: }
062:
063: /**
064: * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbImplementationName()
065: */
066: protected java.lang.String handleGetEjbImplementationName() {
067: return this .getName() + SpringGlobals.EJB_IMPLEMENTATION_SUFFIX;
068: }
069:
070: /**
071: * @see org.andromda.cartridges.spring.metafacades.SpringService#getImplementationName()
072: */
073: protected java.lang.String handleGetImplementationName() {
074: return this .getName() + SpringGlobals.IMPLEMENTATION_SUFFIX;
075: }
076:
077: /**
078: * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedImplementationName()
079: */
080: protected java.lang.String handleGetFullyQualifiedEjbImplementationName() {
081: return SpringMetafacadeUtils.getFullyQualifiedName(this
082: .getEjbPackageName(), this .getName(),
083: SpringGlobals.EJB_IMPLEMENTATION_SUFFIX);
084: }
085:
086: /**
087: * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedEjbName()
088: */
089: protected java.lang.String handleGetFullyQualifiedEjbName() {
090: return SpringMetafacadeUtils.getFullyQualifiedName(this
091: .getEjbPackageName(), this .getName(), null);
092: }
093:
094: /**
095: * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedLocalEjbName()
096: */
097: protected String handleGetFullyQualifiedLocalEjbName() {
098: //add "Local" to local ejb name when viewtype = "both",
099: //to prevent name clashing with remote interface naming
100: return SpringMetafacadeUtils
101: .getFullyQualifiedName(this .getEjbPackageName(), this
102: .getName(), (this .getEjbViewType()
103: .equalsIgnoreCase(EJB_BOTH_VIEW) ? "Local"
104: : null));
105: }
106:
107: /**
108: * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedImplementationName()
109: */
110: protected java.lang.String handleGetFullyQualifiedImplementationName() {
111: return SpringMetafacadeUtils.getFullyQualifiedName(this
112: .getPackageName(), this .getName(),
113: SpringGlobals.IMPLEMENTATION_SUFFIX);
114: }
115:
116: /**
117: * @see org.andromda.cartridges.spring.metafacades.SpringService#getBaseName()
118: */
119: protected java.lang.String handleGetBaseName() {
120: return this .getName() + SpringGlobals.SERVICE_BASE_SUFFIX;
121: }
122:
123: /**
124: * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedBaseName()
125: */
126: protected java.lang.String handleGetFullyQualifiedBaseName() {
127: return SpringMetafacadeUtils.getFullyQualifiedName(this
128: .getPackageName(), this .getName(),
129: SpringGlobals.SERVICE_BASE_SUFFIX);
130: }
131:
132: /**
133: * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbPackageName()
134: */
135: protected java.lang.String handleGetEjbPackageName() {
136: String ejbPackageName = MessageFormat.format(this
137: .getEjbPackageNamePattern(), new Object[] { StringUtils
138: .trimToEmpty(this .getPackageName()) });
139: if (StringUtils.isBlank(this .getPackageName())) {
140: ejbPackageName = ejbPackageName.replaceAll("^\\.", "");
141: }
142: return ejbPackageName;
143: }
144:
145: /**
146: * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbPackageNamePath()
147: */
148: protected java.lang.String handleGetEjbPackageNamePath() {
149: return this .getEjbPackageName().replace('.', '/');
150: }
151:
152: /**
153: * @see org.andromda.cartridges.spring.metafacades.SpringService#getBeanName()
154: */
155: protected java.lang.String handleGetBeanName() {
156: return this .getBeanName(false);
157: }
158:
159: /**
160: * @see org.andromda.cartridges.spring.metafacades.SpringService#getBeanName(boolean)
161: */
162: protected java.lang.String handleGetBeanName(boolean targetSuffix) {
163: StringBuffer beanName = new StringBuffer(StringUtils
164: .uncapitalize(StringUtils.trimToEmpty(this .getName())));
165: if (targetSuffix) {
166: beanName.append(SpringGlobals.BEAN_NAME_TARGET_SUFFIX);
167: }
168: return beanName.toString();
169: }
170:
171: /**
172: * Gets the <code>ejbPackageNamePattern</code> for this EJB.
173: *
174: * @return the defined package pattern.
175: */
176: protected String getEjbPackageNamePattern() {
177: return (String) this
178: .getConfiguredProperty(SpringGlobals.EJB_PACKAGE_NAME_PATTERN);
179: }
180:
181: /**
182: * Gets the <code>ejbJndiNamePrefix</code> for this EJB.
183: *
184: * @return the EJB Jndi name prefix.
185: */
186: protected String getEjbJndiNamePrefix() {
187: return this
188: .isConfiguredProperty(SpringGlobals.EJB_JNDI_NAME_PREFIX) ? ObjectUtils
189: .toString(this
190: .getConfiguredProperty(SpringGlobals.EJB_JNDI_NAME_PREFIX))
191: : null;
192: }
193:
194: /**
195: * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceDelegatorName()
196: */
197: protected String handleGetWebServiceDelegatorName() {
198: return this .getName()
199: + SpringGlobals.WEB_SERVICE_DELEGATOR_SUFFIX;
200: }
201:
202: /**
203: * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedWebServiceDelegatorName()
204: */
205: protected String handleGetFullyQualifiedWebServiceDelegatorName() {
206: return this .getFullyQualifiedName()
207: + SpringGlobals.WEB_SERVICE_DELEGATOR_SUFFIX;
208: }
209:
210: /**
211: * @see org.andromda.cartridges.spring.metafacades.SpringService#isWebService()
212: */
213: protected boolean handleIsWebService() {
214: boolean webService = this
215: .hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE);
216: if (!webService) {
217: webService = !this .getWebServiceOperations().isEmpty();
218: }
219: return webService;
220: }
221:
222: /**
223: * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleGetInterceptors()
224: */
225: protected String[] handleGetInterceptors() {
226: String serviceInterceptorString = this
227: .isConfiguredProperty(SpringGlobals.SERVICE_INTERCEPTORS) ? ObjectUtils
228: .toString(this
229: .getConfiguredProperty(SpringGlobals.SERVICE_INTERCEPTORS))
230: : null;
231: String[] interceptors = null;
232: if (StringUtils.isNotEmpty(serviceInterceptorString)) {
233: interceptors = serviceInterceptorString.split(",");
234: }
235: return SpringMetafacadeUtils.getServiceInterceptors(this ,
236: interceptors);
237: }
238:
239: /**
240: * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotable()
241: */
242: protected boolean handleIsRemotable() {
243: return !this .isRemotingTypeNone();
244: }
245:
246: /**
247: * Gets the remoting type for this service.
248: */
249: private String getRemotingType() {
250: final String serviceRemotingType = StringUtils
251: .trimToEmpty(String
252: .valueOf(this
253: .getConfiguredProperty(SpringGlobals.SERVICE_REMOTING_TYPE)));
254: return SpringMetafacadeUtils.getServiceRemotingType(this ,
255: serviceRemotingType);
256: }
257:
258: /**
259: * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleGetRemoteServer()
260: */
261: protected String handleGetRemoteServer() {
262: return StringUtils
263: .trimToEmpty(String
264: .valueOf(this
265: .getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_SERVER)));
266: }
267:
268: /**
269: * @see org.andromda.cartridges.spring.metafacades.SpringService#getRemotePort()
270: */
271: protected String handleGetRemotePort() {
272: String serviceRemotePort = StringUtils
273: .trimToEmpty(String
274: .valueOf(this
275: .getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_PORT)));
276: return SpringMetafacadeUtils.getServiceRemotePort(this ,
277: serviceRemotePort);
278: }
279:
280: /**
281: * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleGetRemoteContext()
282: */
283: protected String handleGetRemoteContext() {
284: return this
285: .isConfiguredProperty(SpringGlobals.SERVICE_REMOTE_CONTEXT) ? ObjectUtils
286: .toString(this
287: .getConfiguredProperty(SpringGlobals.SERVICE_REMOTE_CONTEXT))
288: : "";
289: }
290:
291: /**
292: * Checks whether this service has a remote port assigned.
293: *
294: * @return <code>true</code> if the service has a remote port, <code>false</code> otherwise.
295: */
296: private boolean hasServiceRemotePort() {
297: final String serviceRemotePort = this .getRemotePort();
298: return StringUtils.isNotEmpty(serviceRemotePort);
299: }
300:
301: /**
302: * Checks whether the service has a remote context assigned.
303: *
304: * @return <code>true</code> if the service has a remote context, <code>false</code> otherweise.
305: */
306: private boolean hasServiceRemoteContext() {
307: final String serviceRemoteContext = this .getRemoteContext();
308: return StringUtils.isNotEmpty(serviceRemoteContext);
309: }
310:
311: /**
312: * @see org.andromda.cartridges.spring.metafacades.SpringService#getRemoteUrl()
313: */
314: protected String handleGetRemoteUrl() {
315: String result = "";
316:
317: String propertyPrefix = ObjectUtils
318: .toString(this
319: .getConfiguredProperty(SpringGlobals.CONFIG_PROPERTY_PREFIX));
320:
321: if (this .isRemotingTypeNone()) {
322: // nothing
323: } else if (this .isRemotingTypeHttpInvoker()
324: || this .isRemotingTypeHessian()
325: || this .isRemotingTypeBurlap()) {
326: // server
327: result = "http://${" + propertyPrefix + "remoteServer}";
328:
329: // port
330: if (hasServiceRemotePort()) {
331: result += ":${" + propertyPrefix + "remotePort}";
332: }
333:
334: // context
335: if (hasServiceRemoteContext()) {
336: result += "/${" + propertyPrefix + "remoteContext}";
337: }
338:
339: // service name
340: result += "/" + getName();
341: } else if (this .isRemotingTypeRmi()) {
342: // server
343: result = "rmi://${" + propertyPrefix + "remoteServer}";
344:
345: // port
346: if (hasServiceRemotePort()) {
347: result += ":${" + propertyPrefix + "remotePort}";
348: }
349:
350: // service name
351: result += "/" + getName();
352: }
353: return result;
354: }
355:
356: /**
357: * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceOperations()
358: */
359: protected Collection handleGetWebServiceOperations() {
360: Collection operations = this .getOperations();
361: if (!this .hasStereotype(UMLProfile.STEREOTYPE_WEBSERVICE)) {
362: operations = new FilteredCollection(operations) {
363: public boolean evaluate(Object object) {
364: return object instanceof SpringServiceOperation
365: && ((SpringServiceOperation) object)
366: .isWebserviceExposed();
367: }
368: };
369: }
370: return operations;
371: }
372:
373: /**
374: * Override to retrieve any abstract operations from an abstract
375: * generalization.
376: *
377: * @see org.andromda.metafacades.uml.ClassifierFacade#getOperations()
378: */
379: public Collection getOperations() {
380: final Collection operations = super .getOperations();
381: if (!this .isAbstract()) {
382: for (ClassifierFacade generalization = (ClassifierFacade) this
383: .getGeneralization(); generalization != null; generalization = (ClassifierFacade) generalization
384: .getGeneralization()) {
385: if (generalization.isAbstract()) {
386: CollectionUtils.forAllDo(generalization
387: .getOperations(), new Closure() {
388: public void execute(Object object) {
389: if (((OperationFacade) object).isAbstract()) {
390: operations.add(object);
391: }
392: }
393: });
394: }
395: }
396: }
397: return operations;
398: }
399:
400: /**
401: * @see org.andromda.cartridges.spring.metafacades.SpringService#getDefaultExceptionName()
402: */
403: protected String handleGetDefaultExceptionName() {
404: String name = StringUtils
405: .trimToEmpty(String
406: .valueOf(this
407: .getConfiguredProperty(SpringGlobals.DEFAULT_SERVICE_EXCEPTION_NAME_PATTERN)));
408: return name.replaceAll("\\{0\\}", this .getName());
409: }
410:
411: /**
412: * @see org.andromda.cartridges.spring.metafacades.SpringService#getFullyQualifiedDefaultExceptionName()
413: */
414: protected String handleGetFullyQualifiedDefaultExceptionName() {
415: StringBuffer fullyQualifiedName = new StringBuffer(
416: "java.lang.RuntimeException");
417: if (this .isAllowDefaultServiceException()) {
418: fullyQualifiedName = new StringBuffer();
419: if (StringUtils.isNotBlank(this .getPackageName())) {
420: fullyQualifiedName.append(this .getPackageName());
421: fullyQualifiedName.append('.');
422: }
423: fullyQualifiedName.append(this .getDefaultExceptionName());
424: }
425: return fullyQualifiedName.toString();
426: }
427:
428: /**
429: * @see org.andromda.cartridges.spring.metafacades.SpringService#isAllowDefaultServiceException()
430: */
431: protected boolean handleIsAllowDefaultServiceException() {
432: return Boolean
433: .valueOf(
434: String
435: .valueOf(this
436: .getConfiguredProperty(SpringGlobals.DEFAULT_SERVICE_EXCEPTIONS)))
437: .booleanValue();
438: }
439:
440: /**
441: * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeRmi()
442: */
443: protected boolean handleIsRemotingTypeRmi() {
444: return this .getRemotingType().equalsIgnoreCase(
445: SpringGlobals.REMOTING_PROTOCOL_RMI);
446: }
447:
448: /**
449: * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeBurlap()
450: */
451: protected boolean handleIsRemotingTypeBurlap() {
452: return this .getRemotingType().equalsIgnoreCase(
453: SpringGlobals.REMOTING_PROTOCOL_BURLAP);
454: }
455:
456: /**
457: * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeHessian()
458: */
459: protected boolean handleIsRemotingTypeHessian() {
460: return this .getRemotingType().equalsIgnoreCase(
461: SpringGlobals.REMOTING_PROTOCOL_HESSIAN);
462: }
463:
464: /**
465: * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeHttpInvoker()
466: */
467: protected boolean handleIsRemotingTypeHttpInvoker() {
468: return this .getRemotingType().equalsIgnoreCase(
469: SpringGlobals.REMOTING_PROTOCOL_HTTPINVOKER);
470: }
471:
472: /**
473: * @see org.andromda.cartridges.spring.metafacades.SpringService#isRemotingTypeNone()
474: */
475: protected boolean handleIsRemotingTypeNone() {
476: return this .getRemotingType().equalsIgnoreCase(
477: SpringGlobals.REMOTING_PROTOCOL_NONE);
478: }
479:
480: /**
481: * Stores the namespace property indicating whether or not the hibernate
482: * interceptor is enabled for this service.
483: */
484: private static final String HIBERNATE_INTERCEPTOR_ENABLED = "serviceHibernateInterceptorEnabled";
485:
486: /**
487: * @see org.andromda.cartridges.spring.metafacades.SpringService#isHibernateInterceptorEnabled()
488: */
489: protected boolean handleIsHibernateInterceptorEnabled() {
490: return Boolean
491: .valueOf(
492: String
493: .valueOf(this
494: .getConfiguredProperty(HIBERNATE_INTERCEPTOR_ENABLED)))
495: .booleanValue();
496: }
497:
498: /**
499: * Stores the view type for an EJB service.
500: */
501: private static final String EJB_VIEW_TYPE = "ejbViewType";
502:
503: /**
504: * Gets the view for this service (if wrapped by an EJB).
505: */
506: private String getEjbViewType() {
507: Object value = this
508: .findTaggedValue(SpringProfile.TAGGEDVALUE_EJB_VIEW_TYPE);
509: if (value == null) {
510: value = this .getConfiguredProperty(EJB_VIEW_TYPE);
511: }
512: return ObjectUtils.toString(value);
513: }
514:
515: /**
516: * The three EJB view type values.
517: */
518: private static final String EJB_REMOTE_VIEW = "remote";
519: private static final String EJB_LOCAL_VIEW = "local";
520: private static final String EJB_BOTH_VIEW = "both";
521:
522: /**
523: * @see org.andromda.cartridges.spring.metafacades.SpringService#isEjbRemoteView()
524: */
525: protected boolean handleIsEjbRemoteView() {
526: return (this .getEjbViewType().equalsIgnoreCase(EJB_REMOTE_VIEW) || this
527: .getEjbViewType().equalsIgnoreCase(EJB_BOTH_VIEW));
528: }
529:
530: /**
531: * @see org.andromda.cartridges.spring.metafacades.SpringService#isEjbLocalView()
532: */
533: protected boolean handleIsEjbLocalView() {
534: return (this .getEjbViewType().equalsIgnoreCase(EJB_LOCAL_VIEW) || this
535: .getEjbViewType().equalsIgnoreCase(EJB_BOTH_VIEW));
536: }
537:
538: /**
539: * @see org.andromda.cartridges.spring.metafacades.SpringService#getEjbTransactionType()
540: */
541: protected String handleGetEjbTransactionType() {
542: String transactionType;
543: final boolean ejbTransactionsEnabled = BooleanUtils
544: .toBoolean(ObjectUtils
545: .toString(this
546: .getConfiguredProperty(SpringGlobals.EJB_TRANSACTIONS_ENABLED)));
547: if (ejbTransactionsEnabled) {
548: transactionType = "Container";
549: } else {
550: transactionType = "Bean";
551: }
552: return transactionType;
553: }
554:
555: /**
556: * @see org.andromda.cartridges.spring.metafacades.SpringServiceLogic#handleIsConfigonly()
557: */
558: protected boolean handleIsConfigonly() {
559: String value = (String) this
560: .findTaggedValue(SpringProfile.TAGGEDVALUE_SERVICE_CONFIG_ONLY);
561: return BooleanUtils.toBoolean(StringUtils.trimToEmpty(value));
562: }
563:
564: /**
565: * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceOutgoingAttachmentHandlerCall()
566: */
567: protected String handleGetWebServiceOutgoingAttachmentHandlerCall() {
568: return this
569: .getWebServiceAttachmentHandlerCall(SpringGlobals.WEBSERVICE_OUTGOING_ATTACHMENT_HANDLER_CALL_PATTERN);
570: }
571:
572: /**
573: * @see org.andromda.cartridges.spring.metafacades.SpringService#getWebServiceIncomingAttachmentHandlerCall()
574: */
575: protected String handleGetWebServiceIncomingAttachmentHandlerCall() {
576: return this
577: .getWebServiceAttachmentHandlerCall(SpringGlobals.WEBSERVICE_INCOMING_ATTACHMENT_HANDLER_CALL_PATTERN);
578: }
579:
580: /**
581: * Constructs the webservice attachment handler call or returns null if
582: * one isn't found or is not appropriate it for the service (i.e. it isn't
583: * a webservice).
584: *
585: * @param patternProperty the property defining the pattern type.
586: * @return the call value.
587: */
588: private String getWebServiceAttachmentHandlerCall(
589: final String patternProperty) {
590: String call = null;
591: if (this .isWebService()) {
592: final String value = ObjectUtils.toString(this
593: .getConfiguredProperty(patternProperty));
594: if (StringUtils.isNotBlank(value)) {
595: call = value;
596: }
597: }
598: return call;
599: }
600:
601: /**
602: * @see org.andromda.cartridges.spring.metafacades.SpringService#isRichClient()
603: */
604: protected boolean handleIsRichClient() {
605: final String richClient = StringUtils
606: .trimToEmpty(String
607: .valueOf(this
608: .getConfiguredProperty(SpringGlobals.RICH_CLIENT)));
609: return richClient.equalsIgnoreCase("true");
610: }
611: }
|