Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / development / DevAppServerPortPropertyHelper.java
blobd3e09d414d3fc5b006ac33c6f1a5568198f31d15
1 package com.google.appengine.tools.development;
3 import java.util.Map;
5 /**
6 * Utility methods for managing {@link DevAppServer} port related service
7 * properties.
9 */
10 public class DevAppServerPortPropertyHelper {
11 public static final int DEFAULT_PORT = 0;
12 public static final String SYSTEM_PROPERTY_STATIC_SERVER_PORT_NUM_PREFIX =
13 "com.google.appengine.devappserver_server.";
15 private DevAppServerPortPropertyHelper() {
18 /**
19 * Adds a service property to configure {@link DevAppServer} to use the
20 * the specified port for the named server's main instance to the passed in
21 * service properties. To read the added service property use
22 * {@link #getPort(String, Map)}.
24 public static int setPort(String serverName, int port, Map<String, String> serviceProperties) {
25 return setPort(serverName, LocalEnvironment.MAIN_INSTANCE, port, serviceProperties);
28 /**
29 * Adds a service property to configure {@link DevAppServer} to use the
30 * the specified port for the specified server instance to the passed in
31 * service properties. To read the added service property use
32 * {@link #getPort(String, Map)}.
33 * @param serverName the server name.
34 * @param instance the server instance or {link {@link LocalEnvironment#MAIN_INSTANCE} for the
35 * main instance.
36 * @param port the port for the specified server instance.
37 * @param serviceProperties the service properties.
39 public static int setPort(String serverName, int instance, int port, Map<String,
40 String> serviceProperties) {
41 String propertyName = getInstancePropertyName(serverName, instance);
42 serviceProperties.put(propertyName, Integer.toString(port));
43 return port;
46 /**
47 * Returns the configured port for the named server's main instance from the
48 * the provided service properties or {@link #DEFAULT_PORT} if no port has
49 * been configured. To add the service property to configure the port use
50 * {@link #setPort(String, int, Map)}.
52 public static int getPort(String serverName, Map<String, String> serviceProperties) {
53 return getPort(serverName, LocalEnvironment.MAIN_INSTANCE, serviceProperties);
56 /**
57 * Returns the configured port for the specified server instance from the
58 * the provided service properties or {@link #DEFAULT_PORT} if no port has
59 * been configured. To add the service property to configure the port use
60 * {@link #setPort(String, int, Map)}.
62 * @param serverName the server name.
63 * @param instance the server instance or {link {@link LocalEnvironment#MAIN_INSTANCE} for the
64 * main instance.
65 * @param serviceProperties the service properties.
67 public static int getPort(String serverName, int instance,
68 Map<String, String> serviceProperties) {
69 String propertyName = getInstancePropertyName(serverName, instance);
70 if (serviceProperties.containsKey(propertyName)) {
71 return Integer.parseInt(serviceProperties.get(propertyName));
72 } else {
73 return 0;
77 private static String getInstancePropertyName(String serverName, int instance) {
78 String instanceSpec = instance == LocalEnvironment.MAIN_INSTANCE ? "" : "." + instance;
79 return SYSTEM_PROPERTY_STATIC_SERVER_PORT_NUM_PREFIX + serverName + instanceSpec + ".port";