001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.midp.jump.push.executive;
026:
027: import com.sun.midp.jump.push.executive.persistence.Store;
028: import com.sun.midp.push.gcf.ReservationDescriptor;
029: import com.sun.midp.push.gcf.ReservationDescriptorFactory;
030: import java.io.IOException;
031: import javax.microedition.io.ConnectionNotFoundException;
032:
033: /**
034: * Push controller.
035: *
036: * <p>
037: * Manages both connections and alarms.
038: * </p>
039: */
040: final class PushController {
041: /** Alarm controller. */
042: private final AlarmController alarmController;
043:
044: /** Connection controller. */
045: private final ConnectionController connectionController;
046:
047: /**
048: * Creates an implementation.
049: *
050: * @param store Store to use
051: * (cannot be <code>null</code>)
052: *
053: * @param lifecycleAdapter lifecycle adapter to use
054: * (cannot be <code>null</code>)
055: *
056: * @param reservationDescriptorFactory reservation descriptor factory
057: * (cannot be <code>null</code>)
058: */
059: public PushController(
060: final Store store,
061: final LifecycleAdapter lifecycleAdapter,
062: final ReservationDescriptorFactory reservationDescriptorFactory) {
063: this .alarmController = new AlarmController(store,
064: lifecycleAdapter);
065: this .connectionController = new ConnectionController(store,
066: reservationDescriptorFactory, lifecycleAdapter);
067: }
068:
069: /**
070: * Registers the connection.
071: *
072: * <p>
073: * Saves the connection into persistent store and reserves it
074: * for <code>MIDlet</code>.
075: * </p>
076: *
077: * <p>
078: * The connection should be already preverified (see
079: * <code>reservationDescriptor</code> parameter) and all the security
080: * checks should be performed.
081: * </p>
082: *
083: * @param midletSuiteID <code>MIDlet</code> suite ID
084: *
085: * @param midlet <code>MIDlet</code> class name
086: * (cannot be <code>null</code>)
087: *
088: * @param reservationDescriptor reservation descriptor
089: * (cannot be <code>null</code>)
090: *
091: * @throws IOException if the connection is already registered or
092: * if there are insufficient resources to handle the registration request
093: */
094: public void registerConnection(final int midletSuiteID,
095: final String midlet,
096: final ReservationDescriptor reservationDescriptor)
097: throws IOException {
098: connectionController.registerConnection(midletSuiteID, midlet,
099: reservationDescriptor);
100: }
101:
102: /**
103: * Unregisters the connection.
104: *
105: * <p>
106: * Removes the connection from persistent store and cancels connection
107: * reservation.
108: * </p>
109: *
110: * @param midletSuiteID <code>MIDlet</code> suite ID
111: *
112: * @param connectionName connection to unregister
113: * (cannot be <code>null</code>)
114: *
115: * @return <code>true</code> if the unregistration was successful,
116: * <code>false</code> if the connection was not registered
117: *
118: * @throws SecurityException if the connection was registered by
119: * another <code>MIDlet</code> suite
120: */
121: public boolean unregisterConnection(final int midletSuiteID,
122: final String connectionName) throws SecurityException {
123: return connectionController.unregisterConnection(midletSuiteID,
124: connectionName);
125: }
126:
127: /**
128: * Returns a list of registered connections for <code>MIDlet</code> suite.
129: *
130: * @param midletSuiteID <code>MIDlet</code> suite ID
131: *
132: * @param available if <code>true</code>, only return
133: * the list of connections with input available, otherwise
134: * return the complete list of registered connections for
135: * <code>MIDlet</code> suite
136: *
137: * @return array of registered connection strings, where each connection
138: * is represented by the generic connection <em>protocol</em>,
139: * <em>host</em> and <em>port</em> number identification
140: */
141: public String[] listConnections(final int midletSuiteID,
142: final boolean available) {
143: return connectionController.listConnections(midletSuiteID,
144: available);
145: }
146:
147: /**
148: * Fetches the <code>MIDlet</code> by the connection.
149: *
150: * @param midletSuiteID <code>MIDlet</code> suite ID to query for
151: *
152: * @param connectionName connectionName as passed into
153: * {@link #registerConnection}
154: * (cannot be <code>null</code>)
155: *
156: * @return <code>MIDlet</code> associated with <code>connectionName</code>
157: * or <code>null</code> if there is no appropriate association
158: */
159: public synchronized String getMIDlet(final int midletSuiteID,
160: final String connectionName) {
161: return connectionController.getMIDlet(midletSuiteID,
162: connectionName);
163: }
164:
165: /**
166: * Fetches the filter by the connection.
167: *
168: * @param midletSuiteID <code>MIDlet</code> suite ID to query for
169: *
170: * @param connectionName connectionName as passed into
171: * {@link #registerConnection}
172: * (cannot be <code>null</code>)
173: *
174: * @return filter associated with <code>connectionName</code> or
175: * <code>null</code> if there is no appropriate association
176: */
177: public synchronized String getFilter(final int midletSuiteID,
178: final String connectionName) {
179: return connectionController.getFilter(midletSuiteID,
180: connectionName);
181: }
182:
183: /**
184: * Registers an alarm.
185: *
186: * <p>
187: * NOTE: <code>midletSuiteID</code> parameter should refer to a valid
188: * <code>MIDlet</code> suite and <code>midlet</code> should refer to
189: * valid <code>MIDlet</code> from the given suite. <code>timer</code>
190: * parameters is the same as for corresponding <code>Date</code>
191: * constructor. No checks are performed and no guarantees are
192: * given if parameters are invalid.
193: * </p>
194: *
195: * @param midletSuiteID <code>MIDlet suite</code> ID
196: *
197: * @param midlet <code>MIDlet</code> class name
198: * (cannot be <code>null</code>)
199: *
200: * @param time alarm time
201: *
202: * @throws ConnectionNotFoundException if for any reason alarm cannot be
203: * registered
204: *
205: * @return previous alarm time or 0 if none
206: */
207: public long registerAlarm(final int midletSuiteID,
208: final String midlet, final long time)
209: throws ConnectionNotFoundException {
210: return alarmController.registerAlarm(midletSuiteID, midlet,
211: time);
212: }
213:
214: /**
215: * Removes Push-related information for the suite being uninstalled;
216: *
217: * <p>
218: * NOTE: <code>midletSuiteID</code> must refer to valid installed
219: * <code>MIDlet</code> suite. However, it might refer to the
220: * suite without alarms.
221: * </p>
222: *
223: * @param midletSuiteID ID of the suite to remove Push info for
224: */
225: public void removeSuiteInfo(final int midletSuiteID) {
226: alarmController.removeSuiteAlarms(midletSuiteID);
227: connectionController.removeSuiteConnections(midletSuiteID);
228: }
229:
230: /**
231: * Logs error message.
232: *
233: * @param message message to log
234: */
235: private static void logError(final String message) {
236: // TBD: proper logging
237: System.err.println("[error, " + PushController.class.getName()
238: + "]: " + message);
239: }
240:
241: /**
242: * Disposes the controller.
243: *
244: * <p>
245: * This method needs to be called when closing the system.
246: * </p>
247: */
248: public void dispose() {
249: alarmController.dispose();
250: connectionController.dispose();
251: }
252: }
|