package com.digi.config.util; import java.io.File; import java.util.*; import java.net.*; /** * A helper class for determining path names. */ public class PathLocater { /** If loaded from the network, this identifies where to find network resources */ static URL documentBase = null; /** * Sets the document base currently in use */ public static void setDocumentBase(URL documentBase) { PathLocater.documentBase = documentBase; } /** * Returns the document base currently in use */ public static URL getDocumentBase() { return PathLocater.documentBase; } /** * Number of directories in the classpath. This is saved after the first time it is * calculated as a trade off between using 4 bytes of storage versus the cost to * re-calc the number. * * NOTE: Obviously, if the application ever is in a situation where the classpath can * dynamically be updated during runtime, caching this number is not a good idea. */ static int numDirInClasspath=0; /** * Returns the fully qualified file name of the provided class. * * @param name Name of the class. Use package qualified name e.g.com.digi.config.util.PathLocater * @return the fully qualified file name for the class file. * Such as d:\\config\\com\\digi\\util\\PathLocater.class */ public static String locateFullClassFileName(String name) { String fullName=null; fullName=locateFullPath(name); if (fullName!=null) { fullName=fullName+File.separatorChar+ name.replace('.', File.separatorChar)+".class"; } return fullName; } //end locateFullClassFileName /** * Returns the name of the path from where the provided class was loaded. * * @param name Name of the class. Use package qualified name such as * com.digi.config.util.PathLocator * @return the name of the path from where the specified class was loaded. * Such as d:\\config NOTE: there is no trailing path separator * character on the returned value. * */ public static String locateFullPath(String name) { String fullName=name.replace('.', File.separatorChar)+".class"; String classPath=System.getProperty("java.class.path"); // Counts the actual number of directories in the classpath int pathLength=getNumDirInClasspath(); if (!classPath.endsWith(";")) { classPath=new String(classPath+";"); } // Forms the array of CLASSPATHS defined in the system String[] paths=new String[pathLength]; int first=0; for (int i=0; i * @param fileName Name of the file to locate. * @return Absoulte path for the file. */ public static String locateFile(String fileName) { System.out.println("Locating file "+fileName); String fullName=fileName.replace('/', File.separatorChar); // make the path platform neutral. // Try finding file using the class loader we were loaded from... try { URL url = PathLocater.class.getResource(fullName); return url.toString(); } catch (Exception e) { System.out.println("Couldn't load from class loader. exc="+e.getMessage()); } // Try finding the file in user.home, user.dir or on the classpath String classPath=System.getProperty("java.class.path"); String userDir=System.getProperty("user.dir"); if (userDir!=null) { classPath=userDir+";"+classPath; } String userHome=System.getProperty("user.home"); if (userHome!=null) { classPath=userHome+";"+classPath; } System.out.println("Checking Path:"+classPath); // loop through the paths, looking for the ini file StringTokenizer StringTokenizer tok = new StringTokenizer(classPath, ";", false); while (tok.hasMoreTokens()) { String path = tok.nextToken(); if (path.endsWith(".jar")) { // if this is a jar file, just look in the same directory path = path.substring(0,path.lastIndexOf(File.separatorChar)); } if (!path.endsWith(""+File.separatorChar)) { path += File.separatorChar; } String fullpath=path+fullName; System.out.println(" Looking at: "+fullpath); if ((new File(fullpath)).exists()) { return fullpath; } } // if we got here, we didn't find it, return null return null; } /** * Creates a fully qualified file name of the Persistance file. The path to the file * is generated as follows: * If user.home is specified, then that path is used as the default path * otherwise the classpath directory containing the config class files will be used. * The subdirectory "Data" will be appended to whichever path is found above. */ public static String getDefaultSaveFileName(String fileName) { fileName=fileName.replace('/', File.separatorChar); // multiplatform support // If the user dir is specified, then use that in the path String userDir=null; try { userDir=System.getProperty("user.home"); } catch (Exception e) { } if (userDir==null) { try { userDir=System.getProperty("user.dir"); } catch (Exception e) { } } if (userDir==null) { // Otherwise, use the root of the codebase as the path userDir=locateFullPath("com.digi.config.util.PathLocater"); } // Finally append the data directory and file name to produce the fully qualified name String fullName=userDir+File.separatorChar+"Data" + File.separatorChar+fileName; return fullName; } /** * Ensures that the directory path portion of the specified full file name exists. * If directories in the path do not exist, then this method will create them if * possible. * * @param fullName the fully qualified file name whose path is to be created as needed. * * @throws SecurityException * If a security manager exists and its {@link * java.lang.SecurityManager#checkWrite(java.lang.String)} method does not * permit the named directory and all necessary parent directories * and to be created */ public static void createPathToFile(String fullName) { createPathToFile(new File(fullName)); } /** * Ensures that the directory path portion of the specified file exists. * If directories in the path do not exist, then this method will create them if * possible. * * @param file the file whose path is to be created as needed. * * @throws SecurityException * If a security manager exists and its {@link * java.lang.SecurityManager#checkWrite(java.lang.String)} method does not * permit the named directory and all necessary parent directories * and to be created */ public static void createPathToFile(File file) { File path=file.getParentFile(); if (path!=null) { path.mkdirs(); } } } //end PathLocater