Example usage for org.apache.commons.vfs2 FileName getRootURI

List of usage examples for org.apache.commons.vfs2 FileName getRootURI

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileName getRootURI.

Prototype

String getRootURI();

Source Link

Document

Returns the root URI of the file system this file belongs to.

Usage

From source file:org.apache.accumulo.start.classloader.vfs.providers.HdfsFileSystem.java

@Override
public FileObject resolveFile(final FileName name) throws FileSystemException {

    synchronized (this) {
        if (null == this.fs) {
            final String hdfsUri = name.getRootURI();
            final Configuration conf = new Configuration(true);
            conf.set(org.apache.hadoop.fs.FileSystem.FS_DEFAULT_NAME_KEY, hdfsUri);
            this.fs = null;
            try {
                fs = org.apache.hadoop.fs.FileSystem.get(conf);
            } catch (final IOException e) {
                log.error("Error connecting to filesystem " + hdfsUri, e);
                throw new FileSystemException("Error connecting to filesystem " + hdfsUri, e);
            }//from  www.  jav a 2s  .  c  o  m
        }
    }

    boolean useCache = (null != getContext().getFileSystemManager().getFilesCache());
    FileObject file;
    if (useCache) {
        file = this.getFileFromCache(name);
    } else {
        file = null;
    }
    if (null == file) {
        String path = null;
        try {
            path = URLDecoder.decode(name.getPath(), UTF_8.name());
        } catch (final UnsupportedEncodingException e) {
            path = name.getPath();
        }
        final Path filePath = new Path(path);
        file = new HdfsFileObject((AbstractFileName) name, this, fs, filePath);
        if (useCache) {
            this.putFileToCache(file);
        }

    }

    /**
     * resync the file information if requested
     */
    if (getFileSystemManager().getCacheStrategy().equals(CacheStrategy.ON_RESOLVE)) {
        file.refresh();
    }

    return file;
}

From source file:org.pentaho.big.data.impl.cluster.NamedClusterImpl.java

private String processURLsubstitution(String incomingURL, String hdfsScheme, IMetaStore metastore,
        VariableSpace variableSpace) {//from www.java2 s  .com

    String outgoingURL = null;
    String clusterURL = null;
    if (!hdfsScheme.equals(MAPRFS_SCHEME)) {
        clusterURL = generateURL(hdfsScheme, metastore, variableSpace);
    }
    try {
        if (clusterURL == null || isHdfsHostEmpty(variableSpace)) {
            outgoingURL = incomingURL;
        } else if (incomingURL.equals("/")) {
            outgoingURL = clusterURL;
        } else if (clusterURL != null) {
            String noVariablesURL = incomingURL.replaceAll("[${}]", "/");

            String fullyQualifiedIncomingURL = incomingURL;
            if (!incomingURL.startsWith(hdfsScheme) && !incomingURL.startsWith(NC_SCHEME)) {
                fullyQualifiedIncomingURL = clusterURL + incomingURL;
                noVariablesURL = clusterURL + incomingURL.replaceAll("[${}]", "/");
            }

            UrlFileNameParser parser = new UrlFileNameParser();
            FileName fileName = parser.parseUri(null, null, noVariablesURL);
            String root = fileName.getRootURI();
            String path = fullyQualifiedIncomingURL.substring(root.length() - 1);
            StringBuffer buffer = new StringBuffer();
            // Check for a special case where a fully qualified path (one that has the protocol in it).
            // This can only happen through variable replacement. See BACKLOG-15849. When this scenario
            // occurs we do not prepend the cluster uri to the url.
            boolean prependCluster = true;
            if (variableSpace != null) {
                String filePath = variableSpace.environmentSubstitute(path);
                StringBuilder pattern = new StringBuilder();
                pattern.append("^(").append(HDFS_SCHEME).append("|").append(WASB_SCHEME).append("|")
                        .append(MAPRFS_SCHEME).append("|").append(NC_SCHEME).append("):\\/\\/");
                Pattern r = Pattern.compile(pattern.toString());
                Matcher m = r.matcher(filePath);
                prependCluster = !m.find();
            }
            if (prependCluster) {
                buffer.append(clusterURL);
            }
            buffer.append(path);
            outgoingURL = buffer.toString();
        }
    } catch (Exception e) {
        outgoingURL = null;
    }
    return outgoingURL;
}

From source file:org.pentaho.big.data.kettle.plugins.hdfs.trans.HadoopFileInputMeta.java

public String getUrlPath(String incomingURL) {
    String path = null;/*from   w w  w.  j av a  2s  . com*/
    FileName fileName = getUrlFileName(incomingURL);
    if (fileName != null) {
        String root = fileName.getRootURI();
        path = incomingURL.substring(root.length() - 1);
    }
    return path;
}

From source file:org.pentaho.big.data.kettle.plugins.hdfs.trans.HadoopFileOutputDialog.java

public static String getUrlPath(String incomingURL) {
    String path = incomingURL;/*from   w  ww  .  j  av a 2  s  .  co m*/
    try {
        String noVariablesURL = incomingURL.replaceAll("[${}]", "/");
        FileName fileName = KettleVFS.getInstance().getFileSystemManager().resolveURI(noVariablesURL);
        String root = fileName.getRootURI().replaceFirst("/$", "");
        if (noVariablesURL.startsWith(root)) {
            path = incomingURL.length() > root.length() ? incomingURL.substring(root.length()) : "/";
        }
    } catch (FileSystemException e) {
        path = incomingURL;
    }
    return path;
}

From source file:org.pentaho.di.core.namedcluster.NamedClusterManager.java

/**
 * This method performs the root URL substitution with the URL of the specified NamedCluster
 *
 * @param clusterName/*from ww  w .  jav  a2 s.co  m*/
 *          the NamedCluster to use to generate the URL for the substitution
 * @param incomingURL
 *          the URL whose root will be replaced
 * @param scheme
 *          the scheme to be used to generate the URL of the specified NamedCluster
 * @return the generated URL or the incoming URL if an error occurs
 */
public String processURLsubstitution(String clusterName, String incomingURL, String scheme,
        IMetaStore metastore, VariableSpace variableSpace) {
    String outgoingURL = null;
    String clusterURL = null;
    if (!scheme.equals(NamedCluster.MAPRFS_SCHEME)) {
        clusterURL = generateURL(scheme, clusterName, metastore, variableSpace);
    }
    try {
        if (clusterURL == null) {
            outgoingURL = incomingURL;
        } else if (incomingURL.equals("/")) {
            outgoingURL = clusterURL;
        } else if (clusterURL != null) {
            String noVariablesURL = incomingURL.replaceAll("[${}]", "/");

            String fullyQualifiedIncomingURL = incomingURL;
            if (!incomingURL.startsWith(scheme)) {
                fullyQualifiedIncomingURL = clusterURL + incomingURL;
                noVariablesURL = clusterURL + incomingURL.replaceAll("[${}]", "/");
            }

            UrlFileNameParser parser = new UrlFileNameParser();
            FileName fileName = parser.parseUri(null, null, noVariablesURL);
            String root = fileName.getRootURI();
            String path = fullyQualifiedIncomingURL.substring(root.length() - 1);
            StringBuffer buffer = new StringBuffer();
            buffer.append(clusterURL);
            buffer.append(path);
            outgoingURL = buffer.toString();
        }
    } catch (Exception e) {
        outgoingURL = null;
    }
    return outgoingURL;
}

From source file:org.pentaho.di.trans.steps.propertyinput.PropertyInputMetaTest.java

@Test
public void testOpenNextFile() throws Exception {

    PropertyInputMeta propertyInputMeta = Mockito.mock(PropertyInputMeta.class);
    PropertyInputData propertyInputData = new PropertyInputData();
    FileInputList fileInputList = new FileInputList();
    FileObject fileObject = Mockito.mock(FileObject.class);
    FileName fileName = Mockito.mock(FileName.class);
    Mockito.when(fileName.getRootURI()).thenReturn("testFolder");
    Mockito.when(fileName.getURI()).thenReturn("testFileName.ini");

    String header = "test ini data with umlauts";
    String key = "key";
    String testValue = "value-with-";
    String testData = "[" + header + "]\r\n" + key + "=" + testValue;
    String charsetEncode = "Windows-1252";

    InputStream inputStream = new ByteArrayInputStream(testData.getBytes(Charset.forName(charsetEncode)));
    FileContent fileContent = Mockito.mock(FileContent.class);
    Mockito.when(fileObject.getContent()).thenReturn(fileContent);
    Mockito.when(fileContent.getInputStream()).thenReturn(inputStream);
    Mockito.when(fileObject.getName()).thenReturn(fileName);
    fileInputList.addFile(fileObject);/*from   w  w w .j a  va2s  .c om*/

    propertyInputData.files = fileInputList;
    propertyInputData.propfiles = false;
    propertyInputData.realEncoding = charsetEncode;

    PropertyInput propertyInput = Mockito.mock(PropertyInput.class);

    Field logField = BaseStep.class.getDeclaredField("log");
    logField.setAccessible(true);
    logField.set(propertyInput, Mockito.mock(LogChannelInterface.class));

    Mockito.doCallRealMethod().when(propertyInput).dispose(propertyInputMeta, propertyInputData);

    propertyInput.dispose(propertyInputMeta, propertyInputData);

    Method method = PropertyInput.class.getDeclaredMethod("openNextFile");
    method.setAccessible(true);
    method.invoke(propertyInput);

    Assert.assertEquals(testValue, propertyInputData.wini.get(header).get(key));
}

From source file:org.pentaho.googledrive.vfs.GoogleDriveFileSystem.java

private synchronized FileObject processFile(FileName name, boolean useCache) throws FileSystemException {
    if (!super.getRootName().getRootURI().equals(name.getRootURI())) {
        throw new FileSystemException("vfs.provider/mismatched-fs-for-name.error",
                new Object[] { name, super.getRootName(), name.getRootURI() });
    } else {/*w  ww. j av  a  2  s .c  om*/
        FileObject file;
        if (useCache) {
            file = super.getFileFromCache(name);
        } else {
            file = null;
        }

        if (file == null) {
            try {
                file = this.createFile((AbstractFileName) name);
            } catch (Exception var5) {
                return null;
            }

            file = super.decorateFileObject(file);
            if (useCache) {
                super.putFileToCache(file);
            }
        }

        if (super.getFileSystemManager().getCacheStrategy().equals(CacheStrategy.ON_RESOLVE)) {
            file.refresh();
        }

        return file;
    }
}