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