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

📄 servlethttptransport.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/************************************************************************
 *
 * $Id: ServletHttpTransport.java,v 1.26 2002/05/08 16:07:14 hamada Exp $
 *
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *    must not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 ******************************************************************************/

package net.jxta.impl.endpoint.servlethttp;

import net.jxta.document.Advertisement;
import net.jxta.endpoint.*;
import net.jxta.exception.PeerGroupException;
import net.jxta.id.ID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.EndpointAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.TransportAdvertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.TextElement;
import net.jxta.document.Element;

import net.jxta.impl.endpoint.*;
import net.jxta.impl.protocol.HTTPAdv;
import net.jxta.impl.relay.*;

import java.io.*;
import java.net.*;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.log4j.Category; import org.apache.log4j.Priority;

import net.jxta.platform.Module;

/**
 * Implementation of an http transport.
 *
 * This class is really a facade for the following:
 *  <li>An HTTP client message sender
 *  <li>An HTTP-server-based message receiver
 *  <li>An HTTP-server-based message relay server
 *  <li>An HTTP relay client message receiver that polls/blocks against
 *      an HTTP relay server
 */
public class ServletHttpTransport implements EndpointProtocol, Module {
    
    private static final Category LOG =
    Category.getInstance(ServletHttpTransport.class.getName());
    
    /** Normal http client message sender **/
    private HttpClientMessageSender httpClientSender = null;
    
    /** Message sender for sending to relay clients **/
    private RelayClientMessageSender relayClientSender = null;
    
    /** The local endpoint address that we are known by w.r.t. this transport.
     */
    private EndpointAddress localAddress = null;
    
    /**
     * The protocol name for the transport. Set by the advertisement on init()
     */
    private static String protocolName = "http";
    
    //// constants ////
    
    /** http relay client designator **/
    protected static final String MAGIC_WORD = "JxtaHttpClient";
    
    /** the relative URI of where the message receiver servlet will be
     * mounted */
    private static final String MSG_RECEIVER_RELATIVE_URI = "/";
    
    /** the relative URI of where the relay servlet will be mounted */
    private static final String RELAY_RELATIVE_URI = "/relay";
    
    /** the min threads that the http server will use for handling requests **/
    private static int minThreads = 10;
    
    /** the max threads that the http server will use for handling requests **/
    private static int maxThreads = 100;
    
    /** how long a thread can remain idle until the worker thread is let go **/
    private static int maxThreadIdleTime = 2 * 60 * 1000;
    
    /** how long an http request has to finish transferring before the http
     *  server discards the request.
     */
    private static int maxReqReadTime = 1 * 60 * 1000;
    
    
    /**
     * Returns true if this protocol accepts to be overloaded.
     * That is let a protocol with the name protocol name in a
     * descendant group be registered.
     * XXX jbeatty: I don't quite understand what this means.
     *
     * @return boolean true if overload is allowed.
     */
    public boolean allowOverLoad() {
        return false;
    }
    
    /**
     * Mark this module as up and running.
     */
    public int startApp(String[] args) {
        return 0;
    }
    
    /**
     * Stops the HTTP server message receive or the http relay client receiver
     * XXX TODO: not implemented
     */
    public void stopApp() {
    }
    
