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 public abstract class AbstractServer
implements Server
{
15 static final Logger LOGGER
= Logger
.getLogger(AbstractServer
.class.getName());
17 private final ServerConfigurationHandle serverConfigurationHandle
;
18 private final String serverInfo
;
19 private final File externalResourceDir
;
20 private final String address
;
21 private final DevAppServerImpl devAppServer
;
22 private final List
<InstanceHolder
> instancesHolders
;
24 private LocalServerEnvironment localServerEnvironment
;
26 protected AbstractServer(ServerConfigurationHandle serverConfigurationHandle
,
27 String serverInfo
, File externalResourceDir
, String address
,
28 DevAppServerImpl devAppServer
,
29 List
<InstanceHolder
> instances
) {
30 this.serverConfigurationHandle
= serverConfigurationHandle
;
31 this.serverInfo
= serverInfo
;
32 this.externalResourceDir
= externalResourceDir
;
33 this.address
= address
;
34 this.devAppServer
= devAppServer
;
35 this.instancesHolders
= new CopyOnWriteArrayList
<InstanceHolder
>(instances
);
39 public String
getServerName() {
40 return serverConfigurationHandle
.getModule().getServerName();
43 protected List
<InstanceHolder
> getInstanceHolders() {
44 return instancesHolders
;
48 public LocalServerEnvironment
getLocalServerEnvironment() {
49 return localServerEnvironment
;
53 public final void configure(Map
<String
, Object
> containerConfigProperties
) throws Exception
{
54 if (localServerEnvironment
== null) {
55 localServerEnvironment
= doConfigure(serverConfigurationHandle
, serverInfo
,
56 externalResourceDir
, address
, containerConfigProperties
, devAppServer
);
61 public void createConnection() throws Exception
{
62 for (InstanceHolder instanceHolder
: instancesHolders
) {
63 ContainerService containerService
= instanceHolder
.getContainerService();
64 containerService
.createConnection();
69 public void startup() throws Exception
{
70 for (InstanceHolder instanceHolder
: instancesHolders
) {
71 ContainerService containerService
= instanceHolder
.getContainerService();
72 containerService
.startup();
73 String listeningHostAndPort
= getHostAndPort(instanceHolder
);
74 if (instanceHolder
.isMainInstance()) {
75 LOGGER
.info(String
.format("Server %s is running at http://%s/", getServerName(),
76 listeningHostAndPort
));
78 LOGGER
.info(String
.format("Server %s instance %s is running at http://%s/",
79 getServerName(), instanceHolder
.getInstance(), listeningHostAndPort
));
81 LOGGER
.info("The admin console is running at http://" + listeningHostAndPort
+ "/_ah/admin");
86 public String
getHostAndPort(int instance
) {
87 InstanceHolder instanceHolder
= getInstanceHolder(instance
);
88 if (instanceHolder
== null) {
91 return getHostAndPort(instanceHolder
);
95 private String
getHostAndPort(InstanceHolder instanceHolder
) {
96 ContainerService containerService
= instanceHolder
.getContainerService();
97 String prettyAddress
= containerService
.getAddress();
98 if (prettyAddress
.equals("0.0.0.0") || prettyAddress
.equals("127.0.0.1")) {
99 prettyAddress
= "localhost";
101 String listeningHostAndPort
= prettyAddress
+ ":" + containerService
.getPort();
102 return listeningHostAndPort
;
106 public InstanceHolder
getInstanceHolder(int instance
) {
107 if (instance
< LocalEnvironment
.MAIN_INSTANCE
|| instance
+ 1 > instancesHolders
.size()) {
110 return instancesHolders
.get(instance
+ 1);
115 public int getInstanceCount() {
116 return instancesHolders
.size();
120 public void shutdown() throws Exception
{
121 for (InstanceHolder instanceHolder
: instancesHolders
) {
122 instanceHolder
.getContainerService().shutdown();
123 if (instanceHolder
.isMainInstance()) {
124 LOGGER
.info("Shutting down server " + getServerName());
126 LOGGER
.info("Shutting down server " + getServerName() + " instance "
127 + instanceHolder
.getInstance());
133 public ContainerService
getMainContainer() {
134 return instancesHolders
.get(0).getContainerService();
138 public int getFreeInstance() {
139 throw new UnsupportedOperationException();
143 * Configures the containers for a {@link Server} and returns the
144 * {@link LocalServerEnvironment} for the main container.
146 protected abstract LocalServerEnvironment
doConfigure(
147 ServerConfigurationHandle serverConfigurationHandle
, String serverInfo
,
148 File externalResourceDir
, String address
, Map
<String
, Object
> containerConfigProperties
,
149 DevAppServerImpl devAppServer
) throws Exception
;
152 * Holder for instance related state.
154 static class InstanceHolder
{
155 private final ContainerService containerService
;
156 private final int instance
;
159 * Construct an instance holder.
160 * @param containerService for the instance.
161 * @param instance nonnegative instance number or
162 * {link {@link LocalEnvironment#MAIN_INSTANCE}.
164 InstanceHolder(ContainerService containerService
, int instance
) {
165 this.containerService
= containerService
;
166 this.instance
= instance
;
169 final ContainerService
getContainerService() {
170 return containerService
;
173 final int getInstance() {
177 final boolean isMainInstance() {
182 public String
toString() {
183 return "InstanceHolder: containerservice=" + containerService
+ " instance=" + instance
;