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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.yarn.util.timeline;
020
021import java.io.IOException;
022import java.net.InetSocketAddress;
023
024import org.apache.hadoop.classification.InterfaceAudience.Public;
025import org.apache.hadoop.classification.InterfaceStability.Evolving;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.io.Text;
028import org.apache.hadoop.security.SecurityUtil;
029import org.apache.hadoop.yarn.conf.YarnConfiguration;
030import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider;
031import org.codehaus.jackson.JsonGenerationException;
032import org.codehaus.jackson.map.JsonMappingException;
033import org.codehaus.jackson.map.ObjectMapper;
034
035/**
036 * The helper class for the timeline module.
037 * 
038 */
039@Public
040@Evolving
041public class TimelineUtils {
042
043  private static ObjectMapper mapper;
044
045  static {
046    mapper = new ObjectMapper();
047    YarnJacksonJaxbJsonProvider.configObjectMapper(mapper);
048  }
049
050  /**
051   * Serialize a POJO object into a JSON string not in a pretty format
052   * 
053   * @param o
054   *          an object to serialize
055   * @return a JSON string
056   * @throws IOException
057   * @throws JsonMappingException
058   * @throws JsonGenerationException
059   */
060  public static String dumpTimelineRecordtoJSON(Object o)
061      throws JsonGenerationException, JsonMappingException, IOException {
062    return dumpTimelineRecordtoJSON(o, false);
063  }
064
065  /**
066   * Serialize a POJO object into a JSON string
067   * 
068   * @param o
069   *          an object to serialize
070   * @param pretty
071   *          whether in a pretty format or not
072   * @return a JSON string
073   * @throws IOException
074   * @throws JsonMappingException
075   * @throws JsonGenerationException
076   */
077  public static String dumpTimelineRecordtoJSON(Object o, boolean pretty)
078      throws JsonGenerationException, JsonMappingException, IOException {
079    if (pretty) {
080      return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
081    } else {
082      return mapper.writeValueAsString(o);
083    }
084  }
085
086  public static InetSocketAddress getTimelineTokenServiceAddress(
087      Configuration conf) {
088    InetSocketAddress timelineServiceAddr = null;
089    if (YarnConfiguration.useHttps(conf)) {
090      timelineServiceAddr = conf.getSocketAddr(
091          YarnConfiguration.TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS,
092          YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS,
093          YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_PORT);
094    } else {
095      timelineServiceAddr = conf.getSocketAddr(
096          YarnConfiguration.TIMELINE_SERVICE_WEBAPP_ADDRESS,
097          YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_ADDRESS,
098          YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_PORT);
099    }
100    return timelineServiceAddr;
101  }
102
103  public static Text buildTimelineTokenService(Configuration conf) {
104    InetSocketAddress timelineServiceAddr =
105        getTimelineTokenServiceAddress(conf);
106    return SecurityUtil.buildTokenService(timelineServiceAddr);
107  }
108}