@Override
  public Iterable<Cursor> makeCursors(Filter filter, Interval interval, QueryGranularity gran) {
    Interval actualInterval = interval;
    if (!actualInterval.overlaps(index.dataInterval)) {
      return ImmutableList.of();
    }

    if (actualInterval.getStart().isBefore(index.dataInterval.getStart())) {
      actualInterval = actualInterval.withStart(index.dataInterval.getStart());
    }
    if (actualInterval.getEnd().isAfter(index.dataInterval.getEnd())) {
      actualInterval = actualInterval.withEnd(index.dataInterval.getEnd());
    }

    final Iterable<Cursor> iterable;
    if (filter == null) {
      iterable = new NoFilterCursorIterable(index, actualInterval, gran);
    } else {
      Offset offset = new ConciseOffset(filter.goConcise(new MMappedInvertedIndexSelector(index)));

      iterable = new CursorIterable(index, actualInterval, gran, offset);
    }

    return FunctionalIterable.create(iterable).keep(Functions.<Cursor>identity());
  }
 @SuppressWarnings({"unchecked", "rawtypes"})
 private void validate() {
   checkNotNull(source, "Entity source");
   checkNotNull(sensor, "Sensor");
   if (readiness == null) readiness = GroovyJavaMethods.truthPredicate();
   if (postProcess == null) postProcess = (Function) Functions.identity();
 }
Ejemplo n.º 3
0
  @Test
  public void testIgnorePaths() throws IOException {
    ProjectFilesystem filesystem = EasyMock.createMock(ProjectFilesystem.class);
    EasyMock.expect(filesystem.getPathRelativizer())
        .andReturn(Functions.<String>identity())
        .times(2);
    BuildTargetParser parser = EasyMock.createMock(BuildTargetParser.class);
    EasyMock.replay(filesystem, parser);

    Reader reader =
        new StringReader(
            Joiner.on('\n').join("[project]", "ignore = .git, foo, bar/, baz//, a/b/c"));
    BuckConfig config = BuckConfig.createFromReader(reader, filesystem, parser, Platform.detect());

    ImmutableSet<String> ignorePaths = config.getIgnorePaths();
    assertEquals(
        "Should ignore paths, sans trailing slashes",
        ignorePaths,
        ImmutableSet.of(
            BuckConstant.BUCK_OUTPUT_DIRECTORY,
            ".idea",
            System.getProperty(BuckConfig.BUCK_BUCKD_DIR_KEY, ".buckd"),
            config.getCacheDir(),
            ".git",
            "foo",
            "bar",
            "baz",
            "a/b/c"));

    EasyMock.verify(filesystem, parser);
  }
Ejemplo n.º 4
0
  private Function<String, String> createFunction(boolean isForObfuscation) {
    if (!rawMap.isPresent()) {
      return Functions.identity();
    }

    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (Map.Entry<String, String> entry : rawMap.get().entrySet()) {
      String original = entry.getKey().replace('.', '/');
      String obfuscated = entry.getValue().replace('.', '/');
      builder.put(
          isForObfuscation ? original : obfuscated, isForObfuscation ? obfuscated : original);
    }
    final Map<String, String> map = builder.build();

    return new Function<String, String>() {
      @Override
      public String apply(String input) {
        String mapped = map.get(input);
        if (mapped != null) {
          return mapped;
        } else {
          return input;
        }
      }
    };
  }
 @Override
 public Iterator<Tree> childrenIterator() {
   return Iterators.concat(
       Iterators.singletonIterator(openCurlyBrace),
       properties.elementsAndSeparators(Functions.<Tree>identity()),
       Iterators.singletonIterator(closeCurlyBrace));
 }
 public Loader<Cursor> build(Context context) {
   return new ComposedCursorLoader<Cursor>(
       context,
       query.getQueryData(),
       ImmutableList.copyOf(notificationUris),
       Functions.<Cursor>identity());
 }
Ejemplo n.º 7
0
 private static Optional<Tool> getOptionalTool(
     String tool,
     ImmutableList<Path> toolSearchPaths,
     ExecutableFinder executableFinder,
     String version) {
   return getOptionalToolPath(tool, toolSearchPaths, executableFinder)
       .transform(VersionedTool.fromPath(tool, version))
       .transform(Functions.<Tool>identity());
 }
  @Test
  public void jsonRepresentationContainsAllRuleKeysWithTransform() throws IOException {
    Iterable<RuleKey> ruleKeysInATransform =
        Iterables.transform(TEST_RULE_KEYS, Functions.<RuleKey>identity());

    HttpArtifactCacheEvent.Finished finishedEvent =
        createBuilder().setRuleKeys(ruleKeysInATransform).build();
    configureEvent(finishedEvent);
    String json = JSON_CONVERTER.writeValueAsString(finishedEvent);
    assertTrue(json.contains(TEST_RULE_KEYS_JSON));
  }
Ejemplo n.º 9
0
  public SqlTask createInitialTask() {
    TaskId taskId = new TaskId("query", "stage", "task" + nextTaskId.incrementAndGet());
    URI location = URI.create("fake://task/" + taskId);

    return new SqlTask(
        taskId,
        location,
        sqlTaskExecutionFactory,
        taskNotificationExecutor,
        Functions.<SqlTask>identity(),
        new DataSize(32, MEGABYTE));
  }
Ejemplo n.º 10
0
  @Test
  public void testIgnorePathsWithRelativeCacheDir() throws IOException {
    ProjectFilesystem filesystem = EasyMock.createMock(ProjectFilesystem.class);
    EasyMock.expect(filesystem.getPathRelativizer()).andReturn(Functions.<String>identity());
    BuildTargetParser parser = EasyMock.createMock(BuildTargetParser.class);
    EasyMock.replay(filesystem, parser);

    Reader reader = new StringReader(Joiner.on('\n').join("[cache]", "dir = cache_dir"));
    BuckConfig config = BuckConfig.createFromReader(reader, filesystem, parser, Platform.detect());

    ImmutableSet<String> ignorePaths = config.getIgnorePaths();
    assertTrue(
        "Relative cache directory should be in set of ignored paths",
        ignorePaths.contains("cache_dir"));

    EasyMock.verify(filesystem, parser);
  }
