Skip to main content
summaryrefslogtreecommitdiffstats
blob: bb397a98fbc5f4669c6bb00ded29ba6d8b0ba25f (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*******************************************************************************
 * Copyright (c) 2008 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:
 *    Cameron Bateman - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.facelet.core.internal.registry.taglib;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jst.j2ee.common.ParamValue;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.jst.j2ee.model.IModelProvider;
import org.eclipse.jst.j2ee.webapplication.ContextParam;
import org.eclipse.jst.j2ee.webapplication.WebApp;
import org.eclipse.jst.jsf.common.internal.componentcore.AbstractVirtualComponentQuery;
import org.eclipse.jst.jsf.facelet.core.internal.FaceletCorePlugin;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.WebappConfiguration.WebappListener.WebappChangeEvent;
import org.eclipse.wst.common.componentcore.resources.IVirtualFile;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;

/**
 * Manages the web.xml elements of interest to Facelet tag libraries
 * 
 * @author cbateman
 * 
 */
public class WebappConfiguration
{
    // TODO: move these to jsf core.
    /**
     * The param key for Facelet 1.x libraries declared in web.xml
     */
    public static final String FACELET_10_LIBRARIES_CONTEXT_PARAM_NAME = "facelets.LIBRARIES"; //$NON-NLS-1$
    /**
     * The param key for Facelet 2.0 libraries declared in web.xml
     */
    public static final String JSF20_FACELET_LIBRARIES_CONTEXT_PARAM_NAME = "javax.faces.FACELETS_LIBRARIES"; //$NON-NLS-1$
    private final IProject _project;
    /**
     * Cached instance of ContextParamAdapter.
     */
    private final ContextParamAdapter _contextParamAdapter;
    private List<IFile> _cachedFiles;
    private IModelProvider _modelProvider;
    private AbstractVirtualComponentQuery _vcQuery;

    /**
     * @param project
     * @param modelProvider
     * @param vcQuery
     */
    public WebappConfiguration(final IProject project,
            final IModelProvider modelProvider,
            final AbstractVirtualComponentQuery vcQuery)
    {
        _project = project;
        _contextParamAdapter = new ContextParamAdapter();
        _modelProvider = modelProvider;
        _vcQuery = vcQuery;
    }

    /**
     * @param listener
     */
    public void addListener(final WebappListener listener)
    {
        _contextParamAdapter.addListener(listener);
    }

    /**
     * @param listener
     */
    public void removeListener(final WebappListener listener)
    {
        _contextParamAdapter.removeListener(listener);
    }

    /**
     * @return the list of IFile's
     */
    public List<IFile> getFiles()
    {
        final IVirtualFolder folder = _vcQuery.getWebContentFolder(_project);

        if (folder == null)
        {
            return Collections.emptyList();
        }

        final List<String> filenames = getConfigFilesFromContextParam(_project,
                _modelProvider);
        final List<IFile> files = new ArrayList<IFile>();

        for (final String filename : filenames)
        {
            final IVirtualFile vfile = folder.getFile(new Path(filename));
            if (vfile != null)
            {
                files.add(vfile.getUnderlyingFile());
            }
        }
        _cachedFiles = files;
        return Collections.unmodifiableList(files);
    }

    private Object getModelObject()
    {
        return _modelProvider.getModelObject();
    }

    /**
     * 
     */
    public void start()
    {
        final Object webAppObj = getModelObject();
        if (webAppObj != null)
        {
            if (webAppObj instanceof WebApp)
            {
                startLocatingJ2EEConfigs((WebApp) webAppObj);
            } else if (webAppObj instanceof org.eclipse.jst.javaee.web.WebApp)
            {
                startLocatingJEEConfigs((org.eclipse.jst.javaee.web.WebApp) webAppObj);
            }
        } else
        {
            FaceletCorePlugin
                    .log(
                            "Could not get webApp for project: " + _project, new Exception()); //$NON-NLS-1$
        }
    }

    /**
     * 
     */
    public void dispose()
    {
        if (_contextParamAdapter != null)
        {
            final Object webAppObj = getModelObject();
            if (webAppObj != null)
            {
                if (webAppObj instanceof WebApp)
                {
                    stopLocatingJ2EEConfigs((WebApp) webAppObj);
                } else if (webAppObj instanceof org.eclipse.jst.javaee.web.WebApp)
                {
                    stopLocatingJEEConfigs((org.eclipse.jst.javaee.web.WebApp) webAppObj);
                }
            } else
            {
                FaceletCorePlugin
                        .log(
                                "Failed stopping locator for project: " + _project.getName() //$NON-NLS-1$
                                , new Exception());
            }
        }
    }

    private void startLocatingJ2EEConfigs(final WebApp webApp)
    {
        webApp.eAdapters().add(_contextParamAdapter);
        final EList contexts = webApp.getContexts();
        if (contexts != null)
        {
            final Iterator itContexts = contexts.iterator();
            while (itContexts.hasNext())
            {
                final ContextParam contextParam = (ContextParam) itContexts
                        .next();
                contextParam.eAdapters().add(_contextParamAdapter);
            }
        }
        final EList contextParams = webApp.getContextParams();
        if (contextParams != null)
        {
            final Iterator itContextParams = contextParams.iterator();
            while (itContextParams.hasNext())
            {
                final ParamValue paramValue = (ParamValue) itContextParams
                        .next();
                paramValue.eAdapters().add(_contextParamAdapter);
            }
        }
    }

    private void startLocatingJEEConfigs(
            final org.eclipse.jst.javaee.web.WebApp webApp)
    {
        ((EObject) webApp).eAdapters().add(_contextParamAdapter);
        // System.out.println(((EObject)webApp).eDeliver());
        final List params = webApp.getContextParams();
        if (params != null)
        {
            final Iterator itContexts = params.iterator();
            while (itContexts.hasNext())
            {
                final EObject contextParam = (EObject) itContexts.next();
                contextParam.eAdapters().add(_contextParamAdapter);
            }
        }
    }

    private void stopLocatingJ2EEConfigs(final WebApp webApp)
    {
        webApp.eAdapters().remove(_contextParamAdapter);
        final EList contexts = webApp.getContexts();
        if (contexts != null)
        {
            final Iterator itContexts = contexts.iterator();
            while (itContexts.hasNext())
            {
                final ContextParam contextParam = (ContextParam) itContexts
                        .next();
                contextParam.eAdapters().remove(_contextParamAdapter);
            }
        }
        final EList contextParams = webApp.getContextParams();
        if (contextParams != null)
        {
            final Iterator itContextParams = contextParams.iterator();
            while (itContextParams.hasNext())
            {
                final ParamValue paramValue = (ParamValue) itContextParams
                        .next();
                paramValue.eAdapters().remove(_contextParamAdapter);
            }
        }
    }

    private void stopLocatingJEEConfigs(
            final org.eclipse.jst.javaee.web.WebApp webApp)
    {
        ((EObject) webApp).eAdapters().remove(_contextParamAdapter);
        final List contextParams = webApp.getContextParams();
        if (contextParams != null)
        {
            final Iterator itContextParams = contextParams.iterator();
            while (itContextParams.hasNext())
            {
                final EObject paramValue = (EObject) itContextParams.next();
                paramValue.eAdapters().remove(_contextParamAdapter);
            }
        }
    }

    /**
     * Gets list of application configuration file names as listed in the JSF
     * CONFIG_FILES context parameter ("javax.faces.CONFIG_FILES"). Will return
     * an empty list if WebArtifactEdit is null, if WebApp is null, if context
     * parameter does not exist, or if trimmed context parameter's value is an
     * empty String.
     * 
     * @param project
     *            IProject instance for which to get the context parameter's
     *            value.
     * @param provider
     * @return List of application configuration file names as listed in the JSF
     *         CONFIG_FILES context parameter ("javax.faces.CONFIG_FILES"); list
     *         may be empty.
     */
    public static List<String> getConfigFilesFromContextParam(
            final IProject project, IModelProvider provider)
    {
        List<String> filesList = Collections.EMPTY_LIST;
        // if (JSFAppConfigUtils.isValidJSFProject(project))
        {
            final Object webAppObj = provider.getModelObject();
            if (webAppObj != null)
            {
                if (webAppObj instanceof WebApp)
                {
                    filesList = getConfigFilesForJ2EEApp((WebApp) webAppObj);
                } else if (webAppObj instanceof org.eclipse.jst.javaee.web.WebApp)
                {
                    filesList = getConfigFilesForJEEApp((org.eclipse.jst.javaee.web.WebApp) webAppObj);
                }
            }

        }
        return filesList;
    }

    private static List<String> getConfigFilesForJEEApp(
            final org.eclipse.jst.javaee.web.WebApp webApp)
    {
        String filesString = null;
        final List contextParams = webApp.getContextParams();
        final Iterator itContextParams = contextParams.iterator();
        while (itContextParams.hasNext())
        {
            final org.eclipse.jst.javaee.core.ParamValue paramValue = (org.eclipse.jst.javaee.core.ParamValue) itContextParams
                    .next();
            if (paramValue.getParamName().equals(
                    FACELET_10_LIBRARIES_CONTEXT_PARAM_NAME)
                    || paramValue.getParamName().equals(
                            JSF20_FACELET_LIBRARIES_CONTEXT_PARAM_NAME))
            {
                filesString = paramValue.getParamValue();
                break;
            }
        }
        return parseFilesString(filesString);
    }

    private static List<String> getConfigFilesForJ2EEApp(final WebApp webApp)
    {
        List<String> filesList = new ArrayList<String>();

        if (webApp != null)
        {
            String filesString = null;
            // need to branch here due to model version differences
            // (BugZilla #119442)
            if (webApp.getVersionID() == J2EEVersionConstants.WEB_2_3_ID)
            {
                final EList contexts = webApp.getContexts();
                final Iterator itContexts = contexts.iterator();
                while (itContexts.hasNext())
                {
                    final ContextParam contextParam = (ContextParam) itContexts
                            .next();
                    if (contextParam.getParamName().equals(
                            FACELET_10_LIBRARIES_CONTEXT_PARAM_NAME)
                            || contextParam.getParamName().equals(
                                    JSF20_FACELET_LIBRARIES_CONTEXT_PARAM_NAME))
                    // if (contextParam.getParamName().equals(
                    // FACELET_LIBRARIES_CONTEXT_PARAM_NAME))
                    {
                        filesString = contextParam.getParamValue();
                        break;
                    }
                }
            } else
            {
                final EList contextParams = webApp.getContextParams();
                final Iterator itContextParams = contextParams.iterator();
                while (itContextParams.hasNext())
                {
                    final ParamValue paramValue = (ParamValue) itContextParams
                            .next();
                    if (paramValue.getName().equals(
                            FACELET_10_LIBRARIES_CONTEXT_PARAM_NAME)
                            || paramValue.getName().equals(
                                    JSF20_FACELET_LIBRARIES_CONTEXT_PARAM_NAME))
                    // if (paramValue.getName().equals(
                    // FACELET_LIBRARIES_CONTEXT_PARAM_NAME))
                    {
                        filesString = paramValue.getValue();
                        break;
                    }
                }
            }
            filesList = parseFilesString(filesString);
        }

        return filesList;
    }

    private static List<String> parseFilesString(final String filesString)
    {
        final List<String> filesList = new ArrayList<String>();
        if (filesString != null && filesString.trim().length() > 0)
        {
            final StringTokenizer stFilesString = new StringTokenizer(
                    filesString, ";"); //$NON-NLS-1$
            while (stFilesString.hasMoreTokens())
            {
                final String configFile = stFilesString.nextToken().trim();
                filesList.add(configFile);
            }
        }
        return filesList;
    }

    /**
     * Adapter implementation used to monitor addition/removal of context-param
     * nodes and change in name of existing nodes in order to respond to changes
     * to the JSF CONFIG_FILES context-param.
     * 
     * @author Ian Trimble - Oracle
     */
    private class ContextParamAdapter extends AdapterImpl
    {
        private final CopyOnWriteArrayList<WebappListener> _listeners = new CopyOnWriteArrayList<WebappListener>();

        public void addListener(final WebappListener listener)
        {
            _listeners.addIfAbsent(listener);
        }

        public void removeListener(final WebappListener listener)
        {
            _listeners.remove(listener);
        }

        private void fireEvent(final WebappChangeEvent event)
        {
            for (final WebappListener listener : _listeners)
            {
                listener.webappChanged(event);
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.
         * eclipse.emf.common.notify.Notification)
         */
        @Override
        public void notifyChanged(final Notification notification)
        {
            final Object objNotifier = notification.getNotifier();
            // System.out.println(objNotifier.toString());
            if (objNotifier instanceof WebApp
                    || objNotifier instanceof org.eclipse.jst.javaee.web.WebApp)
            {
                final int eventType = notification.getEventType();
                switch (eventType)
                {
                case Notification.ADD:
                    final Object objNewValue = notification.getNewValue();
                    if (objNewValue instanceof ContextParam
                            || objNewValue instanceof org.eclipse.jst.javaee.core.ParamValue)
                    {
                        contextParamAdded((EObject) objNewValue);
                    } else if (objNewValue instanceof ParamValue)
                    {
                        paramValueAdded((EObject) objNewValue);
                    }
                    break;
                case Notification.REMOVE:
                    final Object objOldValue = notification.getOldValue();
                    if (objOldValue instanceof ContextParam
                            || objOldValue instanceof org.eclipse.jst.javaee.core.ParamValue)
                    {
                        contextParamRemoved((EObject) objOldValue);
                    } else if (objOldValue instanceof ParamValue)
                    {
                        paramValueRemoved((EObject) objOldValue);
                    }
                    break;
                }
            }
            // else if (objNotifier instanceof ContextParam
            // || objNotifier instanceof org.eclipse.jst.javaee.core.ParamValue)
            // {
            // if (notification.getEventType() != Notification.REMOVING_ADAPTER)
            // {
            // _listener
            // .tagLibChanged(ContextParamSpecifiedFaceletTaglibLocator.this);
            // }
            // }
            // else if (objNotifier instanceof ParamValue)
            // {
            // if (notification.getEventType() != Notification.REMOVING_ADAPTER)
            // {
            // _listener
            // .tagLibChanged(ContextParamSpecifiedFaceletTaglibLocator.this);
            // }
            // }
        }

        /**
         * Called when a new ContextParam instance is added.
         * 
         * @param contextParam
         *            ContextParam instance.
         */
        protected void contextParamAdded(final EObject contextParam)
        {
            if (isConfigFilesContextParam(contextParam))
            {
                checkAndFireFileChanges();
            }
            contextParam.eAdapters().add(this);
        }

        private void checkAndFireFileChanges()
        {
            final List<IFile> oldFiles = _cachedFiles == null ? Collections.EMPTY_LIST
                    : _cachedFiles;
            final List<IFile> newFiles = getFiles();

            final List<IFile> filesAdded = new ArrayList<IFile>();
            final List<IFile> filesRemoved = new ArrayList<IFile>();

            for (final IFile oldFile : oldFiles)
            {
                if (!newFiles.contains(oldFile))
                {
                    filesRemoved.add(oldFile);
                }
            }

            for (final IFile newFile : newFiles)
            {
                if (!oldFiles.contains(newFile))
                {
                    filesAdded.add(newFile);
                }
            }

            if (filesAdded.size() > 0 || filesRemoved.size() > 0)
            {
                fireEvent(new WebappChangeEvent(filesRemoved, filesAdded));
            }
        }

        /**
         * Called when a new ParamValue instance is added.
         * 
         * @param paramValue
         *            ParamValue instance.
         */
        protected void paramValueAdded(final EObject paramValue)
        {
            if (isConfigFilesParamValue(paramValue))
            {
                checkAndFireFileChanges();
            }
            paramValue.eAdapters().add(this);
        }

        /**
         * Called when a ContextParam instance is removed.
         * 
         * @param contextParam
         *            ContextParam instance.
         */
        protected void contextParamRemoved(final EObject contextParam)
        {
            if (isConfigFilesContextParam(contextParam))
            {
                checkAndFireFileChanges();
            }
            contextParam.eAdapters().remove(this);
        }

        /**
         * Called when a ParamValue instance is removed.
         * 
         * @param paramValue
         *            ParamValue instance.
         */
        protected void paramValueRemoved(final EObject paramValue)
        {
            if (isConfigFilesParamValue(paramValue))
            {
                checkAndFireFileChanges();
            }
            paramValue.eAdapters().remove(this);
        }

        /**
         * Tests if the passed ContextParam instance is the JSF CONFIG_FILES
         * context parameter.
         * 
         * @param contextParam
         *            ContextParam instance.
         * @return true if the passed ContextParam instance is the JSF
         *         CONFIG_FILES context parameter, else false
         */
        protected boolean isConfigFilesContextParam(final EObject contextParam)
        {
            boolean isConfigFiles = false;
            if (contextParam != null)
            {
                String name = null;
                if (contextParam instanceof ContextParam)
                {
                    name = ((ContextParam) contextParam).getParamName();
                } else if (contextParam instanceof org.eclipse.jst.javaee.core.ParamValue)
                {
                    name = ((org.eclipse.jst.javaee.core.ParamValue) contextParam)
                            .getParamName();
                }

                if (FACELET_10_LIBRARIES_CONTEXT_PARAM_NAME.equals(name)
                        || JSF20_FACELET_LIBRARIES_CONTEXT_PARAM_NAME
                                .equals(name))
                {
                    isConfigFiles = true;
                }
            }
            return isConfigFiles;
        }

        /**
         * Tests if the passed ParamValue instance is the JSF CONFIG_FILES
         * context parameter.
         * 
         * @param paramVal
         *            as EObject ParamValue instance.
         * @return true if the passed ParamValue instance is the JSF
         *         CONFIG_FILES context parameter, else false
         */
        protected boolean isConfigFilesParamValue(final EObject paramVal)
        {
            boolean isConfigFiles = false;
            if (paramVal != null)
            {
                String name = null;
                if (paramVal instanceof ParamValue)
                {
                    name = ((ParamValue) paramVal).getName();
                } else if (paramVal instanceof org.eclipse.jst.javaee.core.ParamValue)
                {
                    name = ((org.eclipse.jst.javaee.core.ParamValue) paramVal)
                            .getParamName();
                }

                if (FACELET_10_LIBRARIES_CONTEXT_PARAM_NAME.equals(name)
                        || JSF20_FACELET_LIBRARIES_CONTEXT_PARAM_NAME
                                .equals(name))
                {
                    isConfigFiles = true;
                }
            }
            return isConfigFiles;
        }
    }

    abstract static class WebappListener
    {
        public static class WebappChangeEvent
        {
            private final List<IFile> _removed;
            private final List<IFile> _added;

            WebappChangeEvent(final List<IFile> removed, final List<IFile> added)
            {
                _removed = Collections.unmodifiableList(removed);
                _added = Collections.unmodifiableList(added);
            }

            public final List<IFile> getRemoved()
            {
                return _removed;
            }

            public final List<IFile> getAdded()
            {
                return _added;
            }
        }

        public abstract void webappChanged(final WebappChangeEvent event);
    }
}

Back to the top