Search in sources :

Example 1 with RestSpecCodec

use of com.linkedin.restli.restspec.RestSpecCodec in project rest.li by linkedin.

the class ResourceSchemaCollection method createFromIdls.

/**
   * Create {@link ResourceSchemaCollection} from idl files.
   *
   * @param restspecSearchPaths file system paths to search for idl files
   * @return constructed ResourceSchemaCollection
   */
public static ResourceSchemaCollection createFromIdls(String[] restspecSearchPaths) {
    final RestSpecCodec codec = new RestSpecCodec();
    final Map<String, ResourceSchema> resourceSchemaMap = new HashMap<String, ResourceSchema>();
    for (String path : restspecSearchPaths) {
        final File dir = new File(path);
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException(String.format("path '%s' is not a directory", dir.getAbsolutePath()));
        }
        final File[] idlFiles = dir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(RestConstants.RESOURCE_MODEL_FILENAME_EXTENSION);
            }
        });
        for (File idlFile : idlFiles) {
            try {
                final FileInputStream is = new FileInputStream(idlFile);
                final ResourceSchema resourceSchema = codec.readResourceSchema(is);
                resourceSchemaMap.put(resourceSchema.getName(), resourceSchema);
            } catch (IOException e) {
                throw new RestLiInternalException(String.format("Error loading restspec IDL file '%s'", idlFile.getName()), e);
            }
        }
    }
    return new ResourceSchemaCollection(resourceSchemaMap);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) RestSpecCodec(com.linkedin.restli.restspec.RestSpecCodec) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) FileFilter(java.io.FileFilter) File(java.io.File)

Aggregations

RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)1 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)1 RestSpecCodec (com.linkedin.restli.restspec.RestSpecCodec)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1