Ejemplo n.º 11
0
    public Function<Path, Path> getBasePathTransform(int graphSize) {

      if (this == AUTO && graphSize < MIN_SHALLOW_GRAPH_SIZE || this == NONE) {
        return Functions.identity();
      }

      return new Function<Path, Path>() {
        @Nullable
        @Override
        public Path apply(@Nullable Path input) {
          if (input == null || input.getNameCount() <= SHALLOW_MAX_PATH_LENGTH) {
            return input;
          }
          return input.subpath(0, SHALLOW_MAX_PATH_LENGTH);
        }
      };
    }
Ejemplo n.º 12
0
  /** @return the framework search paths with any embedded macros expanded. */
  static ImmutableSet<Path> getFrameworkSearchPaths(
      Optional<ImmutableSortedSet<FrameworkPath>> frameworks,
      CxxPlatform cxxPlatform,
      SourcePathResolver resolver) {

    ImmutableSet<Path> searchPaths =
        FluentIterable.from(frameworks.get())
            .transform(
                FrameworkPath.getUnexpandedSearchPathFunction(
                    resolver.getPathFunction(), Functions.<Path>identity()))
            .toSet();

    return FluentIterable.from(Optional.of(searchPaths).or(ImmutableSet.<Path>of()))
        .transform(Functions.toStringFunction())
        .transform(CxxFlags.getTranslateMacrosFn(cxxPlatform))
        .transform(MorePaths.TO_PATH)
        .toSet();
  }
  @Override
  public void markExpiredPendingUnhealthy(
      final AutoScalingGroupMetadata group, final Collection<String> instanceIds, final long maxAge)
      throws AutoScalingMetadataException {
    final AutoScalingInstance example = exampleForGroup(group);
    example.setHealthStatus(HealthStatus.Healthy);

    final List<AutoScalingInstance> instancesToMark =
        instanceIds.isEmpty()
            ? Collections.<AutoScalingInstance>emptyList()
            : persistenceSupport.listByExample(
                example,
                LifecycleState.Pending,
                Property.forName("displayName").in(instanceIds),
                Collections.<String, String>emptyMap(),
                Functions.<AutoScalingInstance>identity());

    for (final AutoScalingInstance instance : instancesToMark) {
      try {
        persistenceSupport.updateByExample(
            AutoScalingInstance.withUuid(instance.getNaturalId()),
            group.getOwner(),
            instance.getInstanceId(),
            new Callback<AutoScalingInstance>() {
              @Override
              public void fire(final AutoScalingInstance instance) {
                if (instance.getCreationTimestamp().getTime() < maxAge) {
                  logger.info("Marking pending instance unhealthy: " + instance.getInstanceId());
                  instance.setHealthStatus(HealthStatus.Unhealthy);
                } else {
                  logger.debug(
                      "Not marking pending instance unhealthy (within timeout): "
                          + instance.getInstanceId());
                }
              }
            });
      } catch (final AutoScalingMetadataNotFoundException e) {
        // removed, no need to mark unhealthy
      }
    }
  }
  @Override
  public void markMissingInstancesUnhealthy(
      final AutoScalingGroupMetadata group, final Collection<String> instanceIds)
      throws AutoScalingMetadataException {
    final AutoScalingInstance example = exampleForGroup(group);
    example.setHealthStatus(HealthStatus.Healthy);

    final List<AutoScalingInstance> instancesToMark =
        persistenceSupport.listByExample(
            example,
            LifecycleState.InService,
            instanceIds.isEmpty()
                ? Restrictions.conjunction()
                : Restrictions.not(Property.forName("displayName").in(instanceIds)),
            Collections.<String, String>emptyMap(),
            Functions.<AutoScalingInstance>identity());

    for (final AutoScalingInstance instance : instancesToMark) {
      try {
        persistenceSupport.updateByExample(
            AutoScalingInstance.withUuid(instance.getNaturalId()),
            group.getOwner(),
            instance.getInstanceId(),
            new Callback<AutoScalingInstance>() {
              @Override
              public void fire(final AutoScalingInstance instance) {
                if (instance.healthStatusGracePeriodExpired()) {
                  logger.info("Marking instance unhealthy: " + instance.getInstanceId());
                  instance.setHealthStatus(HealthStatus.Unhealthy);
                } else {
                  logger.debug(
                      "Instance not healthy but within grace period: " + instance.getInstanceId());
                }
              }
            });
      } catch (final AutoScalingMetadataNotFoundException e) {
        // removed, no need to mark unhealthy
      }
    }
  }
  @Test
  public void shouldRewriteLineMarkers() {
    BuildRuleResolver ruleResolver =
        new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(ruleResolver);

    Path original = Paths.get("buck-out/foo#bar/world.h");
    Path finalPath = Paths.get("SANITIZED/world.h");

    HeaderPathNormalizer.Builder normalizerBuilder =
        new HeaderPathNormalizer.Builder(pathResolver, Functions.<Path>identity());
    normalizerBuilder.addHeader(new FakeSourcePath("hello/////world.h"), original);
    HeaderPathNormalizer normalizer = normalizerBuilder.build();

    DebugPathSanitizer sanitizer =
        new DebugPathSanitizer(
            9,
            File.separatorChar,
            Paths.get("PWD"),
            ImmutableBiMap.of(Paths.get("hello"), Paths.get("SANITIZED")));
    FakeProjectFilesystem fakeProjectFilesystem = new FakeProjectFilesystem();

    CxxPreprocessorOutputTransformerFactory transformer =
        new CxxPreprocessorOutputTransformerFactory(
            fakeProjectFilesystem.getRootPath(), normalizer, sanitizer);

    // Fixup line marker lines properly.
    assertThat(
        String.format("# 12 \"%s\"", Escaper.escapePathForCIncludeString(finalPath)),
        equalTo(transformer.transformLine(String.format("# 12 \"%s\"", original))));
    assertThat(
        String.format("# 12 \"%s\" 2 1", Escaper.escapePathForCIncludeString(finalPath)),
        equalTo(transformer.transformLine(String.format("# 12 \"%s\" 2 1", original))));

    // test.h isn't in the replacement map, so shouldn't be replaced.
    assertThat("# 4 \"test.h\"", equalTo(transformer.transformLine("# 4 \"test.h\"")));

    // Don't modify non-line-marker lines.
    assertThat("int main() {", equalTo(transformer.transformLine("int main() {")));
  }
