001: /*
002: * Copyright 1990-2006 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.jumpimpl.module.push;
026:
027: import com.sun.jump.module.JUMPModuleFactory;
028: import java.util.Map;
029: import sun.misc.MIDPConfig;
030:
031: /** Factory to initialize push module. */
032: public final class PushFactoryImpl extends JUMPModuleFactory {
033: /** Name of MIDP component factory class. */
034: private static final String FACTORY_CLASS_NAME = "com.sun.midp.jump.push.executive.PushRegistryModuleFactoryImpl";
035:
036: /** MIDP component-side push module factory. */
037: private JUMPModuleFactory pushModuleFactory = null;
038:
039: /** {@inheritDoc} */
040: public void load(final Map map) {
041: try {
042: final Class cls = loadMIDPClass(FACTORY_CLASS_NAME);
043: pushModuleFactory = (JUMPModuleFactory) (cls.newInstance());
044: pushModuleFactory.load(map);
045: } catch (ClassNotFoundException cnfe) {
046: logError("failed to load Push module: " + cnfe);
047: } catch (InstantiationException ie) {
048: logError("failed to load Push module: " + ie);
049: } catch (IllegalAccessException iae) {
050: logError("failed to load Push module: " + iae);
051: }
052: }
053:
054: /** {@inheritDoc} */
055: public void unload() {
056: if (pushModuleFactory == null) {
057: throw new IllegalStateException(
058: "attempt to unload push module" + " before load");
059: }
060: pushModuleFactory.unload();
061: pushModuleFactory = null;
062: }
063:
064: /**
065: * Gets MIDP class loader.
066: *
067: * @return MIDP class loader
068: */
069: private static ClassLoader getMIDPClassLoader() {
070: /*
071: * IMPL_NOTE: technically speaking might return <code>null</code>,
072: * but it shouldn't happen if initialization order is correct.
073: */
074: final ClassLoader cl = MIDPConfig
075: .getMIDPImplementationClassLoader();
076: if (cl == null) {
077: logError("failed to fetch MIDP class loader, got null instead");
078: }
079: return cl;
080: }
081:
082: /**
083: * Loads MIDP class.
084: *
085: * <p>
086: * Throws all the exceptions {@link java.lang.Class#forName} throws.
087: * </p>
088: *
089: * @param className name of class to load
090: * @return loaded class
091: * @throws ClassNotFoundException if unable to find a class
092: */
093: private static Class loadMIDPClass(final String className)
094: throws ClassNotFoundException {
095: return Class.forName(className, true, getMIDPClassLoader());
096: }
097:
098: /**
099: * Logs an error.
100: *
101: * <p>
102: * TBD: proper logging
103: * </p>
104: *
105: * @param message message to log
106: */
107: private static void logError(final String message) {
108: System.err.println("[ERROR] PushFactoryImpl: " + message);
109: }
110: }
|