Exemplo n.º 1
0
  /**
   * Includes the first IP based on priority for the given type. It adds the content to the given
   * UIComponent root. If the IP content looks like a URL (contains ://), a StaticText component
   * will be added with the value of the content from the URL.
   */
  @Handler(
      id = "includeFirstIntegrationPoint",
      input = {
        @HandlerInput(name = "type", type = String.class, required = true),
        @HandlerInput(name = "root", type = UIComponent.class, required = false)
      })
  public static void includeFirstIntegrationPoint(HandlerContext handlerCtx)
      throws java.io.IOException {
    // Get the input
    String type = (String) handlerCtx.getInputValue("type");
    UIComponent root = (UIComponent) handlerCtx.getInputValue("root");

    // Get the IntegrationPoints
    FacesContext ctx = handlerCtx.getFacesContext();
    Set<IntegrationPoint> points = getSortedIntegrationPoints(getIntegrationPoints(ctx, type));
    if (points != null) {
      Iterator<IntegrationPoint> it = points.iterator();
      if (it.hasNext()) {
        // Get the first one...
        IntegrationPoint point = it.next();
        root = getIntegrationPointParent(ctx, root, point);

        // Check to see if IP points to an external URL...
        if (point.getContent().lastIndexOf("://", 15) != -1) {
          // Treat content as a url...
          URL contentURL = FileUtil.searchForFile(point.getContent(), null);
          if (contentURL == null) {
            throw new IOException("Unable to locate file: " + point.getContent());
          }

          // Read the content...
          String content = new String(FileUtil.readFromURL(contentURL));

          // Create a StaticText component and add it under the
          // "root" component.
          LayoutComponent stDesc =
              new LayoutComponent(
                  null,
                  "externalContent",
                  new ComponentType(
                      "tmpTextCT",
                      "com.sun.jsftemplating.component.factory.basic.StaticTextFactory"));
          stDesc.addOption("value", content);
          ComponentUtil.getInstance(ctx).createChildComponent(ctx, stDesc, root);
        } else {
          // Include the first one...
          includeIntegrationPoint(ctx, root, point);
        }
      }
    }
  }