Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c32678644f2a165e536c6536057cac5af3d77525 (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
/*******************************************************************************
 * Copyright (c) 2005 Oracle Corporation.
 * 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:
 *    Gerry Kessler - initial API and implementation
 *    Ian Trimble - JSFLibraryRegistry work
 *******************************************************************************/
package org.eclipse.jst.jsf.core.internal;

import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jst.jsf.context.IDelegatingFactory;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IStructuredDocumentContextResolverFactory;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IStructuredDocumentContextResolverFactory2;
import org.eclipse.jst.jsf.designtime.context.AbstractDTExternalContextFactory;
import org.eclipse.jst.jsf.designtime.el.AbstractDTMethodResolver;
import org.eclipse.jst.jsf.designtime.el.AbstractDTPropertyResolver;
import org.eclipse.jst.jsf.designtime.el.AbstractDTVariableResolver;
import org.eclipse.jst.jsf.designtime.internal.BasicExtensionFactory;
import org.eclipse.jst.jsf.designtime.internal.DecoratableExtensionFactory;
import org.eclipse.jst.jsf.designtime.internal.BasicExtensionFactory.ExtensionData;
import org.eclipse.jst.jsf.designtime.internal.resolver.ViewBasedTaglibResolverFactory;
import org.eclipse.jst.jsf.designtime.internal.view.AbstractDTViewHandler;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.eclipse.wst.common.frameworks.internal.WTPPlugin;
import org.osgi.framework.BundleContext;

/**
 * JSF Core plugin.
 * 
 * @author Gerry Kessler - Oracle, Ian Trimble - Oracle
 */
public class JSFCorePlugin extends WTPPlugin
{
    /**
     * The plugin id
     */
    public static final String             PLUGIN_ID = "org.eclipse.jst.jsf.core"; // org.eclipse.jst.jsf.core.internal.JSFCorePlugin";
    // //$NON-NLS-1$

    // The shared instance.
    private static JSFCorePlugin           plugin;

    private IPreferenceStore               preferenceStore;

    private ViewBasedTaglibResolverFactory _tagLibResolverFactory;

    /**
     * The constructor.
     */
    public JSFCorePlugin()
    {
        plugin = this;
    }

    /**
     * This method is called upon plug-in activation
     * 
     * @param context
     * @throws Exception
     */
    @Override
    public void start(final BundleContext context) throws Exception
    {
        super.start(context);
        final IStructuredDocumentContextResolverFactory2 factory = IStructuredDocumentContextResolverFactory2.INSTANCE;
        if (factory instanceof IDelegatingFactory)
        {
            _tagLibResolverFactory = new ViewBasedTaglibResolverFactory();
            ((IDelegatingFactory) factory)
            .addFactoryDelegate(_tagLibResolverFactory);
        }
        else
        {
            log("Error adding tag resolver delegate", new Throwable());
        }
    }

    /**
     * This method is called when the plug-in is stopped
     * 
     * @param context
     * @throws Exception
     */
    @Override
    public void stop(final BundleContext context) throws Exception
    {
        super.stop(context);

        final IStructuredDocumentContextResolverFactory factory = IStructuredDocumentContextResolverFactory.INSTANCE;

        if (factory instanceof IDelegatingFactory
                && _tagLibResolverFactory != null)
        {
            ((IDelegatingFactory) factory)
            .removeFactoryDelegate(_tagLibResolverFactory);
        }
        plugin = null;
    }

    /**
     * Returns the shared instance.
     * 
     * @return the shared instance
     */
    public static JSFCorePlugin getDefault()
    {
        return plugin;
    }

    /**
     * @param e
     * @param msg
     */
    public static void log(final Exception e, final String msg)
    {
        final ILog log = getDefault().getLog();

        log.log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, msg, e));
    }

    /**
     * Logs using the default ILog implementation provided by getLog().
     * 
     * @param severity
     *            Severity (IStatus constant) of log entry
     * @param message
     *            Human-readable message describing log entry
     * @param ex
     *            Throwable instance (can be null)
     */
    public static void log(final int severity, final String message,
            final Throwable ex)
    {
        getDefault().getLog().log(
                new Status(severity, PLUGIN_ID, IStatus.OK, message, ex));
    }

    /**
     * Logs using the default ILog implementation provided by getLog().
     * 
     * @param severity
     *            Severity (IStatus constant) of log entry
     * @param message
     *            Human-readable message describing log entry
     */
    public static void log(final int severity, final String message)
    {
        log(severity, message, null);
    }

    /**
     * Logs a message for this plugin
     * 
     * @param message
     * @param t
     */
    public static void log(final String message, final Throwable t)
    {
        final ILog log = plugin.getLog();
        log.log(new Status(IStatus.ERROR, plugin.getBundle().getSymbolicName(),
                0, message, t));
    }

    @Override
    public String getPluginID()
    {
        return PLUGIN_ID;
    }

    /**
     * @param id 
     * @return all registered symbol source providers
     */
    public synchronized static ExtensionData<AbstractDTVariableResolver> getVariableResolvers(final String id)
    {
        checkAndInitVariableResolverFactory();
        return _variableResolverFactory.getExtensions().get(id);
    }

    /**
     * @param forName
     * @return the ids of variable resolvers for 'forName'.
     */
    public synchronized static List<String> getVariableResolversForName(
            final String forName)
    {
        checkAndInitVariableResolverFactory();
        return Collections.unmodifiableList(_variableResolverFactory
                .getIdsForName(forName));
    }

    /**
     * @return true if there any decorative resolvers in the system.  This
     * is provided so that decorative variable resolvers can avoid expensive
     * calculations looking for these resolvers when there are none.
     */
    public synchronized static boolean hasDecorativeVariableResolvers()
    {
        checkAndInitVariableResolverFactory();
        return _variableResolverFactory.getNumDecorativeResolvers() > 0;
    }
    
    private synchronized static void checkAndInitVariableResolverFactory()
    {
        if (_variableResolverFactory == null)
        {
            _variableResolverFactory = new DecoratableExtensionFactory<AbstractDTVariableResolver>(
                    getDefault().getBundle(), VARIABLE_RESOLVER_EXT_POINT_NAME,
                    VARIABLE_RESOLVER_ELEMENT_NAME);
        }
    }

    private static DecoratableExtensionFactory<AbstractDTVariableResolver> _variableResolverFactory;
    private final static String                                            VARIABLE_RESOLVER_EXT_POINT_NAME = "variableresolver"; //$NON-NLS-1$
    private final static String                                            VARIABLE_RESOLVER_ELEMENT_NAME   = "variableresolver"; //$NON-NLS-1$

    /**
     * @param id 
     * @return a map of all registered property resolvers by id
     */
    public synchronized static ExtensionData<AbstractDTPropertyResolver> getPropertyResolver(final String id)
    {
        checkAndInitPropertyFactory();
        return _propertyResolverFactory.getExtensions().get(id);
    }

    private synchronized static void checkAndInitPropertyFactory()
    {
        if (_propertyResolverFactory == null)
        {
            _propertyResolverFactory = new DecoratableExtensionFactory<AbstractDTPropertyResolver>(
                    getDefault().getBundle(), PROPERTY_RESOLVER_EXT_POINT_NAME,
                    PROPERTY_RESOLVER_ELEMENT_NAME);
        }
    }

    /**
     * @param forName
     * @return the ids of variable resolvers for 'forName'.
     */
    public synchronized static List<String> getPropertyResolversForName(
            final String forName)
    {
        checkAndInitVariableResolverFactory();
        return Collections.unmodifiableList(_propertyResolverFactory
                .getIdsForName(forName));
    }

    /**
     * @return true if there an decorating resolvers
     */
    public synchronized static boolean hasDecorativePropertyResolvers()
    {
        checkAndInitVariableResolverFactory();
        return _propertyResolverFactory.getNumDecorativeResolvers() > 0;
    }
    
    private static DecoratableExtensionFactory<AbstractDTPropertyResolver> _propertyResolverFactory;
    private final static String                                     PROPERTY_RESOLVER_EXT_POINT_NAME = "propertyresolver"; //$NON-NLS-1$
    private final static String                                     PROPERTY_RESOLVER_ELEMENT_NAME   = "propertyresolver"; //$NON-NLS-1$

    /**
     * @param id 
     * @return a map of all registered method resolvers by id
     */
    public synchronized static ExtensionData<AbstractDTMethodResolver> getMethodResolvers(final String id)
    {
        checkAndInitMethodResolverFactory();
        return _methodResolverFactory.getExtensions().get(id);
    }

    private synchronized static void checkAndInitMethodResolverFactory()
    {
        if (_methodResolverFactory == null)
        {
            _methodResolverFactory = new BasicExtensionFactory<AbstractDTMethodResolver>(
                    getDefault().getBundle(), METHOD_RESOLVER_EXT_POINT_NAME,
                    METHOD_RESOLVER_ELEMENT_NAME);
        }
    }
    private static BasicExtensionFactory<AbstractDTMethodResolver> _methodResolverFactory;
    private final static String                                   METHOD_RESOLVER_EXT_POINT_NAME = "methodresolver"; //$NON-NLS-1$
    private final static String                                   METHOD_RESOLVER_ELEMENT_NAME   = "methodresolver"; //$NON-NLS-1$

    /**
     * @param id 
     * @return a map of all registered external context providers by id
     */
    public synchronized static ExtensionData<AbstractDTExternalContextFactory> getExternalContextProviders(final String id)
    {
        checkAndInitExternalContextFactory();
        return _externalContextResolverFactory.getExtensions().get(id);
    }

    private synchronized static void checkAndInitExternalContextFactory()
    {
        if (_externalContextResolverFactory == null)
        {
            _externalContextResolverFactory = new BasicExtensionFactory<AbstractDTExternalContextFactory>(
                    getDefault().getBundle(), EXTERNAL_CONTEXT_EXT_POINT_NAME,
                    EXTERNAL_CONTEXT_ELEMENT_NAME);
        }
    }
    
    private static BasicExtensionFactory<AbstractDTExternalContextFactory> _externalContextResolverFactory;
    private final static String                                           EXTERNAL_CONTEXT_EXT_POINT_NAME = "externalcontext"; //$NON-NLS-1$
    private final static String                                           EXTERNAL_CONTEXT_ELEMENT_NAME   = "externalcontext"; //$NON-NLS-1$

    /**
     * @param id 
     * @return a map of all registered external context providers by id
     */
    public synchronized static ExtensionData<AbstractDTViewHandler> getViewHandlers(final String id)
    {
        checkAndInitViewHandler();
        return _viewHandlerFactory.getExtensions().get(id);
    }

    private synchronized static void checkAndInitViewHandler()
    {
        if (_viewHandlerFactory == null)
        {
            _viewHandlerFactory = new BasicExtensionFactory<AbstractDTViewHandler>(
                    getDefault().getBundle(), VIEWHANDLER_EXT_POINT_NAME,
                    VIEWHANDLER_ELEMENT_NAME);
        }
    }

    private static BasicExtensionFactory<AbstractDTViewHandler> _viewHandlerFactory;
    private final static String                                VIEWHANDLER_EXT_POINT_NAME = "viewhandler"; //$NON-NLS-1$
    private final static String                                VIEWHANDLER_ELEMENT_NAME   = "viewhandler"; //$NON-NLS-1$

    /**
     * @return the preference store for this bundle TODO: this is copied from
     *         AbstractUIPlugin; need to upgrade to new IPreferencesService
     */
    public IPreferenceStore getPreferenceStore()
    {
        // Create the preference store lazily.
        if (this.preferenceStore == null)
        {
            this.preferenceStore = new ScopedPreferenceStore(
                    new InstanceScope(), getBundle().getSymbolicName());

        }
        return this.preferenceStore;
    }

    /**
     * @param name
     * @return the extension point called name for this bundle
     */
    public IExtensionPoint getExtension(final String name)
    {
        return Platform.getExtensionRegistry().getExtensionPoint(
                plugin.getBundle().getSymbolicName(), name);
    }
}

Back to the top