Ejemplo n.º 16
0
/** Utility for configuring an action to generate a deploy archive. */
public class DeployArchiveBuilder {
  /**
   * Memory consumption of SingleJar is about 250 bytes per entry in the output file. Unfortunately,
   * the JVM tends to kill the process with an OOM long before we're at the limit. In the most
   * recent example, 400 MB of memory was enough for about 500,000 entries.
   */
  private static final String SINGLEJAR_MAX_MEMORY = "-Xmx1600m";

  private final RuleContext ruleContext;

  private final IterablesChain.Builder<Artifact> runtimeJarsBuilder = IterablesChain.builder();

  private final JavaSemantics semantics;

  private JavaTargetAttributes attributes;
  private boolean includeBuildData;
  private Compression compression = Compression.UNCOMPRESSED;
  @Nullable private Artifact runfilesMiddleman;
  private Artifact outputJar;
  @Nullable private String javaStartClass;
  private ImmutableList<String> deployManifestLines = ImmutableList.of();
  @Nullable private Artifact launcher;
  private Function<Artifact, Artifact> derivedJars = Functions.identity();

  /** Type of compression to apply to output archive. */
  public enum Compression {

    /** Output should be compressed */
    COMPRESSED,

    /** Output should not be compressed */
    UNCOMPRESSED;
  }

  /** Creates a builder using the configuration of the rule as the action configuration. */
  public DeployArchiveBuilder(JavaSemantics semantics, RuleContext ruleContext) {
    this.ruleContext = ruleContext;
    this.semantics = semantics;
  }

  /** Sets the processed attributes of the rule generating the deploy archive. */
  public DeployArchiveBuilder setAttributes(JavaTargetAttributes attributes) {
    this.attributes = attributes;
    return this;
  }

  /** Sets whether to include build-data.properties in the deploy archive. */
  public DeployArchiveBuilder setIncludeBuildData(boolean includeBuildData) {
    this.includeBuildData = includeBuildData;
    return this;
  }

  /** Sets whether to enable compression of the output deploy archive. */
  public DeployArchiveBuilder setCompression(Compression compress) {
    this.compression = Preconditions.checkNotNull(compress);
    return this;
  }

  /**
   * Sets additional dependencies to be added to the action that creates the deploy jar so that we
   * force the runtime dependencies to be built.
   */
  public DeployArchiveBuilder setRunfilesMiddleman(@Nullable Artifact runfilesMiddleman) {
    this.runfilesMiddleman = runfilesMiddleman;
    return this;
  }

  /** Sets the artifact to create with the action. */
  public DeployArchiveBuilder setOutputJar(Artifact outputJar) {
    this.outputJar = Preconditions.checkNotNull(outputJar);
    return this;
  }

  /** Sets the class to launch the Java application. */
  public DeployArchiveBuilder setJavaStartClass(@Nullable String javaStartClass) {
    this.javaStartClass = javaStartClass;
    return this;
  }

  /** Adds additional jars that should be on the classpath at runtime. */
  public DeployArchiveBuilder addRuntimeJars(Iterable<Artifact> jars) {
    this.runtimeJarsBuilder.add(jars);
    return this;
  }

  /** Sets the list of extra lines to add to the archive's MANIFEST.MF file. */
  public DeployArchiveBuilder setDeployManifestLines(Iterable<String> deployManifestLines) {
    this.deployManifestLines = ImmutableList.copyOf(deployManifestLines);
    return this;
  }

  /** Sets the optional launcher to be used as the executable for this deploy JAR */
  public DeployArchiveBuilder setLauncher(@Nullable Artifact launcher) {
    this.launcher = launcher;
    return this;
  }

  public DeployArchiveBuilder setDerivedJarFunction(Function<Artifact, Artifact> derivedJars) {
    this.derivedJars = derivedJars;
    return this;
  }

  public static CustomCommandLine.Builder defaultSingleJarCommandLine(
      Artifact outputJar,
      String javaMainClass,
      ImmutableList<String> deployManifestLines,
      Iterable<Artifact> buildInfoFiles,
      ImmutableList<Artifact> classpathResources,
      Iterable<Artifact> runtimeClasspath,
      boolean includeBuildData,
      Compression compress,
      Artifact launcher) {

    CustomCommandLine.Builder args = CustomCommandLine.builder();
    args.addExecPath("--output", outputJar);
    if (compress == Compression.COMPRESSED) {
      args.add("--compression");
    }
    args.add("--normalize");
    if (javaMainClass != null) {
      args.add("--main_class");
      args.add(javaMainClass);
    }

    if (!deployManifestLines.isEmpty()) {
      args.add("--deploy_manifest_lines");
      args.add(deployManifestLines);
    }

    if (buildInfoFiles != null) {
      for (Artifact artifact : buildInfoFiles) {
        args.addExecPath("--build_info_file", artifact);
      }
    }
    if (!includeBuildData) {
      args.add("--exclude_build_data");
    }
    if (launcher != null) {
      args.add("--java_launcher");
      args.add(launcher.getExecPathString());
    }

    args.addExecPaths("--classpath_resources", classpathResources);
    args.addExecPaths("--sources", runtimeClasspath);
    return args;
  }

  /** Computes input artifacts for a deploy archive based on the given attributes. */
  public static IterablesChain<Artifact> getArchiveInputs(JavaTargetAttributes attributes) {
    return getArchiveInputs(attributes, Functions.<Artifact>identity());
  }

  private static IterablesChain<Artifact> getArchiveInputs(
      JavaTargetAttributes attributes, Function<Artifact, Artifact> derivedJarFunction) {
    IterablesChain.Builder<Artifact> inputs = IterablesChain.builder();
    inputs.add(
        ImmutableList.copyOf(
            Iterables.transform(attributes.getRuntimeClassPathForArchive(), derivedJarFunction)));
    // TODO(bazel-team): Remove?  Resources not used as input to singlejar action
    inputs.add(ImmutableList.copyOf(attributes.getResources().values()));
    inputs.add(attributes.getClassPathResources());
    return inputs.build();
  }

