MISC: Code Refactorings. Prevented null pointer dereferences. 37/15537/1
authorkh5325.kim <kh5325.kim@samsung.com>
Thu, 23 Jan 2014 04:44:48 +0000 (13:44 +0900)
committerkh5325.kim <kh5325.kim@samsung.com>
Thu, 23 Jan 2014 04:44:48 +0000 (13:44 +0900)
Code Refactorings. Prevented null pointer dereferences.

Change-Id: I28b097b4ee1e88c270764f57e3483f11ba288fcd
Signed-off-by: kh5325.kim <kh5325.kim@samsung.com>
org.tizen.common.sdblib/src/org/tizen/sdblib/DeviceMonitor.java
org.tizen.common.sdblib/src/org/tizen/sdblib/command/Command.java

index 00bc09e..2d90394 100755 (executable)
@@ -33,6 +33,7 @@ import java.io.IOException;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.SocketChannel;
 import java.util.ArrayList;
+import java.util.List;
 
 import org.tizen.sdblib.daemon.AbstractServer;
 import org.tizen.sdblib.daemon.ServerState;
@@ -59,7 +60,7 @@ extends AbstractServer
     /**
      * Devices to be monitored
      */
-    protected ArrayList<Device> devices = null;
+    protected List<Device> devices = null;
 
     private SocketChannel mMainSdbConnection = null;
     private boolean mMonitoring = false;
@@ -74,10 +75,10 @@ extends AbstractServer
      *            the running {@link SmartDevelopmentBridge}.
      */
     DeviceMonitor(
-       final SmartDevelopmentBridge bridge
+        final SmartDevelopmentBridge bridge
     )
     {
-       super( "Devices Monitor" );
+        super( "Devices Monitor" );
         this.bridge = bridge;
     }
 
@@ -105,14 +106,14 @@ extends AbstractServer
     Device[]
     getDevices()
     {
-       while ( null == devices )
-       {
-               try {
-                               this.wait();
-                       } catch (InterruptedException e) {
-                       }
-       }
-       return devices.toArray( new Device[devices.size()] );
+        while ( null == devices )
+        {
+            try {
+                this.wait();
+            } catch (InterruptedException e) {
+            }
+        }
+        return devices.toArray( new Device[devices.size()] );
     }
     
     /**
@@ -123,20 +124,19 @@ extends AbstractServer
     synchronized protected
     void
     removeDevice(
-       final Device device
+        final Device device
     )
     {
         devices.remove(device);
     }
 
-
     SmartDevelopmentBridge getServer() {
         return bridge;
     }
 
     private void handleExpectioninMonitorLoop(Exception e) {
-       if ( isState( ServerState.Running ) )
-       {
+        if ( isState( ServerState.Running ) )
+        {
             if (e instanceof TimeoutException) {
                 Log.e("DeviceMonitor", "sdb connection Error: timeout");
             } else {
@@ -164,13 +164,12 @@ extends AbstractServer
         Log.d("DeviceMonitor", "connecting to sdb for Device List Monitoring...");
 
         try {
-               final SocketChannel channel = SmartDevelopmentBridge.getBridge().openChannel();
-               bridge.setStarted( true );
-               return channel;
-
-        } catch (IOException e)        {
-               Log.e("DeviceMonitor", "Failed to open socket channel" );
-               Log.e("DeviceMonitor", e );
+            final SocketChannel channel = SmartDevelopmentBridge.getBridge().openChannel();
+            bridge.setStarted( true );
+            return channel;
+        } catch (IOException e) {
+            Log.e("DeviceMonitor", "Failed to open socket channel");
+            Log.e("DeviceMonitor", e);
         }
         return null;
     }
@@ -181,8 +180,7 @@ extends AbstractServer
      * @throws IOException
      */
     private boolean sendDeviceListMonitoringRequest() throws TimeoutException, IOException {
-        if ( null == mMainSdbConnection )
-        {
+        if ( null == mMainSdbConnection ) {
             return false;
         }
         byte[] request = SdbHelper.formSdbRequest("host:track-devices"); //$NON-NLS-1$
@@ -240,26 +238,25 @@ extends AbstractServer
     }
 
     /**
-     * Update device as new infomations.
+     * Update device as new informations.
      * 
      * @throws AsynchronousCloseException if device is disconnected before being processed
      */
     protected
     void
     updateDevices(
-       final ArrayList<Device> newList
+        final ArrayList<Device> newList
     )
     throws AsynchronousCloseException
     {
         synchronized (SmartDevelopmentBridge.getLock()) {
-               final ArrayList<Device> devicesToQuery = new ArrayList<Device>();
-               if (null == this.devices )
-               {
-                       this.devices = new ArrayList<Device>();
-               }
-               synchronized ( this )
+            final List<Device> devicesToQuery = new ArrayList<Device>();
+            if ( null == this.devices )
+            {
+                this.devices = new ArrayList<Device>();
+            }
+            synchronized ( this )
             {
-                   
                 for ( int d = 0; d < devices.size() ; )
                 {
                     final Device device = devices.get(d);
@@ -275,7 +272,6 @@ extends AbstractServer
                             }
 
                             if (device.getState() != newDevice.getState()) {
-                                
                                 // get old device state
                                 DeviceState oldDeviceState = device.getState();
                                 
@@ -308,7 +304,7 @@ extends AbstractServer
                         }
                     }
 
-                    if (foundMatch == false) {
+                    if (!foundMatch) {
                         removeDevice(device);
                         bridge.deviceDisconnected(device);
                         Log.d("DeviceMonitor", "updateDevices: " + device.getSerialNumber() + " off-lined");
@@ -331,7 +327,6 @@ extends AbstractServer
                 }
                 this.notifyAll();
             }
-               
         }
         newList.clear();
     }
@@ -364,74 +359,72 @@ extends AbstractServer
         }
     }
 
