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

📄 nameserviceproxy.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 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 name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``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
 * EXOFFICE TECHNOLOGIES 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.
 *
 * Copyright 2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: NameServiceProxy.java,v 1.2 2003/08/07 13:32:58 tanderson Exp $
 */
package org.exolab.jms.jndi.mipc;

import java.util.Vector;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingException;

import org.apache.avalon.excalibur.naming.NamingProvider;

import org.exolab.core.ipc.Client;


/**
 * The underlying communication interface for remote contexts over the tcp
 * connector.
 *
 * @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
 * @version $Revision: 1.2 $
 */
public class NameServiceProxy implements NamingProvider {

    /**
     * The server connection
     */
    private Client _connection;


    public NameServiceProxy(Client connection) {
        _connection = connection;
    }

    public NameParser getNameParser() throws NamingException {
        NameParser parser;
        Vector request = pack("getNameParser", 0);
        synchronized (_connection) {
            send(request);
            parser = (NameParser) checkReply("getNameParser");
        }
        return parser;
    }

    public void bind(Name name, String className, Object object)
        throws NamingException {
        Vector request = pack("bind", 3);
        request.add(name);
        request.add(className);
        request.add(object);
        synchronized (_connection) {
            send(request);
            checkReply("bind");
        }
    }

    public void rebind(Name name, String className, Object object)
        throws NamingException {
        Vector request = pack("rebind", 3);
        request.add(name);
        request.add(className);
        request.add(object);
        synchronized (_connection) {
            send(request);
            checkReply("rebind");
        }
    }

    public Context createSubcontext(Name name) throws NamingException {
        Context subcontext;
        Vector request = pack("createSubContext", 1);
        request.add(name);
        synchronized (_connection) {
            send(request);
            subcontext = (Context) checkReply("createSubContext");
        }
        return subcontext;
    }

    public void destroySubcontext(Name name) throws NamingException {
        Vector request = pack("destroySubContext", 1);
        request.add(name);
        synchronized (_connection) {
            send(request);
            checkReply("destroySubContext");
        }
    }

    public NameClassPair[] list(Name name) throws NamingException {
        NameClassPair[] pairs;
        Vector request = pack("list", 1);
        request.add(name);
        synchronized (_connection) {
            send(request);
            pairs = (NameClassPair[]) checkReply("list");
        }
        return pairs;
    }

    public Binding[] listBindings(Name name) throws NamingException {
        Binding[] bindings;
        Vector request = pack("listBindings", 1);
        request.add(name);
        synchronized (_connection) {
            send(request);
            bindings = (Binding[]) checkReply("listBindings");
        }
        return bindings;
    }

    public Object lookup(Name name) throws NamingException {
        Object object;
        Vector request = pack("lookup", 1);
        request.add(name);
        synchronized (_connection) {
            send(request);
            object = checkReply("lookup");
        }
        return object;
    }

    public void unbind(Name name) throws NamingException {
        Vector request = pack("unbind", 1);
        request.add(name);
        synchronized (_connection) {
            send(request);
            checkReply("unbind");
        }
    }

    /**
     * Pack all the data that is required by the server in a vector.
     * Set the size of the vector to be exactly the right size for efficiency.
     *
     * @param method The function to activate on the server.
     * @param numParams The number of paramaters this method will require.
     * @return Vector The vector containing all the data.
     */
    protected Vector pack(String method, int numParams) {
        Vector request = new Vector(3 + numParams);
        request.add(getClassName());
        request.add(method);
        request.add("fooey");
        return request;
    }

    /**
     * Return the Server class name that will handle this request.
     *
     * @return The class name.
     */
    protected String getClassName() {
        return "org.exolab.jms.jndi.mipc.IpcJmsJndiServer";
    }

    /**
     * A convenience method to check the success of operations which return
     * a true on sucess.
     *
     * @param method The requested server function.
     * @throws NamingException on any failure.
     */
    protected Object checkReply(String method) throws NamingException {
        Object result = null;

        try {
            Vector v = (Vector) _connection.receive();
            if (v != null) {
                Boolean b = (Boolean) v.get(0);
                if (!b.booleanValue()) {
                    NamingException error = (NamingException) v.get(1);
                    throw error;
                } else {
                    result = v.get(1);
                }
            } else {
                throw new NamingException("Unknown connection error for "
                    + method);
            }
        } catch (NamingException error) {
            throw error;
        } catch (Exception error) {
            // rethrow as a NamingException
            throw new NamingException("Operation " + method + " failed: "
                + error);
        }
        return result;
    }

    /**
     * A convenience method to send a packed command to the server.
     *
     * @throws NamingException on any failure.
     */
    protected void send(Vector v) throws NamingException {
        try {
            _connection.send(v);
        } catch (Exception error) {
            // rethrow as a NamingException
            throw new NamingException("Operation failed: " + error);
        }
    }

} //-- NameServiceProxy

⌨️ 快捷键说明

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