javaextensions/wma/mms/javasrc/com/nokia/mj/impl/mms/MMSConnectionImpl.java
branchRCL_3
changeset 83 26b2b12093af
parent 19 04becd199f91
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
     1 /*
     1 /*
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    19 package com.nokia.mj.impl.mms;
    19 package com.nokia.mj.impl.mms;
    20 
    20 
    21 import java.io.IOException;
    21 import java.io.IOException;
    22 import java.io.InterruptedIOException;
    22 import java.io.InterruptedIOException;
    23 import javax.wireless.messaging.*;
    23 import javax.wireless.messaging.*;
       
    24 import java.lang.ref.WeakReference;
    24 
    25 
    25 import com.nokia.mj.impl.utils.Logger;
    26 import com.nokia.mj.impl.utils.Logger;
       
    27 import com.nokia.mj.impl.gcf.PushValidator;
       
    28 import com.nokia.mj.impl.rt.support.Finalizer;
    26 import com.nokia.mj.impl.mms.MmsPropertyRetriever;
    29 import com.nokia.mj.impl.mms.MmsPropertyRetriever;
    27 import com.nokia.mj.impl.rt.support.ShutdownListener;
    30 import com.nokia.mj.impl.rt.support.ShutdownListener;
       
    31 import com.nokia.mj.impl.rt.support.ApplicationInfo;
    28 import com.nokia.mj.impl.rt.support.ApplicationUtils;
    32 import com.nokia.mj.impl.rt.support.ApplicationUtils;
    29 
    33 
    30 public final class MMSConnectionImpl implements MessageConnection
    34 public final class MMSConnectionImpl implements MessageConnection
    31 {
    35 {
    32     private static final int INITIAL = 0;
    36     private static final int INITIAL = 0;
    76 
    80 
    77     private String iUri;
    81     private String iUri;
    78 
    82 
    79     // Application Id
    83     // Application Id
    80     private String iAppID = null;
    84     private String iAppID = null;
    81 
    85     
       
    86     private Finalizer iFinalizer;
       
    87 
       
    88     private ShutdownListener iShutDownListener;
    82     // the listener used for messages receiving notifications
    89     // the listener used for messages receiving notifications
    83     private MessageListener iMessageListener;
    90     private MessageListener iMessageListener;
    84 
    91 
    85     public MMSConnectionImpl(String aUri, boolean aServerConnection)
    92     public MMSConnectionImpl(String aUri, boolean aServerConnection)
    86     throws IOException
    93     throws IOException
    88         Logger.LOG(Logger.EWMA, Logger.EInfo,
    95         Logger.LOG(Logger.EWMA, Logger.EInfo,
    89                    "+ MMSConnectionImpl::MMSConnectionImpl()");
    96                    "+ MMSConnectionImpl::MMSConnectionImpl()");
    90         iUri = aUri;
    97         iUri = aUri;
    91         iState = INITIAL;
    98         iState = INITIAL;
    92         iServerConnection = aServerConnection;
    99         iServerConnection = aServerConnection;
       
   100         if (iServerConnection)
       
   101         {
       
   102             // Check if this url is push registered by other application 
       
   103             // Get the insatnce of ApplicationInfo.
       
   104             ApplicationInfo appInfo = ApplicationInfo.getInstance();
       
   105             if (PushValidator.isRegisteredPushUriStartingWith(aUri, 
       
   106                  appInfo.getSuiteUid(), PushValidator.RUNTIME_COMMS))
       
   107             {
       
   108                 throw new IOException("Connection already exists");
       
   109             }
       
   110             iAppID = aUri.substring("mms://:".length());
       
   111         }
    93         // create the native side peer
   112         // create the native side peer
    94         iNativeHandle = _createPeer(iServerConnection, iUri);
   113         iNativeHandle = _createPeer(iServerConnection, iUri);
    95         iReadLock = new Object();
   114         iReadLock = new Object();
    96         iWriteLock = new Object();
   115         iWriteLock = new Object();
    97         iCloseLock = new Object();
   116         iCloseLock = new Object();
    98         iMessageLock = new Object();
   117         iMessageLock = new Object();
    99         iSendLock = new Object();
   118         iSendLock = new Object();
   100         // register for shutdown listening
   119         // register for shutdown listening
   101         setShutdownListener();
   120         iShutDownListener = new MmsShutDownListener(this);
   102         if (iServerConnection)
   121         // register for finalization
   103         {
   122         iFinalizer = registerForFinalization();
   104             iAppID = aUri.substring("mms://:".length());
       
   105         }
       
   106         Logger.LOG(Logger.EWMA, Logger.EInfo,
   123         Logger.LOG(Logger.EWMA, Logger.EInfo,
   107                    "- MMSConnectionImpl::MMSConnectionImpl()");
   124                    "- MMSConnectionImpl::MMSConnectionImpl()");
   108     }
   125     }
   109 
   126 
   110     /*
   127     /*
   111     * This function registers this object for shutDown.
   128     * This function registers this object for Finalization.
   112     */
   129     */ 
   113     private void setShutdownListener()
   130     public Finalizer registerForFinalization()
   114     {
   131     {
   115         Logger.LOG(Logger.EWMA, Logger.EInfo,
   132         return new Finalizer()
   116                    "+ MMSConnectionImpl::setShutdownListener()");
   133         {
   117         // Get the insatnce of ApplicationUtils.
   134             public void finalizeImpl()
   118         ApplicationUtils appUtils = ApplicationUtils.getInstance();
       
   119 
       
   120         // Get the name of the application.
       
   121         appUtils.addShutdownListener(new ShutdownListener()
       
   122         {
       
   123             //The method that gets called when Application is shutting down
       
   124             public void shuttingDown()
       
   125             {
   135             {
   126                 try
   136                 try
   127                 {
   137                 {
   128                     close();
   138                     close();
   129                 }
   139                 }
   131                 {
   141                 {
   132                     //Nothing to do, just ignore
   142                     //Nothing to do, just ignore
   133                     Logger.LOG(Logger.EWMA, Logger.EInfo, e.toString());
   143                     Logger.LOG(Logger.EWMA, Logger.EInfo, e.toString());
   134                 }
   144                 }
   135             }
   145             }
   136         });
   146         };
   137         Logger.LOG(Logger.EWMA, Logger.EInfo,
   147     }
   138                    "- MMSConnectionImpl::setShutdownListener()");
   148     
       
   149     /*
       
   150     * Registering for shutDown Listener.
       
   151     */
       
   152     private static class MmsShutDownListener implements ShutdownListener
       
   153     {
       
   154         private final WeakReference iImpl;
       
   155 
       
   156         private MmsShutDownListener(MMSConnectionImpl impl)
       
   157         {
       
   158             // Get the instance of ApplicationUtils.
       
   159             ApplicationUtils appUtils = ApplicationUtils.getInstance();
       
   160             
       
   161             // Add the listener.
       
   162             appUtils.addShutdownListener(this);
       
   163             
       
   164             // Create weak reference to impl object.
       
   165             iImpl = new WeakReference(impl);
       
   166         }
       
   167 
       
   168         public void shuttingDown()
       
   169         {
       
   170             // Get a strong reference to impl class if it is not yet GC'ed.
       
   171             MMSConnectionImpl impl = (MMSConnectionImpl)iImpl.get();
       
   172             if (impl != null)
       
   173             {
       
   174                 try
       
   175                 {
       
   176                     impl.close();
       
   177                 }
       
   178                 catch (IOException e)
       
   179                 {
       
   180                     //Nothing to do, just ignore
       
   181                     Logger.LOG(Logger.EWMA, Logger.EInfo, e.toString());
       
   182                 }
       
   183             }
       
   184         }
   139     }
   185     }
   140 
   186 
   141     /**
   187     /**
   142      * open() is only called when a MIDlet calls Connector.open() a MIDlet will
   188      * open() is only called when a MIDlet calls Connector.open() a MIDlet will
   143      * not be able to access the Connection until open() has completed and a
   189      * not be able to access the Connection until open() has completed and a
   195                 synchronized (iReadLock)
   241                 synchronized (iReadLock)
   196                 {
   242                 {
   197                     iReadLock.notify();
   243                     iReadLock.notify();
   198                 }
   244                 }
   199                 _dispose(iNativeHandle);
   245                 _dispose(iNativeHandle);
       
   246                 // Remove the shutdown listener.
       
   247                 ApplicationUtils.getInstance().removeShutdownListener(
       
   248                                                           iShutDownListener);
   200             }
   249             }
   201         }
   250         }
   202         Logger.LOG(Logger.EWMA, Logger.EInfo,
   251         Logger.LOG(Logger.EWMA, Logger.EInfo,
   203                    "- MMSConnectionImpl::close()");
   252                    "- MMSConnectionImpl::close()");
   204     }
   253     }