com.linkedin.restli.server.util.FileClassNameScanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of restli-server Show documentation
Show all versions of restli-server Show documentation
Pegasus is a framework for building robust, scalable service architectures using dynamic discovery and simple asychronous type-checked REST + JSON APIs.
/*
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.server.util;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Scanner to find class names under a directory according to their source file names.
*
* @author Keren Jin
*/
public class FileClassNameScanner
{
/**
* Construct map from fully qualified class name to filename whose sources are found under a given source directory.
* All source files are required to have an extension. No specific extension is required.
*
* @param sourceDir the source directory to scan
* @return map from fully qualified class name to filename for scanned source files.
*/
public static Map scan(String sourceDir)
{
return scan(sourceDir, null);
}
/**
* Construct map from fully qualified class name to filename whose sources are found under a given source directory.
* All source files are required to have an extension.
*
* @param sourceDir the source directory to scan
* @param requiredExtension only include files whose extension equals to this parameter
* null if no specific extension is required
* @return map from fully qualified class name to filename for scanned source files.
*/
public static Map scan(String sourceDir, String requiredExtension)
{
final String sourceDirWithSeparator = sourceDir.endsWith(File.separator) ? sourceDir : sourceDir + File.separator;
final File dir = new File(sourceDirWithSeparator);
if (!dir.exists() || !dir.isDirectory())
{
return Collections.emptyMap();
}
// suppress the warning because of inconsistent FileUtils interface
@SuppressWarnings("unchecked")
final Collection files = (Collection) FileUtils.listFiles(dir, null, true);
final Map classFileNames = new HashMap();
final int prefixLength = sourceDirWithSeparator.length();
for (File f : files)
{
assert(f.exists() && f.isFile());
final int extensionIndex = f.getName().lastIndexOf('.');
final String filePath = f.getPath();
if (extensionIndex < 0 || !filePath.startsWith(sourceDirWithSeparator))
{
continue;
}
final int reverseExtensionIndex = f.getName().length() - extensionIndex;
final String classPathName = filePath.substring(prefixLength, filePath.length() - reverseExtensionIndex);
if (classPathName.contains("."))
{
// dot is not allowed in package name, thus not allowed in the directory path
continue;
}
if (requiredExtension != null)
{
final String extension = f.getName().substring(extensionIndex + 1);
if (!extension.equals(requiredExtension))
{
continue;
}
}
classFileNames.put(classPathName.replace(File.separator, "."),
filePath);
}
return classFileNames;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy