Skip to main content
summaryrefslogtreecommitdiffstats
blob: b3c373ba0545d5a5ef158da6240cd45994b045ca (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
/*******************************************************************************
 * 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.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jst.j2ee.ejb.ContainerManagedEntity;
import org.eclipse.jst.j2ee.ejb.EJBJar;
import org.eclipse.jst.j2ee.ejb.EnterpriseBean;
import org.eclipse.jst.j2ee.ejb.Entity;
import org.eclipse.jst.j2ee.internal.J2EEConstants;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

/**
 * Core EJB Validation rules
 */
public class EJBValidationRuleFactory extends AbstractEJBValidationRuleFactory {
	private static EJBValidationRuleFactory _inst = null;
	private static final Map contextMap = Collections.synchronizedMap(new HashMap());

	private Logger logger = null;
	
	public EJBValidationRuleFactory() {
		createRuleList();
	}
	
	public static EJBValidationRuleFactory getFactory() {
		if(_inst == null) {
			_inst = new EJBValidationRuleFactory();
		}
		return _inst;
	}
	protected Logger getMsgLogger(){
	    if(logger == null)
	        logger = Logger.getLogger(IEJBValidatorConstants.J2EE_CORE_PLUGIN);
	    return logger;
	}
	private void createRuleList() {
		_ruleList = new HashMap();

		_ruleList.put(IValidationRuleList.EJB11_EJBJAR, new org.eclipse.jst.j2ee.model.internal.validation.EJBJar11VRule());
		_ruleList.put(IValidationRuleList.EJB20_EJBJAR, new org.eclipse.jst.j2ee.model.internal.validation.EJBJar20VRule());
	
		_ruleList.put(IValidationRuleList.EJB11_ENTERPRISEBEAN, new org.eclipse.jst.j2ee.model.internal.validation.EnterpriseBean11VRule());
		_ruleList.put(IValidationRuleList.EJB20_ENTERPRISEBEAN, new org.eclipse.jst.j2ee.model.internal.validation.EnterpriseBean20VRule());
		
		_ruleList.put(IValidationRuleList.EJB11_SESSION_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.ValidateSessionBean());
		_ruleList.put(IValidationRuleList.EJB11_SESSION_REMOTE, new org.eclipse.jst.j2ee.model.internal.validation.ValidateSessionRemote());
		_ruleList.put(IValidationRuleList.EJB11_SESSION_HOME, new org.eclipse.jst.j2ee.model.internal.validation.ValidateSessionHome());
		
		_ruleList.put(IValidationRuleList.EJB20_STATELESS_SESSION_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.StatelessSessionBeanClassVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATELESS_SESSION_REMOTE, new org.eclipse.jst.j2ee.model.internal.validation.StatelessSessionRemoteComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATELESS_SESSION_HOME, new org.eclipse.jst.j2ee.model.internal.validation.StatelessSessionRemoteHomeVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATELESS_SESSION_LOCAL, new org.eclipse.jst.j2ee.model.internal.validation.StatelessSessionLocalComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATELESS_SESSION_LOCALHOME, new org.eclipse.jst.j2ee.model.internal.validation.StatelessSessionLocalHomeVRule());
		
		_ruleList.put(IValidationRuleList.EJB20_STATEFUL_SESSION_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.StatefulSessionBeanClassVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATEFUL_SESSION_REMOTE, new org.eclipse.jst.j2ee.model.internal.validation.StatefulSessionRemoteComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATEFUL_SESSION_HOME, new org.eclipse.jst.j2ee.model.internal.validation.StatefulSessionRemoteHomeVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATEFUL_SESSION_LOCAL, new org.eclipse.jst.j2ee.model.internal.validation.StatefulSessionLocalComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_STATEFUL_SESSION_LOCALHOME, new org.eclipse.jst.j2ee.model.internal.validation.StatefulSessionLocalHomeVRule());
		
		_ruleList.put(IValidationRuleList.EJB11_CMP_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.ValidateCMPBean());
		_ruleList.put(IValidationRuleList.EJB11_CMP_REMOTE, new org.eclipse.jst.j2ee.model.internal.validation.ValidateCMPRemote());
		_ruleList.put(IValidationRuleList.EJB11_CMP_KEYCLASS, new org.eclipse.jst.j2ee.model.internal.validation.ValidateCMPKey());
		
		_ruleList.put(IValidationRuleList.EJB20_CMP_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.CMPBeanClassVRule());
		_ruleList.put(IValidationRuleList.EJB20_CMP_REMOTE, new org.eclipse.jst.j2ee.model.internal.validation.CMPRemoteComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_CMP_HOME, new org.eclipse.jst.j2ee.model.internal.validation.CMPRemoteHomeVRule());
		_ruleList.put(IValidationRuleList.EJB20_CMP_LOCAL, new org.eclipse.jst.j2ee.model.internal.validation.CMPLocalComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_CMP_LOCALHOME, new org.eclipse.jst.j2ee.model.internal.validation.CMPLocalHomeVRule());
		_ruleList.put(IValidationRuleList.EJB20_CMP_KEYCLASS, new org.eclipse.jst.j2ee.model.internal.validation.CMPKeyClassVRule());
		
		_ruleList.put(IValidationRuleList.EJB11_BMP_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.ValidateBMPBean());
		_ruleList.put(IValidationRuleList.EJB11_BMP_REMOTE, new org.eclipse.jst.j2ee.model.internal.validation.ValidateBMPRemote());
		_ruleList.put(IValidationRuleList.EJB11_BMP_HOME, new org.eclipse.jst.j2ee.model.internal.validation.ValidateBMPHome());
		_ruleList.put(IValidationRuleList.EJB11_BMP_KEYCLASS, new org.eclipse.jst.j2ee.model.internal.validation.ValidateBMPKey());

		_ruleList.put(IValidationRuleList.EJB20_BMP_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.BMPBeanClassVRule());
		_ruleList.put(IValidationRuleList.EJB20_BMP_REMOTE, new org.eclipse.jst.j2ee.model.internal.validation.BMPRemoteComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_BMP_HOME, new org.eclipse.jst.j2ee.model.internal.validation.BMPRemoteHomeVRule());
		_ruleList.put(IValidationRuleList.EJB20_BMP_LOCAL, new org.eclipse.jst.j2ee.model.internal.validation.BMPLocalComponentVRule());
		_ruleList.put(IValidationRuleList.EJB20_BMP_LOCALHOME, new org.eclipse.jst.j2ee.model.internal.validation.BMPLocalHomeVRule());
		_ruleList.put(IValidationRuleList.EJB20_BMP_KEYCLASS, new org.eclipse.jst.j2ee.model.internal.validation.BMPKeyClassVRule());
		
		_ruleList.put(IValidationRuleList.EJB20_MESSAGE_BEANCLASS, new org.eclipse.jst.j2ee.model.internal.validation.MessageDrivenBeanClassVRule());
		
		addDependentRuleToRuleList();
		logAllDependentRules();
	}
	
	/**
	 * If the IFileDelta represents a static load model (i.e., the file name is always the same),
	 * then return the Integer which loads that model. Otherwise return null.
	 */
	public  Object getRuleId(IEJBValidationContext vc, String fileName) {
		if(fileName == null) {
			Logger aLogger = vc.getMsgLogger();
			if (aLogger != null && aLogger.isLoggingLevel(Level.FINE)) {
				aLogger.write(Level.FINE, "Cannot load rule from fileName: " + fileName); //$NON-NLS-1$
			}
			return null;
		}

		EJBJar ejbJar = getEJBJar(vc);

		if(fileName.endsWith(J2EEConstants.EJBJAR_DD_SHORT_NAME)) {
			if(ejbJar.getVersionID() == J2EEVersionConstants.EJB_1_1_ID) {
				return IValidationRuleList.EJB11_EJBJAR;
			}
			return IValidationRuleList.EJB20_EJBJAR;
		}
		return null; // must be a JavaClass
	}
	
	/**
	 * If the IFileDelta represents a dynamic load model (i.e., the file name can be changed by the
	 * user), then return the Integer which loads that model. Return null if no such model exists.
	 */
	public final Object getRuleId(IEJBValidationContext vc, JavaClass clazz, EnterpriseBean bean) {
		// Return the rule id which will validate the JavaClass as the 
		// type which it is used as by the EnterpriseBean.
		if((clazz == null) || (bean == null)) {
			Logger aLogger = vc.getMsgLogger();
			if (aLogger != null && aLogger.isLoggingLevel(Level.FINE)) {
				aLogger.write(Level.FINE, "Cannot load rule because either bean (" + bean + ") or class (" + clazz + ") is null"); //$NON-NLS-1$  //$NON-NLS-2$  //$NON-NLS-3$
			}
			return null;
		}
		
		boolean isStatelessSession = (bean.isSession() && ValidationRuleUtility.isStateless(bean));
		boolean isStatefulSession = (bean.isSession() && ValidationRuleUtility.isStateful(bean));
		
		if(bean.getVersionID() >= J2EEVersionConstants.EJB_2_0_ID) {
			if(clazz.equals(bean.getEjbClass())) {
				if(isStatelessSession) {
						return IValidationRuleList.EJB20_STATELESS_SESSION_BEANCLASS;
				}
				else if(isStatefulSession) {
						return IValidationRuleList.EJB20_STATEFUL_SESSION_BEANCLASS;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB20_CMP_BEANCLASS;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB20_BMP_BEANCLASS;
				}
				else if(bean.isMessageDriven()) {
					return IValidationRuleList.EJB20_MESSAGE_BEANCLASS;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger != null && aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion2_X() && clazz.equals(bean.getEjbClass()"); //$NON-NLS-1$
					}
					
					return null;
				}
			}
			else if(clazz.equals(bean.getRemoteInterface())) {
				if(isStatelessSession) {
					return IValidationRuleList.EJB20_STATELESS_SESSION_REMOTE;
				}
				else if(isStatefulSession) {
					return IValidationRuleList.EJB20_STATEFUL_SESSION_REMOTE;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB20_CMP_REMOTE;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB20_BMP_REMOTE;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion2_X() && clazz.equals(bean.getRemoteInterface()"); //$NON-NLS-1$
					}
					return null;
				}
			}
			else if(clazz.equals(bean.getHomeInterface())) {
				if(isStatelessSession) {
					return IValidationRuleList.EJB20_STATELESS_SESSION_HOME;
				}
				else if(isStatefulSession) {
					return IValidationRuleList.EJB20_STATEFUL_SESSION_HOME;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB20_CMP_HOME;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB20_BMP_HOME;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion2_X() && clazz.equals(bean.getHomeInterface()"); //$NON-NLS-1$
					}
					return null;
				}
			}
			else if(clazz.equals(bean.getLocalHomeInterface())) {
				if(isStatelessSession) {
					return IValidationRuleList.EJB20_STATELESS_SESSION_LOCALHOME;
				}
				else if(isStatefulSession) {
					return IValidationRuleList.EJB20_STATEFUL_SESSION_LOCALHOME;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB20_CMP_LOCALHOME;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB20_BMP_LOCALHOME;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion2_X() && clazz.equals(bean.getLocalHomeInterface()"); //$NON-NLS-1$
					}
					return null;
				}
			}
			else if(clazz.equals(bean.getLocalInterface())) {
				if(isStatelessSession) {
					return IValidationRuleList.EJB20_STATELESS_SESSION_LOCAL;
				}
				else if(isStatefulSession) {
					return IValidationRuleList.EJB20_STATEFUL_SESSION_LOCAL;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB20_CMP_LOCAL;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB20_BMP_LOCAL;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion2_X() && clazz.equals(bean.getLocalInterface()"); //$NON-NLS-1$
					}
					return null;
				}
			}
			else if(bean instanceof Entity) {
				if(clazz.equals(((Entity)bean).getPrimaryKey())) {
					if(bean.isContainerManagedEntity()) {
						ContainerManagedEntity cmp = (ContainerManagedEntity)bean;
						// If it's a primitive primary key, it's exempt from these checks.
						if(ValidationRuleUtility.isPrimitivePrimaryKey(cmp)) {
							return null;
						}
						else if(ValidationRuleUtility.usesUnknownPrimaryKey(cmp)) {
							return null;
						}
						return IValidationRuleList.EJB20_CMP_KEYCLASS;
					}
					else if(bean.isBeanManagedEntity()) {
						return IValidationRuleList.EJB20_BMP_KEYCLASS;
					}
					else {
						Logger aLogger = vc.getMsgLogger();
						if (aLogger.isLoggingLevel(Level.FINE)) {
							aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion2_X() && clazz.equals(((Entity)bean).getPrimaryKey()"); //$NON-NLS-1$
						}
						return null;
					}
				}
			}
			
			Logger aLogger = vc.getMsgLogger();
			if (aLogger.isLoggingLevel(Level.FINE)) {
				aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion2_X()"); //$NON-NLS-1$
			}
			return null;
		}
		else if(bean.getVersionID() <= J2EEVersionConstants.EJB_1_1_ID) {
			if(clazz.equals(bean.getEjbClass())) {
				if(bean.isSession()) {
						return IValidationRuleList.EJB11_SESSION_BEANCLASS;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB11_CMP_BEANCLASS;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB11_BMP_BEANCLASS;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion1_X() && clazz.equals(bean.getEjbClass()"); //$NON-NLS-1$
					}
					return null;
				}
			}
			else if(clazz.equals(bean.getRemoteInterface())) {
				if(bean.isSession()) {
					return IValidationRuleList.EJB11_SESSION_REMOTE;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB11_CMP_REMOTE;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB11_BMP_REMOTE;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion1_X() && clazz.equals(bean.getRemoteInterface()"); //$NON-NLS-1$
					}
					return null;
				}
			}
			else if(clazz.equals(bean.getHomeInterface())) {
				if(bean.isSession()) {
					return IValidationRuleList.EJB11_SESSION_HOME;
				}
				else if(bean.isContainerManagedEntity()) {
					return IValidationRuleList.EJB11_CMP_HOME;
				}
				else if(bean.isBeanManagedEntity()) {
					return IValidationRuleList.EJB11_BMP_HOME;
				}
				else {
					Logger aLogger = vc.getMsgLogger();
					if (aLogger.isLoggingLevel(Level.FINE)) {
						aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion1_X() && clazz.equals(bean.getHomeInterface()"); //$NON-NLS-1$
					}
					return null;
				}
			}
			else if(bean instanceof Entity) {
				if(clazz.equals(((Entity)bean).getPrimaryKey())) {
					if(bean.isContainerManagedEntity()) {
						ContainerManagedEntity cmp = (ContainerManagedEntity)bean;
						// If it's a primitive primary key, it's exempt from these checks.
						if(ValidationRuleUtility.isPrimitivePrimaryKey(cmp)) {
							return null;
						}
						else if(ValidationRuleUtility.usesUnknownPrimaryKey(cmp)) {
							return null;
						}
						return IValidationRuleList.EJB11_CMP_KEYCLASS;
					}
					else if(bean.isBeanManagedEntity()) {
						return IValidationRuleList.EJB11_BMP_KEYCLASS;
					}
					else {
						Logger aLogger = vc.getMsgLogger();
						if (aLogger.isLoggingLevel(Level.FINE)) {
							aLogger.write(Level.FINE, "Cannot load rule: bean.isVersion1_X() && clazz.equals(((Entity)bean).getPrimaryKey()"); //$NON-NLS-1$
						}
						return null;
					}
				}
			}
		}
		
		Logger aLogger = vc.getMsgLogger();
		if (aLogger.isLoggingLevel(Level.FINE)) {
			aLogger.write(Level.FINE, "Cannot load rule: !bean.isVersion1_X() && !bean.isVersion2_X()"); //$NON-NLS-1$
		}
		return null;
	}
	
	public IValidationRule getRule(IEJBValidationContext vc, Object ruleId) {
		Map ruleMap = (Map) contextMap.get(vc.getReporter());
		if(ruleMap == null){
			// instantiate a new set of rules for each instance of EJB validator as the rule classes
			// are not thread safe, in case when multiple ejb validators run on multiple projects at
			// the same time, rule objects can report incorrect errors see bug 187286
			EJBValidationRuleFactory factory = new EJBValidationRuleFactory();
			ruleMap = factory._ruleList;
			contextMap.put(vc.getReporter(), ruleMap);
		}
		Object rule = ruleMap.get(ruleId);
    	if(rule == null) {
    		// No such ruleId registered.
    		Logger logger = vc.getMsgLogger();
    		if (logger != null && logger.isLoggingLevel(Level.FINE)) {
    			logger.write(Level.FINE, "Cannot load rule from ruleId: " + ruleId); //$NON-NLS-1$
    		}
    		return null;
    	}
    	return (IValidationRule)rule;
	}
	
	public void clearRuleMap(IReporter reporter){
		contextMap.remove(reporter);
	}
}

Back to the top