RCP获取资源工具类
来源:互联网 发布:网络系统集成课程设计 编辑:程序博客网 时间:2024/05/22 09:39
在RCP开发中,需要获取Bundle中的资源,或者跟目录的资源,一下工具类用于获取当前插件目录中的资源或者Workspace、以及安装目录下的资源,根据类的class对象获取类所在的插件,在插件中获取相应的资源。通过以下插件获取资源,如果资源在jar包中,获取以后会复制相应的资源到“configuration\org.eclipse.osgi\bundles”目录下对应的bundle目录中。
package cn.elwy.ide.config.util;import java.io.File;import java.net.URL;import javax.swing.filechooser.FileSystemView;import org.eclipse.core.runtime.FileLocator;import org.eclipse.core.runtime.Path;import org.eclipse.core.runtime.Platform;import org.osgi.framework.Bundle;import org.osgi.framework.FrameworkUtil;import cn.elwy.common.exception.RunException;/** * 获取资源工具类 * @author huangsq * @version 2.0, 2014-01-12 * @since 2.0, 2014-01-12 */public class ResourceUtil { public static File USER_DIR; public static File USER_HOME; /** 安装路径 */ public static File INSTALL_PATH; /** 安装URL */ public static URL INSTALL_URL; /** 产品路径 */ public static File PRODUCT_PATH; /** 产品URL */ public static URL PRODUCT_URL; /** 工作空间路径 */ public static File WORKSPACE_PATH; /** 工作空间URL */ public static URL WORKSPACE_URL; /** 插件配置文件目录 */ public static File CONFIGURATION_PATH; /** 插件日志文件 */ public static File LOG_FILE; static { init(); } /** * 获取插件资源工具类的构造函数 * @param pluginId 插件ID */ private ResourceUtil() { } public static void init() { INSTALL_URL = getInstallURL(); INSTALL_PATH = getInstallPath(); PRODUCT_URL = getProductURL(); PRODUCT_PATH = getProductPath(); WORKSPACE_URL = getWorkspaceURL(); WORKSPACE_PATH = getWorkspacePath(); CONFIGURATION_PATH = getConfigurationPath(); LOG_FILE = getLogFile(); // 设置应用安装路径到系统属性 System.setProperty("INSTALL_PATH", INSTALL_PATH.getAbsolutePath()); // 设置插件路径到系统属性 System.setProperty("PRODUCT_PATH", PRODUCT_PATH.getAbsolutePath()); // 设置工作空间路径到系统属性 System.setProperty("WORKSPACE_PATH", WORKSPACE_PATH.getAbsolutePath()); } /** 获取系统运行目录 */ public static File getUserDir() { if (USER_DIR == null) { USER_DIR = new File(System.getProperty("user.dir")); } return USER_DIR; } /** 获取用户根目录 */ public static File getUserHome() { if (USER_HOME == null) { USER_HOME = new File(System.getProperty("user.home")); } return USER_HOME; } /** 获取桌面路径 */ public static File getDesktopPath() { FileSystemView fsv = FileSystemView.getFileSystemView(); return fsv.getHomeDirectory(); } /** 获取我的文档路径 */ public static File getDocumentsPath() { FileSystemView fsv = FileSystemView.getFileSystemView(); return fsv.getDefaultDirectory(); } /** 获取安装目录 */ public static File getInstallPath() { if (INSTALL_PATH == null) { INSTALL_PATH = toFile(getInstallURL()); } return INSTALL_PATH; } /** 获取安装路径 */ public static URL getInstallURL() { if (INSTALL_URL == null) { INSTALL_URL = Platform.getInstallLocation().getURL(); } return INSTALL_URL; } /** 获取工作空间根目录 */ public static File getWorkspacePath() { if (WORKSPACE_PATH == null) { WORKSPACE_PATH = toFile(getWorkspaceURL()); } return WORKSPACE_PATH; } /** 获取工作空间根目录 */ public static URL getWorkspaceURL() { if (WORKSPACE_URL == null) { WORKSPACE_URL = Platform.getInstanceLocation().getURL(); } return WORKSPACE_URL; } /** 获取产品根路径 */ public static File getProductPath() { if (PRODUCT_PATH == null) { PRODUCT_PATH = toFile(getProductURL()); } return PRODUCT_PATH; } /** 获取产品根目录 */ public static URL getProductURL() { if (PRODUCT_URL == null) { PRODUCT_URL = toFileURL(Platform.getProduct().getDefiningBundle().getEntry("")); } return PRODUCT_URL; } /** 获取产品配置根目录 */ public static File getConfigurationPath() { if (CONFIGURATION_PATH == null) { CONFIGURATION_PATH = toFile(Platform.getConfigurationLocation().getURL()); } return CONFIGURATION_PATH; } /** 获取产品LOG路径 */ public static File getLogFile() { if (LOG_FILE == null) { LOG_FILE = Platform.getLogFileLocation().toFile(); } return LOG_FILE; } /** 获取插件绝对路径 */ public static File getStateLocation(Bundle bundle) { return Platform.getStateLocation(bundle).toFile(); } /** * 先获取安装根路径下的资源,如果不存在则获取配置插件目录下的资源,如果不存在最后获取产品插件下的资源,如果不存在指定的资源将返回null * @param filePath 相对于插件的路径 * @return */ public static URL getResource(String filePath) { return getResource((Class<?>) null, filePath); } /** * 先获取安装根路径下的资源,如果不存在则获取配置插件目录下的资源,如果不存在则获取当前插件目录下的资源, * 如果不存在最后获取产品插件下的资源,如果不存在指定的资源将返回null * @param clazz 插件下的类 * @param filePath 相对于插件的路径 * @return */ public static URL getResource(Class<?> clazz, String filePath) { URL url = getResource(getInstallURL(), filePath); if (url != null) { return url; } if (clazz != null) { url = getPluginResource(clazz, filePath); if (url != null) { return url; } } return getPluginResource(ResourceUtil.class, filePath); } /** * 获取相对于URL的资源 * @param url 查找路径 * @param filePath 文件路径 * @return */ public static URL getResource(URL url, String filePath) { filePath = getFilePath(filePath); try { URL resource = new URL(url, filePath); File file = toFile(resource); if (file != null && file.exists()) { return resource; } } catch (Exception e) { throw new RunException(e); } return null; } /** * 先获取安装根路径下的资源,如果不存在则获取配置插件目录下的资源,如果不存在最后获取产品插件下的资源,如果不存在指定的资源将返回null * @param filePath 相对于插件的路径 * @return */ public static File getResourceFile(String filePath) { return getResourceFile((Class<?>) null, filePath); } /** * 先获取安装根路径下的资源,如果不存在则获取配置插件目录下的资源,如果不存在则获取当前插件目录下的资源, * 如果不存在最后获取产品插件下的资源,如果不存在指定的资源将返回null * @param clazz 插件下的类 * @param filePath 相对于插件的路径 * @return */ public static File getResourceFile(Class<?> clazz, String filePath) { return toFile(getResource(clazz, filePath)); } /** * 获取相对于URL的资源 * @param url 查找路径 * @param filePath 文件路径 * @return */ public static File getResourceFile(URL url, String filePath) { return toFile(getResource(url, filePath)); } /** * 获取相对于插件的URL资源,如果不存在指定的资源将返回null * @param clazz 所在插件的类 * @param filePath 相对于插件的路径 * @return */ public static URL getPluginResource(Class<?> clazz, String filePath) { Bundle bundle = FrameworkUtil.getBundle(clazz); return getPluginResource(bundle, filePath); } /** * 获取相对于插件的URL资源,如果不存在指定的资源将返回null * @param pluginId 插件ID * @param filePath 相对于插件的路径 * @return */ public static URL getPluginResource(String pluginId, String filePath) { return getPluginResource(Platform.getBundle(pluginId), filePath); } /** * 获取相对于插件的URL资源,如果不存在指定的资源将返回null * @param bundle 插件Bundle * @param filePath 相对于插件的路径 * @return */ public static URL getPluginResource(Bundle bundle, String filePath) { return toFileURL(bundle.getEntry(getFilePath(filePath))); } /** * 获取相对于插件的URL资源,如果不存在指定的资源将返回null * @param pluginId 插件ID * @param filePath * @return */ public static File getPluginResourceFile(String pluginId, String filePath) { return toFile(getPluginResource(pluginId, filePath)); } /** * 获取相对于插件的URL资源,如果不存在指定的资源将返回null * @param clazz 所在插件的类 * @param filePath * @return */ public static File getPluginResourceFile(Class<?> clazz, String filePath) { return toFile(getPluginResource(clazz, filePath)); } /** * 获取相对于插件的URL资源,如果不存在指定的资源将返回null * @param filePath * @return */ public static File getPluginResourceFile(Bundle bundle, String filePath) { return toFile(getPluginResource(bundle, filePath)); } /** * 插件的URL转换成File * @param url 插件的URL * @return */ public static File toFile(URL url) { if (url == null) { return null; } else { Path path = new Path(url.getFile()); return path.toFile(); } } /** * 插件的URL转换成URL * @param url 插件的URL * @return */ public static URL toFileURL(URL url) { if (url == null) { return null; } try { return FileLocator.toFileURL(url); } catch (Exception e) { throw new RunException(e); } } private static String getFilePath(String filePath) { if (filePath != null && filePath.length() > 0) { if (filePath.startsWith("/")) { filePath = filePath.substring(1); } } return filePath; } public static String test() { StringBuffer sb = new StringBuffer(); sb.append("--------------------Start TestUtil--------------------" + "\r\n"); // String installPath = TestUtil.getInstallPath(); // sb.append("getInstallPath: " + installPath + "\r\n"); sb.append("--------------------Start RCPPath--------------------" + "\r\n"); // sb.append("bundle: " + bundle + "\r\n"); sb.append("INSTALL_URL: " + INSTALL_URL + "\r\n"); sb.append("INSTALL_PATH: " + INSTALL_PATH + "\r\n"); sb.append("PRODUCT_URL: " + PRODUCT_URL + "\r\n"); sb.append("WORKSPACE_URL: " + WORKSPACE_URL + "\r\n"); sb.append("WORKSPACE_PATH: " + WORKSPACE_PATH + "\r\n"); sb.append("CONFIGURATION_PATH: " + CONFIGURATION_PATH + "\r\n"); sb.append("LOG_FILE: " + LOG_FILE + "\r\n"); // sb.append("getPluginPath: " + getPluginPath() + "\r\n"); sb.append("getInstallPath: " + getInstallPath() + "\r\n"); sb.append("getUserHomePath: " + getUserHome() + "\r\n"); sb.append("getDesktopPath:" + getDesktopPath() + "\r\n"); sb.append("getDocumentsPath:" + getDocumentsPath() + "\r\n"); sb.append("getUserDirPath: " + getUserDir() + "\r\n"); // sb.append("getStatePath: " + getStateLocation() + "\r\n"); sb.append("getWorkspacePath: " + getWorkspacePath() + "\r\n"); sb.append("getConfigurationRootPath: " + getConfigurationPath() + "\r\n"); sb.append("getLogFileRoot: " + getLogFile() + "\r\n"); String pId = "cn.elwy.common"; sb.append("getResourceFile('/'): " + getPluginResourceFile(pId, "/") + "\r\n"); sb.append("getResourceFile('../'): " + getPluginResourceFile(pId, "../") + "\r\n"); sb.append("getResourceFile('/src/'): " + getPluginResourceFile(pId, "/src/") + "\r\n"); sb.append("getResourceFile('notexist/'): " + getPluginResourceFile(pId, "/notexist/") + "\r\n"); sb.append("getResourceFile('plugin.xml'): " + getPluginResourceFile(pId, "plugin.xml") + "\r\n"); sb.append("getResourceFile('/'): " + getResourceFile("/") + "\r\n"); sb.append("getResourceFile('../'): " + getResourceFile("../") + "\r\n"); sb.append("getResourceFile('/src/'): " + getResourceFile("/src/") + "\r\n"); sb.append("getResourceFile('theme/'): " + getResourceFile("theme/") + "\r\n"); sb.append("getResourceFile('notexist/'): " + getResourceFile("/notexist/") + "\r\n"); sb.append("getResourceFile('icons'): " + getResourceFile("icons") + "\r\n"); sb.append("getResourceFile('/content/root.css'): " + getResourceFile("/content/root.css") + "\r\n"); sb.append("getResourceFile('plugin.xml'): " + getResourceFile("plugin.xml") + "\r\n"); // sb.append("getClassPath: " + getClassPath(BundlePathUtil.class)+ "\r\n"); // sb.append("getClassPath(com): " + getResourceFile(BundlePathUtil.class, "com")+ "\r\n"); // sb.append("getClassPath(/com/): " + getResourceFile(BundlePathUtil.class, "/com/")+ "\r\n"); // sb.append("getClassPath(../com): " + getResourceFile(BundlePathUtil.class, "../com")+ "\r\n"); // sb.append("getClassPath(../conf): " + getResourceFile(BundlePathUtil.class, "../conf")+ "\r\n"); sb.append("--------------------End RCPPath--------------------" + "\r\n"); return sb.toString(); }}
0 0
- RCP获取资源工具类
- 获取资源工具类
- android 获取资源id的工具类
- rcp 生成jar包后如何获取资源文件
- RCP应用程序工具类编写小结
- RCP应用程序工具类编写小结
- RCP应用程序工具类编写小结
- RCP资源目录
- 关于Spring工具类获取classpath下的文件资源
- java 获取当前项目的环境资源文件工具类
- Android反射获取资源 id通用工具类
- spring 工具类ResourceUtils 获取静态资源下载模板
- android通过反射获取资源ID的工具类
- RCP 工具代码
- Eclipse RCP中获取Plugin/Bundle中文件资源的绝对路径
- Eclipse RCP中获取Plugin/Bundle中文件资源的绝对路径(转)
- Eclipse RCP中获取Plugin/Bundle中文件资源的绝对路径
- rcp(插件开发)获取Plugin/Bundle中文件资源的绝对路径
- java中HashMap学习
- Maximum Product Subarray--leetcode
- 在sql server中实现查找一个月中有多少星期天
- Java中信号量 Semaphore
- 竞态条件与临界区
- RCP获取资源工具类
- JAVA加密算法实现用例 消息摘要
- 啊是的覅就看来就阿桑地方快乐撒地方
- JAVA加密算法实现用例 数字签名
- 禁止EditText 输入粘贴版内容
- JAVA加密算法实现用例 密钥一致协议
- zTree 改变指定节点文本样式
- 啊速度发来卡快啦水电费
- Freemarker 入门示例