use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.
the class HadoopServerUtil method upload.
/**
* DOC ycbai Comment method "upload".
*
* Upload a local file to the distributed file system
*
* @param localFile the file which you want to upload from local
* @param dfsPath the path on file system which you want to upload.
* @param fileSystem
* @param classLoader
* @param monitor
* @throws OozieJobDeployException
*/
public static void upload(File localFile, String dfsPath, Object fileSystem, ClassLoader classLoader, IProgressMonitor monitor) throws HadoopServerException {
if (classLoader == null) {
classLoader = HadoopServerUtil.class.getClassLoader();
}
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.setTaskName("Upload file " + dfsPath);
BufferedInputStream istream = null;
DataOutputStream ostream = null;
Object destPath = null;
try {
// $NON-NLS-1$
destPath = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { dfsPath });
if (localFile.isDirectory()) {
// $NON-NLS-1$
ReflectionUtils.invokeMethod(fileSystem, "mkdirs", new Object[] { destPath });
monitor.worked(1);
for (File child : localFile.listFiles()) {
if (monitor.isCanceled()) {
return;
}
// $NON-NLS-1$
upload(child, dfsPath + "/" + child.getName(), fileSystem, classLoader, monitor);
}
} else if (localFile.isFile()) {
istream = new BufferedInputStream(new FileInputStream(localFile));
// $NON-NLS-1$
ostream = (DataOutputStream) ReflectionUtils.invokeMethod(fileSystem, "create", new Object[] { destPath });
int bytes;
final int taskSize = 1024;
byte[] buffer = new byte[taskSize];
while ((bytes = istream.read(buffer)) >= 0) {
if (monitor.isCanceled()) {
return;
}
ostream.write(buffer, 0, bytes);
monitor.worked(1);
}
}
} catch (Exception e) {
// $NON-NLS-1$
throw new HadoopServerException(String.format("Unable to upload file %s to %s", localFile, destPath), e);
} finally {
try {
if (istream != null) {
istream.close();
}
} catch (IOException e) {
e.printStackTrace();
// nothing we can do here
}
try {
if (ostream != null) {
ostream.close();
}
} catch (IOException e) {
e.printStackTrace();
// nothing we can do here
}
}
}
use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.
the class HadoopOperationManager method getFileContent.
public InputStream getFileContent(Object fileSystem, Object configuration, ClassLoader classLoader, String filePath) throws HadoopServerException {
InputStream stream = null;
if (fileSystem == null) {
return null;
}
ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(classLoader);
Object pathObj = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { filePath });
Object factory = ReflectionUtils.newInstance("org.apache.hadoop.io.compress.CompressionCodecFactory", classLoader, new Object[] { configuration });
Object codec = ReflectionUtils.invokeMethod(factory, "getCodec", new Object[] { pathObj });
if (codec != null) {
Object originStream = ReflectionUtils.invokeMethod(fileSystem, "open", new Object[] { pathObj });
stream = (InputStream) ReflectionUtils.invokeMethod(codec, "createInputStream", new Object[] { originStream }, java.io.InputStream.class);
} else {
stream = (InputStream) ReflectionUtils.invokeMethod(fileSystem, "open", new Object[] { pathObj });
}
} catch (Exception e) {
throw new HadoopServerException(e);
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
}
return stream;
}
use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.
the class HadoopOperationManager method getFileSize.
public long getFileSize(Object fileSystem, ClassLoader classLoader, String filePath) throws HadoopServerException {
long size = 0;
try {
Object pathObj = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { filePath });
Object fileStatus = ReflectionUtils.invokeMethod(fileSystem, "getFileStatus", new Object[] { pathObj });
size = (Long) ReflectionUtils.invokeMethod(fileStatus, "getLen", new Object[0]);
} catch (Exception e) {
throw new HadoopServerException(e);
}
return size;
}
use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.
the class HadoopOperationManager method loadHDFSFolderChildren.
public void loadHDFSFolderChildren(HDFSConnectionBean connection, Object fileSystem, ClassLoader classLoader, HDFSPath parent, String path) throws HadoopServerException {
if (connection == null || fileSystem == null || classLoader == null || parent == null || path == null) {
return;
}
ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(classLoader);
Object pathObj = ReflectionUtils.newInstance("org.apache.hadoop.fs.Path", classLoader, new Object[] { path });
Object[] statusList = (Object[]) ReflectionUtils.invokeMethod(fileSystem, "listStatus", new Object[] { pathObj });
if (statusList == null) {
return;
}
for (Object status : statusList) {
HDFSPath content = null;
Object statusPath = ReflectionUtils.invokeMethod(status, "getPath", new Object[0]);
if (statusPath == null) {
continue;
}
String pathName = (String) ReflectionUtils.invokeMethod(statusPath, "getName", new Object[0]);
if (StringUtils.isBlank(pathName)) {
continue;
}
String absolutePath = ((URI) ReflectionUtils.invokeMethod(statusPath, "toUri", new Object[0])).toString();
if (StringUtils.isBlank(absolutePath)) {
continue;
}
String relativePath = URI.create(absolutePath).getPath();
if ((Boolean) ReflectionUtils.invokeMethod(status, "isDir", new Object[0])) {
content = new HDFSFolder(parent);
} else {
content = new HDFSFile(parent);
content.setTable(createTable(trimFileExtention(pathName)));
}
content.setPath(relativePath);
content.setValue(pathName);
parent.addChild(content);
}
} catch (Exception e) {
throw new HadoopServerException(e);
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
}
}
use of org.talend.designer.hdfsbrowse.exceptions.HadoopServerException in project tbd-studio-se by Talend.
the class AbstractCheckedServiceProvider method checkService.
@Override
public boolean checkService(final HadoopServiceProperties serviceProperties, final int timeout) throws HadoopServerException {
boolean checkedOK = true;
ClassLoader classLoader = getClassLoader(serviceProperties);
try {
Thread.currentThread().setContextClassLoader(classLoader);
ICheckedWorkUnit workUnit = new CheckedWorkUnit() {
@Override
protected Object run(ClassLoader cl) throws Exception {
return check(serviceProperties, cl);
}
};
workUnit.setTimeout(timeout);
workUnit.setClassLoader(classLoader);
workUnit.execute();
} catch (Exception e) {
checkedOK = false;
throw new HadoopServerException(e);
}
return checkedOK;
}
Aggregations