protected void addToWar(IVirtualComponent warComp, IVirtualComponent j2eeComp, String moduleURI) {
    final IDataModel dataModel =
        DataModelFactory.createDataModel(
            new AddWebFragmentComponentToWebApplicationDataModelProvider());
    Map map =
        (Map)
            dataModel.getProperty(
                IAddWebFragmentComponentToWebApplicationDataModelProperties
                    .TARGET_COMPONENTS_TO_URI_MAP);
    map.put(j2eeComp, moduleURI);

    dataModel.setProperty(ICreateReferenceComponentsDataModelProperties.SOURCE_COMPONENT, warComp);

    List modList =
        (List)
            dataModel.getProperty(
                ICreateReferenceComponentsDataModelProperties.TARGET_COMPONENT_LIST);
    modList.add(j2eeComp);
    dataModel.setProperty(
        ICreateReferenceComponentsDataModelProperties.TARGET_COMPONENT_LIST, modList);
    dataModel.setProperty(
        ICreateReferenceComponentsDataModelProperties.TARGET_COMPONENTS_DEPLOY_PATH,
        "/WEB-INF/lib"); //$NON-NLS-1$
    try {
      dataModel.getDefaultOperation().execute(null, null);
    } catch (ExecutionException e) {
      J2EEPlugin.logError(e);
    }
  }
  private boolean hasParentInServer(IProject project, IServer server, IProgressMonitor pm) {
    IModule[] modules = ServerUtil.getModules(project);
    if (modules == null || modules.length == 0) {
      return false;
    }
    IModule[] parents = null;
    boolean parentFound = false;
    for (IModule module : modules) {
      try {
        parents = server.getRootModules(module, pm);
      } catch (CoreException e) {
        org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin.logError(e);
      }

      if (parents == null || parents.length == 0) {
        return false;
      }
      for (IModule parent : parents) {
        if (!parent.equals(module) && ServerUtil.containsModule(server, parent, pm)) {
          parentFound = true;
          break;
        }
      }
    }
    return parentFound;
  }