-       /* (non-Javadoc)
-        * @see org.tizen.sdblib.daemon.AbstractServer#initialize()
-        */
-       @Override
+    /* (non-Javadoc)
+     * @see org.tizen.sdblib.daemon.AbstractServer#initialize()
+     */
+    @Override
     protected void initialize() throws ServerException
     {
         Log.d("DeviceMonitor", "start monitoring");
     }
 
-       /* (non-Javadoc)
-        * @see org.tizen.sdblib.daemon.AbstractServer#process()
-        */
-       @Override
+    /* (non-Javadoc)
+     * @see org.tizen.sdblib.daemon.AbstractServer#process()
+     */
+    @Override
     protected void process() throws Exception
     {
-               try {
-                       if (mMainSdbConnection == null) {
-                               Log.d("DeviceMonitor", "opening sdb connection");
-                               mMainSdbConnection = openSdbConnection();
-                               if (mMainSdbConnection == null) {
-                                       Log.d("DeviceMonitor", "start sdb");
-                                       if (bridge.startSdb() == false) {
-                                               synchronized ( bridge )
+        try {
+            if (mMainSdbConnection == null) {
+                Log.d("DeviceMonitor", "opening sdb connection");
+                mMainSdbConnection = openSdbConnection();
+                if (mMainSdbConnection == null) {
+                    Log.d("DeviceMonitor", "start sdb");
+                    if (bridge.startSdb() == false) {
+                        synchronized ( bridge )
                         {
-                                                       bridge.notifyAll();
+                            bridge.notifyAll();
                         }
-                                               mRestartAttemptCount++;
-                                               Log.e("DeviceMonitor", "sdb restart attempts: " + mRestartAttemptCount);
-                                       }
-                                       trySleep( 1000 );
-                               } else {
-                                       Log.d("DeviceMonitor", "connected to sdb for device monitoring");
-                               }
-                       }
-
-                       if ( !mMonitoring )
-                       {
-                               mMonitoring = sendDeviceListMonitoringRequest();
-                       }
-
-                       if (mMonitoring) {
-                               // read the length of the incoming message
-                               int length = readLength(mMainSdbConnection);
-
-                               if (length >= 0) {
-                                       // read the incoming message
-                                       processIncomingDeviceData(length);
-
-                                       // flag the fact that we have build the list at least
-                                       // once.
-                                       mDeviceListInitialized = true;
-                               }
-                       }
-               } catch (AsynchronousCloseException ace) {
+                        mRestartAttemptCount++;
+                        Log.e("DeviceMonitor", "sdb restart attempts: " + mRestartAttemptCount);
+                    }
+                    trySleep( 1000 );
+                } else {
+                    Log.d("DeviceMonitor", "connected to sdb for device monitoring");
+                }
+            }
+
+            if (!mMonitoring) {
+                mMonitoring = sendDeviceListMonitoringRequest();
+            }
+
+            if (mMonitoring) {
+                // read the length of the incoming message
+                int length = readLength(mMainSdbConnection);
+
+                if (length >= 0) {
+                    // read the incoming message
+                    processIncomingDeviceData(length);
+
+                    // flag the fact that we have build the list at least once
+                    mDeviceListInitialized = true;
+                }
+            }
+        } catch (AsynchronousCloseException ace) {
             Log.e("DeviceMonitor", "sdb closed by the other" );
-                       // this happens because of a call to Quit. We do nothing, and the loop will break.
-               } catch (TimeoutException ioe) {
-                       handleExpectioninMonitorLoop(ioe);
-               } catch (IOException ioe) {
-                       handleExpectioninMonitorLoop(ioe);
-               }
-               trySleep( 100 );
+            // this happens because of a call to Quit. We do nothing, and the loop will break.
+        } catch (TimeoutException ioe) {
+            handleExpectioninMonitorLoop(ioe);
+        } catch (IOException ioe) {
+            handleExpectioninMonitorLoop(ioe);
+        }
+        trySleep( 100 );
     }
 
