Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a56b804d795b65502c2dcacf1bff0278991d3f4 (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
/*******************************************************************************
 * Copyright (c) 2001, 2008 Oracle 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:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.core.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jst.jsf.common.internal.policy.CanonicallyOrderedIteratorPolicy;
import org.eclipse.jst.jsf.common.internal.policy.IIteratorPolicy;
import org.eclipse.jst.jsf.common.internal.policy.IdentifierOrderedIteratorPolicy;
import org.eclipse.jst.jsf.common.internal.strategy.AbstractIdentifiableStrategy;
import org.eclipse.jst.jsf.common.internal.strategy.IIdentifiableStrategy;
import org.eclipse.jst.jsf.common.internal.strategy.IteratorPolicyBasedStrategyComposite;
import org.eclipse.jst.jsf.designtime.internal.view.model.ITagRegistry;
import org.eclipse.jst.jsf.designtime.internal.view.model.TagRegistryFactory.TagRegistryFactoryException;

/**
 * Employs a policy-based strategy to construct a TagRegistry for a particular
 * content-type in a particular project.
 * 
 * @author cbateman
 * 
 */
public final class CompositeTagRegistryFactory
{
    private static CompositeTagRegistryFactory INSTANCE;

    private static ITagRegistryFactoryProvider   TEST_PROVIDER;
    /**
     * For JUNIT TEST ONLY!!!!!
     * @param factoryProvider
     */
    public synchronized void setTestInjectedProvider(final ITagRegistryFactoryProvider factoryProvider)
    {
        TEST_PROVIDER = factoryProvider;
        // TODO: this is risky
        _cachedExtensionsByType.clear();
    }

    /**
     * @return the single instance of the registry factory
     */
    public static synchronized CompositeTagRegistryFactory getInstance()
    {
        if (INSTANCE == null)
        {
            INSTANCE = new CompositeTagRegistryFactory();
        }
        return INSTANCE;
    }

    private final Map<TagRegistryIdentifier, Set<ITagRegistryFactoryInfo>> _cachedExtensionsByType =
        new HashMap<TagRegistryIdentifier, Set<ITagRegistryFactoryInfo>>(4);

    private CompositeTagRegistryFactory()
    {
        // singleton: no external instantiation
    }

    
    /**
     * @param id
     * @return a tag registry for the id or null if there isn't one.
     */
    public final ITagRegistry getRegistry(final TagRegistryIdentifier id)
    {
        final Set<ITagRegistryFactoryInfo> handlers = getAllTagRegistryFactories();

        final Set<ITagRegistryFactoryInfo> matchingHandlers = findMatchingExtensions(
                id, handlers);

        if (matchingHandlers.size() > 0)
        {
            // optimize: if there is only one handler, no need for strategy
            if (matchingHandlers.size() == 1)
            {
                try
                {
                    return matchingHandlers.iterator().next().getTagRegistryFactory()
                            .createTagRegistry(id.getProject());
                }
                catch (TagRegistryFactoryException e)
                {
                    JSFCorePlugin.log(e, "While trying to acquire a registry"); //$NON-NLS-1$
                }
            }
            else
            {
                // for now, we will order by the canonical name of the extension
                // id in ascending order. Later, we may make this preference
                // based.
                final TagRegistrySelectionStrategy selectionStrategy = new TagRegistrySelectionStrategy(
                        new CanonicallyOrderedIteratorPolicy<String>());

                for (final Iterator<ITagRegistryFactoryInfo> it = matchingHandlers
                        .iterator(); it.hasNext();)
                {
                    selectionStrategy.addStrategy(it.next().getTagRegistryFactory());
                }
                return selectionStrategy.perform(id.getProject());
            }
        }

        return null;
    }

    /**
     * @return get all tag registry factories
     */
    public Set<ITagRegistryFactoryInfo> getAllTagRegistryFactories()
    {
        List<String>  selectionOrder = new ArrayList<String>();
        selectionOrder.add("testInjection"); //$NON-NLS-1$
        selectionOrder.add("extensionPointInjection"); //$NON-NLS-1$
        selectionOrder.add("platformDefault"); //$NON-NLS-1$

        IdentifierOrderedIteratorPolicy<String> policy = new IdentifierOrderedIteratorPolicy<String>(selectionOrder);
        // ignore iterator values that don't exist in the list of possible selections.
        policy.setExcludeNonExplicitValues(true);
        final TagRegistryFactoryProviderSelectionStrategy providerSelector
            = new TagRegistryFactoryProviderSelectionStrategy(policy);
        providerSelector.addStrategy(
          new AbstractIdentifiableStrategy<IProject, ITagRegistryFactoryProvider, String>("testInjection", "FIXME: not for display", null) //$NON-NLS-1$ //$NON-NLS-2$
        {
            @Override
            public ITagRegistryFactoryProvider perform(IProject input)
                    throws Exception
            {
                ITagRegistryFactoryProvider injectedProvider = null;
                synchronized(CompositeTagRegistryFactory.class)
                {
                     injectedProvider = TEST_PROVIDER;
                }
                if (injectedProvider != null)
                {
                    final ITagRegistryFactoryProvider useMe = injectedProvider;
                    return new AbstractTagRegistryFactoryProvider()
                    {
                        @Override
                        public Set<ITagRegistryFactoryInfo> getTagRegistryFactories()
                        {
                            return useMe.getTagRegistryFactories();
                        }
                    };
                }
                return null;
            }
            });
        providerSelector.addStrategy(
                new AbstractIdentifiableStrategy<IProject, ITagRegistryFactoryProvider, String>("platformDefault", "FIXME: not for display", null) //$NON-NLS-1$ //$NON-NLS-2$
              {
                  @Override
                  public ITagRegistryFactoryProvider perform(IProject input)
                          throws Exception
                  {
                      return new AbstractTagRegistryFactoryProvider()
                      {
                          @Override
                          public Set<ITagRegistryFactoryInfo> getTagRegistryFactories()
                          {
                              return TagLibraryRegistryLoader.getAllHandlers();
                          }
                      };
                  }
              });

        ITagRegistryFactoryProvider provider = providerSelector.perform(null);
        if (provider != null)
        {
            return provider.getTagRegistryFactories();
        }
        return Collections.emptySet();
    }

    private Set<ITagRegistryFactoryInfo> findMatchingExtensions(
            TagRegistryIdentifier id, Set<ITagRegistryFactoryInfo> handlers)
    {
        Set<ITagRegistryFactoryInfo> matching = _cachedExtensionsByType.get(id);

        if (matching == null)
        {
            matching = new HashSet<ITagRegistryFactoryInfo>(4);

            for (final ITagRegistryFactoryInfo handler : handlers)
            {
                if (handler.getContentTypes().contains(id.getContentType())
                        && handler.getTagRegistryFactory().projectIsValid(id.getProject()))
                {
                    matching.add(handler);
                }
            }

            // if there is nothing matching, just store the empty set and
            // discard the extra memory
            if (matching.size() > 0)
            {
                _cachedExtensionsByType.put(id, matching);
            }
            else
            {
                _cachedExtensionsByType.put(id, Collections.EMPTY_SET);
            }
        }
        return matching;
    }

    /**
     * Identifies a content type/project context in which to request a tag
     * registry.
     * 
     * @author cbateman
     * 
     */
    public final static class TagRegistryIdentifier
    {
        private final IProject     _project;
        private final IContentType _contentType;

        /**
         * @param project
         * @param contentType
         */
        public TagRegistryIdentifier(final IProject project,
                final IContentType contentType)
        {
            _project = project;
            _contentType = contentType;
        }

        /**
         * @return project
         */
        public IProject getProject()
        {
            return _project;
        }

        /**
         * @return content type
         */
        public IContentType getContentType()
        {
            return _contentType;
        }
        
        public boolean equals(final Object o) {
        	if (o instanceof TagRegistryIdentifier) {        	
	        	final TagRegistryIdentifier other = (TagRegistryIdentifier)o;
	        	final int otherProjectHash = other.getProject() != null ? other.getProject().hashCode() : 0;
	        	final int thisProjectHash = getProject() != null ? getProject().hashCode() : 0;
	        	if (otherProjectHash == thisProjectHash &&
	        			other.getContentType().equals(this.getContentType()))
	        		return true;
        	}
        	return false;
        }
        
        public int hashCode() {
        	return (getProject() != null ? getProject().hashCode() : 0) + 7*_contentType.hashCode();
        }

    }

    private static class TagRegistrySelectionStrategy
            extends
            IteratorPolicyBasedStrategyComposite<IProject, ITagRegistry, ITagRegistry, String, IIdentifiableStrategy<IProject, ITagRegistry, String>>
    {
        private static final ITagRegistry NO_RESULT = null;

        protected TagRegistrySelectionStrategy(IIteratorPolicy<String> policy)
        {
            super(policy);
        }

        @Override
        public ITagRegistry getNoResult()
        {
            return NO_RESULT;
        }
    }
    
    private static class TagRegistryFactoryProviderSelectionStrategy
            extends
            IteratorPolicyBasedStrategyComposite<IProject, ITagRegistryFactoryProvider, ITagRegistryFactoryProvider, String, IIdentifiableStrategy<IProject, ITagRegistryFactoryProvider, String>>
    {
        protected TagRegistryFactoryProviderSelectionStrategy(
                IIteratorPolicy<String> policy)
        {
            super(policy);
        }

        private static final ITagRegistryFactoryProvider NO_RESULT = null;

        @Override
        public ITagRegistryFactoryProvider getNoResult()
        {
            return NO_RESULT;
        }
    }
}

Back to the top