Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9955fcd3b2e2c8e219720999d57b0cd56fc69c48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.j2ee.model.internal.validation;

import java.util.HashSet;

import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.java.JavaHelpers;
import org.eclipse.jem.java.JavaParameter;
import org.eclipse.jem.java.Method;
import org.eclipse.jst.j2ee.ejb.EnterpriseBean;
import org.eclipse.jst.j2ee.ejb.Entity;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;


/**
 * This class checks entity home classes for errors or potential errors.
 * If any problems are found, an error, warning, or info marker is added to the task list.
 *
 * The following paragraph is taken from
 * Enterprise JavaBeans Specification ("Specification")
 * Version: 1.1
 * Status: Final Release
 * Release: 12/17/99
 * Copyright 1999 Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, CA 94303, U.S.A.
 * All rights reserved.
 *
 * 9.2.8 Entity bean's home interface
 * The following are the requirements for the entity bean's home interface:
 *   - The interface must extend the javax.ejb.EJBHome interface.
 *   - The methods defined in this interface must follow the rules for RMI-IIOP. 
 *     This means that their argument and return types must be of valid types for 
 *     RMI-IIOP, and that their throws clause must include the java.rmi.RemoteException.
 *   - The home interface is allowed to have superinterfaces. Use of interface 
 *     inheritance is subject to the RMI-IIOP rules for the definition of remote interfaces.
 *   - Each method defined in the home interface must be one of the following:
 *      - A create method.
 *      - A finder method.
 *   - Each create method must be named "create", and it must match one of the 
 *     ejbCreate methods defined in the enterprise Bean class. The matching 
 *     ejbCreate method must have the same number and types of its arguments. 
 *     (Note that the return type is different.)
 *   - The return type for a create method must be the entity bean's remote interface type.
 *   - All the exceptions defined in the throws clause of the matching ejbCreate 
 *     and ejbPostCreate methods of the enterprise Bean class must be included in 
 *     the throws clause of the matching create method of the home interface 
 *     (i.e the set of exceptions defined for the create method must be a superset
 *     of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
 *   - The throws clause of a create method must include the javax.ejb.CreateException.
 *   - Each finder method must be named "find<METHOD>" (e.g. findLargeAccounts), and it
 *     must match one of the ejbFind<METHOD> methods defined in the entity bean class 
 *     (e.g. ejbFindLargeAccounts). The matching ejbFind<METHOD> method must have the 
 *     same number and types of arguments. (Note that the return type may be different.)
 *   - The return type for a find<METHOD> method must be the entity bean's remote 
 *     interface type (for a single-object finder), or a collection thereof (for a 
 *     multi-object finder).
 *   - The home interface must always include the findByPrimaryKey method, which is 
 *     always a single-object finder. The method must declare the primary key class 
 *     as the method argument.
 *   - All the exceptions defined in the throws clause of an ejbFind method of the 
 *     entity bean class must be included in the throws clause of the matching find 
 *     method of the home interface.
 *   - The throws clause of a finder method must include the javax.ejb.FinderException. 
 */
public abstract class AValidateEntityHome extends AValidateHome {
	private HashSet findByPKMethods = null;

	public AValidateEntityHome() {
		findByPKMethods = new HashSet();
	}

	public void reset() {
		super.reset();
		findByPKMethods.clear();
	}

	protected void incrementFindByPrimaryKeyCount(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) {
		if (method == null) {
			return;
		}

		// Need to track number of findByPrimaryKey methods for the
		// validateMethodExists() checks. Do not check only the current
		// class, or a findByPrimaryKey which is inherited from a parent interface
		// will not be detected.
		if (method.getName().equals(IMethodAndFieldConstants.METHODNAME_FINDBYPRIMARYKEY)) {
			findByPKMethods.add(method);
		}
	}
	
	/**
	 * This method returns true if the given method returns the remote interface,
	 * and false otherwise.
	 */
	public boolean isSingleObjectFinder(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();
		if (method == null) {
			return false;
		}

		// The findByPrimaryKey method is always a single-object finder.
		JavaClass remoteIntf = bean.getRemoteInterface();
		ValidationRuleUtility.isValidTypeHierarchy(bean, remoteIntf);

		if (!ValidationRuleUtility.isAssignableFrom(method.getReturnType(), remoteIntf)) {
			return false;
		}

		// The method is a single-object finder.
		return true;
	}
	