示例#3
0
  /** Get the WAR file for validation */
  public EObject loadEarFile() {

    IVirtualComponent comp = ComponentCore.createComponent(getProject());
    edit = ComponentUtilities.getArtifactEditForRead(comp);

    try {
      Archive archive = ((EARArtifactEdit) edit).asArchive(false, false, true);
      earFile = (EARFile) archive;
      return archive;
    } catch (OpenFailureException e1) {
      J2EEPlugin.logError(e1);
    }
    return null;
  }
  protected void installWARFacet(
      final String j2eeVersionText,
      final String warProjectName,
      final IRuntime runtime,
      final IProgressMonitor monitor) {
    IProject project = ProjectUtilities.getProject(warProjectName);
    if (project.exists()) return;

    IFacetedProjectWorkingCopy fpjwc = null;
    try {
      fpjwc = FacetedProjectFramework.createNewProject();

      fpjwc.setProjectName(warProjectName);

      if (runtime != null) {
        fpjwc.setTargetedRuntimes(Collections.singleton(runtime));
      }
      ArrayList<IProjectFacet> requiredFacets = new ArrayList<IProjectFacet>();
      requiredFacets.add(JavaFacetUtils.JAVA_FACET);
      requiredFacets.add(IJ2EEFacetConstants.DYNAMIC_WEB_FACET);
      final Collection<IProjectFacet> fixedFacets = requiredFacets;
      fpjwc.setFixedProjectFacets(new HashSet<IProjectFacet>(fixedFacets));
      // fpjwc.setFixedProjectFacets( Collections.singleton( JavaFacetUtils.JAVA_FACET) );
      fpjwc.setSelectedPreset(FacetedProjectFramework.DEFAULT_CONFIGURATION_PRESET_ID);

      if (j2eeVersionText != null) {
        final IProjectFacetVersion defaultWarFacetVersion =
            fpjwc.getProjectFacetVersion(IJ2EEFacetConstants.DYNAMIC_WEB_FACET);

        if (!defaultWarFacetVersion.getVersionString().equals(j2eeVersionText)) {
          String presetId = null;

          if (runtime != null) {
            for (IRuntimeComponent rc : runtime.getRuntimeComponents()) {
              presetId =
                  RuntimePresetMappingRegistry.INSTANCE.getPresetID(
                      rc.getRuntimeComponentType().getId(),
                      rc.getRuntimeComponentVersion().getVersionString(),
                      IJ2EEFacetConstants.DYNAMIC_WEB_FACET.getId(),
                      j2eeVersionText);

              if (presetId != null) {
                break;
              }
            }
          }

          final IProjectFacetVersion warFacetVersion =
              IJ2EEFacetConstants.DYNAMIC_WEB_FACET.getVersion(j2eeVersionText);

          // Note that the next call is necessary even if a preset is going to be selected
          // later since it allows the dynamic preset to adjust for the war facet version.
          ArrayList<IProjectFacetVersion> requiredFacetVersions =
              new ArrayList<IProjectFacetVersion>();
          requiredFacetVersions.add(JavaFacetUtils.JAVA_FACET.getVersion("1.5")); // $NON-NLS-1$
          requiredFacetVersions.add(warFacetVersion);
          final Collection<IProjectFacetVersion> fixedFacetVersions = requiredFacetVersions;
          fpjwc.setProjectFacets(new HashSet<IProjectFacetVersion>(fixedFacetVersions));

          // fpjwc.setProjectFacets( Collections.singleton( warFacetVersion ) );

          if (presetId != null) {
            fpjwc.setSelectedPreset(presetId);
          }
        }

        Set<IFacetedProject.Action> actions = fpjwc.getProjectFacetActions();
        for (IFacetedProject.Action action : actions) {
          Object actionConfig = action.getConfig();
          if (actionConfig instanceof JavaFacetInstallConfig) {
            JavaFacetInstallConfig c = (JavaFacetInstallConfig) actionConfig;
            c.setDefaultOutputFolder(
                new Path(
                    J2EEPlugin.getDefault()
                        .getJ2EEPreferences()
                        .getString(Keys.DYN_WEB_OUTPUT_FOLDER)));
          }
        }
      }
      try {
        fpjwc.commitChanges(null);
      } catch (CoreException e) {
        J2EEPlugin.logError(e);
      }
    } finally {
      if (fpjwc != null) {
        fpjwc.dispose();
      }
    }
  }
  public void execute(
      final IProject project,
      final IProjectFacetVersion fv,
      final Object cfg,
      final IProgressMonitor monitor)
      throws CoreException {
    if (monitor != null) {
      monitor.beginTask("", 1); // $NON-NLS-1$
    }

    try {
      IDataModel model = (IDataModel) cfg;

      if (monitor != null) {
        monitor.worked(1);
      }
      // Add WTP natures.

      WtpUtils.addNaturestoEAR(project);

      final IVirtualComponent c = ComponentCore.createComponent(project, false);
      c.create(0, null);

      final IVirtualFolder earroot = c.getRootFolder();
      Path contentDirPath =
          new Path(model.getStringProperty(IEarFacetInstallDataModelProperties.CONTENT_DIR));
      earroot.createLink(contentDirPath, 0, null);
      J2EEModuleVirtualComponent.setDefaultDeploymentDescriptorFolder(
          earroot, contentDirPath, null);

      try {
        ((IDataModelOperation) model.getProperty(FacetDataModelProvider.NOTIFICATION_OPERATION))
            .execute(monitor, null);
      } catch (ExecutionException e) {
        J2EEPlugin.logError(e);
      }

      if (fv == IJ2EEFacetConstants.ENTERPRISE_APPLICATION_70
          || fv == IJ2EEFacetConstants.ENTERPRISE_APPLICATION_60
          || fv == IJ2EEFacetConstants.ENTERPRISE_APPLICATION_50) {
        if (model.getBooleanProperty(IJ2EEFacetInstallDataModelProperties.GENERATE_DD)) {
          // Create the deployment descriptor (application.xml) if one doesn't exist
          IFile appXmlFile =
              earroot.getUnderlyingFolder().getFile(new Path(J2EEConstants.APPLICATION_DD_URI));
          if (!appXmlFile.exists()) {
            try {
              if (!appXmlFile.getParent().exists()
                  && (appXmlFile.getParent().getType() == IResource.FOLDER)) {
                ((IFolder) appXmlFile.getParent()).create(true, true, monitor);
              }
              String appXmlContents = null;
              if (fv == IJ2EEFacetConstants.ENTERPRISE_APPLICATION_70) {
                appXmlContents =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<application id=\"Application_ID\" version=\"7\" xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd\">\n <display-name>"
                        + XMLWriter.getEscaped(project.getName())
                        + "</display-name> \n </application> "; //$NON-NLS-1$ //$NON-NLS-2$
              } else if (fv == IJ2EEFacetConstants.ENTERPRISE_APPLICATION_60) {
                appXmlContents =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<application id=\"Application_ID\" version=\"6\" xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd\">\n <display-name>"
                        + XMLWriter.getEscaped(project.getName())
                        + "</display-name> \n </application> "; //$NON-NLS-1$ //$NON-NLS-2$
              } else {
                appXmlContents =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<application id=\"Application_ID\" version=\"5\" xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd\">\n <display-name>"
                        + XMLWriter.getEscaped(project.getName())
                        + "</display-name> \n </application> "; //$NON-NLS-1$ //$NON-NLS-2$
              }
              appXmlFile.create(
                  new ByteArrayInputStream(appXmlContents.getBytes("UTF-8")),
                  true,
                  monitor); //$NON-NLS-1$
            } catch (UnsupportedEncodingException e) {
              J2EEPlugin.logError(e);
            }
          }
        }
      } else {
        if (!earroot.getFile(J2EEConstants.APPLICATION_DD_URI).exists()) {
          String ver = model.getStringProperty(IFacetDataModelProperties.FACET_VERSION_STR);
          int nVer = J2EEVersionUtil.convertVersionStringToInt(ver);
          EARArtifactEdit.createDeploymentDescriptor(project, nVer);
        }
      }
    } finally {
      if (monitor != null) {
        monitor.done();
      }
    }
  }
 /** This returns Listener.gif */
 @Override
 public Object getImage(Object object) {
   return J2EEPlugin.getPlugin().getImage("listener"); // $NON-NLS-1$
 }
 /**
  * Return the resource locator for this item provider's resources.
  * <!-- begin-user-doc -->
  * <!--
  * end-user-doc -->
  */
 @Override
 public ResourceLocator getResourceLocator() {
   return J2EEPlugin.getDefault();
 }