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

📄 ftpletcontainer.java

📁 用java写的ftp服务器程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// $Id: FtpletContainer.java 348197 2005-11-22 10:33:33 -0700 (Tue, 22 Nov 2005) rana_b $
/*
 * Copyright 2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.ftpserver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.ftpserver.ftplet.Configuration;
import org.apache.ftpserver.ftplet.EmptyConfiguration;
import org.apache.ftpserver.ftplet.FtpConfig;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpResponse;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.FtpletEnum;

/**
 * This ftplet calls other ftplet methods and returns appropriate return value.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
class FtpletContainer implements Ftplet {
    
    private Log m_log;
    private ArrayList m_ftplets = new ArrayList();
    
    private static class FtpletEntry {
        public FtpletEntry(String name, Ftplet ftplet) {
            this.name = name;
            this.ftplet = ftplet;
        }
        final String name;
        final Ftplet ftplet;
    }
    
    /**
     * Dummy method - does nothing.
     */
    public void init(FtpConfig ftpConfig, Configuration config) throws FtpException {
    }
    
    /**
     * Initializes all the ftplets registered.
     */
    public void init(FtpConfig ftpConfig, 
                     String ftpletNames, 
                     Configuration ftpletConf) throws FtpException {
        
        if(ftpletNames == null) {
            return;
        }
        
        m_log = ftpConfig.getLogFactory().getInstance(getClass());
        StringTokenizer st = new StringTokenizer(ftpletNames, " ,;\r\n\t");
        try {
            while(st.hasMoreTokens()) {
                String ftpletName = st.nextToken();
                m_log.info("Configuring ftplet : " + ftpletName);
                Configuration subConfig = ftpletConf.getConfiguration(ftpletName, EmptyConfiguration.INSTANCE);
                
                String className = subConfig.getString("class", null);
                if(className == null) {
                    continue;
                }
                Ftplet ftplet = (Ftplet)Class.forName(className).newInstance();
                ftplet.init(ftpConfig, subConfig);
                m_ftplets.add(new FtpletEntry(ftpletName, ftplet));
            }
        }
        catch(FtpException ex) {
            destroy();
            throw ex;
        }
        catch(Exception ex) {
            destroy();
            m_log.fatal("FtpletContainer.init()", ex);
            throw new FtpException("FtpletContainer.init()", ex);
        }
    }
    
    /**
     * Get Ftplet for the given name.
     */
    public Ftplet getFtplet(String name) {
        if(name == null) {
            return null;
        }
        
        Ftplet ftplet = null;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            if(ftpletEnt.name.equals(name)) {
                ftplet = ftpletEnt.ftplet;
                break;
            }
        }
        return ftplet;
    }
    
    /**
     * Destroy all ftplets.
     */
    public void destroy() {
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            try {
                ftpletEnt.ftplet.destroy();
            }
            catch(Exception ex) {
                m_log.error(ftpletEnt.name + " :: FtpletHandler.destroy()", ex);
            }
        }
        m_ftplets.clear();
    }
    
    /**
     * Call ftplet onConnect.
     */
    public FtpletEnum onConnect(FtpRequest request, FtpResponse response) throws FtpException, IOException {
        FtpletEnum retVal = FtpletEnum.RET_DEFAULT;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            retVal = ftpletEnt.ftplet.onConnect(request, response);
            if(retVal == null) {
                retVal = FtpletEnum.RET_DEFAULT;
            }
            
            // proceed only if the return value is FtpletEnum.RET_DEFAULT
            if(retVal != FtpletEnum.RET_DEFAULT) {
                break;
            }
        }
        return retVal;
    }

    /**
     * Call ftplet onDisconnect.
     */
    public FtpletEnum onDisconnect(FtpRequest request, FtpResponse response) throws FtpException, IOException {
        FtpletEnum retVal = FtpletEnum.RET_DEFAULT;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            retVal = ftpletEnt.ftplet.onDisconnect(request, response);
            if(retVal == null) {
                retVal = FtpletEnum.RET_DEFAULT;
            }
            
            // proceed only if the return value is FtpletEnum.RET_DEFAULT
            if(retVal != FtpletEnum.RET_DEFAULT) {
                break;
            }
        }
        return retVal;
    }
    
    /**
     * Call ftplet onLogin.
     */
    public FtpletEnum onLogin(FtpRequest request, FtpResponse response) throws FtpException, IOException {
        FtpletEnum retVal = FtpletEnum.RET_DEFAULT;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            retVal = ftpletEnt.ftplet.onLogin(request, response);
            if(retVal == null) {
                retVal = FtpletEnum.RET_DEFAULT;
            }
            
            // proceed only if the return value is FtpletEnum.RET_DEFAULT
            if(retVal != FtpletEnum.RET_DEFAULT) {
                break;
            }
        }
        return retVal;
    }

    /** 
     * Call ftplet onDeleteStart.
     */
    public FtpletEnum onDeleteStart(FtpRequest request, FtpResponse response) throws FtpException, IOException {
        FtpletEnum retVal = FtpletEnum.RET_DEFAULT;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            retVal = ftpletEnt.ftplet.onDeleteStart(request, response);
            if(retVal == null) {
                retVal = FtpletEnum.RET_DEFAULT;
            }
            
            // proceed only if the return value is FtpletEnum.RET_DEFAULT
            if(retVal != FtpletEnum.RET_DEFAULT) {
                break;
            }
        }
        return retVal;
    }

    
    /**
     * Call ftplet onDeleteEnd.
     */
    public FtpletEnum onDeleteEnd(FtpRequest request, FtpResponse response) throws FtpException, IOException {
        FtpletEnum retVal = FtpletEnum.RET_DEFAULT;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            retVal = ftpletEnt.ftplet.onDeleteEnd(request, response);
            if(retVal == null) {
                retVal = FtpletEnum.RET_DEFAULT;
            }
            
            // proceed only if the return value is FtpletEnum.RET_DEFAULT
            if(retVal != FtpletEnum.RET_DEFAULT) {
                break;
            }
        }
        return retVal;
    }

    /**
     * Call ftplet onUploadStart.
     */
    public FtpletEnum onUploadStart(FtpRequest request, FtpResponse response) throws FtpException, IOException {
        FtpletEnum retVal = FtpletEnum.RET_DEFAULT;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            retVal = ftpletEnt.ftplet.onUploadStart(request, response);
            if(retVal == null) {
                retVal = FtpletEnum.RET_DEFAULT;
            }
            
            // proceed only if the return value is FtpletEnum.RET_DEFAULT
            if(retVal != FtpletEnum.RET_DEFAULT) {
                break;
            }
        }
        return retVal;
    }

    /**
     * Call ftplet onUploadEnd.
     */
    public FtpletEnum onUploadEnd(FtpRequest request, FtpResponse response) throws FtpException, IOException {
        FtpletEnum retVal = FtpletEnum.RET_DEFAULT;
        int sz = m_ftplets.size();
        for(int i=0; i<sz; ++i) {
            FtpletEntry ftpletEnt = (FtpletEntry)m_ftplets.get(i);
            retVal = ftpletEnt.ftplet.onUploadEnd(request, response);
            if(retVal == null) {
                retVal = FtpletEnum.RET_DEFAULT;
            }
            
            // proceed only if the return value is FtpletEnum.RET_DEFAULT
            if(retVal != FtpletEnum.RET_DEFAULT) {
                break;
            }

⌨️ 快捷键说明

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