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.wake.impl;
020
021import org.apache.reef.wake.Identifier;
022import org.apache.reef.wake.IdentifierFactory;
023import org.apache.reef.wake.remote.exception.RemoteRuntimeException;
024import org.apache.reef.wake.remote.impl.SocketRemoteIdentifier;
025
026import javax.inject.Inject;
027import java.lang.reflect.Constructor;
028import java.lang.reflect.InvocationTargetException;
029import java.util.HashMap;
030import java.util.Map;
031import java.util.logging.Level;
032import java.util.logging.Logger;
033
034/**
035 * Default remote identifier factory that creates a specific remote identifier
036 * from a string representation.
037 * <p>
038 * A string representation is broken into two parts type and type-specific details separated by "://"
039 * A remote identifier implementation should implement a constructor that accepts a string.
040 * The factory invokes a proper constructor by reflection.
041 */
042public class DefaultIdentifierFactory implements IdentifierFactory {
043
044  private static final Logger LOG = Logger.getLogger(DefaultIdentifierFactory.class.getName());
045
046  /** Map between type and remote identifier class. */
047  private final Map<String, Class<? extends Identifier>> typeToClazzMap;
048
049  /**
050   * Constructs a default remote identifier factory.
051   */
052  @Inject
053  public DefaultIdentifierFactory() {
054    typeToClazzMap = new HashMap<>();
055    typeToClazzMap.put("socket", SocketRemoteIdentifier.class);
056  }
057
058  /**
059   * Constructs a default remote identifier factory.
060   *
061   * @param typeToClazzMap the map of type strings to classes of remote identifiers
062   */
063  public DefaultIdentifierFactory(final Map<String, Class<? extends Identifier>> typeToClazzMap) {
064    this.typeToClazzMap = typeToClazzMap;
065  }
066
067  private static final Class<?>[] CONSTRUCTOR_ARG_TYPES = {String.class};
068
069  /**
070   * Creates a new remote identifier instance.
071   *
072   * @param str a string representation
073   * @return a remote identifier
074   * @throws RemoteRuntimeException
075   */
076  @Override
077  public Identifier getNewInstance(final String str) {
078
079    final int index = str.indexOf("://");
080    if (index < 0) {
081      throw new RemoteRuntimeException("Invalid remote identifier name: " + str);
082    }
083
084    final String type = str.substring(0, index);
085    final Class<? extends Identifier> clazz = typeToClazzMap.get(type);
086
087    try {
088
089      final Constructor<? extends Identifier> constructor = clazz.getDeclaredConstructor(CONSTRUCTOR_ARG_TYPES);
090      final Object[] args = new Object[] {str.substring(index + 3)};
091
092      final Identifier instance = constructor.newInstance(args);
093      LOG.log(Level.FINER, "Created new identifier: {0} for {1}", new Object[] {instance, str});
094      return instance;
095
096    } catch (final NoSuchMethodException | SecurityException | InstantiationException
097        | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
098      LOG.log(Level.SEVERE, "Cannot create new identifier for: " + str, e);
099      throw new RemoteRuntimeException(e);
100    }
101  }
102}