  /** Builds the action as configured. */
  public void build() throws InterruptedException {
    ImmutableList<Artifact> classpathResources = attributes.getClassPathResources();
    Set<String> classPathResourceNames = new HashSet<>();
    for (Artifact artifact : classpathResources) {
      String name = artifact.getExecPath().getBaseName();
      if (!classPathResourceNames.add(name)) {
        ruleContext.attributeError(
            "classpath_resources",
            "entries must have different file names (duplicate: " + name + ")");
        return;
      }
    }

    IterablesChain<Artifact> runtimeJars = runtimeJarsBuilder.build();

    // TODO(kmb): Consider not using getArchiveInputs, specifically because we don't want/need to
    // transform anything but the runtimeClasspath and b/c we currently do it twice here and below
    IterablesChain.Builder<Artifact> inputs = IterablesChain.builder();
    inputs.add(getArchiveInputs(attributes, derivedJars));

    inputs.add(ImmutableList.copyOf(Iterables.transform(runtimeJars, derivedJars)));
    if (runfilesMiddleman != null) {
      inputs.addElement(runfilesMiddleman);
    }

    ImmutableList<Artifact> buildInfoArtifacts = ruleContext.getBuildInfo(JavaBuildInfoFactory.KEY);
    inputs.add(buildInfoArtifacts);

    Iterable<Artifact> runtimeClasspath =
        Iterables.transform(
            Iterables.concat(runtimeJars, attributes.getRuntimeClassPathForArchive()), derivedJars);

    if (launcher != null) {
      inputs.addElement(launcher);
    }

    CommandLine commandLine =
        semantics.buildSingleJarCommandLine(
            ruleContext.getConfiguration(),
            outputJar,
            javaStartClass,
            deployManifestLines,
            buildInfoArtifacts,
            classpathResources,
            runtimeClasspath,
            includeBuildData,
            compression,
            launcher);

    List<String> jvmArgs = ImmutableList.of("-client", SINGLEJAR_MAX_MEMORY);
    ResourceSet resourceSet =
        ResourceSet.createWithRamCpuIo(/*memoryMb = */ 200.0, /*cpuUsage = */ .2, /*ioUsage=*/ .2);

    // If singlejar's name ends with .jar, it is Java application, otherwise it is native.
    // TODO(asmundak): once b/28640279 is fixed (that is, the native singlejar is released),
    // eliminate this check, allowing only native singlejar.
    Artifact singlejar = getSingleJar(ruleContext);
    if (singlejar.getFilename().endsWith(".jar")) {
      ruleContext.registerAction(
          new SpawnAction.Builder()
              .addInputs(inputs.build())
              .addTransitiveInputs(JavaHelper.getHostJavabaseInputs(ruleContext))
              .addOutput(outputJar)
              .setResources(resourceSet)
              .setJarExecutable(
                  ruleContext.getHostConfiguration().getFragment(Jvm.class).getJavaExecutable(),
                  singlejar,
                  jvmArgs)
              .setCommandLine(commandLine)
              .alwaysUseParameterFile(ParameterFileType.SHELL_QUOTED)
              .setProgressMessage("Building deploy jar " + outputJar.prettyPrint())
              .setMnemonic("JavaDeployJar")
              .setExecutionInfo(ImmutableMap.of("supports-workers", "1"))
              .build(ruleContext));
    } else {
      ruleContext.registerAction(
          new SpawnAction.Builder()
              .addInputs(inputs.build())
              .addTransitiveInputs(JavaHelper.getHostJavabaseInputs(ruleContext))
              .addOutput(outputJar)
              .setResources(resourceSet)
              .setExecutable(singlejar)
              .setCommandLine(commandLine)
              .alwaysUseParameterFile(ParameterFileType.SHELL_QUOTED)
              .setProgressMessage("Building deploy jar " + outputJar.prettyPrint())
              .setMnemonic("JavaDeployJar")
              .build(ruleContext));
    }
  }

  /** Returns the SingleJar deploy jar Artifact. */
  private static Artifact getSingleJar(RuleContext ruleContext) {
    Artifact singleJar = JavaToolchainProvider.fromRuleContext(ruleContext).getSingleJar();
    if (singleJar != null) {
      return singleJar;
    }
    return ruleContext.getPrerequisiteArtifact("$singlejar", Mode.HOST);
  }
}
Ejemplo n.º 17
0
 /** Computes input artifacts for a deploy archive based on the given attributes. */
 public static IterablesChain<Artifact> getArchiveInputs(JavaTargetAttributes attributes) {
   return getArchiveInputs(attributes, Functions.<Artifact>identity());
 }
Ejemplo n.º 18
0
 @Override
 public Function<T, T> makeMetricManipulatorFn(QueryType query, MetricManipulationFn fn) {
   return Functions.identity();
 }
Ejemplo n.º 19
0
 @Override
 protected void submit(NearlineStorage storage, Iterable<RemoveRequestImpl> requests) {
   storage.remove(transform(requests, Functions.<RemoveRequest>identity()));
 }
Ejemplo n.º 20
0
 public static <T> TokenBackedBasicResultsPage<T, T> getFirstPage(
     BatchingVisitable<T> v, int numToVisitArg) {
   return getFirstPage(v, numToVisitArg, Functions.<T>identity());
 }
Ejemplo n.º 21
0
 private static Map<String, Specialization> mapToSpecialisation(
     Set<String> services, Specialization spec) {
   return Maps.<String, String, Specialization>transformValues(
       Maps.uniqueIndex(services, Functions.<String>identity()), Functions.constant(spec));
 }
Ejemplo n.º 22
0
public class Suffix {

  private Suffix() {}

  public static final String lowerUnaccentedVowels = "aeiou";
  public static final String lowerAccentedVowels = "āēīōū";

  public interface Transformer {
    public char transform(char c);
  }

  public static boolean isVowel(char c) {
    char lc = Character.toLowerCase(c);
    return lowerUnaccentedVowels.indexOf(lc) >= 0 || lowerAccentedVowels.indexOf(lc) >= 0;
  }

  public static boolean isConsonant(char c) {
    return Character.isLetter(c) && !isVowel(c);
  }

  public static boolean isAccented(char c) {
    return lowerAccentedVowels.indexOf(Character.toLowerCase(c)) >= 0;
  }

  public static char accented(char c) {
    boolean lp = Character.isLowerCase(c);
    char lc = lp ? c : Character.toLowerCase(c);
    int p = lowerUnaccentedVowels.indexOf(lc);
    if (p < 0) {
      return c;
    } else {
      char lac = lowerAccentedVowels.charAt(p);
      return lp ? lac : Character.toUpperCase(lac);
    }
  }

