Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / development / ContainerUtils.java
blob851fe3f6e7d586487c34408b58e9958eed5f7f35
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.tools.development;
5 import com.google.appengine.tools.info.SdkInfo;
7 import java.util.Iterator;
9 /**
10 * helper to load a {@link ContainerService} instance
13 public class ContainerUtils {
14 /**
15 * Load a {@link ContainerService} instance based on the implementation
16 * located by the {@code ServiceLoader}.
18 * When more than one implementations are found, an
19 * <code>IllegalArgumentException</code> will be thrown. In other words,
20 * only one {@link ContainerService} implementation should be packaged and
21 * deployed.
23 * To ease debugging/testing (e.g. from IDE), an internal system property is
24 * defined to explicitly specify a required container service provider, such
25 * as -Ddevappserver.container=OpenGSEContainerService.
27 * Classpath resource(s) that match
28 * META-INF/services/com.google.appengine.tools.development.ContainerService
29 * are used to resolve the ContainerService implementations.
31 * If using an IDE, create a package/directory called META-INF/services/ and
32 * add a text file with the name
33 * "com.google.appengine.tools.development.ContainerService" containing one
34 * line - the implementation class (some IDEs have a resource pattern that
35 * is used to determine which files are added to the runtime classpath. If
36 * your implementation class isn't being recognized, adjusting this pattern
37 * to include the text file may help.
40 * @return the deployed {@link ContainerService} instance.
41 * @throws IllegalArgumentException if {@code ServiceLoader} fails to find
42 * any {@link ContainerService} implementation or more than one
43 * {@link ContainerService} implementations are available.
45 public static ContainerService loadContainer() {
46 @SuppressWarnings({"unchecked", "sunapi"})
47 Iterator<ContainerService> containerLoader =
48 sun.misc.Service.providers(ContainerService.class,
49 DevAppServerImpl.class.getClassLoader());
50 String containerClazz = System.getProperty("devappserver.container");
51 ContainerService result = null;
52 while (containerLoader.hasNext()) {
53 ContainerService container = containerLoader.next();
54 if (containerClazz == null || containerClazz.length() == 0) {
55 if (result == null) {
56 result = container;
57 } else {
58 System.err.println("Warning: Found more than one servlet container provider: "
59 + result.getClass() + ", " + container.getClass()
60 + ". And the first one will be used!");
61 break;
63 } else {
64 if (container.getClass().getName().endsWith(containerClazz)) {
65 result = container;
66 break;
70 if (result == null) {
71 throw new IllegalArgumentException("Cannot load any servlet container.");
73 return result;
76 /**
77 * @return the server info string with the dev-appserver version
79 public static String getServerInfo() {
80 return "Google App Engine Development/" + SdkInfo.getLocalVersion().getRelease();