sysmodelmgr/com.symbian.pde.test.utils/src/com/symbian/pde/test/utils/PDETestPortLocator.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 package com.symbian.pde.test.utils;
       
    16 
       
    17 import java.io.File;
       
    18 import java.io.FileOutputStream;
       
    19 import java.io.IOException;
       
    20 import java.io.OutputStream;
       
    21 import java.net.ServerSocket;
       
    22 
       
    23 public class PDETestPortLocator {
       
    24 
       
    25     public static void main(String[] args) {
       
    26         new PDETestPortLocator().savePortToFile();
       
    27     }
       
    28 
       
    29     public void savePortToFile() {
       
    30         int port = locatePDETestPortNumber();
       
    31         File propsFile = new File("pde_test_port.properties");
       
    32         System.out.println("PDE Test port: " + port);
       
    33         OutputStream os = null;
       
    34         try {
       
    35             os = new FileOutputStream(propsFile);
       
    36             os.write(new String("pde.test.port=" + port).getBytes());
       
    37             os.flush();
       
    38             System.out.println("PDE Test port saved to file " + propsFile.getAbsolutePath());
       
    39         } catch (IOException ioe) {
       
    40             ioe.printStackTrace();
       
    41         } finally {
       
    42             if (os != null) {
       
    43                 try {
       
    44                     os.close();
       
    45                 } catch (IOException ioe) {
       
    46                     // ignore
       
    47                 }
       
    48             }
       
    49             os = null;
       
    50         }
       
    51     }
       
    52 
       
    53     private int locatePDETestPortNumber() {
       
    54         ServerSocket socket = null;
       
    55         try {
       
    56             socket = new ServerSocket(0);
       
    57             return socket.getLocalPort();
       
    58         } catch (IOException e) {
       
    59             // ignore
       
    60         } finally {
       
    61             if (socket != null) {
       
    62                 try {
       
    63                     socket.close();
       
    64                 } catch (IOException e) {
       
    65                     // ignore
       
    66                 }
       
    67             }
       
    68         }
       
    69         return -1;
       
    70     }
       
    71 }