  public static char unaccented(char c) {
    boolean lp = Character.isLowerCase(c);
    char lc = lp ? c : Character.toLowerCase(c);
    int p = lowerAccentedVowels.indexOf(lc);
    if (p < 0) {
      return c;
    } else {
      char lac = lowerUnaccentedVowels.charAt(p);
      return lp ? lac : Character.toUpperCase(lac);
    }
  }

  public static String unaccentString(CharSequence charSequence) {
    if (charSequence == null) {
      return null;
    }
    StringBuilder sb = new StringBuilder(charSequence);
    int s = sb.length();
    for (int i = 0; i < s; i++) {
      char c = sb.charAt(i);
      if (c == ' ') {
        sb.setCharAt(i, '_');
      } else if (isAccented(c)) {
        sb.setCharAt(i, unaccented(c));
      }
    }
    return sb.toString();
  }

  public static Iterable<String> ssplitter(String str) {
    return Splitter.on(CharMatcher.BREAKING_WHITESPACE).omitEmptyStrings().split(str);
  }

  public static List<String> ssplit(String str) {
    return Lists.newArrayList(ssplitter(str));
  }

  public static String makeFormString(String fstr) {
    return CharMatcher.is('_').collapseFrom(fstr.trim(), ' ');
    // return fstr.trim().replace('_', ' ');
  }

  public static final Function<String, String> toSameString = Functions.identity();

  public static final Function<String, String> toFormString =
      new Function<String, String>() {
        @Override
        public String apply(@Nullable String s) {
          Preconditions.checkNotNull(s);
          return makeFormString(s);
        }
      };

  public static <T> Iterable<T> csplitter(String str, Function<? super String, T> tfunc) {
    str = str.trim();
    if (str.isEmpty()) {
      return Collections.emptyList();
    } else {
      return Iterables.transform(Splitter.on(',').trimResults().split(str), tfunc);
    }
  }

  public static <T> List<T> csplit(String cstr, Function<? super String, T> tfunc) {
    return ImmutableList.copyOf(csplitter(cstr, tfunc));
  }

  public static List<String> csplit(String cstr) {
    return csplit(cstr, toFormString);
  }

  public static boolean charMatch(char cc, char mc) {
    if (Character.toLowerCase(cc) == mc) {
      return true;
    } else if (mc == '*') {
      return true;
    } else if (mc == 'V') {
      return isVowel(cc);
    } else if (mc == 'C') {
      return isConsonant(cc);
    } else {
      return false;
    }
  }