	/**
	 * This method checks that the entity home's methods comply with the EJB 1.1 specification.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *...
	 *   - The methods defined in this interface must follow the rules for RMI-IIOP. 
	 *     This means that their argument and return types must be of valid types for 
	 *     RMI-IIOP, and that their throws clause must include the java.rmi.RemoteException.
	 *...
	 *   - Each method defined in the home interface must be one of the following:
	 *      - A create method.
	 *      - A finder method.
	 *   - Each create method must be named "create", and it must match one of the 
	 *     ejbCreate methods defined in the enterprise Bean class. The matching 
	 *     ejbCreate method must have the same number and types of its arguments. 
	 *     (Note that the return type is different.)
	 *   - The return type for a create method must be the entity bean's remote interface type.
	 *   - All the exceptions defined in the throws clause of the matching ejbCreate 
	 *     and ejbPostCreate methods of the enterprise Bean class must be included in 
	 *     the throws clause of the matching create method of the home interface 
	 *     (i.e the set of exceptions defined for the create method must be a superset
	 *     of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
	 *   - The throws clause of a create method must include the javax.ejb.CreateException.
	 *   - Each finder method must be named "find<METHOD>" (e.g. findLargeAccounts), and it
	 *     must match one of the ejbFind<METHOD> methods defined in the entity bean class 
	 *     (e.g. ejbFindLargeAccounts). The matching ejbFind<METHOD> method must have the 
	 *     same number and types of arguments. (Note that the return type may be different.)
	 *   - The return type for a find<METHOD> method must be the entity bean's remote 
	 *     interface type (for a single-object finder), or a collection thereof (for a 
	 *     multi-object finder).
	 *   - The home interface must always include the findByPrimaryKey method, which is 
	 *     always a single-object finder. The method must declare the primary key class 
	 *     as the method argument.
	 *   - All the exceptions defined in the throws clause of an ejbFind method of the 
	 *     entity bean class must be included in the throws clause of the matching find 
	 *     method of the home interface.
	 *   - The throws clause of a finder method must include the javax.ejb.FinderException. 
	 */
	public void primValidate(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method hiMethod) throws InvalidInputException {
		// Can't invoke an abstract method
		// super.primValidate(hiMethod);

		vc.terminateIfCancelled();

		String name = hiMethod.getName();

		// Each method defined in the home interface must be one of the following:
		//   - A create method.
		//   - A finder method.
		if (name.equals(IMethodAndFieldConstants.METHODNAME_CREATE))
			validateCreateMethod(vc, bean, clazz, hiMethod);
		else if (name.startsWith(IMethodAndFieldConstants.PREFIX_FIND))
			validateFindMethod(vc, bean, clazz, hiMethod);
		else {
			IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2005, IEJBValidationContext.ERROR, bean, clazz, hiMethod, this);
			vc.addMessage(message);
		}
	}
	
	/**
	 * Checks to see if @ejbMethod is one of the required methods.
	 */
	protected void primValidateExistence(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method ejbMethod) throws InvalidInputException {
		// Can't invoke an abstract method
		//super.validateExistence(ejbMethod);

		if (IMethodAndFieldConstants.METHODNAME_FINDBYPRIMARYKEY.equals(ejbMethod.getName()))
			incrementFindByPrimaryKeyCount(vc, bean, clazz, ejbMethod);
	}
	
	/**
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *   - The interface must extend the javax.ejb.EJBHome interface.
	 *...
	 *   - The home interface is allowed to have superinterfaces. Use of interface 
	 *     inheritance is subject to the RMI-IIOP rules for the definition of remote interfaces.
	 *...
	 */
	public void validateClass(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz) throws InvalidInputException {
		super.validateClass(vc, bean, clazz);
		// The superclass checks that the home extends javax.ejb.EJBHome,
		// and that the interface inheritance complies with RMI-IIOP rules.
	}
	
	/**
	 * Checks that the create method on the entity home follows the EJB 1.1. specification.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *   - Each create method must be named "create", and it must match one of the 
	 *     ejbCreate methods defined in the enterprise Bean class. The matching 
	 *     ejbCreate method must have the same number and types of its arguments. 
	 *     (Note that the return type is different.)
	 *   - The return type for a create method must be the entity bean's remote interface type.
	 *   - All the exceptions defined in the throws clause of the matching ejbCreate 
	 *     and ejbPostCreate methods of the enterprise Bean class must be included in 
	 *     the throws clause of the matching create method of the home interface 
	 *     (i.e the set of exceptions defined for the create method must be a superset
	 *     of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
	 */
	public void validateCreateMethod(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();
		if (method == null) {
			return;
		}

		// Each create method must be named "create", and it must match one of the 
		// ejbCreate methods defined in the enterprise Bean class. The matching 
		// ejbCreate method must have the same number and types of its arguments. 
		// (Note that the return type is different.)
		//
		// All the exceptions defined in the throws clause of the matching ejbCreate 
		// and ejbPostCreate methods of the enterprise Bean class must be included in 
		// the throws clause of the matching create method of the home interface 
		// (i.e the set of exceptions defined for the create method must be a superset
		// of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
		validateCreateMethod_beanDep(vc, bean, clazz, method);

		vc.terminateIfCancelled();

		// The return type for a create method must be the entity bean's remote interface type.
		validateCreateMethod_remoteDep(vc, bean, clazz, method);

		// The throws clause of a create method must include the javax.ejb.CreateException.
		if (!ValidationRuleUtility.throwsCreateException(bean, method)) {
			String[] msgParm = { ITypeConstants.CLASSNAME_JAVAX_EJB_CREATEEXCEPTION };
			IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2414, IEJBValidationContext.ERROR, bean, clazz, method, msgParm, this);
			vc.addMessage(message);
		}

		// The methods defined in this interface must follow the rules for RMI-IIOP. 
		// This means that their argument and return types must be of valid types for 
		// RMI-IIOP, and that their throws clause must include the java.rmi.RemoteException.
		validateLegalRMIMethod(vc, bean, clazz, method);

	}
	
	/**
	 * Checks that the create method on the entity home follows the EJB 1.1. specification.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *   - Each create method must be named "create", and it must match one of the 
	 *     ejbCreate methods defined in the enterprise Bean class. The matching 
	 *     ejbCreate method must have the same number and types of its arguments. 
	 *     (Note that the return type is different.)
	 *   - The return type for a create method must be the entity bean's remote interface type.
	 *   - All the exceptions defined in the throws clause of the matching ejbCreate 
	 *     and ejbPostCreate methods of the enterprise Bean class must be included in 
	 *     the throws clause of the matching create method of the home interface 
	 *     (i.e the set of exceptions defined for the create method must be a superset
	 *     of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
	 */
	public void validateCreateMethod_beanDep(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();
		if (method == null) {
			return;
		}

		// Each create method must be named "create", and it must match one of the 
		// ejbCreate methods defined in the enterprise Bean class. The matching 
		// ejbCreate method must have the same number and types of its arguments. 
		// (Note that the return type is different.)
		super.validateCreateMethod_beanDep(vc, bean, clazz, method);

		//
		// All the exceptions defined in the throws clause of the matching ejbCreate 
		// and ejbPostCreate methods of the enterprise Bean class must be included in 
		// the throws clause of the matching create method of the home interface 
		// (i.e the set of exceptions defined for the create method must be a superset
		// of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
		validateMatchingBeanPostCreateMethod(vc, bean, clazz, method);
	}
	
	/**
	 * In addition to regular find rules, findByPrimaryKey needs to follow some other rules.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *...
	 *   - The home interface must always include the findByPrimaryKey method, which is 
	 *     always a single-object finder. The method must declare the primary key class 
	 *     as the method argument.
	 */
	public void validateFindByPrimaryKeyMethod_keyDep(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();
		if (method == null) {
			return;
		}

		boolean hasValidFBPKMethod = false;
		Entity entityBean = (Entity) bean; // bean is checked for null in AValidateEJB.validate() method, so don't need to check for it here.
		JavaClass keyClass = entityBean.getPrimaryKey();
		
		ValidationRuleUtility.isValidTypeHierarchy(bean, keyClass);

		vc.terminateIfCancelled();

		// The method must declare the primary key class as the method argument.
		// This check also validates case 9.4.7.3 (CMP unknown primary key class,
		// which must be declared as java.lang.Object.)
		JavaParameter[] parms = method.listParametersWithoutReturn();
		if (parms.length == 1) {
			JavaHelpers type = parms[0].getJavaType();
			if (ValidationRuleUtility.isAssignableFrom(type, keyClass)) {
				// check that it's a single-object finder
				if (isSingleObjectFinder(vc, bean, clazz, method)) {
					hasValidFBPKMethod = true;
				}
			}
			else {
				// check if it's java.lang.Object. If it is, this could be a case of 9.4.7.3.
				if (ValidationRuleUtility.isSameType(type, ValidationRuleUtility.getType(ITypeConstants.CLASSNAME_JAVA_LANG_OBJECT, entityBean))) {
					hasValidFBPKMethod = true;
				}
			}
		}

		if (!hasValidFBPKMethod) {
			IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2405, IEJBValidationContext.WARNING, bean, clazz, method, new String[] { keyClass.getQualifiedName()}, this);
			vc.addMessage(message);
		}
	}
	
	/**
	 * In addition to regular find rules, findByPrimaryKey needs to follow some other rules.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *...
	 *   - The home interface must always include the findByPrimaryKey method, which is 
	 *     always a single-object finder. The method must declare the primary key class 
	 *     as the method argument.
	 */
	public void validateFindByPrimaryKeyMethod_remoteDep(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();
		if (!isSingleObjectFinder(vc, bean, clazz, method)) {
			String remoteIntfName = bean.getRemoteInterface().getName(); // Can assume that remote interface isn't null, or isSingleObjectFinder would have thrown an InvalidInputException.
			IMessage message = MessageUtility.getUtility().getMessage(vc, IEJBValidatorMessageConstants.CHKJ2403, IEJBValidationContext.WARNING, bean, clazz, method, new String[] { remoteIntfName }, this);
			vc.addMessage(message);
		}
	}
	
	/**
	 * Checks that the finder method on the entity home class follows the EJB 1.1 specification.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *...
	 *   - Each finder method must be named "find<METHOD>" (e.g. findLargeAccounts), and it
	 *     must match one of the ejbFind<METHOD> methods defined in the entity bean class 
	 *     (e.g. ejbFindLargeAccounts). The matching ejbFind<METHOD> method must have the 
	 *     same number and types of arguments. (Note that the return type may be different.)
	 *   - The return type for a find<METHOD> method must be the entity bean's remote 
	 *     interface type (for a single-object finder), or a collection thereof (for a 
	 *     multi-object finder).
	 *...
	 *   - All the exceptions defined in the throws clause of an ejbFind method of the 
	 *     entity bean class must be included in the throws clause of the matching find 
	 *     method of the home interface.
	 *   - The throws clause of a finder method must include the javax.ejb.FinderException. 
	 */
	public void validateFindMethod(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();
		if (method == null) {
			return;
		}

		// The return type for a find<METHOD> method must be the entity bean's remote 
		// interface type (for a single-object finder), or a collection thereof (for a 
		// multi-object finder).
		//
		// Whether or not the parameter is a type or an array of types, the following
		// call will return the base type. i.e., if it's an array of "MyClass",
		// the returnTypeName will be set to "MyClass", not an array type.
		validateFindMethod_remoteDep(vc, bean, clazz, method);

		// The throws clause of a finder method must include the javax.ejb.FinderException. 
		if (!ValidationRuleUtility.throwsFinderException(bean, method)) {
			String[] msgParm = { ITypeConstants.CLASSNAME_JAVAX_EJB_FINDEREXCEPTION };
			IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2414, IEJBValidationContext.ERROR, bean, clazz, method, msgParm, this);
			vc.addMessage(message);
		}

		// The home interface must always include the findByPrimaryKey method, which is 
		// always a single-object finder. The method must declare the primary key class 
		// as the method argument.
		boolean validateRMIParameters = true; // should the parameters be validated to see if they follow RMI-IIOP rules?
		boolean validateRMIRetType = true; // should the return type be validated to see if it follows RMI-IIOP rules?
		if (method.getName().equals(IMethodAndFieldConstants.METHODNAME_FINDBYPRIMARYKEY)) {
			// - The home interface must always include the findByPrimaryKey method, which is 
			// always a single-object finder. The method must declare the primary key class 
			// as the method argument.
			//
			// The findByPrimaryKey method is always a single-object finder.
			// The call to validateFindByPrimaryKeyMethod_remoteDep is done in
			// validateFindMethod_remoteDep(method);
			//validateFindByPrimaryKeyMethod_remoteDep(method);

			// The method must declare the primary key class as the method argument.
			validateFindByPrimaryKeyMethod_keyDep(vc, bean, clazz, method);

			if(ValidationRuleUtility.usesUnknownPrimaryKey(bean)) {
				validateRMIParameters = false;
			}
		}
					
		// The methods defined in this interface must follow the rules for RMI-IIOP. 
		// This means that their argument and return types must be of valid types for 
		// RMI-IIOP, and that their throws clause must include the java.rmi.RemoteException.
		// However, the return type of "Enumeration" or "Collection" is exempted from this
		// check, because the spec states, in 9.1.8.2, that Enumeration or Collection must be returned
		// for multi-object finders.
		JavaHelpers retType = method.getReturnType();
		validateRMIRetType = !((ValidationRuleUtility.isAssignableFrom(retType, ValidationRuleUtility.getType(ITypeConstants.CLASSNAME_JAVA_UTIL_ENUMERATION, bean))) || 
								(ValidationRuleUtility.isAssignableFrom(retType, ValidationRuleUtility.getType(ITypeConstants.CLASSNAME_JAVA_UTIL_COLLECTION, bean))));
		
		if(validateRMIParameters) {
			validateLegalRMIMethodArguments(vc, bean, clazz, method);
		}
		
		if(validateRMIRetType) {
			validateLegalRMIMethodReturnType(vc, bean, clazz, method);
		}
		
		validateLegalRMIMethodExceptions(vc, bean, clazz, method);
	}
	
	/**
	 * Checks that the finder method on the entity home class follows the EJB 1.1 specification.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *...
	 *   - Each finder method must be named "find<METHOD>" (e.g. findLargeAccounts), and it
	 *     must match one of the ejbFind<METHOD> methods defined in the entity bean class 
	 *     (e.g. ejbFindLargeAccounts). The matching ejbFind<METHOD> method must have the 
	 *     same number and types of arguments. (Note that the return type may be different.)
	 *   - The return type for a find<METHOD> method must be the entity bean's remote 
	 *     interface type (for a single-object finder), or a collection thereof (for a 
	 *     multi-object finder).
	 *...
	 *   - All the exceptions defined in the throws clause of an ejbFind method of the 
	 *     entity bean class must be included in the throws clause of the matching find 
	 *     method of the home interface.
	 *   - The throws clause of a finder method must include the javax.ejb.FinderException. 
	 */
	public void validateFindMethod_remoteDep(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();
		if (method == null) {
			return;
		}

		// The return type for a find<METHOD> method must be the entity bean's remote 
		// interface type (for a single-object finder), or a collection thereof (for a 
		// multi-object finder).
		//
		// Whether or not the parameter is a type or an array of types, the following
		// call will return the base type. i.e., if it's an array of "MyClass",
		// the returnTypeName will be set to "MyClass", not an array type.
		JavaClass remoteIntf = bean.getRemoteInterface();
		ValidationRuleUtility.isValidTypeHierarchy(bean, remoteIntf);

		// Perform this check for all finders but the findByPrimaryKey method.
		// The findByPrimaryKey method must return the remote interface, because the method is
		// a single-object finder method; but other finders may return a Collection or Enumeration.
		if (!method.getName().equals(IMethodAndFieldConstants.METHODNAME_FINDBYPRIMARYKEY)) {
			JavaHelpers returnType = method.getReturnType();
			
			if (! (ValidationRuleUtility.isAssignableFromCollection(returnType, bean) || 
			       ValidationRuleUtility.isAssignableFromEnumeration(returnType, bean) || 
			       ValidationRuleUtility.isAssignableFrom(returnType, remoteIntf))) {
				String[] msgParm = { remoteIntf.getName()};
				IMessage message = MessageUtility.getUtility().getMessage(vc, IEJBValidatorMessageConstants.CHKJ2403, IEJBValidationContext.WARNING, bean, clazz, method, msgParm, this);
				vc.addMessage(message);
			}
		}

		// The home interface must always include the findByPrimaryKey method, which is 
		// always a single-object finder. The method must declare the primary key class 
		// as the method argument.
		if (method.getName().equals(IMethodAndFieldConstants.METHODNAME_FINDBYPRIMARYKEY)) {
			validateFindByPrimaryKeyMethod_remoteDep(vc, bean, clazz, method);
		}
	}

	public void validateMatchingBeanCreateMethod(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method method) throws InvalidInputException {
		vc.terminateIfCancelled();

		super.validateMatchingBeanCreateMethod(vc, bean, clazz, method);
		JavaClass beanClass = bean.getEjbClass();
		Method ejbCreateMethod = ValidationRuleUtility.getMethodExtended(beanClass, method, IMethodAndFieldConstants.METHODNAME_EJBCREATE);
		if (ejbCreateMethod == null) {
			// already reported in super
			return;
		}
	}
	
	/**
	 * Checks that the finder method on the entity home class follows the EJB 1.1 specification.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *   - Each finder method must be named "find<METHOD>" (e.g. findLargeAccounts), and it
	 *     must match one of the ejbFind<METHOD> methods defined in the entity bean class 
	 *     (e.g. ejbFindLargeAccounts). The matching ejbFind<METHOD> method must have the 
	 *     same number and types of arguments. (Note that the return type may be different.)
	 *...
	 *   - All the exceptions defined in the throws clause of an ejbFind method of the 
	 *     entity bean class must be included in the throws clause of the matching find 
	 *     method of the home interface.
	 *...
	 */
	public void validateMatchingBeanFindMethod(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method homeMethod) throws InvalidInputException {
		vc.terminateIfCancelled();

		if (homeMethod == null) {
			return;
		}

		// Each finder method must be named "find<METHOD>" (e.g. findLargeAccounts), and it
		// must match one of the ejbFind<METHOD> methods defined in the entity bean class 
		// (e.g. ejbFindLargeAccounts). The matching ejbFind<METHOD> method must have the 
		// same number and types of arguments. (Note that the return type may be different.)
		String findMethodName = IMethodAndFieldConstants.PREFIX_EJBF + (homeMethod.getName()).substring(1); // e.g. if the home method is named findX, then the bean method will be named ejbFindX

		JavaClass beanClass = bean.getEjbClass();
		ValidationRuleUtility.isValidTypeHierarchy(bean, beanClass);

		Method beanMethod = ValidationRuleUtility.getMethodExtended(beanClass, homeMethod, findMethodName);
		if (beanMethod == null) {
			IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2030, IEJBValidationContext.ERROR, bean, clazz, homeMethod, new String[] { beanClass.getName()}, this);
			vc.addMessage(message);
			return;
		}

		// Check if return types match
		vc.terminateIfCancelled();
		JavaHelpers homeRetType = homeMethod.getReturnType();
		JavaHelpers beanRetType = beanMethod.getReturnType();
		if (!ValidationRuleUtility.isAssignableFrom(homeRetType, beanRetType)) {
			// emit the error only if it's a multi-finder. Single-finders are supposed to have different return types on the home & bean class.
			boolean homeRetTypeIsRemote = ValidationRuleUtility.isAssignableFrom(homeRetType, bean.getRemoteInterface());
			boolean beanRetTypeIsKey = ValidationRuleUtility.isAssignableFrom(beanRetType, ((Entity)bean).getPrimaryKey());
			if(!(homeRetTypeIsRemote && beanRetTypeIsKey)) {
				JavaHelpers retType = (beanRetTypeIsKey) ? bean.getRemoteInterface() : beanRetType; // if the bean class returns a key, the home must return the remote; if the bean class returns a Coll/Enum, the home must return the same
				IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2415, IEJBValidationContext.ERROR, bean, clazz, homeMethod, new String[] { retType.getJavaName(), beanClass.getName()}, this);
				vc.addMessage(message);
			}
		}

		// All the exceptions defined in the throws clause of an ejbFind method of the 
		// entity bean class must be included in the throws clause of the matching find
		// method of the home interface.
		/*
		// Don't check for the exceptions here - let the bean class do it. When the home
		// changes, a "dependent" validation of the bean's checks should be done automatically.
		// If it is checked in this method as well, there are duplicate messages on the task list.
		HashSet exceptions = getNotSubsetExceptions(homeMethod, beanMethod);
		if(exceptions.size() > 0) {
			Iterator iterator = exceptions.iterator();
			while(iterator.hasNext()) {
				JavaClass exc = (JavaClass)iterator.next();
				String[] msgParm = {exc.getQualifiedName()};
				addValidationMessage(IEJBValidationContext.ERROR, IMessagePrefixEjb11Constants.EJB_METHOD_THROW_NOTHI_EXCEP, msgParm, beanMethod, EJB_BEAN_GROUP);
			}
		}	
		*/
	}
	
	/**
	 * Checks that the create method on the entity home has a matching ejbCreate and ejbPostCreate on the bean.
	 *
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *   - Each create method must be named "create", and it must match one of the 
	 *     ejbCreate methods defined in the enterprise Bean class. The matching 
	 *     ejbCreate method must have the same number and types of its arguments. 
	 *     (Note that the return type is different.)
	 *...
	 *   - All the exceptions defined in the throws clause of the matching ejbCreate 
	 *     and ejbPostCreate methods of the enterprise Bean class must be included in 
	 *     the throws clause of the matching create method of the home interface 
	 *     (i.e the set of exceptions defined for the create method must be a superset
	 *     of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
	 *...
	 */
	public void validateMatchingBeanPostCreateMethod(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz, Method homeMethod) throws InvalidInputException {
		vc.terminateIfCancelled();

		if (homeMethod == null) {
			return;
		}

		// Each create method must be named "create", and it must match one of the 
		// ejbCreate methods defined in the enterprise Bean class. The matching 
		// ejbCreate method must have the same number and types of its arguments. 
		// (Note that the return type is different.)
		JavaClass beanClass = bean.getEjbClass();
		ValidationRuleUtility.isValidTypeHierarchy(bean, beanClass);

		Method beanMethod = ValidationRuleUtility.getMethodExtended(beanClass, homeMethod, IMethodAndFieldConstants.METHODNAME_EJBPOSTCREATE);
		if (beanMethod == null) {
			// Let the bean class report this message. Otherwise, when the bean class reports it, this message looks like a duplicate.
			/*
			IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2031, IEJBValidationContext.WARNING, bean, clazz, homeMethod, new String[] { beanClass.getName()}, this);
			vc.addMessage(message);
			*/
			return;
		}

		// All the exceptions defined in the throws clause of the matching ejbCreate 
		// and ejbPostCreate methods of the enterprise Bean class must be included in 
		// the throws clause of the matching create method of the home interface 
		// (i.e the set of exceptions defined for the create method must be a superset
		// of the union of exceptions defined for the ejbCreate and ejbPostCreate methods)
		/*
		// Don't check for the exceptions here - let the bean class do it. When the home
		// changes, a "dependent" validation of the bean's checks should be done automatically.
		// If it is checked in this method as well, there are duplicate messages on the task list.
		HashSet exceptions = getNotSubsetExceptions(homeMethod, beanMethod);
		if(exceptions.size() > 0) {
			Iterator iterator = exceptions.iterator();
			while(iterator.hasNext()) {
				JavaClass exc = (JavaClass)iterator.next();
				String[] msgParm = {exc.getQualifiedName()};
				addValidationMessage(IEJBValidationContext.ERROR, IMessagePrefixEjb11Constants.EJB_METHOD_THROW_NOTHI_EXCEP, msgParm, beanMethod, EJB_BEAN_GROUP);
			}
		}
		*/
	}
	
	/**
	 * 9.2.8 Entity bean's home interface
	 * The following are the requirements for the entity bean's home interface:
	 *   - The home interface must always include the findByPrimaryKey method, which is 
	 *     always a single-object finder. The method must declare the primary key class 
	 *     as the method argument.
	 */
	protected void validateMethodExists(IEJBValidationContext vc, EnterpriseBean bean, JavaClass clazz) throws InvalidInputException {
		// - The home interface must always include the findByPrimaryKey method, which is 
		// always a single-object finder. The method must declare the primary key class 
		// as the method argument.
		if (findByPKMethods.size() == 0) {
			// must have one findByPrimaryKey method
			IMessage message = MessageUtility.getUtility().getMessage(vc, IMessagePrefixEjb11Constants.CHKJ2011, IEJBValidationContext.ERROR, bean, clazz, new String[] { clazz.getQualifiedName()}, this);
			vc.addMessage(message);
		}
		/*
		// Not sure if this check is useful or not, because it is legal for a child home to declare a findByPrimaryKey,
		// and also for a parent home to declare a findByPrimaryKey.
		else if ( findByPKMethods.size() > 1 ) {
			// if the home interface has multiple findByPrimaryKey methods (implied by 9.2.8)
			Iterator iterator = findByPKMethods.iterator();
			while(iterator.hasNext()) {
				addValidationMessage(IEJBValidationContext.WARNING, IMessagePrefixEjb11Constants.EJB_HAS_MULTIPLE_PK_METHODS, (Method)iterator.next());
			}
		}
		*/
	}
}

Back to the top