Created
June 19, 2011 04:41
-
-
Save takawitter/1033764 to your computer and use it in GitHub Desktop.
gaeasync twitter4j implementation sample
This file contains 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
class GAEAsyncTwitterImpl extends TwitterBaseImpl | |
implements Twitter { | |
public QueryResult search(Query query) throws TwitterException { | |
try { | |
return LazyEntityFactory.newQueryResult( | |
get(conf.getSearchBaseURL() + "search.json", query.asHttpParameterArray()) | |
, conf); | |
} catch (TwitterException te) { | |
if (404 == te.getStatusCode()) { | |
return new QueryResultJSONImpl(query); | |
} else { | |
throw te; | |
} | |
} | |
} | |
} | |
public class LazyEntityFactory{ | |
public static QueryResult newQueryResult(HttpResponse response, Configuration conf) | |
throws TwitterException{ | |
return create(response, conf, QueryResult.class, QueryResultJSONImpl.class); | |
} | |
private static <T> T create( | |
HttpResponse response, Configuration conf | |
, Class<T> modelClass, Class<? extends T> implClass) | |
throws TwitterException{ | |
try { | |
return (T)Proxy.newProxyInstance( | |
Thread.currentThread().getContextClassLoader() | |
, new Class<?>[]{modelClass} | |
, new LazySimpleModelInvocationHandler<T>(response, conf, implClass) | |
); | |
} catch (IllegalArgumentException e) { | |
throw new TwitterException(e); | |
} catch (SecurityException e) { | |
throw new TwitterException(e); | |
} catch (NoSuchMethodException e) { | |
throw new TwitterException(e); | |
} | |
} | |
} | |
public class LazySimpleModelInvocationHandler<T> extends LazyInstanceInvocationHandler<T>{ | |
public LazySimpleModelInvocationHandler( | |
HttpResponse response, Configuration conf, Class<? extends T> implClass) | |
throws SecurityException, NoSuchMethodException{ | |
this.response = response; | |
this.implCtor = implClass.getDeclaredConstructor( | |
HttpResponse.class, Configuration.class); | |
this.implCtor.setAccessible(true); | |
} | |
@Override | |
protected T resolve() throws Throwable { | |
return (T)implCtor.newInstance(response, conf); | |
} | |
private HttpResponse response; | |
private Configuration conf; | |
private Constructor<? extends T> implCtor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment