1 package com
.google
.appengine
.tools
.development
;
3 import com
.google
.appengine
.tools
.development
.ApplicationConfigurationManager
.ServerConfigurationHandle
;
8 import java
.util
.concurrent
.CopyOnWriteArrayList
;
9 import java
.util
.logging
.Logger
;
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
,
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
);
43 public String
getServerName() {
44 return serverConfigurationHandle
.getModule().getServerName();
47 protected List
<I
> getInstanceHolders() {
48 return instancesHolders
;
52 public LocalServerEnvironment
getLocalServerEnvironment() {
53 return localServerEnvironment
;
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
);
65 public void createConnection() throws Exception
{
66 for (I instanceHolder
: instancesHolders
) {
67 instanceHolder
.createConnection();
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
));
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");
88 public String
getHostAndPort(int instance
) {
89 I instanceHolder
= getInstanceHolder(instance
);
90 if (instanceHolder
== null) {
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
;
108 public I
getInstanceHolder(int instance
) {
109 if (instance
< LocalEnvironment
.MAIN_INSTANCE
|| instance
+ 1 > instancesHolders
.size()) {
112 return instancesHolders
.get(instance
+ 1);
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());
123 LOGGER
.info("Shutting down server " + getServerName() + " instance "
124 + instanceHolder
.getInstance());
130 public ContainerService
getMainContainer() {
131 return instancesHolders
.get(0).getContainerService();
135 public I
getAndReserveAvailableInstanceHolder() {
136 throw new UnsupportedOperationException();
140 public void startServing() throws Exception
{
141 throw new UnsupportedOperationException();
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
;