Java Code Examples for org.osgi.framework.Version#parseVersion()

The following examples show how to use org.osgi.framework.Version#parseVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IOSSimulatorLaunchDelegate.java    From thym with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean preLaunchCheck(ILaunchConfiguration configuration,
		String mode, IProgressMonitor monitor) throws CoreException {
	
	XCodeBuild xcode = new XCodeBuild();
	String version = xcode.version();
	try{
		Version v = Version.parseVersion(version);
		if(v.compareTo(MIN_VERSION) < 0){
			throw new CoreException(new HybridMobileStatus(IStatus.ERROR, IOSCore.PLUGIN_ID, 300/*see org.eclipse.thym.ios.ui.xcodeVersionStatusHandler in plugin.xml*/,
					NLS.bind("Hybrid mobile projects require XCode version {0} or greater to build iOS applications",XCodeBuild.MIN_REQUIRED_VERSION ), null));
		}
	}catch (IllegalArgumentException e) {
		//We could not parse the version
		//still let the build continue 
		HybridCore.log(IStatus.WARNING, "Error parsing the xcode version. Version String is "+ version, e);
		return true;
	}

		monitor.done();
	return true;
}
 
Example 2
Source File: UtilsTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void isCompatibleSARLLibraryVersion() {
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.1"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.2"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.3"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.4"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.5"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.6"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.7"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.8"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.9"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.10"));
	assertFalse(Utils.isCompatibleSARLLibraryVersion("0.10.1"));

	assertTrue(Utils.isCompatibleSARLLibraryVersion(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING));
	assertTrue(Utils.isCompatibleSARLLibraryVersion(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + ".0"));
	assertTrue(Utils.isCompatibleSARLLibraryVersion(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + ".1"));

	Version nextVersion = Version.parseVersion(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING);
	assertFalse(Utils.isCompatibleSARLLibraryVersion(nextVersion.getMajor() + "." + (nextVersion.getMinor() + 1)));
}
 
Example 3
Source File: VersionStringValidator.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isValid ( final String value, final ConstraintValidatorContext context )
{
    if ( value == null )
    {
        return true;
    }
    if ( value.isEmpty () )
    {
        return true;
    }

    try
    {
        Version.parseVersion ( value );
        return true;
    }
    catch ( final Exception e )
    {
        return false;
    }
}
 
