⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 env.java

📁 jxta官方例程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    if (ph != null) {//            URL pu = null;                        try {                c.set(Constants.PROXY_HTTP,                    new URL(Constants.PROTOCOL_HTTP + URL_DELIMITER + ph +                        COLON + pp).toString());            } catch (MalformedURLException mue) {                if (LOG.isEnabledFor(Level.DEBUG)) {                    LOG.debug("invalid url: " + ph + " " + pp, mue);                }            }        }        // xxx: hack; in time, support loading multiple networks        String nid = c.get(Constants.NETWORK_ID, "").trim();                if (nid.length() > 0) {            Properties p = new Properties();                        p.put(Constants.NET_PEER_GROUP_ID_KEY, nid);            p.put(Constants.NET_PEER_GROUP_NAME_KEY,                c.get(Constants.NETWORK_NAME, "").trim());            p.put(Constants.NET_PEER_GROUP_DESCRIPTION_KEY,                c.get(Constants.NETWORK_DESCRIPTION, "").trim());                        try {                p.store(new FileOutputStream(new File(home + Env.PLATFORM_CONFIG)),                    Env.PLATFORM_CONFIG_COMMENT);            } catch (FileNotFoundException fnfe) {                if (LOG.isEnabledFor(Level.ERROR)) {                    LOG.error("can't write " + Env.PLATFORM_CONFIG +                        " file", fnfe);                }            } catch (IOException ioe) {                if (LOG.isEnabledFor(Level.ERROR)) {                    LOG.error("can't write " + Env.PLATFORM_CONFIG +                        " file", ioe);                }            }        }        initPreferences(home);                 // xxx: drop the cm for a period of time; see issue #331//        delete(new File(home + JXTA_CACHE));    }    private static void initPreferences(String home) {    	         File f = new File(home + PREFS);    	             	         if (! f.exists()) {    	             try {    	                 createFile(f, DEFAULT_PREFS);    	                     	             } catch (IOException e) {    	                 LOG.fatal("Unable to create file " + f.getAbsolutePath(), e);    	             }    	         }    	             				}	/**     * Ensures that the Log4J config file exists.<br>     * Looks first for a log4j.properties configuration file, then for a log4j.xml file.     *     * @param dirPath The base directory for the config file, relative to the user's home directory.     */    private static void initLog4JConfig(String dirPath) {        LOG.log(Log4J.STATUS_LEVEL, "Begin initLog4JConfig(String)");        File configFile = new File(dirPath + LOG4J);        /*         * log4jConfigFileXml boolean is used by the Log4J utility class.         * That class and its functionality should be moved into platform.         * Till then, I think it is cleaner if we leave this variable intact.         */        log4jConfigFileXml = true;        // If that file doesn't exit, then create a log4j.xml file.        if (!configFile.exists()) {            try {                createFile(configFile, DEFAULT_LOG4J);            } catch (IOException e) {                if (LOG.isEnabledFor(Level.ERROR)) {                    LOG.error("Unable to create file " + configFile.getAbsolutePath(),e);                }                LOG.log(Log4J.STATUS_LEVEL, "Unable to create file " + configFile.getAbsolutePath(),e);                return;            }        }        String filePath = null;        if (configFile.exists()) {            try {                filePath = configFile.getCanonicalPath();            } catch (IOException ignored) {                // Can't get the Canonical path, so we'll settle for the Absolute path.                filePath = configFile.getAbsolutePath();            }        }        log4jConfigFilePath = filePath;        LOG.log(Log4J.STATUS_LEVEL, "log4jConfigFilePath = " + log4jConfigFilePath);    }    /**     * Returns the URI path of the Home Directory.     */    public static URI getHome() {        return myjxta;    }    /**     * Returns the {@link File#getCanonicalPath() Canonical} path of the Home Directory if possible; otherwise,     * returns the {@link File#getAbsolutePath() Absolute} path.     */    public static String getHomeDirPath() {        String p = null;                if (myjxta != null) {            try {                p = Conversion.toFile(myjxta).getCanonicalPath();            } catch (ConversionException ce) {                if (LOG.isEnabledFor(Level.ERROR)) {                    LOG.error("home path", ce);                }            } catch (IOException ioe) {                if (LOG.isEnabledFor(Level.ERROR)) {                    LOG.error("home path", ioe);                }            }        }        return p;    }    /**     * Returns the URL of the current configuration file.     */    public static URL getConfiguration() {        URL u = null;        try {            u = new URL(getHome() + CONFIGURATION);        } catch (MalformedURLException mue) {            if (LOG.isEnabledFor(Level.ERROR)) {                LOG.error("Caught unexpected Exception",mue);            }        }        return u;    }    /**     * Return the path to platform specific configuration      * file      *      * @return the path to the platform specific configuration file     */    public static URL getPlatform() {        URL u = null;        try {            u = new URL(getHome() + PLATFORM);        } catch (MalformedURLException mue) {            if (LOG.isEnabledFor(Level.ERROR)) {                LOG.error("Caught unexpected Exception",mue);            }        }        return u;    }    /**     * Creates or deletes a Reconfigure file (home/reconf) depending on     * whether the reconfigure param is true or false, respectively.     *      * @param reconfigure If true, a new Reconfigure file is created;     * otherwise, the existing Reconfigure file is deleted.     */    public static void setReconfigure(boolean reconfigure) {        File r = new File(new File(myjxta), RECONFIGURE);        if (r != null) {            if (reconfigure) {                try {                    r.createNewFile();                } catch (IOException ioe) {                    if (LOG.isEnabledFor(Level.ERROR)) {                        LOG.error("Caught unexpected Exception",ioe);                    }                }            } else {                delete(r);            }        }    }    /**     * @return The log4jConfigFilePath.     */    public static String getLog4jConfigFilePath() {        return log4jConfigFilePath;    }    /**     * @return true if the log4j config file pointed to by log4jConfigFilePath has a ".xml" extension, and false     * otherwise.     */    public static boolean isLog4jConfigFileXml() {        return log4jConfigFileXml;    }    public static void delete(File f) {        if (f.exists()) {            if (f.isFile()) {                f.delete();            } else {                File[] files = f.listFiles();                for (int i = 0; i < files.length; i++) {                    delete(files[i]);                }		f.delete();            }        }    }    private static void createFile(File newFile, String sourceFilePath) throws IOException {        File p = newFile.getParentFile();        if (p != null) {            p.mkdirs();        }        ResourceManager rm = ResourceManager.getInstance();        InputStream is = rm.getResourceAsStream(sourceFilePath);        if (is != null) {            newFile.createNewFile();            FileOutputStream os = new FileOutputStream(newFile);            BufferedOutputStream out = new BufferedOutputStream(os);            int c = -1;            while ((c = is.read()) > -1) {                out.write(c);            }            out.flush();            is.close();            out.close();        }    }           /**      * Returns the URL of the current preferences file.      */     public static URL getPreferences() {         URL url = null;                  try {             url = new URL(getHome() + PREFS);         }  catch (MalformedURLException mue) {             if (LOG.isEnabledFor(Level.ERROR)) {                 LOG.error("Caught unexpected Exception",mue);             }         }                  return url;     }          public static boolean savePreferences(String prefDoc) {         boolean ret = false;         try {             updateFile(getPreferences(), new ByteArrayInputStream(prefDoc.getBytes()));         }catch(IOException iox) {             if (LOG.isEnabledFor(Level.ERROR)) {                 LOG.error("Caught unexpected Exception",iox);             }         }         return ret;     }          private static void updateFile(URL currentFile, InputStream in) throws IOException {                  File f = null;         try {             f = new File(currentFile.toURI());         } catch (URISyntaxException ex) {             if (LOG.isEnabledFor(Level.ERROR)) {                 LOG.error("Caught unexpected Exception",ex);             }         }                  if(!f.exists()) {             if (LOG.isEnabledFor(Level.ERROR)) {                 LOG.error("File does not exist. Creating");             }                          File p = f.getParentFile();                          if (p != null) {                 p.mkdirs();             }                          f.createNewFile();         }                 FileOutputStream os = new FileOutputStream(f);         BufferedOutputStream out = new BufferedOutputStream(os);                  int c = -1;                 while ((c = in.read()) > -1) {             out.write(c);         }                  out.flush();         in.close();         out.close();     }     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -