001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.io.j2me.btgoep;
027:
028: import java.io.IOException;
029: import javax.microedition.io.Connection;
030: import javax.microedition.io.StreamConnectionNotifier;
031: import javax.microedition.io.StreamConnection;
032: import com.sun.kvem.jsr082.obex.ClientSessionImpl;
033: import com.sun.kvem.jsr082.obex.SessionNotifierImpl;
034: import com.sun.midp.security.SecurityToken;
035: import com.sun.midp.io.BluetoothUrl;
036: import com.sun.midp.io.BluetoothProtocol;
037: import com.sun.midp.security.Permissions;
038: import com.sun.midp.security.ImplicitlyTrustedClass;
039: import com.sun.midp.jsr082.SecurityInitializer;
040:
041: /**
042: * Provides a wrapper for "btgoep" protocol implementation
043: * to answer the GCF style.
044: */
045: public class Protocol extends BluetoothProtocol {
046:
047: /**
048: * Inner class to request security token from SecurityInitializer.
049: * SecurityInitializer should be able to check this inner class name.
050: */
051: static private class SecurityTrusted implements
052: ImplicitlyTrustedClass {
053: };
054:
055: /**
056: * The internal security token is privileged and allows to skip
057: * permissions check for btspp in case ones are allowed for btgoep.
058: */
059: private static SecurityToken internalSecurityToken = SecurityInitializer
060: .requestToken(new SecurityTrusted());
061:
062: /** Originally requested url saved to reused in btspp transport. */
063: String name = null;
064:
065: /**
066: * Constructs an instance.
067: */
068: public Protocol() {
069: super (BluetoothUrl.OBEX);
070: }
071:
072: /**
073: * Cheks permissions and opens requested connection.
074: * Returns either ClientSession or SessionNotifier for OBEX connections,
075: * depending whether client or server URL was specified. Actually all
076: * essential logic is implemented in the superclass, this one only saves
077: * resource name to reuse it for opening btspp transport.
078: *
079: * @param token security token of the calling class
080: * @param name the URL for the connection without protocol.
081: * @param mode obex supports READ_WRITE mode only.
082: * @param timeouts ignored (because it is allowed by spec).
083: * @return ClientSession for client url or SessionNotifier for server url.
084: * @exception IOException if opening connection fails.
085: */
086: public Connection openPrim(Object token, String name, int mode,
087: boolean timeouts) throws IOException {
088: this .name = name;
089: return super .openPrim(token, name, mode, timeouts);
090: }
091:
092: /**
093: * Ensures URL parameters have valid value. Makes nothing actually
094: * for all the required checks will be done durin ntspp transport
095: * creation.
096: * @param url URL to check
097: */
098: protected void checkUrl(BluetoothUrl url) {
099: }
100:
101: /**
102: * Creates ClientSession connection over the rfcomm transport layer
103: * @param token security token of the calling class
104: * @param mode I/O access mode
105: * @return ClientSession connection instance
106: * @exception IOException if openning connection fails.
107: */
108: protected Connection clientConnection(SecurityToken token, int mode)
109: throws IOException {
110: checkForPermission(token, Permissions.OBEX_CLIENT);
111:
112: StreamConnection sock = (StreamConnection) new com.sun.midp.io.j2me.btspp.Protocol()
113: .openPrim(getBtsppToken(token), name, mode, false);
114: return new ClientSessionImpl(new BTGOEPConnection(sock));
115: }
116:
117: /**
118: * Creates server connection over the rfcomm transport layer
119: * @param token security token of the calling class
120: * @param mode I/O access mode
121: * @return server connection instance
122: * @exception IOException if openning connection fails.
123: */
124: protected Connection serverConnection(SecurityToken token, int mode)
125: throws IOException {
126: checkForPermission(token, Permissions.OBEX_SERVER);
127:
128: StreamConnectionNotifier sock = (StreamConnectionNotifier) new com.sun.midp.io.j2me.btspp.Protocol()
129: .openPrim(getBtsppToken(token), name, mode, false);
130: return new SessionNotifierImpl(new BTGOEPNotifier(sock));
131: }
132:
133: /**
134: * Retrieves privileged token if there is no defined one.
135: * @param token security token on hands or <code>null</code>
136: * if there is no one
137: * @return privileged security token that allow acces to btspp if
138: * given token is <code>null</code>, the token given otherwise.
139: */
140: private static SecurityToken getBtsppToken(SecurityToken token) {
141: return (token == null) ? internalSecurityToken : token;
142: }
143: }
|