1 package com
.google
.appengine
.tools
.development
;
3 import com
.google
.appengine
.tools
.development
.ApplicationConfigurationManager
.ModuleConfigurationHandle
;
4 import com
.google
.apphosting
.api
.ApiProxy
;
9 import java
.util
.concurrent
.CopyOnWriteArrayList
;
10 import java
.util
.logging
.Logger
;
13 * Abstract super class for {@link Module} implementations.
15 * @param <I> An {@link InstanceHolder} type which is available to a Module implementation
16 * but not reflected in the {@link Module} interface.
18 public abstract class AbstractModule
<I
extends InstanceHolder
> implements Module
{
19 static final Logger LOGGER
= Logger
.getLogger(AbstractModule
.class.getName());
21 private final ModuleConfigurationHandle moduleConfigurationHandle
;
22 private final String serverInfo
;
23 private final File externalResourceDir
;
24 private final String address
;
25 private final DevAppServer devAppServer
;
26 private final List
<I
> instanceHolders
;
27 private ApiProxy
.Delegate
<?
> apiProxyDelegate
;
29 private LocalServerEnvironment localServerEnvironment
;
31 protected AbstractModule(ModuleConfigurationHandle moduleConfigurationHandle
,
32 String serverInfo
, File externalResourceDir
, String address
,
33 DevAppServer devAppServer
, List
<I
> instanceHolders
) {
34 this.moduleConfigurationHandle
= moduleConfigurationHandle
;
35 this.serverInfo
= serverInfo
;
36 this.externalResourceDir
= externalResourceDir
;
37 this.address
= address
;
38 this.devAppServer
= devAppServer
;
39 this.instanceHolders
= new CopyOnWriteArrayList
<I
>(instanceHolders
);
43 public String
getModuleName() {
44 return moduleConfigurationHandle
.getModule().getModuleName();
47 protected List
<I
> getInstanceHolders() {
48 return instanceHolders
;
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(moduleConfigurationHandle
, serverInfo
,
60 externalResourceDir
, address
, containerConfigProperties
, devAppServer
);
65 public void createConnection() throws Exception
{
66 for (I instanceHolder
: instanceHolders
) {
67 instanceHolder
.createConnection();
72 public void setApiProxyDelegate(ApiProxy
.Delegate
<?
> apiProxyDelegate
) {
73 for (I instanceHolder
: instanceHolders
) {
74 instanceHolder
.getContainerService().setApiProxyDelegate(apiProxyDelegate
);
79 public void startup() throws Exception
{
80 for (I instanceHolder
: instanceHolders
) {
81 instanceHolder
.startUp();
82 String listeningHostAndPort
= getHostAndPort(instanceHolder
);
83 if (instanceHolder
.isMainInstance()) {
84 LOGGER
.info(String
.format("Module instance %s is running at http://%s/", getModuleName(),
85 listeningHostAndPort
));
87 LOGGER
.info(String
.format("Module instance %s instance %s is running at http://%s/",
88 getModuleName(), instanceHolder
.getInstance(), listeningHostAndPort
));
90 LOGGER
.info("The admin console is running at http://" + listeningHostAndPort
+ "/_ah/admin");
95 public String
getHostAndPort(int instance
) {
96 I instanceHolder
= getInstanceHolder(instance
);
97 if (instanceHolder
== null) {
100 return getHostAndPort(instanceHolder
);
104 private String
getHostAndPort(I instanceHolder
) {
105 ContainerService containerService
= instanceHolder
.getContainerService();
106 String prettyAddress
= containerService
.getAddress();
107 if (prettyAddress
.equals("0.0.0.0") || prettyAddress
.equals("127.0.0.1")) {
108 prettyAddress
= "localhost";
110 String listeningHostAndPort
= prettyAddress
+ ":" + containerService
.getPort();
111 return listeningHostAndPort
;
115 public I
getInstanceHolder(int instance
) {
116 if (instance
< LocalEnvironment
.MAIN_INSTANCE
|| instance
+ 1 > instanceHolders
.size()) {
119 return instanceHolders
.get(instance
+ 1);
124 public void shutdown() throws Exception
{
125 for (I instanceHolder
: instanceHolders
) {
126 instanceHolder
.getContainerService().shutdown();
127 if (instanceHolder
.isMainInstance()) {
128 LOGGER
.info("Shutting down module instance " + getModuleName());
130 LOGGER
.info("Shutting down module instance " + getModuleName() + " instance "
131 + instanceHolder
.getInstance());
137 public ContainerService
getMainContainer() {
138 return instanceHolders
.get(0).getContainerService();
142 public I
getAndReserveAvailableInstanceHolder() {
143 throw new UnsupportedOperationException();
147 public void startServing() throws Exception
{
148 throw new UnsupportedOperationException();
152 public void stopServing() throws Exception
{
153 throw new UnsupportedOperationException();
157 * Configures the containers for a {@link Module} and returns the
158 * {@link LocalServerEnvironment} for the main container.
160 protected abstract LocalServerEnvironment
doConfigure(
161 ModuleConfigurationHandle moduleConfigurationHandle
, String serverInfo
,
162 File externalResourceDir
, String address
, Map
<String
, Object
> containerConfigProperties
,
163 DevAppServer devAppServer
) throws Exception
;