Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: efc2b4e2a58f6fa9d073e83386c642db7086740e (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
/*******************************************************************************
 * Copyright (c) 2001, 2010 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/


package org.eclipse.jst.jsf.common.facet.libraryprovider;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jst.common.project.facet.core.libprov.user.KeyClassesValidator;
import org.eclipse.jst.common.project.facet.core.libprov.user.UserLibraryProviderInstallOperationConfig;
import org.eclipse.jst.jsf.common.JSFCommonPlugin;
import org.eclipse.jst.jsf.common.facet.Messages;


/**
 * Checks that a user library is version-compatible with the facet.
 *
 * @author Debajit Adhikary
 *
 */
public abstract class UserLibraryVersionValidator extends KeyClassesValidator
{
    private static final String MANIFEST_SPECIFICATION_VERSION = "Specification-Version"; //$NON-NLS-1$
    private static final String MANIFEST_IMPLEMENTATION_VERSION = "Implementation-Version"; //$NON-NLS-1$

    private final String classNameIdentifyingImplementationJar;


    /**
     * @param classNameIdentifyingImplementationJar
     */
    public UserLibraryVersionValidator (final String classNameIdentifyingImplementationJar)
    {
        this.classNameIdentifyingImplementationJar = classNameIdentifyingImplementationJar;
    }


    @Override
    public IStatus validate (final UserLibraryProviderInstallOperationConfig config)
    {
        // Check super validator
        final IStatus status = super.validate(config);
        if (status.getSeverity() != IStatus.OK)
            return status;

        // Superclass validated this lib successfully.
        // Check user library version now.
        final String facetVersion = getFacetVersion(config);
        final String libraryVersion = getLibraryVersion(config);
        return validateVersionStrings(facetVersion, libraryVersion);
    }


    /**
     * @param facetVersion
     * @param libraryVersion
     * @return the diagnostic for whether the facetVersion and libraryVersion
     *         match.
     */
    protected IStatus validateVersionStrings (final String facetVersion,
                                              final String libraryVersion)
    {
        if (facetVersion == null)
            throw new IllegalArgumentException("Cannot read facet version"); //$NON-NLS-1$

        if (libraryVersion == null)
            return new Status(IStatus.WARNING, JSFCommonPlugin.PLUGIN_ID, Messages.UserLibraryVersionValidator_cannotReadLibraryVersion);

        if (libraryVersion.compareToIgnoreCase(facetVersion) >= 0) // JSF 2.0 lib for JSF 1.2 app, JSF 1.2 lib for JSF 1.2 app
            return Status.OK_STATUS;

        // e.g. JSF 1.2 library used for a JSF 2.0 app
        return new Status(IStatus.WARNING, JSFCommonPlugin.PLUGIN_ID, Messages.UserLibraryVersionValidator_possiblyIncompatibleLibrary);
    }


    private String getFacetVersion (final UserLibraryProviderInstallOperationConfig config)
    {
        return config.getProjectFacetVersion().getVersionString();
    }


    private String getLibraryVersion (final UserLibraryProviderInstallOperationConfig config)
    {
        String libraryVersion = null;

        try
        {
            for (final IClasspathEntry cpe : config.resolve())
            {
                if (isLibrary(cpe))
                {
                    final File libraryFile = cpe.getPath().toFile();

                    if (libraryFile.exists() && isCorrectLibraryJar(cpe, this.classNameIdentifyingImplementationJar))
                    {
                        JarFile jarFile = null;
                        try
                        {
                            jarFile = new JarFile(libraryFile);
                            libraryVersion = getLibraryVersion(jarFile);
                        }
                        finally
                        {
                            if (jarFile != null)
                                jarFile.close();
                        }
                    }
                }
            }
        }
        catch (final IOException e)
        {
            JSFCommonPlugin.log(e, e.getLocalizedMessage());
        }

        return libraryVersion;
    }


    private boolean isLibrary (final IClasspathEntry cpe)
    {
        return cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY;
    }


    private boolean isCorrectLibraryJar (final IClasspathEntry cpe,
                                         final String classNameIdentifyingJar)
    throws IOException
    {
        final File libraryFile = cpe.getPath().toFile();

        if (!libraryFile.exists())
            return false;

        ZipFile zipFile = null;

        try
        {
            zipFile = new ZipFile(libraryFile);

            for (final Enumeration<? extends ZipEntry> entries = zipFile.entries(); entries.hasMoreElements();)
            {
                final ZipEntry entry = entries.nextElement();
                final String entryName = entry.getName();
                if (entryName.equals(classNameIdentifyingJar))
                    return true;
            }
        }
        finally
        {
            if (zipFile != null)
                zipFile.close();
        }

        return false;
    }


    /**
     * @param jarFile
     *            Library jar file to read
     * 
     * @return Version of the specified Jar. Uses the manifest
     *         Specification-Version entry. If that is not available, then uses
     *         the Implementation-Version entry.
     * 
     * @throws IOException
     */
    protected String getLibraryVersion (final JarFile jarFile)
    throws IOException
    {
        final Manifest manifest = jarFile.getManifest();

        if (manifest == null)
            return null;

        final Attributes attributes = manifest.getMainAttributes();

        final String specificationVersion = attributes.getValue(MANIFEST_SPECIFICATION_VERSION);
        if (specificationVersion != null)
            return specificationVersion;

        final String implementationVersion = manifest.getMainAttributes().getValue(MANIFEST_IMPLEMENTATION_VERSION);
        return implementationVersion;
    }
}

Back to the top