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

📄 filestorage.java

📁 java 开发的一个电子邮局,挺实用的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* CVS ID: $Id: FileStorage.java,v 1.6 2000/12/30 10:39:09 wastl Exp $ */
package net.wastl.webmail.storage;

import java.io.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import net.wastl.webmail.exceptions.*;
import net.wastl.webmail.server.*;
import net.wastl.webmail.config.*;
import net.wastl.webmail.misc.*;
import net.wastl.webmail.xml.*;

import org.apache.xalan.xslt.*;

/**
 * FileStorage.java
 *
 * Created: Jan 2000
 *
 * Copyright (C) 2000 Sebastian Schaffert
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/**
 * This is the FileStorage class is common to all other storage classes in WebMail
 * It provides means of getting and storing data in ZIPFiles and ResourceBundles,
 * for example HTML-templates, binary files and MIME-types
 *
 * @see Storage
 * @author Sebastian Schaffert
 * @versin $Revision: 1.6 $
 */
public abstract class FileStorage extends Storage implements ConfigurationListener {
	
    protected Hashtable resources;
    
    protected Hashtable file_resources;

    protected Hashtable stylesheet_cache;
    protected Hashtable binary_cache;
	
    /** Stores Locale/ExpireableCache pairs */
    //protected Hashtable file_cache;;
	
    protected Authenticator auth;
    
    protected static Hashtable mime_types;
	
    protected Logger logger;
	
    protected static DateFormat df=null;
	
    private boolean init_complete=false;
    
    protected int file_cache_size=40;
		
    /**
     * Initialize SimpleStorage.
     * Fetch Configuration Information etc.
     */
    public FileStorage(WebMailServer parent) {
	super(parent);
	
	initConfig();
	
	cs.addConfigurationListener("AUTH",this);
	cs.configRegisterStringKey(this,"MIME TYPES",parent.getProperty("webmail.lib.path")+
				   System.getProperty("file.separator")+"mime.types",
				   "File with mime type information.");
		

	cs.configRegisterYesNoKey("SHOW ADVERTISEMENTS","Whether or not to include the WebMail advertisement "+
				  "messages in default user signatures and HTTP response headers");
	cs.configRegisterStringKey("ADVERTISEMENT MESSAGE","JWebMail "+parent.getVersion()+" WWW to Mail Gateway", "Advertisement to attach to user signatures");
				
	resources=new Hashtable();
	file_resources=new Hashtable();
		
	initCache();
		
	initLog();

	// Now included in configuration:
// 	initVirtualDomains();
		
	initMIME();
		
	initAuth();
		
	initLanguages();
		
	init_complete=true;
    }
	
    /**
     * initialize XMLSystemData sysdata
     */
    protected abstract void initConfig();

    protected void initCache() {
	// Initialize the file cache
	cs.configRegisterIntegerKey(this,"CACHE SIZE FILE","40","Size for the file system cache for every locale");
	try {
	    file_cache_size=Integer.parseInt("CACHE SIZE FILE");
	} catch(NumberFormatException e) {}

	// Now the same for the stylesheet cache
	if(stylesheet_cache == null) {
	    stylesheet_cache = new Hashtable(10);
	}
	Enumeration enum2=stylesheet_cache.keys();
	while(enum2.hasMoreElements()) {
	    ExpireableCache ec=(ExpireableCache)stylesheet_cache.get(enum2.nextElement());
	    ec.setCapacity(file_cache_size);
	}

	// And for binary files
	if(binary_cache == null) {
	    binary_cache = new Hashtable(10);
	}
	Enumeration enum3=binary_cache.keys();
	while(enum3.hasMoreElements()) {
	    ExpireableCache ec=(ExpireableCache)binary_cache.get(enum3.nextElement());
	    ec.setCapacity(file_cache_size);
	}
    }
	
    protected void initLog() {
	logger=new Logger(parent,this);
    }
	
    protected void initAuth() {
	System.err.print("  * Authenticator ... ");
	Authenticator a=parent.getAuthenticatorHandler().getAuthenticator(getConfig("AUTH"));
	if(a!=null) {
	    // IMAP level authentication
	    auth=a;
	    auth.init(this);
	    System.err.println("ok. Using "+auth.getClass().getName()+" (v"+auth.getVersion()+") for authentication.");
	} else {
	    System.err.println("error. Could not initalize any authenticator. Users will not be able to log on.");
	    auth=null;
	}
    }
    
	
    protected void initMIME() {
	System.err.print("  * MIME types ... ");
	if(getConfig("mime types") != null) {
	    try {
		File f=new File(getConfig("mime types"));
		if(f.exists() && f.canRead()) {
		    mime_types=new Hashtable();
		    BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
		    String line=in.readLine();
		    while(line != null) {
			if(!line.startsWith("#")) {
			    StringTokenizer tok=new StringTokenizer(line);
			    if(tok.hasMoreTokens()) {
				String type=tok.nextToken();
				while(tok.hasMoreTokens()) {
				    String key=tok.nextToken();
				    mime_types.put(key,type);
				    //System.err.println(key+" -> "+type);
				}
			    }
			}
			line=in.readLine();
		    }
		    in.close();
		    System.err.println(" loaded from "+getConfig("mime types")+".");
		} else {
		    System.err.println(" could not find "+getConfig("mime types")+". Will use standard MIME types.");
		}
	    } catch(IOException ex) {
		System.err.println(" could not find "+getConfig("mime types")+". Will use standard MIME types.");
	    }
	} else {
	    System.err.println(" not configured. Will use standard MIME types.");
	}
    }
    
    protected void initLanguages() {
	System.err.print("  * Available languages ... ");
	File f=new File(parent.getProperty("webmail.template.path")+System.getProperty("file.separator"));
	String[] flist=f.list(new FilenameFilter() {
		public boolean accept(File myf, String s) {
		    if(myf.isDirectory() && s.equals(s.toLowerCase()) && (s.length()==2 || s.equals("default"))) {
			return true;
		    } else {
			return false;
		    }
		}
	    });

	File cached=new File(parent.getProperty("webmail.data.path")+System.getProperty("file.separator")+"locales.cache");
	Locale[] available1=null;

	/* Now we try to cache the Locale list since it takes really long to gather it! */
	boolean exists=cached.exists();
	if(exists) {
	    try {
		ObjectInputStream in=new ObjectInputStream(new FileInputStream(cached));
		available1=(Locale[])in.readObject();
		in.close();
		System.err.print(" using disk cache ... ");
	    } catch(Exception ex) {
		exists=false;
	    }
	}
	if(!exists) {
	    // We should cache this on disk since it is so slow!
	    available1=Collator.getAvailableLocales();
	    try {
		ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(cached));
		os.writeObject(available1);
		os.close();
	    } catch(Exception ex) {
		ex.printStackTrace();
	    }
	}

	// Do this manually, as it is not JDK 1.1 compatible ...
	//Vector available=new Vector(Arrays.asList(available1));
	Vector available=new Vector(available1.length);
	for(int i=0; i<available1.length; i++) {
	    available.addElement(available1[i]);
	}
	String s="";
	int count=0;       

⌨️ 快捷键说明

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