Example 4
Source File: OsgiUtils.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** Like {@link VersionedName#parseMaybe(String, boolean)} but enforces OSGi syntax for the input version.
 * <p> 
 * Takes a string which might be of the form "symbolic-name" or "symbolic-name:version" (or something else entirely)
 * and returns a VersionedName. The versionedName.getVersion() will be null if if there was no version in the input
 * (or returning {@link Maybe#absent()} if not valid, with a suitable error message). 
 * @deprecated since 0.12.0 use {@link VersionedName#parseMaybe(String, boolean)}; check OSGi version compatibility manually if needed 
 * (but usually not wanted, we let it convert to OSGi syntax as needed) */
@Deprecated // all usages except for tests removed
public static Maybe<VersionedName> parseOsgiIdentifier(String symbolicNameOptionalWithVersion) {
    // TODO deprecate?
    if (Strings.isBlank(symbolicNameOptionalWithVersion)) {
        return Maybe.absent("OSGi identifier is blank");
    }
    String[] parts = symbolicNameOptionalWithVersion.split(":");
    if (parts.length > 2) {
        return Maybe.absent("OSGi identifier has too many parts; max one ':' symbol");
    }
    Version v = null;
    if (parts.length == 2) {
        try {
            v = Version.parseVersion(parts[1]);
        } catch (IllegalArgumentException e) {
            return Maybe.absent("OSGi identifier has invalid version string (" + e.getMessage() + ")");
        }
    }
    return Maybe.of(new VersionedName(parts[0], v));
}
 
Example 5
Source File: InitializeLaunchConfigurations.java    From wildwebdeveloper with Eclipse Public License 2.0 6 votes vote down vote up
private static void validateNodeVersion(String nodeJsLocation) {

		String nodeVersion = null;
		String[] nodeVersionCommand = new String[] { nodeJsLocation, "-v" };

		try (BufferedReader reader = new BufferedReader(
				new InputStreamReader(Runtime.getRuntime().exec(nodeVersionCommand).getInputStream()));) {
			nodeVersion = reader.readLine();
		} catch (IOException e) {
			Activator.getDefault().getLog().log(
					new Status(IStatus.ERROR, Activator.getDefault().getBundle().getSymbolicName(), e.getMessage(), e));
		}

		if (nodeVersion == null) {
			warnNodeJSVersionCouldNotBeDetermined();
		} else {
			Version parsedVersion = Version
					.parseVersion(nodeVersion.startsWith("v") ? nodeVersion.replace("v", "") : nodeVersion);
			if (!SUPPORT_NODEJS_MAJOR_VERSIONS.contains(parsedVersion.getMajor())) {
				warnNodeJSVersionUnsupported(nodeVersion);
			}
		}
	}
 
Example 6
Source File: AbstractSREInstall.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public void setMinimalSARLVersion(String version) {
	if (isDirty()) {
		setDirty(false);
		resolveDirtyFields(true);
	}
	final String newVersion = Strings.nullToEmpty(version);
	if (!newVersion.isEmpty()) {
		// Check version number
		try {
			Version.parseVersion(newVersion);
		} catch (Throwable exception) {
			return;
		}
	}
	if (!newVersion.equals(Strings.nullToEmpty(this.minimalSarlVersion))) {
		final PropertyChangeEvent event = new PropertyChangeEvent(
				this, ISREInstallChangedListener.PROPERTY_MINIMAL_SARL_VERSION,
				this.minimalSarlVersion, newVersion);
		this.minimalSarlVersion = newVersion;
		if (this.notify) {
			SARLRuntime.fireSREChanged(event);
		}
	}
}
 
Example 7
Source File: RequirementsUtility.java    From thym with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks if the Cordova requirements are available. 
 * Returns one of the error codes from {@link CordovaCLIErrors}.
 * 
 * @param project
 * @return error code or 0
 */
private static int doCheckCordovaRequirements() {
	try {
		CordovaCLI cli = new CordovaCLI();
		ErrorDetectingCLIResult cordovaResult = cli.version(new NullProgressMonitor())
				.convertTo(ErrorDetectingCLIResult.class);
		IStatus cordovaStatus = cordovaResult.asStatus();
		if (cordovaStatus.isOK()) {
			return 0;
		}
		if (cordovaStatus.getCode() == CordovaCLIErrors.ERROR_COMMAND_MISSING) {
			// check if node.js is missing as well
			IStatus nodeStatus = cli.nodeVersion(new NullProgressMonitor())
					.convertTo(ErrorDetectingCLIResult.class)
					.asStatus();
			if(nodeStatus.getCode() == CordovaCLIErrors.ERROR_COMMAND_MISSING){
				return CordovaCLIErrors.ERROR_NODE_COMMAND_MISSING;
			}
			return CordovaCLIErrors.ERROR_CORDOVA_COMMAND_MISSING;
		}
		
		Version cVer = Version.parseVersion(cordovaResult.getMessage());
		Version mVer = Version.parseVersion(MIN_CORDOVA_VERSION);
		if(cVer.compareTo(mVer) < 0 ){
			return CordovaCLIErrors.ERROR_CORDOVA_VERSION_OLD;
		}
		return 0;
		
	} catch (CoreException e) {
		return CordovaCLIErrors.ERROR_GENERAL;
	}
}
 
Example 8
Source File: DependencyVersionPart.java    From tesb-studio-se with Apache License 2.0 5 votes vote down vote up
/**
* Validate version.
*
* @param versionString
*            the versionString
* @return the i status
*/
  private static String validateVersion(String versionString) {
      if (versionString.isEmpty()) {
          return null;
      }
      try {
          Version.parseVersion(versionString);
      } catch (IllegalArgumentException e) {
          // String errorMessage =
          // Messages.DependencyVersionPart_InvalidVersionFormat;
          return e.getMessage();
      }
      return null;
  }
 
Example 9
Source File: Utils.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Check if a version is compatible with the expected SARL library.
 *
 * @param version - the version to test.
 * @return {@code true} if a compatible SARL library was found.
 *     Otherwise {@code false}.
 */
public static boolean isCompatibleSARLLibraryVersion(String version) {
	if (version != null) {
		final Version currentVersion = Version.parseVersion(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING);
		final Version paramVersion = Version.parseVersion(version);
		return currentVersion.getMajor() == paramVersion.getMajor()
				&& currentVersion.getMinor() == paramVersion.getMinor();
	}
	return false;
}
 
Example 10
Source File: Utils.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Compare the two strings as they are version numbers.
 *
 * @param v1 - first version to compare.
 * @param v2 - second version to compare.
 * @return Negative integer of <code>v1</code> is lower than <code>v2</code>;
 *     positive integer of <code>v1</code> is greater than <code>v2</code>;
 *     {@code 0} if they are strictly equal.
 */
public static int compareVersions(String v1, String v2) {
	// Remove the SNAPSHOT version.
	//final String fixedv1 = v1.replaceFirst("-SNAPSHOT$", ""); //$NON-NLS-1$ //$NON-NLS-2$
	//final String fixedv2 = v2.replaceFirst("-SNAPSHOT$", ""); //$NON-NLS-1$ //$NON-NLS-2$
	//final Version vobject1 = Version.parseVersion(fixedv1);
	//final Version vobject2 = Version.parseVersion(fixedv2);
	final Version vobject1 = Version.parseVersion(v1);
	final Version vobject2 = Version.parseVersion(v2);
	return vobject1.compareTo(vobject2);
}
 
Example 11
Source File: Utilities.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Null-safe version parser.
 *
 * @param version the version string.
 * @return the version.
 */
public static Version parseVersion(String version) {
	if (!Strings.isNullOrEmpty(version)) {
		try {
			return Version.parseVersion(version);
		} catch (Throwable exception) {
			//
		}
	}
	return null;
}
 
Example 12
Source File: ProductVersion.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
public static boolean canBeImported(final String version) {
    if (version == null) {
        return true;
    }
    final Version initialVersion = new Version("6.0.0");
    Version current = new Version("0.0.0");
    try {
        current = Version.parseVersion(version);
    } catch (final IllegalArgumentException e) {
        return false;
    }
    final Version productVersion = new Version(CURRENT_VERSION);
    return current.compareTo(productVersion) <= 0 && current.compareTo(initialVersion) >= 0;
}
 
Example 13
Source File: VersionedName.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Nullable
public Version getOsgiVersion() {
    if (cachedOsgiVersion==null && v!=null) {
        cachedOsgiVersion = v==null ? null : Version.parseVersion(BrooklynVersionSyntax.toValidOsgiVersion(v));
    }
    return cachedOsgiVersion;
}
 
Example 14
Source File: VersionUtil.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Parse the raw output and return a {@link Version} instance out of it.
 * 
 * @param rawOutput
 * @return A {@link Version} instance. Null if the output did not contain a parseable version number.
 */
public static Version parseVersion(String rawOutput)
{
	Pattern pattern = Pattern.compile(VERSION_SPLIT_PATTERN);
	Matcher matcher = pattern.matcher(rawOutput);
	if (matcher.find())
	{
		String major = matcher.group(1);
		String minor = matcher.group(2);
		String micro;
		String qualifier;
		if (matcher.group(5) != null)
		{
			// We have 3 parts with an optional qualifier
			micro = matcher.group(6);
			qualifier = matcher.group(7);
		}
		else
		{ // We have a major and minor with optional qualifier
			qualifier = matcher.group(4);
			micro = "0"; //$NON-NLS-1$
		}
		String version = major + '.' + minor + '.' + micro;
		if (!StringUtil.isEmpty(qualifier))
		{
			char c = qualifier.charAt(0);
			switch (c)
			{
				case '-':
				case '_':
				case '.':
					qualifier = qualifier.substring(1);
					break;

				default:
					break;
			}
			version = version + '.' + qualifier;
		}
		try
		{
			return Version.parseVersion(version);
		}
		catch (IllegalArgumentException iae)
		{
			// Should never happen, since the matcher found it. But just in case.
			IdeLog.logError(CorePlugin.getDefault(), "Error parsing the version string - " + version, iae); //$NON-NLS-1$
		}
	}
	return null;
}
 
Example 15
Source File: RepositoryXmlParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static Object convertValueIfNecessary(Object value, String type)
    throws Exception {
  if(type == null) {
    // No conversion needed
  } else if("String".equals(type)) {
    // No conversion needed
  } else if ("Version".equals(type)) {
    value = Version.parseVersion(((String)value).trim());
  } else if ("Long".equals(type)) {
    value = Long.parseLong(((String)value).trim());
  } else if ("Double".equals(type)) {
    value = Double.parseDouble(((String)value).trim());
  } else if (type.startsWith("List<")) {
    String scalarType = type.substring("List<".length(), type.length() - 1);
    StringTokenizer values = new StringTokenizer((String)value, ",\\", true);

    ArrayList<Object> list =  new ArrayList<Object>();
    String t = null;
    String v = null;
    while(t != null || values.hasMoreTokens()) {
      if(t == null) t = values.nextToken();
      if(t.equals("\\")) {
        if(values.hasMoreTokens()) {
          t = values.nextToken();
          if(t.equals("\\")) {
            v = v == null ? "\\" : v + "\\" ;
            t = null;
          } else if(t.equals(",")) {
            v = v == null ? "," : v + "," ;
            t = null;
          } else {
            v = v == null ? "\\" : v + "\\" ;
          }
        } else {
          v = v == null ? "\\" : v + "\\" ;
          t = null;
        }
      } else if(t.equals(",")) {
        if(v == null) {
          list.add(convertValueIfNecessary("", scalarType));
        } else if(v.endsWith("\\")) {
          v += ",";
        } else {
          list.add(convertValueIfNecessary(v.trim(), scalarType));
          v = null;
        }
        t = null;
      } else {
        v = v == null ? t : v + t ;
        t = null;
      }
    }
    if(v != null) {
      list.add(convertValueIfNecessary(v.trim(), scalarType));
    }
    value = list;
  } else {
    throw new Exception("Unknown or unsupported type: " + type);
  }
  return value;
}
 
Example 16
Source File: UtilsTest.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Test
public void isCompatibleSARLLibraryOnClasspath() {
	final TypeReferences references = mock(TypeReferences.class);
	JvmType type = mock(JvmType.class);
	when(references.findDeclaredType(any(Class.class), any(Notifier.class))).thenReturn(type);
	final Notifier context = mock(Notifier.class);
	
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	JvmDeclaredType dtype = mock(JvmDeclaredType.class);
	when(references.findDeclaredType(any(Class.class), any(Notifier.class))).thenReturn(dtype);
	JvmField field = mock(JvmField.class);
	when(field.getSimpleName()).thenReturn("X");
	when(dtype.getDeclaredFields()).thenReturn(Arrays.asList(field));
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	when(field.getSimpleName()).thenReturn("SPECIFICATION_RELEASE_VERSION_STRING");
	when(field.getConstantValueAsString()).thenReturn("");
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	when(field.getConstantValueAsString()).thenReturn("2.3.4");
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	when(field.getConstantValueAsString()).thenReturn("0.9");
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	when(field.getConstantValueAsString()).thenReturn("0.10");
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	when(field.getConstantValueAsString()).thenReturn("0.10.1");
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	when(field.getConstantValueAsString()).thenReturn(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING);
	assertTrue(Utils.isCompatibleSARLLibraryOnClasspath(references, context));
	
	when(field.getConstantValueAsString()).thenReturn(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + ".0");
	assertTrue(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	when(field.getConstantValueAsString()).thenReturn(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + ".1");
	assertTrue(Utils.isCompatibleSARLLibraryOnClasspath(references, context));

	Version nextVersion = Version.parseVersion(SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING);
	when(field.getConstantValueAsString()).thenReturn(nextVersion.getMajor() + "." + (nextVersion.getMinor() + 1));
	assertFalse(Utils.isCompatibleSARLLibraryOnClasspath(references, context));
}
 
Example 17
Source File: OSGIBusListener.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Version getBundleVersion(Bundle bundle) {
    Dictionary<?, ?> headers = bundle.getHeaders();
    String version = (String) headers.get(Constants.BUNDLE_VERSION);
    return (version != null) ? Version.parseVersion(version) : Version.emptyVersion;
}
 
Example 18
Source File: ClassLoaderOsgiFramework.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public Version getVersion() {
    return Version.parseVersion(headers.get("Bundle-Version"));
}
 
Example 19
Source File: EndpointDescription.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Provide the version of the given package name.
 * 
 * The version is encoded by prefixing the given package name with
 * {@link RemoteConstants#ENDPOINT_PACKAGE_VERSION_
 * endpoint.package.version.}, and then using this as an endpoint property
 * key. For example:
 * 
 * <pre>
 * endpoint.package.version.com.acme
 * </pre>
 * 
 * The value of this property is in String format and will be converted to a
 * {@code Version} object by this method.
 * 
 * @param packageName The name of the package for which a version is
 *        requested.
 * @return The version of the specified package or
 *         {@code Version.emptyVersion} if the package has no version in
 *         this Endpoint Description.
 * @throws IllegalArgumentException If the version property value is not
 *         String.
 */
public Version getPackageVersion(String packageName) {
	String key = ENDPOINT_PACKAGE_VERSION_ + packageName;
	Object value = properties.get(key);
	String version;
	try {
		version = (String) value;
	} catch (ClassCastException e) {
		IllegalArgumentException iae = new IllegalArgumentException(key + " property value is not a String");
		iae.initCause(e);
		throw iae;
	}
	return Version.parseVersion(version);
}
 
Example 20
Source File: Bundle.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Given a precise version create a version range suitable for an import
 * package specification. Currently an input version of M.N.U.Q will result in
 * an output range "[M.N.U.Q, M+1)" following the version usage recommended by
 * OSGi (a package that is not backwards compatible must increment the major
 * number in its version number).
 *
 * @param version
 *          an OSGi compatibel version on string form.
 * @return a quoted version range starting with the given version (inclusive)
 *         ending with the next major version (exclusive). If the specified
 *         version is <code>null</code> or an empty string a <code>null</code>
 *         is returned.
 */
private static String toImportRange(final String version)
    throws IllegalArgumentException
{
  if (null == version || 0 == version.length()) {
    return null;
  }

  final Version vStart = Version.parseVersion(version);
  final Version vEnd = new Version(vStart.getMajor() + 1, 0, 0, null);
  return "\"[" + vStart.toString() + "," + vEnd.toString() + ")\"";
}