Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 904f49991a1e7ab68701748a0d8503938baf9afb (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
/*******************************************************************************
 * 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.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.resource.Resource.IOWrappedException;
import org.eclipse.emf.ecore.xmi.FeatureNotFoundException;
import org.eclipse.jst.jsf.common.internal.finder.AbstractMatcher.AlwaysMatcher;
import org.eclipse.jst.jsf.common.internal.finder.AbstractMatcher.IMatcher;
import org.eclipse.jst.jsf.common.internal.finder.VisitorMatcher;
import org.eclipse.jst.jsf.common.internal.finder.acceptor.JarEntryMatchingAcceptor;
import org.eclipse.jst.jsf.common.internal.finder.matcher.TaglibJarEntryFinder;
import org.eclipse.jst.jsf.common.internal.resource.ClasspathJarFile;
import org.eclipse.jst.jsf.common.internal.resource.DefaultJarLocator;
import org.eclipse.jst.jsf.common.internal.resource.IJarLocator;
import org.eclipse.jst.jsf.common.internal.resource.IJarLocator.JarChangeEvent;
import org.eclipse.jst.jsf.common.internal.resource.IJarLocator.JarChangeListener;
import org.eclipse.jst.jsf.common.internal.resource.JavaCoreMediator;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.facelet.core.internal.FaceletCorePlugin;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.IFaceletTagRecord.JarTagRecordDescriptor;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.Listener.TaglibChangedEvent;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.Listener.TaglibChangedEvent.CHANGE_TYPE;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib.FaceletTaglib;

/**
 * A locator that finds Facelet taglibs in jars on the classpath
 * 
 * @author cbateman
 * 
 */
public class JarFileFaceletTaglibLocator extends AbstractFaceletTaglibLocator
{
    /**
     * Default taglib finder that looks in meta-inf
     */
    public static final TaglibJarEntryFinder _taglibMetaInfFinder = new TaglibJarEntryFinder(
            Pattern.compile("META-INF/.*\\.taglib\\.xml")); //$NON-NLS-1$
    /**
     * Default finder that looks in the glassfish package.
     */
    public static final TaglibJarEntryFinder _taglibGlassfishFinder = new TaglibJarEntryFinder(
            Pattern.compile("com/sun/faces/metadata/taglib/.*\\.taglib\\.xml")); //$NON-NLS-1$
    private static final List<IMatcher> MATCHERS;
    static
    {
        final List<IMatcher> matchers = new ArrayList<IMatcher>();
        matchers.add( new IMatcher() {
        	public boolean matches(Object matchThis) {
        		if (matchThis instanceof JarEntry)
        		{
        			final String name = ((JarEntry) matchThis).getName();
        			if (name != null)
        			{
        				final int nameLength = name.length();
        				if (nameLength > ".taglib.xml".length() || !name.endsWith(".taglib.xml")) //$NON-NLS-1$ //$NON-NLS-2$
        				{
        					return false;
        				}
        				if (nameLength > "META-INF/.taglib.xml".length()) //$NON-NLS-1$
        				{
        					if (_taglibMetaInfFinder.matches(matchThis))
        					{
        						return true;
        					}
        					if (nameLength > "com/sun/faces/metadata/taglib/.taglib.xml".length()) //$NON-NLS-1$
        					{
        						if (_taglibGlassfishFinder.matches(matchThis))
        						{
        							return true;
        						}
        					}
        				}
        				return false;
        			}
        		}
        		return false;
        	}
        });
        MATCHERS = Collections.unmodifiableList(matchers);
    }
    private static final String DISPLAYNAME = Messages.JarFileFaceletTaglibLocator_0;
    private static final String ID = JarFileFaceletTaglibLocator.class
            .getCanonicalName();
    private final TagRecordFactory _factory;
    private final Map<String, IFaceletTagRecord> _records;
    private final IJarLocator _locator;
    private final List<IMatcher> _jarEntryMatchers;

    /**
     * @param factory
     */
    public JarFileFaceletTaglibLocator(final TagRecordFactory factory)
    {
        this(factory, new DefaultJarLocator(
                Collections.singletonList(new AlwaysMatcher()),
                new JavaCoreMediator()));
    }

    /**
     * @param factory
     * @param jarProvider
     */
    public JarFileFaceletTaglibLocator(final TagRecordFactory factory,
            final IJarLocator jarProvider)
    {
        this(factory, jarProvider, MATCHERS);
    }

    /**
     * @param factory
     * @param jarProvider
     * @param jarEntryMatchers
     */
    public JarFileFaceletTaglibLocator(final TagRecordFactory factory,
            final IJarLocator jarProvider, final List<IMatcher>  jarEntryMatchers)
    {    
        super(ID, DISPLAYNAME);
        _factory = factory;
        _records = new HashMap<String, IFaceletTagRecord>();
        _locator = jarProvider;
        _jarEntryMatchers = jarEntryMatchers;
    }

    @Override
    public void start(final IProject project)
    {
        _locator.start(project);
        final List<LibJarEntry> tagLibsFound = new ArrayList<LibJarEntry>();
        final Collection<? extends ClasspathJarFile> jars = _locator
                .getJars(project);
        long curTime = System.currentTimeMillis();
        for (final ClasspathJarFile cpJarFile : jars)
        {
        	tagLibsFound.addAll(processJar(cpJarFile, _jarEntryMatchers));
        }
        System.out.printf(
        		"Time spent processing jars: %d\n", //$NON-NLS-1$
        		Long.valueOf(System.currentTimeMillis()-curTime));
        for (final LibJarEntry jarEntry : tagLibsFound)
        {
            final IFaceletTagRecord record = _factory.createRecords(jarEntry
                    .getTaglib(), new JarTagRecordDescriptor(
                    jarEntry.getPath(), jarEntry.getEntryName()));
            if (record != null)
            {
                _records.put(record.getURI(), record);
            }
        }
        _locator.addListener(new JarChangeListener()
        {
            @Override
            public void changed(final JarChangeEvent event)
            {
                switch (event.getType())
                {
                    case JAR_ADDED:
                    {
                        final ClasspathJarFile jar = event.getJar();
                        final List<LibJarEntry> foundLibs = processJar(jar, _jarEntryMatchers);
                        for (final LibJarEntry lib : foundLibs)
                        {
                            final IFaceletTagRecord newRecord = _factory.createRecords(
                                    lib.getTaglib(),
                                    new JarTagRecordDescriptor(lib
                                            .getPath(), lib
                                            .getEntryName()));
                            _records.put(newRecord.getURI(), newRecord);
                            fireChangeEvent(new TaglibChangedEvent(
                                    JarFileFaceletTaglibLocator.this, null,
                                    newRecord,
                                    CHANGE_TYPE.ADDED));
                        }
                    }
                    break;
                    case JAR_REMOVED:
                    {
                        final ClasspathJarFile jar = event.getJar();
                        final List<IFaceletTagRecord>  removeRecords = 
                            new ArrayList<IFaceletTagRecord>();
                        for (final Map.Entry<String, IFaceletTagRecord> entry : _records
                                .entrySet())
                        {
                            if (entry.getValue().getDescriptor()
                                    .getPath().equals(jar.getPath()))
                            {
                                removeRecords.add(entry.getValue());
                            }
                        }
                        
                        for (final IFaceletTagRecord removeMe : removeRecords)
                        {
                            _records.remove(removeMe);
                            fireChangeEvent(new TaglibChangedEvent(
                                    JarFileFaceletTaglibLocator.this,
                                    removeMe, null,
                                    CHANGE_TYPE.REMOVED));
                        }
                    }
                    break;
                }
            }
        });
        super.start(project);
    }

    @Override
    public void stop()
    {
        _locator.stop();
        super.stop();
    }

    @Override
    public Map<String, ? extends IFaceletTagRecord> doLocate(
            final IProject project)
    {
        return Collections.unmodifiableMap(_records);
    }

    /**
     * @param entry
     * @param defaultDtdStream
     * @throws Exception
     */
    private static List<LibJarEntry> processJar(final ClasspathJarFile cpJarFile,
            final List<IMatcher> jarEntryMatchers)
    {
        final List<LibJarEntry> tagLibsFound = new ArrayList<LibJarEntry>();
        long curTime = System.currentTimeMillis();
        final JarFile jarFile = cpJarFile.getJarFile();
        try
        {
            if (jarFile != null)
            {
                final JarEntryMatchingAcceptor acceptor = new JarEntryMatchingAcceptor();
                final VisitorMatcher<JarFile, JarEntry, String> matcher = new VisitorMatcher<JarFile, JarEntry, String>(
                        "", "", acceptor, jarEntryMatchers); //$NON-NLS-1$//$NON-NLS-2$
                final Collection<? extends JarEntry> matchingEntries = matcher
                        .find(jarFile);
                for (final JarEntry jarEntry : matchingEntries)
                {
                    InputStream is = null;
                    try
                    {
                        is = jarFile.getInputStream(jarEntry);
                        final String name = jarEntry.getName();
                        final TagModelLoader loader = new TagModelLoader(name);
                        loader.loadFromInputStream(is);
                        final FaceletTaglib tagLib = loader.getTaglib();
                        if (tagLib != null)
                        {
                            tagLibsFound.add(new LibJarEntry(tagLib, cpJarFile
                                    .getPath(), name));
                        }
                    } catch (final Exception ex)
                    {
                    	//Bug 325086 - Error initializing facelet registry entry
                    	StringBuffer sb = new StringBuffer("Error initializing facelet registry entry"); //$NON-NLS-1$
                    	if (jarEntry != null &&
                    			ex instanceof IOWrappedException &&
                    			ex.getCause() instanceof FeatureNotFoundException)
                    	{
                        	FeatureNotFoundException fnfex = (FeatureNotFoundException)ex.getCause();
                    		sb.append(" ("); //$NON-NLS-1$
                        	sb.append(jarFile.getName());
                        	sb.append("!"); //$NON-NLS-1$
                        	sb.append(jarEntry.getName());
                        	sb.append(" is invalid at line "); //$NON-NLS-1$
                           	sb.append(fnfex.getLine());
                           	sb.append(", column "); //$NON-NLS-1$
                           	sb.append(fnfex.getColumn());
                        	sb.append(")"); //$NON-NLS-1$
                    	}
                        FaceletCorePlugin.log(sb.toString(), ex);
                    } finally
                    {
                        if (is != null)
                        {
                            // is.close();
                        }
                    }
                }
            }
        } catch (final Exception e)
        {
            JSFCorePlugin.log(e,
                    "While locating jar based facelet tag libraries"); //$NON-NLS-1$
        } finally
        {
            if (jarFile != null)
            {
                try
                {
                    jarFile.close();
                } catch (final IOException ioe)
                {
                    FaceletCorePlugin.log("Error closing jar file", ioe); //$NON-NLS-1$
                }
            }
        }
        long time = System.currentTimeMillis()-curTime;
        if (time > 10)
        {
        	System.out.printf(
        			"Time spent in processJar for %s was %dms\n", //$NON-NLS-1$
        			cpJarFile.getPath(),
        			Long.valueOf(time));
        }
        return tagLibsFound;
    }

    private static class LibJarEntry
    {
        private final FaceletTaglib _taglib;
        private final String _entryName;
        private final IPath _iPath;

        public LibJarEntry(final FaceletTaglib taglib, final IPath iPath,
                final String entryName)
        {
            super();
            _taglib = taglib;
            _iPath = iPath;
            _entryName = entryName;
        }

        public FaceletTaglib getTaglib()
        {
            return _taglib;
        }

        public String getEntryName()
        {
            return _entryName;
        }

        public IPath getPath()
        {
            return _iPath;
        }
    }
}

Back to the top