1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / tools / development / AbstractModule.java
blobddd267e81a4f49453c4fcc881cb6f1ea4b10129a
1 package com.google.appengine.tools.development;
3 import com.google.appengine.tools.development.ApplicationConfigurationManager.ModuleConfigurationHandle;
4 import com.google.apphosting.api.ApiProxy;
6 import java.io.File;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.concurrent.CopyOnWriteArrayList;
10 import java.util.logging.Logger;
12 /**
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);
42 @Override
43 public String getModuleName() {
44 return moduleConfigurationHandle.getModule().getModuleName();
47 protected List<I> getInstanceHolders() {
48 return instanceHolders;
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(moduleConfigurationHandle, serverInfo,
60 externalResourceDir, address, containerConfigProperties, devAppServer);
64 @Override
65 public void createConnection() throws Exception {
66 for (I instanceHolder : instanceHolders) {
67 instanceHolder.createConnection();
71 @Override
72 public void setApiProxyDelegate(ApiProxy.Delegate<?> apiProxyDelegate) {
73 for (I instanceHolder : instanceHolders) {
74 instanceHolder.getContainerService().setApiProxyDelegate(apiProxyDelegate);
78 @Override
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));
86 } else {
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");
94 @Override
95 public String getHostAndPort(int instance) {
96 I instanceHolder = getInstanceHolder(instance);
97 if (instanceHolder == null) {
98 return null;
99 } else {
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;
114 @Override
115 public I getInstanceHolder(int instance) {
116 if (instance < LocalEnvironment.MAIN_INSTANCE || instance + 1 > instanceHolders.size()) {
117 return null;
118 } else {
119 return instanceHolders.get(instance + 1);
123 @Override
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());
129 } else {
130 LOGGER.info("Shutting down module instance " + getModuleName() + " instance "
131 + instanceHolder.getInstance());
136 @Override
137 public ContainerService getMainContainer() {
138 return instanceHolders.get(0).getContainerService();
141 @Override
142 public I getAndReserveAvailableInstanceHolder() {
143 throw new UnsupportedOperationException();
146 @Override
147 public void startServing() throws Exception {
148 throw new UnsupportedOperationException();
151 @Override
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;