Search in sources :

Example 1 with FileResource

use of lucee.commons.io.res.type.file.FileResource in project Lucee by lucee.

the class Directory method actionList.

/**
 * list all files and directories inside a directory
 * @throws PageException
 */
public static Object actionList(PageContext pageContext, Resource directory, String serverPassword, int type, ResourceFilter filter, int listInfo, boolean recurse, String sort) throws PageException {
    // check directory
    SecurityManager securityManager = pageContext.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pageContext.getConfig(), directory, serverPassword);
    if (type != TYPE_ALL) {
        ResourceFilter typeFilter = (type == TYPE_DIR) ? DIRECTORY_FILTER : FILE_FILTER;
        if (filter == null)
            filter = typeFilter;
        else
            filter = new AndResourceFilter(new ResourceFilter[] { typeFilter, filter });
    }
    // create query Object
    String[] names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory" };
    String[] types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR" };
    boolean hasMeta = directory instanceof ResourceMetaData;
    if (hasMeta) {
        names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory", "meta" };
        types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR", "OBJECT" };
    }
    Array array = null;
    Query query = null;
    Object rtn;
    if (listInfo == LIST_INFO_QUERY_ALL || listInfo == LIST_INFO_QUERY_NAME) {
        boolean listOnlyNames = listInfo == LIST_INFO_QUERY_NAME;
        rtn = query = new QueryImpl(listOnlyNames ? new String[] { "name" } : names, listOnlyNames ? new String[] { "VARCHAR" } : types, 0, "query");
    } else
        rtn = array = new ArrayImpl();
    if (!directory.exists()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
    }
    if (!directory.isDirectory()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
    }
    if (!directory.isReadable()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    }
    long startNS = System.nanoTime();
    try {
        // Query All
        if (listInfo == LIST_INFO_QUERY_ALL)
            _fillQueryAll(query, directory, filter, 0, hasMeta, recurse);
        else // Query Name
        if (listInfo == LIST_INFO_QUERY_NAME) {
            if (recurse || type != TYPE_ALL)
                _fillQueryNamesRec("", query, directory, filter, 0, recurse);
            else
                _fillQueryNames(query, directory, filter, 0);
        } else // Array Name/Path
        if (listInfo == LIST_INFO_ARRAY_NAME || listInfo == LIST_INFO_ARRAY_PATH) {
            boolean onlyName = listInfo == LIST_INFO_ARRAY_NAME;
            if (// QueryNamesRec("",query, directory, filter, 0,recurse);
            !onlyName || recurse || type != TYPE_ALL)
                // QueryNamesRec("",query, directory, filter, 0,recurse);
                _fillArrayPathOrName(array, directory, filter, 0, recurse, onlyName);
            else
                _fillArrayName(array, directory, filter, 0);
        }
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    // sort
    if (sort != null && query != null) {
        String[] arr = sort.toLowerCase().split(",");
        for (int i = arr.length - 1; i >= 0; i--) {
            try {
                String[] col = arr[i].trim().split("\\s+");
                if (col.length == 1)
                    query.sort(col[0].trim());
                else if (col.length == 2) {
                    String order = col[1].toLowerCase().trim();
                    if (order.equals("asc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_ASC);
                    else if (order.equals("desc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_DESC);
                    else
                        throw new ApplicationException("invalid order type [" + col[1] + "]");
                }
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
            }
        }
    }
    if (query != null)
        query.setExecutionTime(System.nanoTime() - startNS);
    return rtn;
}
Also used : SecurityManager(lucee.runtime.security.SecurityManager) Query(lucee.runtime.type.Query) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) ArrayImpl(lucee.runtime.type.ArrayImpl) FileResource(lucee.commons.io.res.type.file.FileResource) IOException(java.io.IOException) Array(lucee.runtime.type.Array) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) QueryImpl(lucee.runtime.type.QueryImpl) ApplicationException(lucee.runtime.exp.ApplicationException) ResourceMetaData(lucee.commons.io.res.ResourceMetaData)

Example 2 with FileResource

use of lucee.commons.io.res.type.file.FileResource in project Lucee by lucee.

the class VideoPlayerJW method toPath.

private String toPath(Resource res) {
    if (!(res instanceof FileResource))
        return res.getAbsolutePath();
    // Config config=pageContext.getConfig();
    PageSource ps = pageContext.toPageSource(res, null);
    if (ps == null)
        return res.getAbsolutePath();
    String realPath = ps.getRealpath();
    realPath = realPath.replace('\\', '/');
    if (realPath.endsWith("/"))
        realPath = realPath.substring(0, realPath.length() - 1);
    // print.out("real:"+realPath);
    String mapping = ps.getMapping().getVirtual();
    mapping = mapping.replace('\\', '/');
    if (mapping.endsWith("/"))
        mapping = mapping.substring(0, mapping.length() - 1);
    return mapping + realPath;
}
Also used : FileResource(lucee.commons.io.res.type.file.FileResource) PageSource(lucee.runtime.PageSource)

Example 3 with FileResource

use of lucee.commons.io.res.type.file.FileResource in project Lucee by lucee.

the class XMLConfigServerFactory method newInstance.

/**
 * creates a new ServletConfig Impl Object
 * @param engine
 * @param initContextes
 * @param contextes
 * @param configDir
 * @return new Instance
 * @throws SAXException
 * @throws ClassNotFoundException
 * @throws PageException
 * @throws IOException
 * @throws TagLibException
 * @throws FunctionLibException
 * @throws BundleException
 */
public static ConfigServerImpl newInstance(CFMLEngineImpl engine, Map<String, CFMLFactory> initContextes, Map<String, CFMLFactory> contextes, Resource configDir) throws SAXException, ClassException, PageException, IOException, TagLibException, FunctionLibException, BundleException {
    boolean isCLI = SystemUtil.isCLICall();
    if (isCLI) {
        Resource logs = configDir.getRealResource("logs");
        logs.mkdirs();
        Resource out = logs.getRealResource("out");
        Resource err = logs.getRealResource("err");
        ResourceUtil.touch(out);
        ResourceUtil.touch(err);
        if (logs instanceof FileResource) {
            SystemUtil.setPrintWriter(SystemUtil.OUT, new PrintWriter((FileResource) out));
            SystemUtil.setPrintWriter(SystemUtil.ERR, new PrintWriter((FileResource) err));
        } else {
            SystemUtil.setPrintWriter(SystemUtil.OUT, new PrintWriter(IOUtil.getWriter(out, "UTF-8")));
            SystemUtil.setPrintWriter(SystemUtil.ERR, new PrintWriter(IOUtil.getWriter(err, "UTF-8")));
        }
    }
    SystemOut.print(SystemUtil.getPrintWriter(SystemUtil.OUT), "===================================================================\n" + "SERVER CONTEXT\n" + "-------------------------------------------------------------------\n" + "- config:" + configDir + "\n" + "- loader-version:" + SystemUtil.getLoaderVersion() + "\n" + "- core-version:" + engine.getInfo().getVersion() + "\n" + "===================================================================\n");
    int iDoNew = doNew(engine, configDir, false).updateType;
    boolean doNew = iDoNew != NEW_NONE;
    Resource configFile = configDir.getRealResource("lucee-server.xml");
    if (!configFile.exists()) {
        configFile.createFile(true);
        // InputStream in = new TextFile("").getClass().getResourceAsStream("/resource/config/server.xml");
        createFileFromResource("/resource/config/server.xml", configFile.getAbsoluteResource(), "tpiasfap");
    }
    Document doc = loadDocumentCreateIfFails(configFile, "server");
    // get version
    Element luceeConfiguration = doc.getDocumentElement();
    String strVersion = luceeConfiguration.getAttribute("version");
    double version = Caster.toDoubleValue(strVersion, 1.0d);
    boolean cleanupDatasources = version < 5.0D;
    ConfigServerImpl config = new ConfigServerImpl(engine, initContextes, contextes, configDir, configFile);
    load(config, doc, false, doNew);
    createContextFiles(configDir, config, doNew, cleanupDatasources);
    ((CFMLEngineImpl) ConfigWebUtil.getEngine(config)).onStart(config, false);
    return config;
}
Also used : CFMLEngineImpl(lucee.runtime.engine.CFMLEngineImpl) Element(org.w3c.dom.Element) Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) FileResource(lucee.commons.io.res.type.file.FileResource) Document(org.w3c.dom.Document) PrintWriter(java.io.PrintWriter)

Example 4 with FileResource

use of lucee.commons.io.res.type.file.FileResource in project Lucee by lucee.

the class HeapDumper method dumpTo.

/**
 * Dumps the heap to the outputFile file in the same format as the hprof heap dump.
 * If this method is called remotely from another process, the heap dump output is written to a file named outputFile on the machine where the target VM is running. If outputFile is a relative path, it is relative to the working directory where the target VM was started.
 * @param res Resource to write the .hprof file.
 * @param live  if true dump only live objects i.e. objects that are reachable from others
 * @throws IOException
 */
public static void dumpTo(Resource res, boolean live) throws IOException {
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    HotSpotDiagnosticMXBean mxbean = ManagementFactory.newPlatformMXBeanProxy(mbserver, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
    String path;
    Resource tmp = null;
    if (res instanceof FileResource)
        path = res.getAbsolutePath();
    else {
        tmp = SystemUtil.getTempFile("hprof", false);
        path = tmp.getAbsolutePath();
    }
    try {
        // it only
        mxbean.dumpHeap(path, live);
    } finally {
        if (tmp != null && tmp.exists()) {
            tmp.moveTo(res);
        }
    }
}
Also used : HotSpotDiagnosticMXBean(com.sun.management.HotSpotDiagnosticMXBean) Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) FileResource(lucee.commons.io.res.type.file.FileResource) MBeanServer(javax.management.MBeanServer)

Example 5 with FileResource

use of lucee.commons.io.res.type.file.FileResource in project Lucee by lucee.

the class JavaProxy method loadClassByPath.

private static Class<?> loadClassByPath(PageContext pc, String className, String[] paths) throws PageException {
    PageContextImpl pci = (PageContextImpl) pc;
    java.util.List<Resource> resources = new ArrayList<Resource>();
    if (paths != null && paths.length > 0) {
        // load resources
        for (int i = 0; i < paths.length; i++) {
            Resource res = ResourceUtil.toResourceExisting(pc, paths[i]);
            if (res.isDirectory()) {
                // a directory was passed, add all of the jar files from it
                FileResource dir = (FileResource) res;
                Resource[] jars = dir.listResources((ResourceNameFilter) new WildCardFilter("*.jar"));
                for (Resource jar : jars) {
                    resources.add(jar);
                }
            } else {
                resources.add(res);
            }
        }
    // throw new FunctionException(pc, "JavaProxy", 2, "path", "argument path has to be a array of strings or a single string, where every string is defining a path");
    }
    // load class
    try {
        ClassLoader cl = resources.isEmpty() ? pci.getClassLoader() : pci.getClassLoader(resources.toArray(new Resource[resources.size()]));
        Class clazz = null;
        try {
            clazz = ClassUtil.loadClass(cl, className);
        } catch (ClassException ce) {
            // try java.lang if no package definition
            if (className.indexOf('.') == -1) {
                try {
                    clazz = ClassUtil.loadClass(cl, "java.lang." + className);
                } catch (ClassException e) {
                    throw ce;
                }
            } else
                throw ce;
        }
        return clazz;
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
}
Also used : Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) ArrayList(java.util.ArrayList) FileResource(lucee.commons.io.res.type.file.FileResource) ClassException(lucee.commons.lang.ClassException) PageContextImpl(lucee.runtime.PageContextImpl) ClassException(lucee.commons.lang.ClassException) SecurityException(lucee.runtime.exp.SecurityException) PageException(lucee.runtime.exp.PageException) WildCardFilter(lucee.commons.io.res.util.WildCardFilter)

Aggregations

FileResource (lucee.commons.io.res.type.file.FileResource)5 Resource (lucee.commons.io.res.Resource)3 HotSpotDiagnosticMXBean (com.sun.management.HotSpotDiagnosticMXBean)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 MBeanServer (javax.management.MBeanServer)1 ResourceMetaData (lucee.commons.io.res.ResourceMetaData)1 AndResourceFilter (lucee.commons.io.res.filter.AndResourceFilter)1 DirectoryResourceFilter (lucee.commons.io.res.filter.DirectoryResourceFilter)1 FileResourceFilter (lucee.commons.io.res.filter.FileResourceFilter)1 NotResourceFilter (lucee.commons.io.res.filter.NotResourceFilter)1 OrResourceFilter (lucee.commons.io.res.filter.OrResourceFilter)1 ResourceFilter (lucee.commons.io.res.filter.ResourceFilter)1 WildCardFilter (lucee.commons.io.res.util.WildCardFilter)1 ClassException (lucee.commons.lang.ClassException)1 PageContextImpl (lucee.runtime.PageContextImpl)1 PageSource (lucee.runtime.PageSource)1 CFMLEngineImpl (lucee.runtime.engine.CFMLEngineImpl)1 ApplicationException (lucee.runtime.exp.ApplicationException)1