/** @return returns a string list of all pathes */ public static Resource[] getClassPathes() { if (classPathes != null) return classPathes; ArrayList pathes = new ArrayList(); String pathSeperator = System.getProperty("path.separator"); if (pathSeperator == null) pathSeperator = ";"; // java.ext.dirs ResourceProvider frp = ResourcesImpl.getFileResourceProvider(); // pathes from system properties String strPathes = System.getProperty("java.class.path"); if (strPathes != null) { Array arr = ListUtil.listToArrayRemoveEmpty(strPathes, pathSeperator); int len = arr.size(); for (int i = 1; i <= len; i++) { Resource file = frp.getResource(Caster.toString(arr.get(i, ""), "").trim()); if (file.exists()) pathes.add(ResourceUtil.getCanonicalResourceEL(file)); } } // pathes from url class Loader (dynamic loaded classes) ClassLoader cl = new Info().getClass().getClassLoader(); if (cl instanceof URLClassLoader) getClassPathesFromClassLoader((URLClassLoader) cl, pathes); return classPathes = (Resource[]) pathes.toArray(new Resource[pathes.size()]); }
/** * gets a file from server and copy it local * * @return FTPCLient * @throws PageException * @throws IOException */ private AFTPClient actionGetFile() throws PageException, IOException { required("remotefile", remotefile); required("localfile", localfile); AFTPClient client = getClient(); Resource local = ResourceUtil.toResourceExistingParent(pageContext, localfile); // new File(localfile); pageContext.getConfig().getSecurityManager().checkFileLocation(local); if (failifexists && local.exists()) throw new ApplicationException( "File [" + local + "] already exist, if you want to overwrite, set attribute failIfExists to false"); OutputStream fos = null; client.setFileType(getType(local)); boolean success = false; try { fos = IOUtil.toBufferedOutputStream(local.getOutputStream()); success = client.retrieveFile(remotefile, fos); } finally { IOUtil.closeEL(fos); if (!success) local.delete(); } writeCfftp(client); return client; }
/** * if no search xml exist create a empty one * * @param searchFile * @throws IOException */ private static final void createSearchFile(Resource searchFile) throws IOException { CFMLEngine e = CFMLEngineFactory.getInstance(); if (searchFile.isFile()) searchFile.createFile(true); InputStream in = new ByteArrayInputStream(DEFAULT_SEARCH_XML.getBytes()); // e.getClass().getResourceAsStream("/resource/search/default.xml"); e.getIOUtil().copy(in, searchFile, true); }
private void checkSize(ConfigWeb config, Resource dir, long maxSize, ResourceFilter filter) { if (!dir.exists()) return; Resource res = null; int count = ArrayUtil.size(filter == null ? dir.list() : dir.list(filter)); long size = ResourceUtil.getRealSize(dir, filter); PrintWriter out = config.getOutWriter(); SystemOut.printDate(out, "check size of directory [" + dir + "]"); SystemOut.printDate(out, "- current size [" + size + "]"); SystemOut.printDate(out, "- max size [" + maxSize + "]"); int len = -1; while (count > 100000 || size > maxSize) { Resource[] files = filter == null ? dir.listResources() : dir.listResources(filter); if (len == files.length) break; // protect from inifinti loop len = files.length; for (int i = 0; i < files.length; i++) { if (res == null || res.lastModified() > files[i].lastModified()) { res = files[i]; } } if (res != null) { size -= res.length(); try { res.remove(true); count--; } catch (IOException e) { SystemOut.printDate(out, "cannot remove resource " + res.getAbsolutePath()); break; } } res = null; } }
public boolean accept(Resource res) { if (res.isDirectory()) return allowDir; // load content String str = null; try { str = IOUtil.toString(res, "UTF-8"); } catch (IOException e) { return false; } int index = str.indexOf(':'); if (index != -1) { long expires = Caster.toLongValue(str.substring(0, index), -1L); // check is for backward compatibility, old files have no expires date inside. they do ot // expire if (expires != -1) { if (expires < System.currentTimeMillis()) { return true; } str = str.substring(index + 1); return false; } } // old files not having a timestamp inside else if (res.lastModified() <= time) { return true; } return false; }
/** @throws PageException */ private void doUpdate() throws PageException { String message = "missing attribute for tag schedule with action update"; String detail = "required attributes are [startDate, startTime, URL, interval, operation]"; Resource file = null; // if(publish) { if (!StringUtil.isEmpty(strFile) && !StringUtil.isEmpty(strPath)) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath); file = file.getRealResource(strFile); } else if (!StringUtil.isEmpty(strFile)) { file = ResourceUtil.toResourceNotExisting(pageContext, strFile); } else if (!StringUtil.isEmpty(strPath)) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath); } if (file != null) pageContext .getConfig() .getSecurityManager() .checkFileLocation(pageContext.getConfig(), file, serverPassword); // missing attributes if (startdate == null || starttime == null || url == null || interval == null) throw new ApplicationException(message, detail); // timeout if (requesttimeout < 0) requesttimeout = pageContext.getRequestTimeout(); // username/password Credentials cr = null; if (username != null) cr = CredentialsImpl.toCredentials(username, password); try { ScheduleTask st = new ScheduleTaskImpl( task, file, startdate, starttime, enddate, endtime, url, port, interval, requesttimeout, cr, ProxyDataImpl.getInstance(proxyserver, proxyport, proxyuser, proxypassword), resolveurl, publish, hidden, readonly, paused, autoDelete); scheduler.addScheduleTask(st, true); } catch (Exception e) { throw Caster.toPageException(e); } // }
public DebuggerPool getDebuggerPool() { if (debuggerPool == null) { Resource dir = getConfigDir().getRealResource("debugger"); dir.mkdirs(); debuggerPool = new DebuggerPool(dir); } return debuggerPool; }
private static Resource getDeployDirectory(Config config) { Resource dir = ConfigWebUtil.getConfigServerDirectory(config); if (dir == null || !dir.isWriteable() || !dir.isReadable()) dir = ResourceUtil.toResource( CFMLEngineFactory.getClassLoaderRoot(SystemUtil.getLoaderClassLoader())); return dir; }
private static String addPlaceHolder(Resource dir, Resource file, String placeholder) { if (ResourceUtil.isChildOf(file, dir)) { try { return StringUtil.replace( file.getCanonicalPath(), dir.getCanonicalPath(), placeholder, true); } catch (IOException e) { } } return null; }
/** * get class pathes from all url ClassLoaders * * @param ucl URL Class Loader * @param pathes Hashmap with allpathes */ private static void getClassPathesFromClassLoader(URLClassLoader ucl, ArrayList pathes) { ClassLoader pcl = ucl.getParent(); // parent first if (pcl instanceof URLClassLoader) getClassPathesFromClassLoader((URLClassLoader) pcl, pathes); ResourceProvider frp = ResourcesImpl.getFileResourceProvider(); // get all pathes URL[] urls = ucl.getURLs(); for (int i = 0; i < urls.length; i++) { Resource file = frp.getResource(urls[i].getPath()); if (file.exists()) pathes.add(ResourceUtil.getCanonicalResourceEL(file)); } }
private static Resource createAgentJar(Log log, Config c) throws IOException { Resource trg = getDeployDirectory(c).getRealResource("lucee-external-agent.jar"); if (!trg.exists() || trg.length() == 0) { log.info("Instrumentation", "create " + trg); InputStream jar = InfoImpl.class.getResourceAsStream("/resource/lib/lucee-external-agent.jar"); if (jar == null) { throw new IOException("could not load jar [/resource/lib/lucee-external-agent.jar]"); } IOUtil.copy(jar, trg, true); } return trg; }
/** * store loaded data to xml file * * @throws SearchException */ protected final synchronized void store() throws SearchException { // Collection.Key[] keys=collections.keys(); Iterator<Key> it = collections.keyIterator(); Key k; while (it.hasNext()) { k = it.next(); Element collEl = getCollectionElement(k.getString()); SearchCollection sc = getCollectionByName(k.getString()); setAttributes(collEl, sc); } OutputFormat format = new OutputFormat(doc, null, true); format.setLineSeparator("\r\n"); format.setLineWidth(72); OutputStream os = null; try { XMLSerializer serializer = new XMLSerializer( os = engine.getIOUtil().toBufferedOutputStream(searchFile.getOutputStream()), format); serializer.serialize(doc.getDocumentElement()); } catch (IOException e) { throw new SearchException(e); } finally { engine.getIOUtil().closeSilent(os); } }
private static void addAttachIfNecessary(Config config, Log log) { String srcName = null, trgName = null; String archBits = (SystemUtil.getJREArch() == SystemUtil.ARCH_64) ? "64" : "32"; // Windows if (SystemUtil.isWindows()) { trgName = "attach.dll"; srcName = "windows" + archBits + "/" + trgName; } // Linux else if (SystemUtil.isLinux()) { trgName = "libattach.so"; srcName = "linux" + archBits + "/" + trgName; } // Solaris else if (SystemUtil.isSolaris()) { trgName = "libattach.so"; srcName = "solaris" + archBits + "/" + trgName; } // Mac OSX else if (SystemUtil.isMacOSX()) { trgName = "libattach.dylib"; srcName = "macosx" + archBits + "/" + trgName; } if (srcName != null) { // create dll if necessary Resource binDir = getBinDirectory(config); Resource trg = binDir.getRealResource(trgName); if (!trg.exists() || trg.length() == 0) { log.info("Instrumentation", "deploy /resource/bin/" + srcName + " to " + trg); InputStream src = InfoImpl.class.getResourceAsStream("/resource/bin/" + srcName); try { IOUtil.copy(src, trg, true); } catch (IOException e) { log.log(Log.LEVEL_ERROR, "Instrumentation", e); } } // set directory to library path SystemUtil.addLibraryPathIfNoExist(binDir, log); } }
/** * translate a file resource to a buffered image * * @param res * @return * @throws IOException */ @Override public final BufferedImage toBufferedImage(Resource res, String format) throws IOException { InputStream is = null; try { return Sanselan.getBufferedImage(is = res.getInputStream()); } catch (ImageReadException e) { throw ExceptionUtil.toIOException(e); } finally { IOUtil.closeEL(is); } }
private static Resource createToolsJar(Config config) throws IOException { Resource dir = getDeployDirectory(config); String os = "bsd"; // used for Mac OS X if (SystemUtil.isWindows()) { os = "windows"; } else if (SystemUtil.isLinux()) { // not MacOSX os = "linux"; } else if (SystemUtil.isSolaris()) { os = "solaris"; } String name = "tools-" + os + "-" + TOOLS_VERSION + ".jar"; Resource trg = dir.getRealResource(name); if (!trg.exists() || trg.length() == 0) { InputStream jar = InfoImpl.class.getResourceAsStream("/resource/lib/" + name); IOUtil.copy(jar, trg, true); } return trg; }
private void checkOldClientFile(ConfigWeb config) { ExtensionResourceFilter filter = new ExtensionResourceFilter(".script", false); // move old structured file in new structure try { Resource dir = config.getClientScopeDir(), trgres; Resource[] children = dir.listResources(filter); String src, trg; int index; for (int i = 0; i < children.length; i++) { src = children[i].getName(); index = src.indexOf('-'); trg = StorageScopeFile.getFolderName( src.substring(0, index), src.substring(index + 1), false); trgres = dir.getRealResource(trg); if (!trgres.exists()) { trgres.createFile(true); ResourceUtil.copy(children[i], trgres); } // children[i].moveTo(trgres); children[i].delete(); } } catch (Throwable t) { } }
/** * copy a local file to server * * @return FTPClient * @throws IOException * @throws PageException */ private AFTPClient actionPutFile() throws IOException, PageException { required("remotefile", remotefile); required("localfile", localfile); AFTPClient client = getClient(); Resource local = ResourceUtil.toResourceExisting(pageContext, localfile); // new File(localfile); // if(failifexists && local.exists()) throw new ApplicationException("File ["+local+"] already // exist, if you want to overwrite, set attribute failIfExists to false"); InputStream is = null; try { is = IOUtil.toBufferedInputStream(local.getInputStream()); client.setFileType(getType(local)); client.storeFile(remotefile, is); } finally { IOUtil.closeEL(is); } writeCfftp(client); return client; }
@Override public void init(lucee.runtime.config.Config config, Resource searchDir) throws IOException, SearchException { this.config = config; this.searchDir = searchDir; this.searchFile = searchDir.getRealResource("search.xml"); if (!searchFile.exists() || searchFile.length() == 0) createSearchFile(searchFile); DOMParser parser = new DOMParser(); InputStream is = null; try { is = engine.getIOUtil().toBufferedInputStream(searchFile.getInputStream()); InputSource source = new InputSource(is); parser.parse(source); } catch (SAXException e) { throw new SearchException(e); } finally { engine.getIOUtil().closeSilent(is); } doc = parser.getDocument(); readCollections(config); }
private static Resource getBinDirectory(Config config) { Resource dir = ConfigWebUtil.getConfigServerDirectory(config); if (dir == null || !dir.isWriteable() || !dir.isReadable()) dir = ResourceUtil.toResource( CFMLEngineFactory.getClassLoaderRoot(SystemUtil.getLoaderClassLoader())); else { dir = dir.getRealResource("bin"); if (!dir.exists()) dir.mkdir(); } return dir; }
/** * returns the Temp Directory of the System * * @return temp directory */ public static Resource getTempDirectory() { if (tempFile != null) return tempFile; ResourceProvider fr = ResourcesImpl.getFileResourceProvider(); String tmpStr = System.getProperty("java.io.tmpdir"); if (tmpStr != null) { tempFile = fr.getResource(tmpStr); if (tempFile.exists()) { tempFile = ResourceUtil.getCanonicalResourceEL(tempFile); return tempFile; } } File tmp = null; try { tmp = File.createTempFile("a", "a"); tempFile = fr.getResource(tmp.getParent()); tempFile = ResourceUtil.getCanonicalResourceEL(tempFile); } catch (IOException ioe) { } finally { if (tmp != null) tmp.delete(); } return tempFile; }
/** @return return System directory */ public static Resource getSystemDirectory() { String pathes = System.getProperty("java.library.path"); ResourceProvider fr = ResourcesImpl.getFileResourceProvider(); if (pathes != null) { String[] arr = ListUtil.toStringArrayEL(ListUtil.listToArray(pathes, File.pathSeparatorChar)); for (int i = 0; i < arr.length; i++) { if (arr[i].toLowerCase().indexOf("windows\\system") != -1) { Resource file = fr.getResource(arr[i]); if (file.exists() && file.isDirectory() && file.isWriteable()) return ResourceUtil.getCanonicalResourceEL(file); } } for (int i = 0; i < arr.length; i++) { if (arr[i].toLowerCase().indexOf("windows") != -1) { Resource file = fr.getResource(arr[i]); if (file.exists() && file.isDirectory() && file.isWriteable()) return ResourceUtil.getCanonicalResourceEL(file); } } for (int i = 0; i < arr.length; i++) { if (arr[i].toLowerCase().indexOf("winnt") != -1) { Resource file = fr.getResource(arr[i]); if (file.exists() && file.isDirectory() && file.isWriteable()) return ResourceUtil.getCanonicalResourceEL(file); } } for (int i = 0; i < arr.length; i++) { if (arr[i].toLowerCase().indexOf("win") != -1) { Resource file = fr.getResource(arr[i]); if (file.exists() && file.isDirectory() && file.isWriteable()) return ResourceUtil.getCanonicalResourceEL(file); } } for (int i = 0; i < arr.length; i++) { Resource file = fr.getResource(arr[i]); if (file.exists() && file.isDirectory() && file.isWriteable()) return ResourceUtil.getCanonicalResourceEL(file); } } return null; }
/** * This private worker method attempts to find [java_home]/lib/tools.jar. Note: The tools.jar is a * part of the SDK, it is not present in the JRE. * * @return If tools.jar can be found, a File representing tools.jar. <br> * If tools.jar cannot be found, null. */ private static Resource findToolsJar(Config config, Log log, RefBoolean useOurOwn) { log.info("Instrumentation", "looking for tools.jar"); String javaHome = System.getProperty("java.home"); Resource javaHomeFile = ResourcesImpl.getFileResourceProvider().getResource(javaHome); Resource toolsJarFile = javaHomeFile.getRealResource("lib" + File.separator + "tools.jar"); if (toolsJarFile.exists()) { useOurOwn.setValue(false); return toolsJarFile; } log.info("Instrumentation", "couldn't find tools.jar at: " + toolsJarFile.getAbsolutePath()); // If we're on an IBM SDK, then remove /jre off of java.home and try again. if (javaHomeFile.getAbsolutePath().endsWith(SEP + "jre")) { javaHomeFile = javaHomeFile.getParentResource(); toolsJarFile = javaHomeFile.getRealResource("lib" + SEP + "tools.jar"); if (!toolsJarFile.exists()) { log.info("Instrumentation", "for IBM SDK couldn't find " + toolsJarFile.getAbsolutePath()); } else { useOurOwn.setValue(false); return toolsJarFile; } } else if (System.getProperty("os.name").toLowerCase().indexOf("mac") >= 0) { // If we're on a Mac, then change the search path to use ../Classes/classes.jar. if (javaHomeFile.getAbsolutePath().endsWith(SEP + "Home")) { javaHomeFile = javaHomeFile.getParentResource(); toolsJarFile = javaHomeFile.getRealResource("Classes" + SEP + "classes.jar"); if (!toolsJarFile.exists()) { log.info("Instrumentation", "for Mac OS couldn't find " + toolsJarFile.getAbsolutePath()); } else { useOurOwn.setValue(false); return toolsJarFile; } } } // if the engine could not find the tools.jar it is using it's own version try { toolsJarFile = createToolsJar(config); } catch (IOException e) { log.error("Instrumentation", e); } if (!toolsJarFile.exists()) { log.info("Instrumentation", "could not be created " + toolsJarFile.getAbsolutePath()); return null; } log.info("Instrumentation", "found " + toolsJarFile.getAbsolutePath()); return toolsJarFile; }
private static URL doURL(Resource res) throws IOException { if (!(res instanceof FileResource)) throw new IOException("resource [" + res.getPath() + "] must be a local file"); return ((FileResource) res).toURL(); }