Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fd710e3dce9171550a38f16a40bfd374c9fbfbf2 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Oracle. 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 - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.eclipselink.core.internal.libval;

import java.util.Set;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.util.IClassFileReader;
import org.eclipse.jdt.core.util.IFieldInfo;
import org.eclipse.jpt.common.eclipselink.core.internal.JptCommonEclipseLinkCoreMessages;
import org.eclipse.jpt.common.eclipselink.core.internal.plugin.JptCommonEclipseLinkCorePlugin;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.osgi.framework.Version;


public class EclipseLinkLibValUtil {
	
	private final static String VERSION_CLASS_PATH = "org/eclipse/persistence/Version.class"; //$NON-NLS-1$
	
	private final static String VERSION_FIELD_NAME = "version"; //$NON-NLS-1$
	
	
	public static IStatus validate(Iterable<IClasspathEntry> libraryEntries, Set<VersionRange> versionRanges) {
		Version version = null;
		for (IClasspathEntry entry : libraryEntries) {
			String versionString = null;
			if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
				IClassFileReader classReader = 
						ToolFactory.createDefaultClassFileReader(
							entry.getPath().toFile().getAbsolutePath(), VERSION_CLASS_PATH, IClassFileReader.FIELD_INFOS);
				if (classReader != null) {
					for (IFieldInfo field : classReader.getFieldInfos()) {
						if (StringTools.stringsAreEqual(field.getName(), VERSION_FIELD_NAME.toCharArray())) {
							try {
								versionString = field.getConstantValueAttribute().getConstantValue().getStringValue();
							}
							catch (Exception e) {
								// potentially a bit could go wrong with that last line, but if any
								// assumptions aren't met, there's no value
							}
							break;
						}
					}
				}
				if (versionString != null) {
					if (version != null) {
						return buildErrorStatus(JptCommonEclipseLinkCoreMessages.EclipseLinkLibraryValidator_multipleEclipseLinkVersions);
					}
					version = new Version(versionString);
				}
			}
		}
		
		if (version == null) {
			return buildErrorStatus(JptCommonEclipseLinkCoreMessages.EclipseLinkLibraryValidator_noEclipseLinkVersion);
		}
		
		for (VersionRange versionRange : versionRanges) {
			if (! versionRange.isIncluded(version)) {
				return buildErrorStatus(JptCommonEclipseLinkCoreMessages.EclipseLinkLibraryValidator_improperEclipseLinkVersion);
			}
		}
		
		return Status.OK_STATUS;
	}

	private static IStatus buildErrorStatus(String message) {
		return JptCommonEclipseLinkCorePlugin.instance().buildErrorStatus(message);
	}
}

Back to the top