All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.restli.internal.server.model.RestLiApiBuilder Maven / Gradle / Ivy

Go to download

Pegasus is a framework for building robust, scalable service architectures using dynamic discovery and simple asychronous type-checked REST + JSON APIs.

There is a newer version: 27.7.18
Show newest version
/*
   Copyright (c) 2012 LinkedIn Corp.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.linkedin.restli.internal.server.model;


import com.linkedin.restli.server.ResourceConfigException;
import com.linkedin.restli.server.RestLiConfig;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *
 * @author dellamag
 */
public class RestLiApiBuilder implements RestApiBuilder
{
  private final static Logger _log = LoggerFactory.getLogger(RestLiApiBuilder.class);

  private final Set _packageNames;
  private final Set _classNames;

  public RestLiApiBuilder(final RestLiConfig config)
  {
    if (config.getResourcePackageNamesSet().isEmpty() && config.getResourceClassNamesSet().isEmpty())
    {
      throw new ResourceConfigException("At least one package containing Rest.li annotated classes or one resource class must be specified");
    }

    _packageNames = config.getResourcePackageNamesSet();
    _classNames = config.getResourceClassNamesSet();
  }

  @Override
  public Map build()
  {
    RestLiClasspathScanner scanner =
        new RestLiClasspathScanner(_packageNames, _classNames, Thread.currentThread().getContextClassLoader());
    scanner.scanPackages();
    final String errorMessage = scanner.scanClasses();
    if (!errorMessage.isEmpty())
    {
      _log.error(errorMessage);
    }

    Set> annotatedClasses = scanner.getMatchedClasses();
    if (annotatedClasses.isEmpty())
    {
      _log.info("Could not find any Rest.li annotated class in the configuration");
      return Collections.emptyMap();
    }

    return buildResourceModels(annotatedClasses);
  }

  public static Map buildResourceModels(
          final Set> restliAnnotatedClasses)
  {
    Map, ResourceModel> resourceModels = new HashMap, ResourceModel>();

    for (Class annotatedClass : restliAnnotatedClasses)
    {
      ResourceModel resourceModel = RestLiAnnotationReader.processResource(annotatedClass);
      resourceModels.put(annotatedClass, resourceModel);
    }

    Map rootResourceModels = new HashMap();

    for (Class annotatedClass : restliAnnotatedClasses)
    {
      ResourceModel resourceModel = resourceModels.get(annotatedClass);
      if (resourceModel.isRoot())
      {
        String path = "/" + resourceModel.getName();
        final ResourceModel existingResource = rootResourceModels.get(path);
        if (existingResource != null)
        {
          _log.warn(String.format("Resource classes \"%s\" and \"%s\" clash on the resource name \"%s\".",
                                  existingResource.getResourceClass().getCanonicalName(),
                                  resourceModel.getResourceClass().getCanonicalName(),
                                  existingResource.getName()));
        }
        rootResourceModels.put(path, resourceModel);
      }
      else
      {
        ResourceModel parentModel = resourceModels.get(resourceModel.getParentResourceClass());
        if (parentModel == null)
        {
          throw new ResourceConfigException("Could not find model for parent class'"
              + resourceModel.getParentResourceClass().getName() + "'for: "
              + resourceModel.getName());
        }
        resourceModel.setParentResourceModel(parentModel);
        parentModel.addSubResource(resourceModel.getName(), resourceModel);
      }
    }

    return rootResourceModels;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy