1 /******************************************************************************* |
|
2 * Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Symbian Foundation - initial contribution. |
|
11 * Contributors: |
|
12 * Description: |
|
13 * Overview: |
|
14 * Details: |
|
15 * Platforms/Drives/Compatibility: |
|
16 * Assumptions/Requirement/Pre-requisites: |
|
17 * Failures and causes: |
|
18 *******************************************************************************/ |
|
19 package org.symbian.tools.wrttools.debug.internal.launch; |
|
20 |
|
21 import java.io.IOException; |
|
22 import java.net.ServerSocket; |
|
23 |
|
24 import org.symbian.tools.wrttools.debug.internal.Activator; |
|
25 |
|
26 public abstract class PortPolicy { |
|
27 // private static class ReuseSamePort extends PortPolicy { |
|
28 // private int port = -1; |
|
29 // |
|
30 // @Override |
|
31 // protected int getPort() { |
|
32 // if (port < 0) { |
|
33 // port = getOpenPort(); |
|
34 // } |
|
35 // return port; |
|
36 // } |
|
37 // } |
|
38 |
|
39 private static class NewPortEachTime extends PortPolicy { |
|
40 @Override |
|
41 protected int getPort() { |
|
42 return getOpenPort(); |
|
43 } |
|
44 } |
|
45 |
|
46 public static final PortPolicy INSTANCE; |
|
47 static { |
|
48 // if (CoreUtil.isMac()) { |
|
49 INSTANCE = new NewPortEachTime(); |
|
50 // } else { |
|
51 // INSTANCE = new ReuseSamePort(); |
|
52 // } |
|
53 } |
|
54 |
|
55 public static synchronized int getPortNumber() { |
|
56 return INSTANCE.getPort(); |
|
57 } |
|
58 |
|
59 public int getOpenPort() { |
|
60 try { |
|
61 final ServerSocket socket = new ServerSocket(0); |
|
62 int port = socket.getLocalPort(); |
|
63 socket.close(); |
|
64 return port; |
|
65 } catch (IOException e) { |
|
66 Activator.log(e); |
|
67 return 7222; |
|
68 } |
|
69 } |
|
70 |
|
71 protected abstract int getPort(); |
|
72 } |
|