Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cache the module instances

  • Loading branch information...
commit 1a7e737b4e57f22e1d6706ab9e11d9d598587224 1 parent 8de3456
@qrtt1 authored
View
1  .classpath
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
- <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
View
8 src/main/java/com/google/jplurk_oauth/example/Example.java
@@ -2,11 +2,7 @@
import java.util.Properties;
-import org.json.JSONObject;
-
-import com.google.jplurk_oauth.Offset;
import com.google.jplurk_oauth.Qualifier;
-import com.google.jplurk_oauth.module.Polling;
import com.google.jplurk_oauth.module.Timeline;
import com.google.jplurk_oauth.skeleton.PlurkOAuth;
import com.google.jplurk_oauth.skeleton.RequestException;
@@ -17,9 +13,9 @@ public static void main(String[] args) throws RequestException {
/* create oauth config */
PlurkOAuth auth = new PlurkOAuth(
- prop.getProperty("apikey"), prop.getProperty("apisecret"),
+ prop.getProperty("appkey"), prop.getProperty("appsecret"),
prop.getProperty("token"), prop.getProperty("tokensecret"));
- auth.using(Timeline.class).plurkAdd("Hello JPlurk-OAuth", Qualifier.SAYS);
+ auth.using(Timeline.class).plurkAdd("嗯,將 apikey 改成 appkey 似乎比較恰當。", Qualifier.SAYS);
}
}
View
16 src/main/java/com/google/jplurk_oauth/skeleton/PlurkOAuth.java
@@ -19,7 +19,7 @@
private static Log log = LogFactory.getLog(PlurkOAuth.class);
@SuppressWarnings("serial")
- static Map<HttpMethod, Verb> actionMap = new HashMap<HttpMethod, Verb>() {
+ private static Map<HttpMethod, Verb> actionMap = new HashMap<HttpMethod, Verb>() {
{
put(HttpMethod.DELETE, Verb.DELETE);
put(HttpMethod.GET, Verb.GET);
@@ -28,15 +28,18 @@
}
};
+ @SuppressWarnings("rawtypes")
+ private static Map<Class, Object> cachedModule = new HashMap<Class, Object>();
+
private OAuthService service;
private Token token;
- public PlurkOAuth(String apiKey, String apiSecret, String token, String tokenSecret) {
+ public PlurkOAuth(String appKey, String appSecret, String token, String tokenSecret) {
super();
this.service = new ServiceBuilder()
.provider(PlurkOAuthConfig.class)
- .apiKey(apiKey)
- .apiSecret(apiSecret).build();
+ .apiKey(appKey)
+ .apiSecret(appSecret).build();
this.token = new Token(token, tokenSecret);
}
@@ -74,12 +77,17 @@ private void addQueryStrings(OAuthRequest request, Args args) {
}
}
+ @SuppressWarnings("unchecked")
public <T> T using(Class<T> clazz) {
try {
+ if (cachedModule.containsKey(clazz) && cachedModule.get(clazz) != null) {
+ return (T) cachedModule.get(clazz);
+ }
T instance = clazz.newInstance();
if (instance instanceof AbstractModule) {
AbstractModule module = (AbstractModule) instance;
module.setPlurkOAuth(this);
+ cachedModule.put(clazz, instance);
return instance;
}
} catch (InstantiationException e) {
Please sign in to comment.
Something went wrong with that request. Please try again.