  public static boolean seqMatches(CharSequence cs, String ms, int sp) {
    if (cs == null) {
      return false;
    }
    int mn = ms.length();
    int cn = cs.length();
    if (sp < 0 || sp + mn > cn) {
      return false;
    }
    for (int i = 0; i < mn; i++) {
      if (!charMatch(cs.charAt(sp + i), ms.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static boolean startMatches(CharSequence cs, String ms) {
    return seqMatches(cs, ms, 0);
  }

  public static boolean endMatches(CharSequence cs, String ms) {
    return seqMatches(cs, ms, cs.length() - ms.length());
  }

  public static String selectEndMatcher(CharSequence cs, List<String> msl) {
    for (String ms : msl) {
      if (endMatches(cs, ms)) {
        return ms;
      }
    }
    return null;
  }

  public static interface StringTest {
    public boolean test(CharSequence charSequence);
  }

  public static class EndMatcher implements StringTest {
    public final ImmutableList<String> matchStrings;

    public EndMatcher(ImmutableList<String> matchStrings) {
      this.matchStrings = matchStrings;
    }

    public boolean test(CharSequence charSequence) {
      for (String ms : matchStrings) {
        if (endMatches(charSequence, ms)) {
          return true;
        }
      }
      return false;
    }
  }

  public static EndMatcher endMatcher(String mss) {
    return new EndMatcher(ImmutableList.copyOf(csplitter(mss, toSameString)));
  }

  public static String removeEnding(String s, String e) {
    return s.endsWith(e) ? s.substring(0, s.length() - e.length()) : null;
  }

  public static boolean checkFormString(String fs) {
    int s = fs.length();
    int lc = -1;
    for (int i = 0; i < s; i++) {
      char c = fs.charAt(i);
      if (Character.isLetter(c)) {
        lc = i;
      } else if (Character.isWhitespace(c)) {
        if (lc < 0 || i - lc > 1) {
          return false;
        }
      } else {
        return false;
      }
    }
    return lc + 1 == s;
  }

  public static List<String> checkFormStrings(List<String> strings) {
    for (String fs : strings) {
      if (!checkFormString(fs)) {
        throw new IllegalArgumentException("bad form string " + fs);
      }
    }
    return strings;
  }

  static class AltsForm implements Formf {

    public final Object spec;
    public final ImmutableList<String> alts;

    public AltsForm(Object spec, List<String> strings) {
      this.spec = spec;
      this.alts = ImmutableList.copyOf(checkFormStrings(strings));
    }

    public boolean apply(IFormBuilder formBuilder, Alts.AltChooser altChooser) {
      String s = Alts.chooseElement(alts, this, altChooser);
      if (s != null) {
        formBuilder.add(s);
        return true;
      } else {
        return false;
      }
    }

    public IFormBuilder apply(Alts.AltChooser altChooser) {
      String s = Alts.chooseElement(alts, this, altChooser);
      if (s != null) {
        FormBuilder formBuilder = new FormBuilder();
        formBuilder.add(s);
        return formBuilder;
      } else {
        return null;
      }
    }

    public String toString() {
      return spec.toString() + ":" + alts.toString();
    }
  }

  public static Formf makeFormf(Object id, List<String> strings) {
    return new AltsForm(id, strings);
  }

  public static Formf makeFormf(String prefix, Object key, List<String> strings) {
    return makeFormf(prefix + "." + key.toString(), strings);
  }

  public static Formf makeFormf(Object id, String fs) {
    return makeFormf(id, Suffix.csplit(fs));
  }

  public static Formf makeFormf(String prefix, Object key, String fs) {
    return makeFormf(prefix, key, Suffix.csplit(fs));
  }
}
Ejemplo n.º 23
0
 public static File copyResources(ClassLoader classLoader, String rootPath, File toDir) {
   return copyResources(classLoader, rootPath, toDir, Functions.<String>identity());
 }
/**
 * Class used for communicating with an HTTP server via JSONP requests.
 *
 * @author [email protected] (Liz Looney)
 */
public class JsonpConnection {
  private static final int CONTACT_REQUEST_TIMER_DELAY = 1000;
  private static final int MINIMUM_UNRESPONSIVE_TIME_LIMIT = 2000;

  private static int jsonpRequestCounter = 0;
  private static final Map<String, AsyncCallback<String>> CALLBACKS =
      new HashMap<String, AsyncCallback<String>>();

  // Functions that will be used to transform the server's responses to the appropriate types.
  // Those that don't need the jsonParser can be static.
  protected static final Function<String, String> stringResponseDecoder =
      Functions.<String>identity();

  protected static final Function<String, Void> voidResponseDecoder =
      new Function<String, Void>() {
        public Void apply(String response) {
          return null;
        }
      };

  @VisibleForTesting
  public static final Function<String, Boolean> booleanResponseDecoder =
      new Function<String, Boolean>() {
        public Boolean apply(String response) {
          return Boolean.valueOf(response);
        }
      };

  @VisibleForTesting
  public static final Function<String, Integer> integerResponseDecoder =
      new Function<String, Integer>() {
        public Integer apply(String response) {
          return Integer.valueOf(response);
        }
      };

  @VisibleForTesting public final Function<String, String[]> stringArrayResponseDecoder;

  /** The connection information for the JSONP server. */
  private final JsonpConnectionInfo connInfo;

  /** The list of {@link ConnectivityListener}. */
  private final List<ConnectivityListener> connectivityListeners;

  /** A timer for sending a contact request periodically to see if the server is still alive. */
  private Timer contactTimer;

  /** The time that last contact request was sent. */
  private long contactRequestTime;

  /**
   * The time limit (in milliseconds) within which we expect to have received a response to the
   * contact request.
   */
  private int unresponsiveTimeLimit;

  /** A timer used to recognize that the server is unresponsive. */
  private Timer unresponsiveConnectionTimer;

  /** The connectivity status. If false, the server might be dead. */
  private boolean connectivityStatus;

  /** The JSON parser used to decode responses. */
  private final JSONParser jsonParser;

  /** A {@link Function} used to escape URL query parameters. */
  private final Function<String, String> escapeQueryParameterFunction;

  /**
   * Creates a JsonpConnection object with the given connection information, JSON parser, and escape
   * function.
   *
   * @param connInfo the JSONP connection information
   * @param jsonParser the JSON parser
   * @param escapeQueryParameterFunction the escape function
   */
  public JsonpConnection(
      JsonpConnectionInfo connInfo,
      final JSONParser jsonParser,
      Function<String, String> escapeQueryParameterFunction) {
    this.connInfo = connInfo;
    this.jsonParser = jsonParser;
    this.escapeQueryParameterFunction = escapeQueryParameterFunction;

    stringArrayResponseDecoder =
        new Function<String, String[]>() {
          public String[] apply(String response) {
            if (response != null) {
              JSONValue jsonValue = jsonParser.parse(response);
              if (jsonValue != null) {
                JSONArray jsonArray = jsonValue.asArray();
                int size = jsonArray.size();
                String[] strings = new String[size];
                for (int i = 0; i < size; i++) {
                  JSONValue element = jsonArray.get(i);
                  strings[i] = (element != null) ? element.asString().getString() : null;
                }
                return strings;
              }
            }
            return null;
          }
        };

    connectivityListeners = new ArrayList<ConnectivityListener>();
    connectivityStatus = true;

    unresponsiveTimeLimit = MINIMUM_UNRESPONSIVE_TIME_LIMIT;
  }

  /**
   * Sends a JSONP request by embedding a script tag into the document. Generates a new id for the
   * request.
   *
   * @param request the request to be made
   * @param parameters the parameters for the request
   * @param function a function that transforms the response into the type that the callback needs
   * @param callback the callback that should be called with the transformed response
   */
  public <T> void sendJsonpRequest(
      String request,
      Map<String, Object> parameters,
      Function<String, ? extends T> function,
      AsyncCallback<T> callback) {
    String id = getNextRequestId();
    sendJsonpRequest(id, request, parameters, function, callback);
  }

  /** Returns the next request id; */
  @VisibleForTesting
  public String getNextRequestId() {
    return "jr_" + (jsonpRequestCounter++);
  }

  /**
   * Sends a JSONP request by embedding a SCRIPT tag into the document.
   *
   * @param id the id used for the script tag and to identify the callback
   * @param request the request to be made
   * @param parameters the parameters for the request
   * @param function a function that transforms the response into the type that the callback needs
   * @param callback the callback that should be called with the transformed response
   */
  private <T> void sendJsonpRequest(
      String id,
      String request,
      Map<String, Object> parameters,
      final Function<String, ? extends T> function,
      final AsyncCallback<T> callback) {
    Preconditions.checkNotNull(id);

    // Prepare an intermediate callback that converts the String result to T.
    if (callback != null) {
      Preconditions.checkNotNull(function);
      CALLBACKS.put(
          id,
          new AsyncCallback<String>() {
            @Override
            public void onSuccess(String jsonResult) {
              T result;
              try {
                result = function.apply(jsonResult);
              } catch (RuntimeException e) {
                callback.onFailure(e);
                return;
              }
              callback.onSuccess(result);
            }

            @Override
            public void onFailure(Throwable caught) {
              callback.onFailure(caught);
            }
          });
    }

    // Insert a script tag into the document.
    Document document = Document.get();
    ScriptElement script = document.createScriptElement();
    String uri = makeURI(request, parameters, id);
    script.setSrc(uri);
    script.setId(id);
    Element bodyElement = document.getElementsByTagName("body").getItem(0);
    Element previous = document.getElementById(id);
    if (previous != null) {
      bodyElement.replaceChild(script, previous);
    } else {
      bodyElement.appendChild(script);
    }
  }

  /**
   * Sends a JSONP request by embedding a SCRIPT tag into the document and then polls for the result
   * by resending the request with the parameter {@link JsonpConstants#POLLING}. We continue polling
   * until we receive a response that is not {@link JsonpConstants#NOT_FINISHED_YET}.
   *
   * @param request the request to be made
   * @param p the parameters for the request
   * @param function a function that transforms the response into the type that the callback needs
   * @param callback the callback that should be called with the transformed response
   */
  public <T> void sendJsonpRequestAndPoll(
      final String request,
      Map<String, Object> p,
      final Function<String, ? extends T> function,
      final AsyncCallback<T> callback) {
    // Make a new parameters Map so we can add the POLLING parameter below.
    final Map<String, Object> parameters = new HashMap<String, Object>();
    if (p != null) {
      parameters.putAll(p);
    }
    final String initialRequestId = getNextRequestId();

    AsyncCallback<String> pollingCallback =
        new AsyncCallback<String>() {
          @Override
          public void onSuccess(String response) {
            if (JsonpConstants.NOT_FINISHED_YET.equals(response)) {
              // No response is available yet, create a timer to try again in a second.
              parameters.put(JsonpConstants.POLLING, initialRequestId);

              final AsyncCallback<String> pollingCallback = this;
              Timer timer =
                  new Timer() {
                    @Override
                    public void run() {
                      sendJsonpRequest(request, parameters, stringResponseDecoder, pollingCallback);
                    }
                  };
              timer.schedule(1000); // schedule the timer
            } else {
              // The response is available, convert it to the correct type T.
              T result;
              try {
                result = function.apply(response);
              } catch (RuntimeException e) {
                callback.onFailure(e);
                return;
              }
              callback.onSuccess(result);
            }
          }

          @Override
          public void onFailure(Throwable caught) {
            callback.onFailure(caught);
          }
        };

    sendJsonpRequest(initialRequestId, request, parameters, stringResponseDecoder, pollingCallback);
  }

  /**
   * Returns the URI for a JSONP request.
   *
   * @param request the request to be made
   * @param parameters the parameters for the request
   * @param id the id for the request
   */
  @VisibleForTesting
  public String makeURI(String request, Map<String, Object> parameters, String id) {
    StringBuilder sb = new StringBuilder();
    sb.append("http://127.0.0.1:").append(connInfo.getPort()).append("/").append(request);
    sb.append("?")
        .append(JsonpConstants.OUTPUT)
        .append("=")
        .append(JsonpConstants.REQUIRED_OUTPUT_VALUE);
    sb.append("&")
        .append(JsonpConstants.CALLBACK)
        .append("=")
        .append(JsonpConstants.REQUIRED_CALLBACK_VALUE);
    sb.append("&")
        .append(JsonpConstants.ID)
        .append("=")
        .append(escapeQueryParameterFunction.apply(id));
    sb.append("&").append(JsonpConstants.SECRET).append("=").append(connInfo.getSecret());
    if (parameters != null) {
      for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
        sb.append("&")
            .append(parameter.getKey())
            .append("=")
            .append(escapeQueryParameterFunction.apply(parameter.getValue().toString()));
      }
    }
    return sb.toString();
  }

  /**
   * Define the jsonpcb method that is called from javascript for all JSONP requests that we make.
   */
  public static native void defineBridgeMethod() /*-{
    $wnd.jsonpcb = function(id, success, s) {
      if (s != null) {
        s = decodeURIComponent(s);
      }
      @com.google.appinventor.client.jsonp.JsonpConnection::jsonpcb(Ljava/lang/String;ZLjava/lang/String;)
      (id, success, s);
    }
  }-*/;

  /**
   * Called from the native jsonpcb method defined above, for all JSONP requests that we make.
   *
   * @param id the id of the script tag that made the JSONP request
   * @param success whether the request was successful or not
   * @param response the response produced by the JSONP request if successful, or the exception
   *     message if not successful
   */
  public static void jsonpcb(String id, boolean success, String response) {
    // Remove the script tag from the document.
    removeScriptTag(id);

    // Remove the callback from the CALLBACKS map.
    AsyncCallback<String> callback = CALLBACKS.remove(id);

    // Call the callback's onSuccess or onFailure method.
    if (callback != null) {
      if (success) {
        callback.onSuccess(response);
      } else {
        callback.onFailure(new RuntimeException(response));
      }
    }
  }

  /** Removes the script tag with the given id from the document. */
  private static void removeScriptTag(String id) {
    Document document = Document.get();
    Element element = document.getElementById(id);
    if (element != null) {
      document.getElementsByTagName("body").getItem(0).removeChild(element);
    }
  }

  /** Removes the JSONP request with the given id. */
  public void removeJsonpRequest(String id) {
    removeScriptTag(id);
    CALLBACKS.remove(id);
  }

  /** Sends a JSONP request to the HTTP server telling it to quit. */
  public void quit() {
    sendJsonpRequest(JsonpConstants.QUIT, null, null, null);
  }

  /** Registers the given @{link ConnectivityListener}. */
  public void addConnectivityListener(ConnectivityListener listener) {
    connectivityListeners.add(listener);
    if (connectivityListeners.size() == 1) {
      createConnectivityTimers();
      contactTimer.schedule(CONTACT_REQUEST_TIMER_DELAY);
    }
  }

  /** Unregisters the given @{link ConnectivityListener}. */
  public void removeConnectivityListener(ConnectivityListener listener) {
    connectivityListeners.remove(listener);
    if (connectivityListeners.size() == 0) {
      contactTimer.cancel();
    }
  }

  private void createConnectivityTimers() {
    // We create these GWT timers lazily.
    // We must not create them during a java test because they will cause test failures.

    if (contactTimer == null) {
      // Create (but don't schedule) a timer to send a contact request.
      contactTimer =
          new Timer() {
            @Override
            public void run() {
              sendContactRequest();
            }
          };
    }
    if (unresponsiveConnectionTimer == null) {
      unresponsiveConnectionTimer =
          new Timer() {
            @Override
            public void run() {
              handleUnresponsiveConnection();
            }
          };
    }
  }

  private void sendContactRequest() {
    contactRequestTime = System.currentTimeMillis();
    unresponsiveConnectionTimer.schedule(unresponsiveTimeLimit);
    sendJsonpRequest(
        JsonpConstants.CONTACT,
        null,
        voidResponseDecoder,
        new AsyncCallback<Void>() {
          @Override
          public void onSuccess(Void response) {
            receivedContactResponse();
          }

          @Override
          public void onFailure(Throwable caught) {
            // We got a response, even if it is a failure response.
            receivedContactResponse();
          }
        });
  }

  private void receivedContactResponse() {
    int elapsedTime = (int) (System.currentTimeMillis() - contactRequestTime);

    contactRequestTime = 0;
    unresponsiveConnectionTimer.cancel();

    setConnectivityStatus(true);

    // Adjust the unresponsiveTimeLimit.
    unresponsiveTimeLimit = Math.max(MINIMUM_UNRESPONSIVE_TIME_LIMIT, elapsedTime);

    if (connectivityListeners.size() >= 1) {
      contactTimer.schedule(CONTACT_REQUEST_TIMER_DELAY);
    }
  }

  private void handleUnresponsiveConnection() {
    // Just in case the unresponsiveConnectionTimer still goes off *after* we've canceled it, check
    // that contactRequestTime is not 0 before calling setConnectivityStatus.
    if (contactRequestTime != 0) {
      setConnectivityStatus(false);
    }
  }

  private void setConnectivityStatus(boolean newConnectivityStatus) {
    // This method may be called repeatedly with true (or false). We only need to do something when
    // the connectivity status changes.
    if (connectivityStatus != newConnectivityStatus) {
      connectivityStatus = newConnectivityStatus;
      fireConnectivityStatusChange();
    }
  }

  private void fireConnectivityStatusChange() {
    // Since listeners may choose to remove themselves during their callback, we use a copy in the
    // for loop here.
    List<ConnectivityListener> listenersCopy =
        new ArrayList<ConnectivityListener>(connectivityListeners);
    for (ConnectivityListener listener : listenersCopy) {
      listener.onConnectivityStatusChange(this, connectivityStatus);
    }
  }
}
Ejemplo n.º 25
0
 @Override
 public Function<Result<SearchResultValue>, Result<SearchResultValue>> makePreComputeManipulatorFn(
     SearchQuery query, MetricManipulationFn fn) {
   return Functions.identity();
 }
 @Before
 public void setup() {
   communicator =
       new TeamCityCommunicator(
           new JsonRestRequester(new Gson(), Functions.<String>identity(), channel), "http://foo");
 }
Ejemplo n.º 27
0
 public <S, P, E, T, D extends Pda<S, P>> D create(
     Cfg<E, T> cfg, FollowerFunction<E> ff, PdaFactory<D, S, P, ? super E> fact) {
   return create(cfg, ff, Functions.<E>identity(), fact);
 }
Ejemplo n.º 28
0
 private static Map<String, MediaType> mapToMediaType(Set<String> services, MediaType type) {
   return Maps.<String, String, MediaType>transformValues(
       Maps.uniqueIndex(services, Functions.<String>identity()), Functions.constant(type));
 }
Ejemplo n.º 29
0
 public static BatchingVisitable<Long> getOrderedVisitableUsingSublists(
     final OrderedSublistProvider<Long> sublistProvider, @Inclusive final long startId) {
   return BatchingVisitables.getOrderedVisitableUsingSublists(
       sublistProvider, startId, Functions.<Long>identity());
 }
Ejemplo n.º 30
0
@ImplementedBy(FailingEntityImpl.class)
public interface FailingEntity extends TestEntity {

  @SetFromFlag("failInSubTask")
  ConfigKey<Boolean> FAIL_IN_SUB_TASK =
      ConfigKeys.newBooleanConfigKey(
          "failInSubTask",
          "Whether to throw exception in a sub-task (if true) or in current thread (if false)",
          false);

  @SetFromFlag("listener")
  ConfigKey<EventListener> LISTENER =
      ConfigKeys.newConfigKey(
          EventListener.class,
          "listener",
          "Whether to throw exception on call to start",
          EventListener.NOOP);

  @SetFromFlag("failOnStart")
  ConfigKey<Boolean> FAIL_ON_START =
      ConfigKeys.newBooleanConfigKey(
          "failOnStart", "Whether to throw exception on call to start", false);

  @SetFromFlag("failOnStop")
  ConfigKey<Boolean> FAIL_ON_STOP =
      ConfigKeys.newBooleanConfigKey(
          "failOnStop", "Whether to throw exception on call to stop", false);

  @SetFromFlag("failOnRestart")
  ConfigKey<Boolean> FAIL_ON_RESTART =
      ConfigKeys.newBooleanConfigKey(
          "failOnRestart", "Whether to throw exception on call to restart", false);

  @SetFromFlag("failOnStartCondition")
  ConfigKey<Predicate<? super FailingEntity>> FAIL_ON_START_CONDITION =
      (ConfigKey)
          ConfigKeys.newConfigKey(
              Predicate.class,
              "failOnStartCondition",
              "Whether to throw exception on call to start",
              null);

  @SetFromFlag("failOnStopCondition")
  ConfigKey<Predicate<? super FailingEntity>> FAIL_ON_STOP_CONDITION =
      (ConfigKey)
          ConfigKeys.newConfigKey(
              Predicate.class,
              "failOnStopCondition",
              "Whether to throw exception on call to stop",
              null);

  @SetFromFlag("failOnRestartCondition")
  ConfigKey<Predicate<? super FailingEntity>> FAIL_ON_RESTART_CONDITION =
      (ConfigKey)
          ConfigKeys.newConfigKey(
              Predicate.class,
              "failOnRestartCondition",
              "Whether to throw exception on call to restart",
              null);

  @SetFromFlag("exceptionClazz")
  ConfigKey<Class<? extends Exception>> EXCEPTION_CLAZZ =
      (ConfigKey)
          ConfigKeys.newConfigKey(
              Class.class,
              "exceptionClazz",
              "Type of exception to throw",
              IllegalStateException.class);

  @SetFromFlag("execOnFailure")
  ConfigKey<Function<? super FailingEntity, ?>> EXEC_ON_FAILURE =
      (ConfigKey)
          ConfigKeys.newConfigKey(
              Function.class,
              "execOnFailure",
              "Callback to execute before throwing an exception, on any failure",
              Functions.identity());

  public interface EventListener {
    public static final EventListener NOOP =
        new EventListener() {
          @Override
          public void onEvent(Entity entity, String action, Object[] args) {}
        };

    public void onEvent(Entity entity, String action, Object[] args);
  }

  public static class RecordingEventListener implements EventListener {
    public final List<Object[]> events = Lists.newCopyOnWriteArrayList();

    @Override
    public void onEvent(Entity entity, String action, Object[] args) {
      events.add(new Object[] {entity, action, args});
    }
  }
}