-       /* (non-Javadoc)
-        * @see org.tizen.sdblib.daemon.AbstractServer#terminate()
-        */
-       @Override
+    /* (non-Javadoc)
+     * @see org.tizen.sdblib.daemon.AbstractServer#terminate()
+     */
+    @Override
     protected
     void
     terminate()
@@ -446,7 +439,5 @@ extends AbstractServer
         } catch (IOException e) {
             Log.e("sdb", "close main sdb connection failed:" + e);
         }
-
     }
-
 }
index ec8bbf5..54e694a 100755 (executable)
@@ -55,66 +55,65 @@ public class
 Command\r
 implements ICommand, ICommandWithArgument, ICommandWithArgument2\r
 {\r
-       /**\r
-        * Smart development bridge to execute command\r
-        */\r
-       protected final SmartDevelopmentBridge sdb;\r
+    /**\r
+     * Smart development bridge to execute command\r
+     */\r
+    protected final SmartDevelopmentBridge sdb;\r
 \r
-       /**\r
-        * Command string to execute\r
-        */\r
-       protected final String cmd;\r
-       \r
-       /**\r
-        * Arguments to be used to execute\r
-        */\r
-       protected final HashMap<Argument, Object> arguments = new HashMap<Argument, Object>();\r
-       \r
-       /**\r
-        * Constructor with sdb and command\r
-        \r
-        * @param sdb Smart development bridge\r
-        * @param cmd command to execute\r
-        */\r
-       public\r
-       Command(\r
-               final SmartDevelopmentBridge sdb,\r
-               final String cmd\r
-       )\r
-       {\r
-               notNull( sdb ); // Early fail\r
-               notNull( cmd ); // Early fail\r
-               \r
-               this.sdb = sdb;\r
-               this.cmd = cmd;\r
-       }\r
+    /**\r
+     * Command string to execute\r
+     */\r
+    protected final String cmd;\r
+    \r
+    /**\r
+     * Arguments to be used to execute\r
+     */\r
+    protected final HashMap<Argument, Object> arguments = new HashMap<Argument, Object>();\r
+    \r
+    /**\r
+     * Constructor with sdb and command\r
+     * \r
+     * @param sdb Smart development bridge\r
+     * @param cmd command to execute\r
+     */\r
+    public\r
+    Command(\r
+        final SmartDevelopmentBridge sdb,\r
+        final String cmd\r
+    )\r
+    {\r
+        notNull( sdb );    // Early fail\r
+        notNull( cmd );    // Early fail\r
+        \r
+        this.sdb = sdb;\r
+        this.cmd = cmd;\r
+    }\r
 \r
-       /* (non-Javadoc)\r
-        * @see org.tizen.sdblib.command.ICommand#in(org.tizen.sdblib.IDevice, org.tizen.sdblib.IShellOutputReceiver)\r
-        */\r
-       @Override\r
+    /* (non-Javadoc)\r
+     * @see org.tizen.sdblib.command.ICommand#in(org.tizen.sdblib.IDevice, org.tizen.sdblib.IShellOutputReceiver)\r
+     */\r
+    @Override\r
     public\r
     void\r
     in(\r
-       final IDevice device,\r
-       final IShellOutputReceiver receiver\r
+        final IDevice device,\r
+        final IShellOutputReceiver receiver\r
     )\r
     throws IOException\r
     {\r
-               notNull( receiver );\r
-               \r
+        notNull( receiver );\r
+        \r
         final SocketChannel channel = sdb.openChannel();\r
         try {\r
             channel.configureBlocking( getMaxTimeToRespond() <= 0 );\r
-               \r
-               sendRequest( device, channel );\r
-               \r
+            \r
+            sendRequest( device, channel );\r
+            \r
             byte[] data = new byte[16384];\r
             ByteBuffer buf = ByteBuffer.wrap(data);\r
             int timeToResponseCount = 0;\r
             final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();\r
             while (true) {\r
-\r
                 if (receiver != null && receiver.isCancelled()) {\r
                     Log.v("sdb", "execute: cancelled");\r
                     break;\r
@@ -123,7 +122,8 @@ implements ICommand, ICommandWithArgument, ICommandWithArgument2
                 int count = channel.read(buf);\r
                 if (count < 0) {\r
                     // we're at the end, we flush the output\r
-                    receiver.flush();\r
+                    if (receiver != null)\r
+                        receiver.flush();\r
                     Log.v("sdb", "execute '" + cmd + "' on '" + device + "' : EOF hit. Read: "\r
                             + count);\r
                     break;\r
@@ -145,15 +145,15 @@ implements ICommand, ICommandWithArgument, ICommandWithArgument2
 \r
                     // send data to receiver if present\r
                     if (receiver != null) {\r
-                       byteOut.write( buf.array(), buf.arrayOffset(), buf.position() );\r
-                       try\r
+                        byteOut.write( buf.array(), buf.arrayOffset(), buf.position() );\r
+                        try\r
                         {\r
-                               receiver.append( SdbHelper.bytes2string( byteOut.toByteArray() ) );\r
+                            receiver.append( SdbHelper.bytes2string( byteOut.toByteArray() ) );\r
                         } catch (Exception e)\r
                         {\r
-                               receiver.append( new String( byteOut.toByteArray() ) );\r
+                            receiver.append( new String( byteOut.toByteArray() ) );\r
                         }\r
-                       byteOut.reset();\r
+                        byteOut.reset();\r
                     }\r
                     buf.rewind();\r
                 }\r
@@ -161,20 +161,20 @@ implements ICommand, ICommandWithArgument, ICommandWithArgument2
         }\r
         finally\r
         {\r
-               tryClose( channel );\r
-               tryClose( receiver );\r
+            tryClose( channel );\r
+            tryClose( receiver );\r
             Log.v("sdb", "execute: returning");\r
         }\r
     }\r
 \r
-       /* (non-Javadoc)\r
-        * @see org.tizen.sdblib.command.ICommand#in(org.tizen.sdblib.IDevice)\r
-        */\r
-       @Override\r
+    /* (non-Javadoc)\r
+     * @see org.tizen.sdblib.command.ICommand#in(org.tizen.sdblib.IDevice)\r
+     */\r
+    @Override\r
     public\r
     void\r
     in(\r
-       final IDevice device\r
+        final IDevice device\r
     )\r
     throws IOException\r
     {\r
@@ -183,33 +183,33 @@ implements ICommand, ICommandWithArgument, ICommandWithArgument2
         final SocketChannel channel = sdb.openChannel();\r
         try\r
         {\r
-               sendRequest( device, channel );\r
+            sendRequest( device, channel );\r
         }\r
         finally\r
         {\r
-               tryClose( channel );\r
+            tryClose( channel );\r
             Log.v("sdb", "execute: returning");\r
         }\r
     }\r
-       \r
-       /**\r
-        * @param device device to send command\r
-        * @param channel connected channel\r
-        \r
-        * @throws IOException If resquest can't be sent\r
-        */\r
-       protected\r
-       void\r
-       sendRequest(\r
-               final IDevice device,\r
-               final SocketChannel channel\r
-       )\r
-       throws IOException\r
-       {\r
+    \r
+    /**\r
+     * @param device device to send command\r
+     * @param channel connected channel\r
+     * \r
+     * @throws IOException If resquest can't be sent\r
+     */\r
+    protected\r
+    void\r
+    sendRequest(\r
+        final IDevice device,\r
+        final SocketChannel channel\r
+    )\r
+    throws IOException\r
+    {\r
         // if the device is not -1, then we first tell sdb we're looking to\r
         // talk\r
         // to a specific device\r
-               SdbHelper.initializeDevice( channel, device.getSerialNumber() );\r
+        SdbHelper.initializeDevice( channel, device.getSerialNumber() );\r
 \r
         byte[] request = SdbHelper.formSdbRequest("shell:" + cmd); //$NON-NLS-1$\r
         SdbHelper.write(channel, request);\r
@@ -219,79 +219,79 @@ implements ICommand, ICommandWithArgument, ICommandWithArgument2
             Log.e("sdb", "sdb rejected shell command (" + cmd + "): " + resp.message);\r
             throw new SdbCommandRejectedException(resp.message);\r
         }\r
-       }\r
+    }\r
 \r
-       /* (non-Javadoc)\r
-        * @see org.tizen.sdblib.command.ICommandWithArgument2#and(org.tizen.sdblib.command.Argument, java.lang.Object)\r
-        */\r
-       @Override\r
+    /* (non-Javadoc)\r
+     * @see org.tizen.sdblib.command.ICommandWithArgument2#and(org.tizen.sdblib.command.Argument, java.lang.Object)\r
+     */\r
+    @Override\r
     public\r
     ICommandWithArgument2\r
     and(\r
-       final Argument key,\r
-       final Object value\r
+        final Argument key,\r
+        final Object value\r
     )\r
     {\r
-           return with( key, value );\r
+        return with( key, value );\r
     }\r
 \r
-       /* (non-Javadoc)\r
-        * @see org.tizen.sdblib.command.ICommandWithArgument#with(org.tizen.sdblib.command.Argument, java.lang.Object)\r
-        */\r
-       @Override\r
+    /* (non-Javadoc)\r
+     * @see org.tizen.sdblib.command.ICommandWithArgument#with(org.tizen.sdblib.command.Argument, java.lang.Object)\r
+     */\r
+    @Override\r
     public\r
     ICommandWithArgument2\r
     with(\r
-       final Argument key, Object value\r
+        final Argument key, Object value\r
     )\r
     {\r
-               notNull( key );\r
-               isFalse( this.arguments.containsKey( key ) );\r
-               \r
-           this.arguments.put( key, value );\r
-           return this;\r
+        notNull( key );\r
+        isFalse( this.arguments.containsKey( key ) );\r
+        \r
+        this.arguments.put( key, value );\r
+        return this;\r
     }\r
-       \r
-       /**\r
-        * Return max time to respond\r
-        \r
-        * @return max time to respond\r
-        \r
-        * @see Argument#MaxTimeToRespond\r
-        */\r
-       protected\r
-       int\r
-       getMaxTimeToRespond()\r
-       {\r
-               final Object obj = this.arguments.get( Argument.MaxTimeToRespond );\r
-               if ( !( obj instanceof Integer ) )\r
-               {\r
-                       return -1;\r
-               }\r
+    \r
+    /**\r
+     * Return max time to respond\r
+     * \r
+     * @return max time to respond\r
+     * \r
+     * @see Argument#MaxTimeToRespond\r
+     */\r
+    protected\r
+    int\r
+    getMaxTimeToRespond()\r
+    {\r
+        final Object obj = this.arguments.get( Argument.MaxTimeToRespond );\r
+        if ( !( obj instanceof Integer ) )\r
+        {\r
+            return -1;\r
+        }\r
 \r
-               return (Integer) obj;\r
-       }\r
+        return (Integer) obj;\r
+    }\r
 \r
-       /* (non-Javadoc)\r
-        * @see org.tizen.sdblib.command.ICommand#in(org.tizen.sdblib.IDevice, org.tizen.sdblib.command.Matcher)\r
-        */\r
-       @Override\r
+    /* (non-Javadoc)\r
+     * @see org.tizen.sdblib.command.ICommand#in(org.tizen.sdblib.IDevice, org.tizen.sdblib.command.Matcher)\r
+     */\r
+    @Override\r
     public <T>\r
     T\r
     in(\r
-       final IDevice device,\r
-       final Matcher<T> matcher\r
+        final IDevice device,\r
+        final Matcher<T> matcher\r
     )\r
     {\r
-               try\r
+        try\r
         {\r
-               in( device, (IShellOutputReceiver) matcher );\r
+            in( device, (IShellOutputReceiver) matcher );\r
         }\r
-               catch ( final IOException e )\r
+        catch ( final IOException e )\r
         {\r
             Log.e( "command", "execute " + cmd + " failed:" + e );\r
         }\r
-               return matcher.getResult();\r
+        return matcher.getResult();\r
     }\r
-       \r
+    \r
 }\r