connectioncontroller.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 622 行 · 第 1/2 页

JAVA
622
字号
/* * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * 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 version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package com.sun.midp.push.controller;import com.sun.midp.push.reservation.ConnectionReservation;import com.sun.midp.push.reservation.DataAvailableListener;import com.sun.midp.push.reservation.ReservationDescriptor;import com.sun.midp.push.persistence.Store;import com.sun.midp.push.reservation.impl.ReservationDescriptorFactory;import com.sun.j2me.security.AccessControlContext;import com.sun.j2me.security.AccessControlContextAdapter;import java.io.IOException;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import java.util.Vector;import javax.microedition.io.ConnectionNotFoundException;/** Push connection controller. */final class ConnectionController {    /** Store to save connection info. */    private final Store store;    /**     * Reservation descriptor factory.     *     * IMPL_NOTE: hopefully will go away     */    private final ReservationDescriptorFactory reservationDescriptorFactory;    /** Lifecycle adapter implementation. */    private final LifecycleAdapter lifecycleAdapter;    /** Current reservations. */    private final Reservations reservations;    /**     * Creates an instance.     *     * @param store persistent store to save connection info into     * (cannot be <code>null</code>)     *     * @param reservationDescriptorFactory reservation descriptor factory     * (cannot be <code>null</code>     *     * @param lifecycleAdapter adapter to launch <code>MIDlet</code>     * (cannot be <code>null</code>)     */    public ConnectionController(            final Store store,            final ReservationDescriptorFactory reservationDescriptorFactory,            final LifecycleAdapter lifecycleAdapter) {        this.store = store;        this.reservationDescriptorFactory = reservationDescriptorFactory;        this.lifecycleAdapter = lifecycleAdapter;        this.reservations = new Reservations();        reserveConnectionsFromStore();    }    /**     * Registers the connection.     *     * <p>     * Saves the connection into persistent store and reserves it     * for <code>MIDlet</code>.     * </p>     *     * <p>     * The connection should be already preverified (see     * <code>reservationDescriptor</code> parameter) and all the security     * checks should be performed.     * </p>     *     * @param midletSuiteID <code>MIDlet</code> suite ID     *     * @param midlet <code>MIDlet</code> class name     * (cannot be <code>null</code>)     *     * @param reservationDescriptor reservation descriptor     * (cannot be <code>null</code>)     *     * @throws IOException if the connection is already registered or     * if there are insufficient resources to handle the registration request     */    public synchronized void registerConnection(            final int midletSuiteID,            final String midlet,            final ReservationDescriptor reservationDescriptor) throws                IOException {        final String connectionName = reservationDescriptor.getConnectionName();        /*         * IMPL_NOTE: due to ReservationDescriptor#reserve limitations,         * need to unregister registered connection first         */        final ReservationHandler previous =                reservations.queryByConnectionName(connectionName);        if (previous != null) {            throw new IOException("registered for another suite");        }        final ReservationHandler reservationHandler = new ReservationHandler(                midletSuiteID, midlet, reservationDescriptor);        final String filter = reservationDescriptor.getFilter();        try {            // TODO: rethink if we need filter in ConnectionInfo            final ConnectionInfo connectionInfo = new ConnectionInfo(                    connectionName, midlet, filter);            store.addConnection(midletSuiteID, connectionInfo);        } catch (IOException ioex) {            reservationHandler.cancel();            throw ioex; // rethrow IOException        }        reservations.add(reservationHandler);    }    /**     * Unregisters the connection.     *     * <p>     * Removes the connection from persistent store and cancels connection     * reservation.     * </p>     *     * @param midletSuiteID <code>MIDlet</code> suite ID     *     * @param connectionName connection to unregister     * (cannot be <code>null</code>)     *     * @return <code>true</code> if the unregistration was successful,     * <code>false</code> if the connection was not registered     *     * @throws SecurityException if the connection was registered by     * another <code>MIDlet</code>  suite     */    public synchronized boolean  unregisterConnection(            final int midletSuiteID,            final String connectionName) throws                SecurityException {        final ReservationHandler reservationHandler =                reservations.queryByConnectionName(connectionName);        if (reservationHandler == null) {            // Connection hasn't been registered            return false;        }        if (reservationHandler.getSuiteId() != midletSuiteID) {            throw new SecurityException(                    "attempt to unregister connection of another suite");        }        try {            removeRegistration(reservationHandler);        } catch (IOException ioex) {            return false;        }        return true;    }    /**     * Transactionally removes the registration.     *     * @param reservationHandler reservation handler.     *     * @throws IOException if persistent store fails     */    private void removeRegistration(final ReservationHandler reservationHandler)            throws IOException {        final ConnectionInfo info = new ConnectionInfo(                reservationHandler.getConnectionName(),                reservationHandler.getMidlet(),                reservationHandler.getFilter());        store.removeConnection(reservationHandler.getSuiteId(), info);        reservationHandler.cancel();        reservations.remove(reservationHandler);    }    /**     * Returns a list of registered connections for <code>MIDlet</code> suite.     *     * @param midletSuiteID <code>MIDlet</code> suite ID     *     * @param available if <code>true</code>, only return     * the list of connections with input available, otherwise     * return the complete list of registered connections for     * <code>MIDlet</code> suite     *     * @return array of registered connection strings, where each connection     * is represented by the generic connection <em>protocol</em>,     * <em>host</em> and <em>port</em> number identification     */    public synchronized String[] listConnections(            final int midletSuiteID,            final boolean available) {        final Vector result = new Vector();        final Iterator it = reservations                .queryBySuiteID(midletSuiteID).iterator();        while (it.hasNext()) {            final ReservationHandler handler = (ReservationHandler) it.next();            if ((!available) || handler.hasAvailableData()) {                result.add(handler.getConnectionName());            }        }        return (String[]) result.toArray(new String[result.size()]);    }    /**     * Fetches the <code>MIDlet</code> by the connection.     *     * @param midletSuiteID <code>MIDlet</code> suite ID to query for     *     * @param connectionName connectionName as passed into     * {@link #registerConnection}     * (cannot be <code>null</code>)     *     * @return <code>MIDlet</code> associated with <code>connectionName</code>     * or <code>null</code> if there is no appropriate association     */    public synchronized String getMIDlet(            final int midletSuiteID, final String connectionName) {        final ReservationHandler reservationHandler =                reservations.queryByConnectionName(connectionName);        if ((reservationHandler == null)            || (reservationHandler.getSuiteId() != midletSuiteID)) {            return null;        }        return reservationHandler.getMidlet();    }    /**     * Fetches the filter by the connection.     *     * @param midletSuiteID <code>MIDlet</code> suite ID to query for     *     * @param connectionName connectionName as passed into     * {@link #registerConnection}     * (cannot be <code>null</code>)     *     * @return filter associated with <code>connectionName</code> or     * <code>null</code> if there is no appropriate association     */    public synchronized String getFilter(            final int midletSuiteID, final String connectionName) {        final ReservationHandler reservationHandler =                reservations.queryByConnectionName(connectionName);        if ((reservationHandler == null)            || (reservationHandler.getSuiteId() != midletSuiteID)) {            return null;        }        return reservationHandler.getFilter();    }    /**     * Removes connections for the given suite.     *     * <p>     * NOTE: <code>midletSuiteID</code> must refer to valid installed     *  <code>MIDlet</code> suite.  However, it might refer to the     *  suite without connections.     * </p>     *     * @param midletSuiteID ID of the suite to remove connections for     */    public synchronized void removeSuiteConnections(final int midletSuiteID) {        /*         * IMPL_NOTE: one shouldn't remove and iterate in same time.         * The solution is to copy first.  It's safe for method is synchronized.         */        final Collection rs = reservations.queryBySuiteID(midletSuiteID);        final ReservationHandler [] handlers =                new ReservationHandler [rs.size()];        rs.toArray(handlers);        for (int i = 0; i < handlers.length; i++) {            try {                removeRegistration(handlers[i]);            } catch (IOException ioex) {

⌨️ 快捷键说明

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