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

📄 filestorage.java

📁 java 开发的一个电子邮局,挺实用的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	for(int i=0;i<flist.length;i++) {
	    String cur_lang=flist[i];
	    Locale loc=new Locale(cur_lang,"","");
	    Enumeration enum=available.elements();
	    boolean added=false;
	    while(enum.hasMoreElements()) {
		Locale l=(Locale)enum.nextElement();
		if(l.getLanguage().equals(loc.getLanguage())) {
		    s+=l.toString()+" ";
		    count++;
		    added=true;
		}
	    }
	    if(!added) {
		s+=loc.toString()+" ";
		count++;
	    }
	}
	System.err.println(count+" languages initialized.");
	cs.configRegisterStringKey(this,"LANGUAGES",s,"Languages available in WebMail");
	setConfig("LANGUAGES",s);
	
	/*
	  Setup list of themes for each language
	*/
	for(int j=0;j<flist.length;j++) {
	    File themes=new File(parent.getProperty("webmail.template.path")+System.getProperty("file.separator")
				 +flist[j]+System.getProperty("file.separator"));
	    String[] themelist=themes.list(new FilenameFilter() {
		    public boolean accept(File myf, String s3) {
			if(myf.isDirectory() && !s3.equals("CVS")) {
			    return true;
			} else {
			    return false;
			}
		    }
		});
	    String s2="";
	    for(int k=0;k<themelist.length;k++) {
		s2+=themelist[k]+" ";
	    }
	    cs.configRegisterStringKey(this,"THEMES_"+flist[j].toUpperCase(),s2,"Themes for language "+flist[j]);
	    setConfig("THEMES_"+flist[j].toUpperCase(),s2);
	}
    }
	
    /**
     * Get the String for key and the specified locale.
     * @param key Identifier for the String
     * @param locale locale of the String to fetch
     */
    public String getStringResource(String key, Locale locale) {
	if(resources.get(locale.getLanguage()) != null) {
		String s = ((ResourceBundle)resources.get(locale.getLanguage())).getString(key);
	    return ((ResourceBundle)resources.get(locale.getLanguage())).getString(key);
	} else {
	    try {
	    // Modified by exce, start.
		// ResourceBundle rc=XMLResourceBundle.getBundle("resources",locale,null);
		System.err.println("Loading locale");
		ResourceBundle rc = ResourceBundle.getBundle("org.bulbul.webmail.xmlresource.Resources", locale);
		// Modified by exce, end.
		resources.put(locale.getLanguage(),rc);
		return rc.getString(key);
	    } catch(Exception e) {
		e.printStackTrace();
		return "";
	    }
	}
    }
	
	
    /**
     * Return the requested Stylesheet, precompiled and fitting to the locale and theme
     */
    public StylesheetRoot getStylesheet(String name, Locale locale, String theme) throws WebMailException {
	String key = locale.getLanguage()+"/"+theme;

	AttributedExpireableCache cache=(AttributedExpireableCache)stylesheet_cache.get(key);

	if(cache == null) {
	    cache=new AttributedExpireableCache(file_cache_size);
	    stylesheet_cache.put(key,cache);
	}

	StylesheetRoot stylesheet=null;
	    
	String basepath=getBasePath(locale,theme);

	File f=new File(basepath+name);
	if(!f.exists()) {
	    throw new StylesheetNotFoundException("The requested stylesheet "+name+" could not be found (path tried: "+basepath+".");
	}

	if(cache.get(name) != null && ((Long)cache.getAttributes(name)).longValue() >= f.lastModified()) {
	    // Keep statistics :-)
	    cache.hit();
	    return (StylesheetRoot)cache.get(name);
	} else {
	    try {
		XSLTInputSource msg_xsl=new XSLTInputSource("file://"+basepath+name);
// 		XSLTInputSource msg_xsl=new XSLTInputSource(basepath+name);
		XSLTProcessor processor=XSLTProcessorFactory.getProcessorUsingLiaisonName("org.apache.xalan.xpath.xdom.XercesLiaison");
		processor.setDiagnosticsOutput(System.err);
		stylesheet=processor.processStylesheet(msg_xsl);
		cache.put(name,stylesheet, new Long(f.lastModified()));
		cache.miss();
	    } catch(Exception ex) { 
		//System.err.println("Error while compiling stylesheet "+name+", language="+locale.getLanguage()+", theme="+theme+".");
		throw new WebMailException("Error while compiling stylesheet "+name+", language="+locale.getLanguage()+", theme="+theme+":\n"+ex.toString()); 
	    }
	    return stylesheet;
	}
    }

    /**
     * Get a binary file for the specified locale.
     * @param key Identifier for the String
     * @param locale locale of the String to fetch
     */
    public synchronized byte[] getBinaryFile(String name, Locale locale, String theme) throws BinaryNotFoundException {
	String key = locale.getLanguage()+"/"+theme;

	AttributedExpireableCache cache=(AttributedExpireableCache)binary_cache.get(key);

	if(cache == null) {
	    cache=new AttributedExpireableCache(file_cache_size);
	    binary_cache.put(key,cache);
	}

	ByteStore bs=null;

      
	String basepath=getBasePath(locale,theme);
	File f=new File(basepath+name);
	if(!f.exists()) {
	    throw new BinaryNotFoundException("The file "+name+" could not be found!");
	}

	if(cache.get(name) != null && ((Long)cache.getAttributes(name)).longValue() >= f.lastModified()) {
	    // Keep statistics :-)
	    cache.hit();	    
	    return ((ByteStore)cache.get(name)).getBytes();
	} else {	    
	    try {
		bs=ByteStore.getBinaryFromIS(new FileInputStream(f),(int)f.length());
	    } catch(IOException ex) { ex.printStackTrace(); }
	    cache.put(name,bs,new Long(f.lastModified()));
	    
	    if(bs != null) {
		return bs.getBytes();
	    } else {
		return new byte[1];
	    }
	}
    }


	
    public Authenticator getAuthenticator() {
	return auth;
    }
	
    /**
     * Send a message to the logging facility.
     * @param level severity level of the message
     * @param message the message
     */
    public synchronized void log(int level, String message) {
	if(logger != null) {
	    logger.log(level,message);
	} else {
	    System.err.println("LOG("+level+"): "+message);
	}
    }   
	
    protected String formatDate(long date) {
	if(df==null) {
	    TimeZone tz=TimeZone.getDefault();
	    df=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.DEFAULT, Locale.getDefault());
	    df.setTimeZone(tz);
	}
	String now=df.format(new Date(date));
	return now;
    }
	
    public void shutdown() {
	logger.shutdown();
    }
	
	
    public String getMimeType(String name) {
	if(mime_types == null) {
	    return super.getMimeType(name);
	} else {
	    if(name != null) {
		String type="application/unknown";
		Enumeration enum=mime_types.keys();
		while(enum.hasMoreElements()) {
		    String s=(String)enum.nextElement();
		    if(name.toLowerCase().endsWith(s)) {
			type= (String)mime_types.get(s);
		    }
		}
		return type;
	    } else {
		return "UNKNOWN";
	    }
	}
    }
	
    public void notifyConfigurationChange(String key) {
	log(Storage.LOG_DEBUG,"FileStorage: Configuration change notify for key "+key+".");
	System.err.println("- Configuration changed: ");
	if(key.toUpperCase().startsWith("AUTH")) {
	    initAuth();
	} else if(key.toUpperCase().startsWith("MIME")) {
	    initMIME();
	}
    }
	
    public String toString() {
	String s="";
	Enumeration enum=stylesheet_cache.keys();
	while(enum.hasMoreElements()) {
	    String name=(String)enum.nextElement();
	    ExpireableCache cache=(ExpireableCache)stylesheet_cache.get(name);
	    s+=" - stylesheet cache for "+name+": Capacity "+cache.getCapacity()+", Usage "+cache.getUsage();
	    s+=", "+cache.getHits()+" hits, "+cache.getMisses()+" misses\n";
	}
	enum=binary_cache.keys();
	while(enum.hasMoreElements()) {
	    String name=(String)enum.nextElement();
	    ExpireableCache cache=(ExpireableCache)binary_cache.get(name);
	    s+=" - binary cache for "+name+": Capacity "+cache.getCapacity()+", Usage "+cache.getUsage();
	    s+=", "+cache.getHits()+" hits, "+cache.getMisses()+" misses\n";
	}
	return s;
    }


}

⌨️ 快捷键说明

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