Example usage for org.apache.commons.vfs2.impl StandardFileSystemManager closeFileSystem

List of usage examples for org.apache.commons.vfs2.impl StandardFileSystemManager closeFileSystem

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.impl StandardFileSystemManager closeFileSystem.

Prototype

@Override
public void closeFileSystem(final FileSystem filesystem) 

Source Link

Document

Closes the given filesystem.

Usage

From source file:com.app.server.JarDeployer.java

/**
 * This method implements the jar deployer which configures the executor services. 
 * Frequently monitors the deploy directory and configures the executor services map 
 * once the jar is deployed in deploy directory and reconfigures if the jar is modified and 
 * placed in the deploy directory.//  w ww.j av a 2s  . c  o  m
 */
public void run() {

    StandardFileSystemManager fsManager = new StandardFileSystemManager();
    try {
        fsManager.init();
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    File file = new File(scanDirectory.split(";")[0]);
    File[] files = file.listFiles();
    CopyOnWriteArrayList<String> classList = new CopyOnWriteArrayList<String>();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory())
            continue;
        //Long lastModified=(Long) fileMap.get(files[i].getName());
        if (files[i].getName().endsWith(".jar")) {
            String filePath = files[i].getAbsolutePath();
            FileObject jarFile = null;
            try {
                jarFile = fsManager.resolveFile("jar:" + filePath);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            //logger.info("filePath"+filePath);
            filePath = filePath.substring(0, filePath.toLowerCase().lastIndexOf(".jar"));
            WebClassLoader customClassLoader = null;
            try {
                URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
                URL[] urls = loader.getURLs();
                try {
                    customClassLoader = new WebClassLoader(urls);
                    log.info(customClassLoader.geturlS());
                    customClassLoader.addURL(new URL("file:/" + files[i].getAbsolutePath()));
                    CopyOnWriteArrayList<String> jarList = new CopyOnWriteArrayList();
                    getUsersJars(new File(libDir), jarList);
                    for (String jarFilePath : jarList)
                        customClassLoader.addURL(new URL("file:/" + jarFilePath.replace("\\", "/")));
                    log.info("deploy=" + customClassLoader.geturlS());
                    this.urlClassLoaderMap.put(scanDirectory + "/" + files[i].getName(), customClassLoader);
                    jarsDeployed.add(files[i].getName());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                log.info(urlClassLoaderMap);
                getChildren(jarFile, classList);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            for (int classCount = 0; classCount < classList.size(); classCount++) {
                String classwithpackage = classList.get(classCount).substring(0,
                        classList.get(classCount).indexOf(".class"));
                classwithpackage = classwithpackage.replace("/", ".");
                log.info("classList:" + classwithpackage.replace("/", "."));
                try {
                    if (!classwithpackage.contains("$")) {
                        Class executorServiceClass = customClassLoader.loadClass(classwithpackage);
                        //log.info("executor class in ExecutorServicesConstruct"+executorServiceClass);
                        //log.info();
                        if (!executorServiceClass.isInterface()) {
                            Annotation[] classServicesAnnot = executorServiceClass.getDeclaredAnnotations();
                            if (classServicesAnnot != null) {
                                for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                    if (classServicesAnnot[annotcount] instanceof RemoteCall) {
                                        RemoteCall remoteCall = (RemoteCall) classServicesAnnot[annotcount];
                                        //registry.unbind(remoteCall.servicename());
                                        log.info(remoteCall.servicename().trim());
                                        try {
                                            //for(int count=0;count<500;count++){
                                            RemoteInterface reminterface = (RemoteInterface) UnicastRemoteObject
                                                    .exportObject((Remote) executorServiceClass.newInstance(),
                                                            2004);
                                            registry.rebind(remoteCall.servicename().trim(), reminterface);
                                            //}
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }

                        Method[] methods = executorServiceClass.getDeclaredMethods();
                        for (Method method : methods) {
                            Annotation[] annotations = method.getDeclaredAnnotations();
                            for (Annotation annotation : annotations) {
                                if (annotation instanceof ExecutorServiceAnnot) {
                                    ExecutorServiceAnnot executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                                    ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                                    executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                                    executorServiceInfo.setMethod(method);
                                    executorServiceInfo.setMethodParams(method.getParameterTypes());
                                    //log.info("method="+executorServiceAnnot.servicename());
                                    //log.info("method info="+executorServiceInfo);
                                    //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                                    executorServiceMap.put(executorServiceAnnot.servicename(),
                                            executorServiceInfo);
                                }
                            }
                        }

                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            ClassLoaderUtil.closeClassLoader(customClassLoader);
            try {
                jarFile.close();
            } catch (FileSystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fsManager.closeFileSystem(jarFile.getFileSystem());
        }
    }
    fsManager.close();
    fsManager = new StandardFileSystemManager();
    try {
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JarFileListener jarFileListener = new JarFileListener(executorServiceMap, libDir, urlClassLoaderMap,
            jarsDeployed);
    DefaultFileMonitor fm = new DefaultFileMonitor(jarFileListener);
    jarFileListener.setFm(fm);
    FileObject listendir = null;
    String[] dirsToScan = scanDirectory.split(";");
    try {
        FileSystemOptions opts = new FileSystemOptions();
        FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        fsManager.init();
        for (String dir : dirsToScan) {
            if (dir.startsWith("ftp://")) {
                listendir = fsManager.resolveFile(dir, opts);
            } else {
                listendir = fsManager.resolveFile(dir);
            }
            fm.addFile(listendir);
        }
    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    fm.setRecursive(true);
    fm.setDelay(3000);
    fm.start();
    //fsManager.close();
}

From source file:com.web.server.JarDeployer.java

/**
 * This method implements the jar deployer which configures the executor services. 
 * Frequently monitors the deploy directory and configures the executor services map 
 * once the jar is deployed in deploy directory and reconfigures if the jar is modified and 
 * placed in the deploy directory./*from   w  ww. j a  va 2 s.c  o m*/
 */
public void run() {

    StandardFileSystemManager fsManager = new StandardFileSystemManager();
    try {
        fsManager.init();
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    File file = new File(scanDirectory.split(";")[0]);
    File[] files = file.listFiles();
    CopyOnWriteArrayList<String> classList = new CopyOnWriteArrayList<String>();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory())
            continue;
        //Long lastModified=(Long) fileMap.get(files[i].getName());
        if (files[i].getName().endsWith(".jar")) {
            String filePath = files[i].getAbsolutePath();
            FileObject jarFile = null;
            try {
                jarFile = fsManager.resolveFile("jar:" + filePath);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            //logger.info("filePath"+filePath);
            filePath = filePath.substring(0, filePath.toLowerCase().lastIndexOf(".jar"));
            WebClassLoader customClassLoader = null;
            try {
                URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
                URL[] urls = loader.getURLs();
                try {
                    customClassLoader = new WebClassLoader(urls);
                    System.out.println(customClassLoader.geturlS());
                    new WebServer().addURL(new URL("file:/" + files[i].getAbsolutePath()), customClassLoader);
                    CopyOnWriteArrayList<String> jarList = new CopyOnWriteArrayList();
                    getUsersJars(new File(libDir), jarList);
                    for (String jarFilePath : jarList)
                        new WebServer().addURL(new URL("file:/" + jarFilePath.replace("\\", "/")),
                                customClassLoader);
                    System.out.println("deploy=" + customClassLoader.geturlS());
                    this.urlClassLoaderMap.put(scanDirectory + "/" + files[i].getName(), customClassLoader);
                    jarsDeployed.add(files[i].getName());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(urlClassLoaderMap);
                getChildren(jarFile, classList);
            } catch (FileSystemException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            for (int classCount = 0; classCount < classList.size(); classCount++) {
                String classwithpackage = classList.get(classCount).substring(0,
                        classList.get(classCount).indexOf(".class"));
                classwithpackage = classwithpackage.replace("/", ".");
                System.out.println("classList:" + classwithpackage.replace("/", "."));
                try {
                    if (!classwithpackage.contains("$")) {
                        Class executorServiceClass = customClassLoader.loadClass(classwithpackage);
                        //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
                        //System.out.println();
                        if (!executorServiceClass.isInterface()) {
                            Annotation[] classServicesAnnot = executorServiceClass.getDeclaredAnnotations();
                            if (classServicesAnnot != null) {
                                for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                    if (classServicesAnnot[annotcount] instanceof RemoteCall) {
                                        RemoteCall remoteCall = (RemoteCall) classServicesAnnot[annotcount];
                                        //registry.unbind(remoteCall.servicename());
                                        System.out.println(remoteCall.servicename().trim());
                                        try {
                                            //for(int count=0;count<500;count++){
                                            RemoteInterface reminterface = (RemoteInterface) UnicastRemoteObject
                                                    .exportObject((Remote) executorServiceClass.newInstance(),
                                                            2004);
                                            registry.rebind(remoteCall.servicename().trim(), reminterface);
                                            //}
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }

                        Method[] methods = executorServiceClass.getDeclaredMethods();
                        for (Method method : methods) {
                            Annotation[] annotations = method.getDeclaredAnnotations();
                            for (Annotation annotation : annotations) {
                                if (annotation instanceof ExecutorServiceAnnot) {
                                    ExecutorServiceAnnot executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                                    ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                                    executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                                    executorServiceInfo.setMethod(method);
                                    executorServiceInfo.setMethodParams(method.getParameterTypes());
                                    //System.out.println("method="+executorServiceAnnot.servicename());
                                    //System.out.println("method info="+executorServiceInfo);
                                    //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                                    executorServiceMap.put(executorServiceAnnot.servicename(),
                                            executorServiceInfo);
                                }
                            }
                        }

                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            ClassLoaderUtil.closeClassLoader(customClassLoader);
            try {
                jarFile.close();
            } catch (FileSystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fsManager.closeFileSystem(jarFile.getFileSystem());
        }
    }
    fsManager.close();
    fsManager = new StandardFileSystemManager();
    try {
        DefaultFileReplicator replicator = new DefaultFileReplicator(new File(cacheDir));
        //fsManager.setReplicator(new PrivilegedFileReplicator(replicator));
        fsManager.setTemporaryFileStore(replicator);
    } catch (FileSystemException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JarFileListener jarFileListener = new JarFileListener(executorServiceMap, libDir, urlClassLoaderMap,
            jarsDeployed);
    DefaultFileMonitor fm = new DefaultFileMonitor(jarFileListener);
    jarFileListener.setFm(fm);
    FileObject listendir = null;
    String[] dirsToScan = scanDirectory.split(";");
    try {
        FileSystemOptions opts = new FileSystemOptions();
        FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        fsManager.init();
        for (String dir : dirsToScan) {
            if (dir.startsWith("ftp://")) {
                listendir = fsManager.resolveFile(dir, opts);
            } else {
                listendir = fsManager.resolveFile(dir);
            }
            fm.addFile(listendir);
        }
    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    fm.setRecursive(true);
    fm.setDelay(3000);
    fm.start();
    //fsManager.close();
}

From source file:com.web.server.EARDeployer.java

/**
 * This method configures the executor services from the jar file.
 * //from   w w w  .  jav  a  2 s  .c  o m
 * @param jarFile
 * @param classList
 * @throws FileSystemException
 */
public void deployExecutorServicesEar(String earFileName, FileObject earFile,
        StandardFileSystemManager fsManager) throws FileSystemException {
    try {
        System.out.println("EARFILE NAMEs=" + earFileName);
        CopyOnWriteArrayList<FileObject> fileObjects = new CopyOnWriteArrayList<FileObject>();
        CopyOnWriteArrayList<FileObject> warObjects = new CopyOnWriteArrayList<FileObject>();
        ConcurrentHashMap jarClassListMap = new ConcurrentHashMap();
        CopyOnWriteArrayList<String> classList;
        obtainUrls(earFile, earFile, fileObjects, jarClassListMap, warObjects, fsManager);
        VFSClassLoader customClassLoaderBaseLib = new VFSClassLoader(
                fileObjects.toArray(new FileObject[fileObjects.size()]), fsManager,
                Thread.currentThread().getContextClassLoader());
        VFSClassLoader customClassLoader = null;
        Set keys = jarClassListMap.keySet();
        Iterator key = keys.iterator();
        FileObject jarFileObject;
        ConcurrentHashMap classLoaderPath = new ConcurrentHashMap();
        filesMap.put(earFileName, classLoaderPath);
        for (FileObject warFileObj : warObjects) {
            if (warFileObj.getName().getBaseName().endsWith(".war")) {
                //logger.info("filePath"+filePath);
                String filePath = scanDirectory + "/" + warFileObj.getName().getBaseName();
                log.info(filePath);
                String fileName = warFileObj.getName().getBaseName();
                WebClassLoader classLoader = new WebClassLoader(new URL[] {});
                log.info(classLoader);
                warDeployer.deleteDir(
                        new File(deployDirectory + "/" + fileName.substring(0, fileName.lastIndexOf(".war"))));
                new File(deployDirectory + "/" + fileName.substring(0, fileName.lastIndexOf(".war"))).mkdirs();
                log.info(deployDirectory + "/" + fileName.substring(0, fileName.lastIndexOf(".war")));
                urlClassLoaderMap.put(
                        deployDirectory + "/" + fileName.substring(0, fileName.lastIndexOf(".war")),
                        classLoader);
                classLoaderPath.put(warFileObj.getName().getBaseName(),
                        deployDirectory + "/" + fileName.substring(0, fileName.lastIndexOf(".war")));
                warDeployer.extractWar(new File(filePath), classLoader);

                if (exec != null) {
                    exec.shutdown();
                }
                new File(scanDirectory + "/" + warFileObj.getName().getBaseName()).delete();
                exec = Executors.newSingleThreadScheduledExecutor();
                exec.scheduleAtFixedRate(task, 0, 1000, TimeUnit.MILLISECONDS);
            }
        }
        for (int keyCount = 0; keyCount < keys.size(); keyCount++) {
            jarFileObject = (FileObject) key.next();
            {
                classList = (CopyOnWriteArrayList<String>) jarClassListMap.get(jarFileObject);
                customClassLoader = new VFSClassLoader(jarFileObject, fsManager, customClassLoaderBaseLib);
                this.urlClassLoaderMap.put(
                        scanDirectory + "/" + earFileName + "/" + jarFileObject.getName().getBaseName(),
                        customClassLoader);
                classLoaderPath.put(jarFileObject.getName().getBaseName(),
                        scanDirectory + "/" + earFileName + "/" + jarFileObject.getName().getBaseName());
                for (int classCount = 0; classCount < classList.size(); classCount++) {
                    String classwithpackage = classList.get(classCount).substring(0,
                            classList.get(classCount).indexOf(".class"));
                    classwithpackage = classwithpackage.replace("/", ".");
                    // System.out.println("classList:"+classwithpackage.replace("/","."));
                    try {
                        if (!classwithpackage.contains("$")) {

                            /*System.out.println("EARFILE NAME="+fileName);
                            System.out
                                  .println(scanDirectory
                            + "/"
                            + fileName
                            + "/"
                            + jarFileObject.getName()
                                  .getBaseName());
                                    
                            System.out.println(urlClassLoaderMap);*/
                            Class executorServiceClass = customClassLoader.loadClass(classwithpackage);

                            Annotation[] classServicesAnnot = executorServiceClass.getDeclaredAnnotations();

                            if (classServicesAnnot != null) {
                                for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                    if (classServicesAnnot[annotcount] instanceof RemoteCall) {
                                        RemoteCall remoteCall = (RemoteCall) classServicesAnnot[annotcount];
                                        //registry.unbind(remoteCall.servicename());
                                        System.out.println(remoteCall.servicename().trim());
                                        try {
                                            for (int count = 0; count < 2; count++) {
                                                RemoteInterface reminterface = (RemoteInterface) UnicastRemoteObject
                                                        .exportObject(
                                                                (Remote) executorServiceClass.newInstance(), 0);
                                                registry.rebind(remoteCall.servicename().trim(), reminterface);
                                            }
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                }
                            }
                            // System.out.println(executorServiceClass.newInstance());
                            // System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
                            // System.out.println();
                            Method[] methods = executorServiceClass.getDeclaredMethods();
                            for (Method method : methods) {
                                Annotation[] annotations = method.getDeclaredAnnotations();
                                for (Annotation annotation : annotations) {
                                    if (annotation instanceof ExecutorServiceAnnot) {
                                        ExecutorServiceAnnot executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                                        ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                                        executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                                        executorServiceInfo.setMethod(method);
                                        executorServiceInfo.setMethodParams(method.getParameterTypes());
                                        //                              System.out.println("serice name="
                                        //                                    + executorServiceAnnot
                                        //                                          .servicename());
                                        //                              System.out.println("method info="
                                        //                                    + executorServiceInfo);
                                        //                              System.out.println(method);
                                        // if(servicesMap.get(executorServiceAnnot.servicename())==null)throw
                                        // new Exception();
                                        executorServiceMap.put(executorServiceAnnot.servicename(),
                                                executorServiceInfo);
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            jarFileObject.close();
        }
        for (FileObject fobject : fileObjects) {
            fobject.close();
        }
        System.out.println("Channel unlocked");
        earFile.close();
        fsManager.closeFileSystem(earFile.getFileSystem());
        // ClassLoaderUtil.closeClassLoader(customClassLoader);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.wso2.carbon.connector.FileRead.java

public void connect(MessageContext messageContext) {
    String fileLocation = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.CONTENT_TYPE);
    String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_PATTERN);
    FileObject fileObj = null;/*from   w w  w  . j  av a  2  s.co  m*/
    StandardFileSystemManager manager = null;
    try {
        manager = FileConnectorUtils.getManager();
        fileObj = manager.resolveFile(fileLocation, FileConnectorUtils.init(messageContext));
        if (fileObj.exists()) {
            if (fileObj.getType() == FileType.FOLDER) {
                FileObject[] children = fileObj.getChildren();
                if (children == null || children.length == 0) {
                    log.warn("Empty folder.");
                    handleException("Empty folder.", messageContext);
                } else if (filePattern != null && !filePattern.trim().equals("")) {
                    boolean bFound = false;
                    for (FileObject child : children) {
                        if (child.getName().getBaseName().matches(filePattern)) {
                            fileObj = child;
                            bFound = true;
                            break;
                        }
                    }
                    if (!bFound) {
                        log.warn("File does not exists for the mentioned pattern.");
                        handleException("File does not exists for the mentioned pattern.", messageContext);
                    }
                } else {
                    fileObj = children[0];
                }
            } else if (fileObj.getType() != FileType.FILE) {
                log.warn("File does not exists, or an empty folder.");
                handleException("File does not exists, or an empty folder.", messageContext);
            }
        } else {
            log.warn("File/Folder does not exists");
            handleException("File/Folder does not exists", messageContext);
        }
        ResultPayloadCreate.buildFile(fileObj, messageContext, contentType);
        if (log.isDebugEnabled()) {
            log.debug("File read completed." + fileLocation);
        }
    } catch (Exception e) {
        handleException(e.getMessage(), messageContext);
    } finally {
        try {
            // Close the File system if it is not already closed by the finally block of
            // processFile method
            if (fileObj != null && fileObj.getParent() != null && fileObj.getParent().getFileSystem() != null) {
                manager.closeFileSystem(fileObj.getParent().getFileSystem());
            }
        } catch (FileSystemException warn) {
            // ignore the warning, since we handed over the stream close job to
            // AutoCloseInputStream..
        }
        try {
            if (fileObj != null) {
                fileObj.close();
            }
        } catch (Exception e) {
            // ignore the warning, since we handed over the stream close job to
            // AutoCloseInputStream..
        }
    }
}

From source file:org.wso2.carbon.connector.FileReadConnector.java

/**
 * Read the file content./*from   w ww . j  av  a  2s  . c  om*/
 *
 * @param messageContext The message context that is generated for processing the read operation.
 */
private void readFile(MessageContext messageContext) {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String contentType = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.CONTENT_TYPE);
    String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_PATTERN);
    boolean streaming = false;
    String enableStreamingParameter = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.ENABLE_STREAMING);
    if (StringUtils.isNotEmpty(enableStreamingParameter)) {
        streaming = Boolean.parseBoolean(enableStreamingParameter);
    }

    FileObject fileObjectToRead = null;
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    try {
        FileObject rootFileObject = manager.resolveFile(source, FileConnectorUtils.init(messageContext));
        if (!rootFileObject.exists()) {
            log.error("File/Folder does not exists.");
        }
        if (FileType.FOLDER.equals(rootFileObject.getType())) {
            FileObject[] children = rootFileObject.getChildren();
            if (children == null || children.length == 0) {
                log.error("Empty folder.");
            } else if (StringUtils.isNotEmpty(filePattern)) {
                for (FileObject child : children) {
                    if (child.getName().getBaseName().matches(filePattern)) {
                        fileObjectToRead = child;
                        break;
                    }
                }
                if (fileObjectToRead == null) {
                    log.error("File does not exists for the mentioned pattern.");
                }
            } else {
                fileObjectToRead = children[0];
            }
        } else if (FileType.FILE.equals(rootFileObject.getType())) {
            fileObjectToRead = rootFileObject;
        } else {
            log.error("File does not exists, or an empty folder");
        }
        ResultPayloadCreator.buildFile(fileObjectToRead, messageContext, contentType, streaming);
        if (log.isDebugEnabled()) {
            log.debug("File read completed." + source);
        }
    } catch (FileSystemException e) {
        throw new SynapseException("Error while reading a file", e);
    } finally {
        try {
            // Close the File system if it is not already closed by the finally block of processFile method
            if (fileObjectToRead != null && fileObjectToRead.getParent() != null
                    && fileObjectToRead.getParent().getFileSystem() != null) {
                manager.closeFileSystem(fileObjectToRead.getParent().getFileSystem());
            }
            if (fileObjectToRead != null) {
                fileObjectToRead.close();
            }
        } catch (FileSystemException e) {
            log.error("Error while closing the FileObject", e);
        }
    }
}