Created
May 17, 2010 08:05
-
-
Save shin1ogawa/403520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import org.apache.commons.httpclient.HttpException; | |
import org.apache.commons.httpclient.URI; | |
import org.apache.commons.httpclient.URIException; | |
import appengine.util.MakeSyncCallServletDelegate; | |
import com.google.appengine.api.datastore.DatastoreService; | |
import com.google.appengine.api.datastore.DatastoreServiceFactory; | |
import com.google.appengine.api.datastore.Entity; | |
import com.google.appengine.api.datastore.FetchOptions; | |
import com.google.appengine.api.datastore.Key; | |
import com.google.appengine.api.datastore.Query; | |
import com.google.appengine.api.datastore.Query.FilterOperator; | |
import com.google.appengine.repackaged.com.google.common.collect.Maps; | |
import com.google.appengine.tools.development.LocalServerEnvironment; | |
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; | |
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | |
import com.google.apphosting.api.ApiProxy; | |
import com.google.apphosting.api.ApiProxy.Environment; | |
/** | |
* Productionで動作確認するためのクラス。 | |
* @require appengine-api-stubs:appengine-api-stubs:1.3.+ | |
* @require appengine-testing:appengine-testing:1.3.+ | |
* @require commons-codec:commons-codec:1.4 | |
* @require commons-httpclient:commons-httpclient:3.1 | |
* @require commons-io:commons-io:1.4 | |
* @require commons-lang:commons-lang:2.4 | |
* @require commons-logging:commons-logging:1.1.1 | |
* @require gaejtools-util:gaejtools-util:1.3.0-SNAPSHOT | |
* @author shin1ogawa | |
*/ | |
public class RemoteApiHelper { | |
LocalServiceTestHelper testHelper; | |
static MakeSyncCallServletDelegate delegate; | |
static String appId = "default"; | |
static String email; | |
static String password; | |
public void startMakeSyncCallServletDelegate(String... deleteKind) | |
throws NullPointerException, HttpException, IOException { | |
if (delegate == null) { | |
createDelegate(); | |
} | |
ApiProxy.setDelegate(delegate); | |
dropKinds(deleteKind); | |
} | |
public void dropKinds(String... deleteKind) { | |
if (deleteKind != null) { | |
final int MAX = 200; | |
final int MAX_ = MAX + 1; | |
final int _MAX = MAX - 1; | |
DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); | |
for (String kind : deleteKind) { | |
Key key = null; | |
do { | |
Query query = new Query(kind).setKeysOnly(); | |
FetchOptions options = FetchOptions.Builder.withLimit(MAX_) | |
.prefetchSize(MAX_); | |
if (key != null) { | |
query.addFilter("__key__", FilterOperator.GREATER_THAN_OR_EQUAL, | |
key); | |
} | |
List<Entity> list = ds.prepare(query).asList(options); | |
if (list.isEmpty()) { | |
break; | |
} | |
List<Key> keyList = new ArrayList<Key>(list.size()); | |
for (Entity e : list) { | |
keyList.add(e.getKey()); | |
} | |
if (keyList.size() > MAX) { | |
ds.delete(keyList.subList(0, _MAX)); | |
key = keyList.get(MAX); | |
} else { | |
ds.delete(keyList); | |
key = null; | |
} | |
} while (key != null); | |
} | |
} | |
} | |
private void createDelegate() throws IOException, URIException, HttpException { | |
if (appId == null || appId.length() == 0 || email == null | |
|| email.length() == 0 || password == null || password.length() == 0) { | |
BufferedReader reader = new BufferedReader(new InputStreamReader( | |
System.in)); | |
System.out.println("enter your appId:"); | |
appId = reader.readLine(); | |
System.out.println("enter your email:"); | |
email = reader.readLine(); | |
System.out.println("enter your password:"); | |
password = reader.readLine(); | |
} | |
URI uri = new URI("http://" + appId + ".appspot.com/sys/makesynccall", true); | |
delegate = new MakeSyncCallServletDelegate(uri); | |
delegate.auth(email, password, "http://" + appId + ".appspot.com/", | |
"sys/makesynccall"); | |
} | |
public void endMakeSyncCallServletDelegate() { | |
if (delegate != null) { | |
ApiProxy.setDelegate(delegate.getOriginal()); | |
} | |
} | |
public static RemoteApiHelper setUp() throws NullPointerException, | |
HttpException, IOException { | |
return setUp(null); | |
} | |
public static RemoteApiHelper setUp(final File testDir) | |
throws NullPointerException, HttpException, IOException { | |
LocalDatastoreServiceTestConfig dsConfig = new LocalDatastoreServiceTestConfig(); | |
dsConfig.setNoStorage(true); | |
dsConfig.setNoIndexAutoGen(false); | |
RemoteApiHelper remoteApiHelper = new RemoteApiHelper(); | |
remoteApiHelper.testHelper = new LocalServiceTestHelper(dsConfig) { | |
@Override | |
protected LocalServerEnvironment newLocalServerEnvironment() { | |
final LocalServerEnvironment env = super.newLocalServerEnvironment(); | |
return new LocalServerEnvironment() { | |
public void waitForServerToStart() throws InterruptedException { | |
env.waitForServerToStart(); | |
} | |
public int getPort() { | |
return env.getPort(); | |
} | |
public File getAppDir() { | |
return testDir == null ? new File("target") : testDir; | |
} | |
public String getAddress() { | |
return env.getAddress(); | |
} | |
}; | |
} | |
@Override | |
protected Environment newEnvironment() { | |
return new ApiProxy.Environment() { | |
public String getAppId() { | |
return appId; | |
} | |
public Map<String, Object> getAttributes() { | |
return Maps.newHashMap(); | |
} | |
public String getAuthDomain() { | |
return null; | |
} | |
public String getEmail() { | |
return null; | |
} | |
public String getRequestNamespace() { | |
return ""; | |
} | |
public String getVersionId() { | |
return null; | |
} | |
public boolean isAdmin() { | |
return false; | |
} | |
public boolean isLoggedIn() { | |
return false; | |
} | |
}; | |
} | |
}; | |
remoteApiHelper.testHelper.setUp(); | |
return remoteApiHelper; | |
} | |
public static void tearDown(RemoteApiHelper helper) { | |
helper.testHelper.tearDown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment