Ejemplo n.º 1
0
  private String generateEmbeddedId(String embeddedType, IdAllocator idAllocator) {
    // Component types may be in folders; strip off the folder part for starters.

    int slashx = embeddedType.lastIndexOf("/");

    String baseId = embeddedType.substring(slashx + 1).toLowerCase();

    // The idAllocator is pre-loaded with all the component ids from the template, so even
    // if the lower-case type matches the id of an existing component, there won't be a name
    // collision.

    return idAllocator.allocateId(baseId);
  }
Ejemplo n.º 2
0
  /**
   * Do you smell something? I'm smelling that this class needs to be redesigned to not need a
   * central method this large and hard to test. I think a lot of instance and local variables need
   * to be bundled up into some kind of process object. This code is effectively too big to be
   * tested except through integration testing.
   */
  private void loadTemplateForComponent(final ComponentPageElement loadingElement) {
    this.loadingElement = loadingElement;
    loadingComponentModel = loadingElement.getComponentResources().getComponentModel();

    String componentClassName = loadingComponentModel.getComponentClassName();
    ComponentTemplate template = templateSource.getTemplate(loadingComponentModel, locale);

    // When the template for a component is missing, we pretend it consists of just a RenderBody
    // phase. Missing is not an error ... many component simply do not have a template.

    if (template.isMissing()) {
      this.loadingElement.addToTemplate(new RenderBodyElement(this.loadingElement));
      return;
    }

    // Pre-allocate ids to avoid later name collisions.

    // Don't have a case-insensitive Set, so we'll make due with a Map
    Map<String, Boolean> embeddedIds = collectedEmbeddedComponentIds(loadingComponentModel);

    idAllocator.clear();

    final Map<String, Location> componentIdsMap = template.getComponentIds();

    for (String id : componentIdsMap.keySet()) {
      idAllocator.allocateId(id);
      embeddedIds.remove(id);
    }

    if (!embeddedIds.isEmpty())
      throw new RuntimeException(
          ServicesMessages.embeddedComponentsNotInTemplate(
              embeddedIds.keySet(), componentClassName, template.getResource()));

    addAttributesAsComponentBindings = false;

    // The outermost elements of the template belong in the loading component's template list,
    // not its body list. This shunt allows everyone else to not have to make that decision,
    // they can add to the "body" and (if there isn't an active component), the shunt will
    // add the element to the component's template.

    BodyPageElement shunt =
        new BodyPageElement() {
          public void addToBody(PageElement element) {
            loadingElement.addToTemplate(element);
          }
        };

    bodyPageElementStack.push(shunt);

    for (TemplateToken token : template.getTokens()) {
      switch (token.getTokenType()) {
        case TEXT:
          text((TextToken) token);
          break;

        case EXPANSION:
          expansion((ExpansionToken) token);
          break;

        case BODY:
          body();
          break;

        case START_ELEMENT:
          startElement((StartElementToken) token);
          break;

        case START_COMPONENT:
          startComponent((StartComponentToken) token);
          break;

        case ATTRIBUTE:
          attribute((AttributeToken) token);
          break;

        case END_ELEMENT:
          endElement();
          break;

        case COMMENT:
          comment((CommentToken) token);
          break;

        case BLOCK:
          block((BlockToken) token);
          break;

        case PARAMETER:
          parameter((ParameterToken) token);
          break;

        case DTD:
          dtd((DTDToken) token);
          break;

        case DEFINE_NAMESPACE_PREFIX:
          defineNamespacePrefix((DefineNamespacePrefixToken) token);
          break;

        case CDATA:
          cdata((CDATAToken) token);
          break;

        default:
          throw new IllegalStateException("Not implemented yet: " + token);
      }
    }

    // For neatness / symmetry:

    bodyPageElementStack.pop(); // the shunt

    // TODO: Check that all stacks are empty. That should never happen, as long
    // as the ComponentTemplate is valid.
  }
  // Called for each page load of administration
  public void load() {

    /* In case plugin is uncheck */
    // List all jobs
    List<String> allJobsName = new ArrayList<String>();

    // List all the jobs that use the plugin (with at least one resource)
    List<String> allExclusionJobs = new ArrayList<String>();

    // Check all projects
    for (Project<?, ?> p : Hudson.getInstance().getProjects()) {

      // Add all jobs names to the list
      allJobsName.add(p.getName());
      // We want to retrieve all components BuildWrappers
      Map<Descriptor<BuildWrapper>, BuildWrapper> buildWrappers = p.getBuildWrappers();
      // For each of them
      for (Iterator i = buildWrappers.keySet().iterator(); i.hasNext(); ) {
        Descriptor<BuildWrapper> key = (Descriptor<BuildWrapper>) i.next();

        // We check if the descriptor is "org.jvnet.hudson.plugins.exclusion.IdAllocator $
        // DescriptorImpl"
        if (buildWrappers
            .get(key)
            .getDescriptor()
            .toString()
            .split("@")[0]
            .equals("org.jvnet.hudson.plugins.exclusion.IdAllocator$DescriptorImpl")) {
          // No duplicates
          if (!allExclusionJobs.contains(p.getName())) {
            allExclusionJobs.add(p.getName());
          }
        }
      }
    }

    // We delete each job that is in the global list and not in the list of exclusions
    for (String jobName : allJobsName) {
      if (!allExclusionJobs.contains(jobName)) {
        IdAllocator.deleteList(jobName);
      }
    }

    // Set all builds to false (build = currently used)
    for (RessourcesMonitor rm : listRessources) {
      rm.setBuild(false);
    }

    // For each resource Job, set build to true if a resource is used
    for (Iterator i = IdAllocationManager.ids.keySet().iterator(); i.hasNext(); ) {
      String resource = (String) i.next();
      IdAllocator.updateBuild(
          IdAllocationManager.ids.get(resource).getProject().getName(), resource, true);
    }

    list = new ArrayList<RessourcesMonitor>();
    // Local copy of the list
    for (RessourcesMonitor rm : listRessources) {
      list.add(new RessourcesMonitor(rm.getJobName(), rm.getRessource(), rm.getBuild()));
    }
  }
 public AdministrationPanel() {
   super();
   listRessources = IdAllocator.getListRessources();
 }