    /**
     * Initializes the transport. The following steps are taken:
     *  <li>the local endpoint address is computed based on the configuration.
     *  <li>the proxy server is configured, if there is one.
     *  <li>the normal http client sender is configured.
     *  <li>the relay client is configured, if there is one.
     *  <li>the http server is configured, if there is one.
     */
    public void init(PeerGroup peerGroup,
    ID assignedID,
    Advertisement impl)
    throws PeerGroupException {
        
        try {
            
            // read settings from the properties file
            Properties prop = getJxtaProperties();
            initFromProperties(prop);
            
            ModuleImplAdvertisement implAdv = (ModuleImplAdvertisement) impl;
            PeerAdvertisement peerAdv =
            (PeerAdvertisement) peerGroup.getConfigAdvertisement();
            
            // Get out invariable parameters from the implAdv
            Element param = implAdv.getParam();
            if (param != null) {
                Enumeration list = param.getChildren("Proto");
                if (list.hasMoreElements()) {
                    TextElement pname = (TextElement) list.nextElement();
                    protocolName = pname.getTextValue();
                }
            }
            
            param = peerAdv.getServiceParam(assignedID);
            
            // FIXME 20011220 bondolo@jxta.org Temporarily accept both nodes of
            // type HTTPAdv and TransportAdvertisement.
            Enumeration httpChilds = param.getChildren(
            TransportAdvertisement.getAdvertisementType());
            
            // get the TransportAdv from either TransportAdv or HttpAdv
            if( httpChilds.hasMoreElements() ) {
                param = (Element) httpChilds.nextElement();
            } else {
                httpChilds = param.getChildren(
                HTTPAdv.getAdvertisementType());
                
                if( httpChilds.hasMoreElements() ) {
                    param = (Element) httpChilds.nextElement();
                }
            }
            
            HTTPAdv httpAdv = (HTTPAdv)
            AdvertisementFactory.newAdvertisement((TextElement) param);
            
            EndpointService endpoint = peerGroup.getEndpointService();
            InetAddress usingInterface = computeInterface(httpAdv);
            localAddress = computeEndpointAddress(endpoint,
            httpAdv,
            peerGroup.getPeerID().getUniqueValue().toString());
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Local address = " + localAddress);
            
            configureProxyServer(httpAdv);
            
            configureHttpClientSender(localAddress, httpAdv);
            
            if (httpAdv.getRouterEnabled()) {
                configureRelayClients(peerGroup, endpoint, httpAdv);
            }
            
            if (httpAdv.getServerEnabled()) {
                configureServer(httpAdv,
                peerGroup,
                usingInterface,
                endpoint,
                prop);
            }
            
            endpoint.addEndpointProtocol(this);
            
            if (LOG.isEnabledFor(Priority.INFO))
                LOG.info( "ServletHttpTransport inited." );
        } catch (Throwable e) {
            if (LOG.isEnabledFor(Priority.ERROR))
                LOG.error("Not initialized: ", e);
            throw new PeerGroupException(e.getMessage());
        }
    }
    
    
    
    
    /**
     * Creates and return an EndpointMessenger for sending Message's. If dest
     * is for a relay client, then the "relay client sender" is used to obtain
     * the messenger.
     *
     * @param dest EndpointAddress of the destination
     * @param type type of the messenger as defined in the class EndpointService
     * @return an EndpointMessenger.
     */
    public EndpointMessenger getMessenger(EndpointAddress dest)
    throws IOException {
        
        if (isRelayClientAddress(dest)) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Returned relay client messenger");
            return relayClientSender.getMessenger(dest);
        } else {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Returning normal http client messenger");
            return httpClientSender.getMessenger(dest);
        }
    }
    
    
    /**
     * Propagates a Message on this EndpointProtocol
     *
     * @param msg the Message to be propagated
     * @param serviceName is a String containing the name of the service
     * @param serviceParams is a String containing the parameters associated
     * to the service. The combination of serviceName and serviceParams
     * is the what must have been registered to the EndpointService's demux.
     * @param prunePeer peer which should not receive the propagated message
     * @param msg the Message to be propagated
     */
    public void propagate(Message msg,
    String serviceName,
    String serviceParams,
    String prunePeer) throws IOException {
        // XXX TODO
    }
    
    /**
     * closes this EndpointProtocol.
     */
    public void close() {
        // XXX TODO
    }
    
    
    /**
     * Returns a String containing the canonical name of this
     * endpoint protocol, as it appears in an EndpointAddress.
     *
     * @return a String containing the name of the protocol
     */
    public String getProtocolName() {
        return getSupportedProtocolName();
    }
    
    public static String getSupportedProtocolName() {
        return protocolName;
    }
    
    /**

⌨️ 快捷键说明

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