Esempio n. 1
0
  /**
   * Determines whether or not this is a valid {@link IRuntime} of a Karaf installation.
   *
   * @return a {@link IStatus} object indicating whether or not this is a valid Karaf runtime. A
   *     valid Karaf Runtime will return {@link Status#OK_STATUS} otherwise a status based on {@link
   *     IStatus#ERROR}
   */
  @Override
  public IStatus validate() {
    final IPath location = getRuntime().getLocation();

    if (location == null || location.isEmpty()) {
      return new Status(IStatus.ERROR, KarafWtpPluginActivator.PLUGIN_ID, 0, "", null);
    }

    final IStatus status = super.validate();
    if (!status.isOK()) {
      return status;
    }

    KarafPlatformModel karafTargetPlatform;
    try {
      karafTargetPlatform = KarafPlatformModelRegistry.findPlatformModel(location);
      if (karafTargetPlatform != null) {
        return Status.OK_STATUS;
      } else {
        return new Status(
            IStatus.ERROR,
            KarafWtpPluginActivator.PLUGIN_ID,
            0,
            "Unable to validate Karaf installation",
            null);
      }
    } catch (final CoreException e) {
      return new Status(
          IStatus.ERROR,
          KarafWtpPluginActivator.PLUGIN_ID,
          0,
          "Unable to locate Karaf platform",
          e);
    }
  }
  @Override
  public Object getAdapter(
      final Object adaptableObject, @SuppressWarnings("rawtypes") final Class adapterType) {

    // TODO: This entire method is ugly and needs to be cleaned up

    final Object adapted;
    if (KarafPlatformModel.class.equals(adapterType)
        && adaptableObject instanceof ILaunchConfiguration) {
      final ILaunchConfiguration configuration = (ILaunchConfiguration) adaptableObject;
      try {
        if (configuration
            .getAttributes()
            .containsKey(KarafLaunchConfigurationConstants.KARAF_LAUNCH_SOURCE_RUNTIME)) {
          final String platformPath =
              (String)
                  configuration
                      .getAttributes()
                      .get(KarafLaunchConfigurationConstants.KARAF_LAUNCH_SOURCE_RUNTIME);
          adapted = KarafPlatformModelRegistry.findPlatformModel(new Path(platformPath));
        } else {
          adapted = null;
        }
      } catch (final CoreException e) {
        KarafUIPluginActivator.getLogger().error("Unable to find Karaf Platform model", e);
        return null;
      }
    } else if (IKarafProject.class.equals(adapterType)
        && adaptableObject instanceof KarafPlatformModel) {
      IKarafProject karafProject = null;
      final KarafPlatformModel karafPlatformModel = (KarafPlatformModel) adaptableObject;
      for (final IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
        if (KarafProject.isKarafProject(project)) {
          karafProject = new KarafProject(project);
          if (karafPlatformModel
              .getRootDirectory()
              .equals(karafProject.getPlatformRootDirectory())) {
            break;
          }
        }
      }

      adapted = karafProject;
    } else if (IKarafProject.class.equals(adapterType) && adaptableObject instanceof IProject) {
      if (KarafProject.isKarafProject((IProject) adaptableObject)) {
        adapted = new KarafProject((IProject) adaptableObject);
      } else {
        return null;
      }
    } else {
      adapted = null;
    }

    return adapted;
  }