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.io.checkpoint; 020 021import org.apache.commons.lang.RandomStringUtils; 022import org.apache.reef.tang.annotations.Name; 023import org.apache.reef.tang.annotations.NamedParameter; 024import org.apache.reef.tang.annotations.Parameter; 025 026import javax.inject.Inject; 027 028/** 029 * A naming service that generates a random checkpoint name by appending a random alphanumeric string (suffix) 030 * of a given length to a user-supplied prefix string. 031 */ 032public class RandomNameCNS implements CheckpointNamingService { 033 034 private final String prefix; 035 private final int lengthOfRandomSuffix; 036 037 @Deprecated 038 public RandomNameCNS(@Parameter(PREFIX.class) final String prefix) { 039 this.prefix = prefix; 040 this.lengthOfRandomSuffix 041 = Integer.parseInt(LengthOfRandomSuffix.class.getAnnotation(NamedParameter.class).default_value()); 042 } 043 044 @Inject 045 private RandomNameCNS(@Parameter(PREFIX.class) final String prefix, 046 @Parameter(LengthOfRandomSuffix.class) final int lengthOfRandomSuffix) { 047 this.prefix = prefix; 048 this.lengthOfRandomSuffix = lengthOfRandomSuffix; 049 } 050 051 @Override 052 public String getNewName() { 053 return this.prefix + RandomStringUtils.randomAlphanumeric(lengthOfRandomSuffix); 054 } 055 056 /** 057 * The prefix used for the random names returned. 058 */ 059 @NamedParameter(doc = "The prefix used for the random names returned.", default_value = "checkpoint_") 060 public static class PREFIX implements Name<String> { 061 } 062 063 /** 064 * Number of alphanumeric characters in the random part of a checkpoint name. 065 */ 066 @NamedParameter(doc = "Number of alphanumeric chars in the random part of a checkpoint name.", default_value = "8") 067 public static class LengthOfRandomSuffix implements Name<Integer> { 068 } 069 070}