Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2a7d799230569610477169172ce64fa1bdddb9fc (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
/*******************************************************************************
 * 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.ejb.internal.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jst.j2ee.ejb.CMPAttribute;
import org.eclipse.jst.j2ee.ejb.ContainerManagedEntity;
import org.eclipse.jst.j2ee.ejb.EJBExtensionFilter;
import org.eclipse.jst.j2ee.ejb.EnterpriseBean;
import org.eclipse.jst.j2ee.ejb.Entity;
import org.eclipse.jst.j2ee.internal.EjbModuleExtensionHelper;
import org.eclipse.jst.j2ee.internal.IEJBModelExtenderManager;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;

public abstract class ContainerManagedEntityFilter implements EJBExtensionFilter {
 	
	private static ThreadLocal<CMPCache> _cache = new ThreadLocal<CMPCache>();

    public List filter(ContainerManagedEntity cmp){
    	List list = getCache(this, cmp);
    	if (list != null)return list;
    	
    	list = filterNotcached(cmp);
    	setCache(this, cmp, list);
    	return list;
    }
    
    /**
     * Subclasses that don't implement the filter method need to override this method 
     * to provided the filtered results. 
     */
    protected List filterNotcached(ContainerManagedEntity cmp){
    	return new ArrayList();
    }

    /**
     * All CMPAttributeFilters only operate on ContainerManagedEntityExtension
     * objects.
     */
    public List filter(EnterpriseBean ejb) {
        if (ejb.isEntity() && ((Entity) ejb).isContainerManagedEntity()) return filter((ContainerManagedEntity) ejb);
        return new ArrayList();
    }

    protected void filterRoleAttributesByName(List allAttributes, List roleAttributes) {
        if (!roleAttributes.isEmpty()) {
            int allSize, roleSize;
            roleSize = roleAttributes.size();
            CMPAttribute roleAtt, allAtt;
            for (int i = 0; i < roleSize; i++) {
                roleAtt = (CMPAttribute) roleAttributes.get(i);
                allSize = allAttributes.size();
                for (int j = allSize - 1; j != -1; j--) {
                    allAtt = (CMPAttribute) allAttributes.get(j);
                    if (roleAtt == allAtt || roleAtt.getName().equals(allAtt.getName())) {
                        allAttributes.remove(j);
                        break;
                    }
                }
            }
        }
    }

    protected EjbModuleExtensionHelper getEjbModuleExtHelper(Object context) {
        return IEJBModelExtenderManager.INSTANCE.getEJBModuleExtension(context);
    }

    protected List getLocalRelationshipRoles(ContainerManagedEntity cmp) {
        EjbModuleExtensionHelper modelExtender = null;
        if (cmp.getVersionID() >= J2EEVersionConstants.EJB_2_0_ID)
            return cmp.getRoles();
        else if ( (modelExtender = getEjbModuleExtHelper(cmp)) != null ){
            return modelExtender.getLocalRelationshipRoles_cmp11(cmp);
        } 
        return Collections.EMPTY_LIST;
    }
    
    protected List getRelationshipRoles(ContainerManagedEntity cmp) { 
        List roles = new ArrayList(); 
        collectRelationshipRoles(cmp, getEjbModuleExtHelper(cmp), roles);
        return Collections.unmodifiableList(roles);
    }
    
    public void collectRelationshipRoles(ContainerManagedEntity cmp, EjbModuleExtensionHelper extensionHelper, List containerList) {
        if(cmp == null)
            return;
        containerList.addAll(getLocalRelationshipRoles(cmp));
        if(extensionHelper != null)
            collectRelationshipRoles((ContainerManagedEntity) extensionHelper.getSuperType(cmp), extensionHelper, containerList);        
    }
    
    /**
     * Clear the cache and turn off caching.
     */
	public void clearCache(){
		if (_cache == null)return;
		getCache().clear();
		getCache().setEnabled(false);
	}
	
	
	protected void setCache(ContainerManagedEntityFilter filter, ContainerManagedEntity cmp, List list) {
		getCache().set(filter, cmp, list);
	}


	protected List getCache(ContainerManagedEntityFilter filter, ContainerManagedEntity cmp) {
		return getCache().get(filter, cmp);
	}
	
	/**
	 * Answer the CMPCache for this thread.
	 * @return
	 */
	private CMPCache getCache(){
		CMPCache cache = _cache.get();
		if (cache == null){
			cache = new CMPCache();
			_cache.set(cache);
		}
		return cache;
	}
	
	/**
	 * This method needs to be called (with the parameter true) if you wish the 
	 * filter results to be cached. By default the results are not cached.
	 * <p>
	 * The cache is thread based. When done the clearCache method should be called.
	 * 
	 * @param isEnabled set this to true to have the filter results cached.
	 */
	public void enableCache(boolean isEnabled){
		getCache().setEnabled(isEnabled);
	}

    /**
     * Keep a thread local cache of filtered results.
     * @author karasiuk
     *
     */
	private static class CMPCache {
		
		/*
		 * We discovered some very deep code paths when validating EJBs. In one example project that 
		 * only had a few beans, it took 12 hours to validate. Keeping a cache during validation
		 * reduced the time to seconds.
		 */
		
		private Map 		_map = new HashMap(30);
		
		/** Is the cache turned on, by default it is not. */
		private boolean		_enabled;
		
		public void setEnabled(boolean isEnabled){
			_enabled = isEnabled;
		}

		public List get(ContainerManagedEntityFilter filter, ContainerManagedEntity cmp) {
			if (!_enabled)return null;
			
			Map map = (Map)_map.get(filter);
			if (map == null){
				map = new HashMap(100);
				_map.put(filter, map);
			}
			return (List)map.get(cmp);
		}

		public void set(ContainerManagedEntityFilter filter, ContainerManagedEntity cmp, List list) {
			if (!_enabled)return; 
			
			Map map = (Map)_map.get(filter);
			if (map == null){
				map = new HashMap(100);
				_map.put(filter, map);
			}
			
			map.put(cmp, list);			
		}
		
		public void clear(){
			_map.clear();
		}
		
	}

}

Back to the top