Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 47f92a0550dd9ed29f3166ece4da0627e0c7ce00 (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
/*******************************************************************************
 * 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
 *******************************************************************************/
/*
 * Abstract class for EJB Validation rules
 */
package org.eclipse.jst.j2ee.model.internal.validation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;

import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jst.j2ee.ejb.EJBJar;

/**
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public abstract class AbstractEJBValidationRuleFactory {

    /**
     * 
     */
    public AbstractEJBValidationRuleFactory() {
		//Default
    }

    protected HashMap _ruleList = null;

    public void release(IValidationRule rule) {
    	if(rule == null) {
    		return;
    	}
    	
    	// Since we don't put a lock on the rule when it's checked out via getRule(Object),
    	// there's nothing to do other than reset the rule to prepare it for reuse.
    	rule.reset();
    }

    protected Iterator addDependentRuleToRuleList() {
    	Iterator iterator = _ruleList.values().iterator();
    	while(iterator.hasNext()) {
    		IValidationRule rule = (IValidationRule)iterator.next();
    		Object[] ids = rule.getDependsOn();
    		if(ids == null) {
    			continue;
    		}
    		
    		for(int i=0; i<ids.length; i++) {
    			IValidationRule dRule = (IValidationRule)_ruleList.get(ids[i]);
    			if(dRule != null) {
    				dRule.addDependent(rule);
    			}
    		}
    	}
    	return iterator;
    }

    protected void logAllDependentRules() {
    	Iterator iterator;
    	Logger logger = getMsgLogger();
    	if (logger.isLoggingLevel(Level.FINEST)) {
    		iterator = _ruleList.values().iterator();
    		Iterator depIterator = null;
    		while(iterator.hasNext()) {
    			IValidationRule rule = (IValidationRule)iterator.next();
    			Set dependents = rule.getDependents();
    			StringBuffer buffer = new StringBuffer();
    			buffer.append("list of dependents for rule:"); //$NON-NLS-1$
    			buffer.append(rule.getClass().getName());
    			buffer.append("\n"); //$NON-NLS-1$
    			if(dependents != null) {
    				depIterator = dependents.iterator();
    				while(depIterator.hasNext()) {
    					IValidationRule dRule = (IValidationRule)depIterator.next();
    					buffer.append("\t"); //$NON-NLS-1$
    					buffer.append(dRule.getClass().getName());
    					buffer.append("\n"); //$NON-NLS-1$
    				}
    			}
    			buffer.append("\n"); //$NON-NLS-1$
    			logger.write(Level.FINEST, buffer.toString());
    		}
    	}
    }

    /**
     * @return
     */
    protected abstract Logger getMsgLogger();

    protected EJBJar getEJBJar(IEJBValidationContext vc) {
    	return (EJBJar)vc.loadModel(EJBValidatorModelEnum.EJB_MODEL);
    }

    /**
     * This method will return the identified IValidationRule.
     * If the ruleid is not registered, or if there is a problem
     * instantiating the IValidationRule, then null will be returned.
     */
    public IValidationRule getRule(IEJBValidationContext vc, Object ruleId) {
    	// To avoid loading a rule until it is needed, intialize
    	// the list with a rule id as the key, and a String class name
    	// as a value. When a rule is loaded, if the value is a String,
    	// then the IValidationRule has not been instantiated. Create an
    	// instance and store that in place of the String. If the value
    	// is already an IValidationRule instance, return it.
    	
    	Object rule = _ruleList.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;
    }
}

Back to the top