Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / development / AbstractServer.java
blobe2d80dfe4aa168d410109e755c87eebba42998d6
1 package com.google.appengine.tools.development;
3 import com.google.appengine.tools.development.ApplicationConfigurationManager.ServerConfigurationHandle;
5 import java.io.File;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.concurrent.CopyOnWriteArrayList;
9 import java.util.logging.Logger;
11 /**
12 * Abstract super class for {@link Server} implementations.
14 * @param <I> An {@link InstanceHolder} type with needed server type specific
15 * state which is available to the server's implementation but not reflected in the
16 * {@link Server} interface.
18 public abstract class AbstractServer<I extends InstanceHolder> implements Server {
19 static final Logger LOGGER = Logger.getLogger(AbstractServer.class.getName());
21 private final ServerConfigurationHandle serverConfigurationHandle;
22 private final String serverInfo;
23 private final File externalResourceDir;
24 private final String address;
25 private final DevAppServerImpl devAppServer;
26 private final List<I> instancesHolders;
28 private LocalServerEnvironment localServerEnvironment;
30 protected AbstractServer(ServerConfigurationHandle serverConfigurationHandle,
31 String serverInfo, File externalResourceDir, String address,
32 DevAppServerImpl devAppServer,
33 List<I> instances) {
34 this.serverConfigurationHandle = serverConfigurationHandle;
35 this.serverInfo = serverInfo;
36 this.externalResourceDir = externalResourceDir;
37 this.address = address;
38 this.devAppServer = devAppServer;
39 this.instancesHolders = new CopyOnWriteArrayList<I>(instances);
42 @Override
43 public String getServerName() {
44 return serverConfigurationHandle.getModule().getServerName();
47 protected List<I> getInstanceHolders() {
48 return instancesHolders;
51 @Override
52 public LocalServerEnvironment getLocalServerEnvironment() {
53 return localServerEnvironment;
56 @Override
57 public final void configure(Map<String, Object> containerConfigProperties) throws Exception {
58 if (localServerEnvironment == null) {
59 localServerEnvironment = doConfigure(serverConfigurationHandle, serverInfo,
60 externalResourceDir, address, containerConfigProperties, devAppServer);
64 @Override
65 public void createConnection() throws Exception {
66 for (I instanceHolder : instancesHolders) {
67 instanceHolder.createConnection();
71 @Override
72 public void startup() throws Exception {
73 for (I instanceHolder : instancesHolders) {
74 instanceHolder.startUp();
75 String listeningHostAndPort = getHostAndPort(instanceHolder);
76 if (instanceHolder.isMainInstance()) {
77 LOGGER.info(String.format("Server %s is running at http://%s/", getServerName(),
78 listeningHostAndPort));
79 } else {
80 LOGGER.info(String.format("Server %s instance %s is running at http://%s/",
81 getServerName(), instanceHolder.getInstance(), listeningHostAndPort));
83 LOGGER.info("The admin console is running at http://" + listeningHostAndPort + "/_ah/admin");
87 @Override
88 public String getHostAndPort(int instance) {
89 I instanceHolder = getInstanceHolder(instance);
90 if (instanceHolder == null) {
91 return null;
92 } else {
93 return getHostAndPort(instanceHolder);
97 private String getHostAndPort(I instanceHolder) {
98 ContainerService containerService = instanceHolder.getContainerService();
99 String prettyAddress = containerService.getAddress();
100 if (prettyAddress.equals("0.0.0.0") || prettyAddress.equals("127.0.0.1")) {
101 prettyAddress = "localhost";
103 String listeningHostAndPort = prettyAddress + ":" + containerService.getPort();
104 return listeningHostAndPort;
107 @Override
108 public I getInstanceHolder(int instance) {
109 if (instance < LocalEnvironment.MAIN_INSTANCE || instance + 1 > instancesHolders.size()) {
110 return null;
111 } else {
112 return instancesHolders.get(instance + 1);
116 @Override
117 public void shutdown() throws Exception {
118 for (I instanceHolder : instancesHolders) {
119 instanceHolder.getContainerService().shutdown();
120 if (instanceHolder.isMainInstance()) {
121 LOGGER.info("Shutting down server " + getServerName());
122 } else {
123 LOGGER.info("Shutting down server " + getServerName() + " instance "
124 + instanceHolder.getInstance());
129 @Override
130 public ContainerService getMainContainer() {
131 return instancesHolders.get(0).getContainerService();
134 @Override
135 public I getAndReserveAvailableInstanceHolder() {
136 throw new UnsupportedOperationException();
139 @Override
140 public void startServing() throws Exception {
141 throw new UnsupportedOperationException();
144 @Override
145 public void stopServing() throws Exception {
146 throw new UnsupportedOperationException();
150 * Configures the containers for a {@link Server} and returns the
151 * {@link LocalServerEnvironment} for the main container.
153 protected abstract LocalServerEnvironment doConfigure(
154 ServerConfigurationHandle serverConfigurationHandle, String serverInfo,
155 File externalResourceDir, String address, Map<String, Object> containerConfigProperties,
156 DevAppServerImpl devAppServer) throws Exception;