This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.webserver;
020
021import org.apache.avro.io.DatumWriter;
022import org.apache.avro.io.EncoderFactory;
023import org.apache.avro.io.JsonEncoder;
024import org.apache.avro.specific.SpecificDatumWriter;
025import org.apache.reef.driver.evaluator.EvaluatorDescriptor;
026
027import javax.inject.Inject;
028import java.io.ByteArrayOutputStream;
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.Date;
032import java.util.List;
033import java.util.Map;
034
035/**
036 * Serializer for Evaluator list.
037 * It is the default implementation for interface EvaluatorListSerializer.
038 */
039public class AvroEvaluatorListSerializer implements EvaluatorListSerializer {
040
041  @Inject
042  AvroEvaluatorListSerializer() {
043  }
044
045  /**
046   * Build AvroEvaluatorList object.
047   */
048  @Override
049  public AvroEvaluatorList toAvro(
050      final Map<String, EvaluatorDescriptor> evaluatorMap,
051      final int totalEvaluators, final String startTime) {
052
053    final List<AvroEvaluatorEntry> evaluatorEntities = new ArrayList<>();
054
055    for (final Map.Entry<String, EvaluatorDescriptor> entry : evaluatorMap.entrySet()) {
056      final EvaluatorDescriptor descriptor = entry.getValue();
057      evaluatorEntities.add(AvroEvaluatorEntry.newBuilder()
058              .setId(entry.getKey())
059              .setName(descriptor.getNodeDescriptor().getName())
060              .build());
061    }
062
063    return AvroEvaluatorList.newBuilder()
064        .setEvaluators(evaluatorEntities)
065        .setTotal(totalEvaluators)
066        .setStartTime(startTime != null ? startTime : new Date().toString())
067        .build();
068  }
069
070  /**
071   * Convert AvroEvaluatorList to JSON string.
072   */
073  @Override
074  public String toString(final AvroEvaluatorList avroEvaluatorList) {
075    final DatumWriter<AvroEvaluatorList> evaluatorWriter = new SpecificDatumWriter<>(AvroEvaluatorList.class);
076    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
077      final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(avroEvaluatorList.getSchema(), out);
078      evaluatorWriter.write(avroEvaluatorList, encoder);
079      encoder.flush();
080      return out.toString(AvroHttpSerializer.JSON_CHARSET);
081    } catch (final IOException e) {
082      throw new RuntimeException(e);
083    }
084  }
085}