Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
TFJ-562 introduced jsonStoreEnabled configuration parameter.
Browse files Browse the repository at this point in the history
Now you can disable/enable raw-json store.
disabled by default.

http://twitter4j.org/en/configuration.html#Misc.
  • Loading branch information
yusuke committed Feb 22, 2011
1 parent b10fff6 commit c00b7e4
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 35 deletions.
18 changes: 9 additions & 9 deletions package.sh
Expand Up @@ -100,6 +100,7 @@ cp twitter4j-stream/target/twitter4j-stream-$1-javadoc.jar -d $DIR/twitter4j-str
cp twitter4j-stream/target/twitter4j-stream-$1-sources.jar -d $DIR/twitter4j-stream/

mkdir $DIR/lib
cp readme-libs.txt $DIR/lib
cp twitter4j-core/target/twitter4j-core-$1.jar $DIR/lib
cp twitter4j-media-support/target/twitter4j-media-support-$1.jar $DIR/lib
cp twitter4j-examples/target/twitter4j-examples-$1.jar $DIR/lib
Expand Down Expand Up @@ -139,16 +140,15 @@ cd ../twitter4j-stream
mvn clean compile jar:jar -Dmaven.test.skip=true
cd ..

echo packaging android-じp
echo packaging android-zip
pwd
rm $DIR/lib/*
rmdir $DIR/lib
mkdir $DIR/lib-android

cp twitter4j-core/target/twitter4j-core-$1.jar $DIR/lib-android/twitter4j-core-android-$1.jar
cp twitter4j-media-support/target/twitter4j-media-support-$1.jar $DIR/lib-android/twitter4j-media-support-android-$1.jar
cp twitter4j-async/target/twitter4j-async-$1.jar $DIR/lib-android/twitter4j-async-android-$1.jar
cp twitter4j-stream/target/twitter4j-stream-$1.jar $DIR/lib-android/twitter4j-stream-android-$1.jar
rm $DIR/lib/*.jar

cp twitter4j-core/target/twitter4j-core-$1.jar $DIR/lib/twitter4j-core-android-$1.jar
cp twitter4j-media-support/target/twitter4j-media-support-$1.jar $DIR/lib/twitter4j-media-support-android-$1.jar
cp twitter4j-examples/target/twitter4j-examples-$1.jar $DIR/lib/
cp twitter4j-async/target/twitter4j-async-$1.jar $DIR/lib/twitter4j-async-android-$1.jar
cp twitter4j-stream/target/twitter4j-stream-$1.jar $DIR/lib/twitter4j-stream-android-$1.jar

cd $DIR
zip -r ../../twitter4j-android-$1.zip .
Expand Down
10 changes: 10 additions & 0 deletions readme-libs.txt
@@ -0,0 +1,10 @@
Twittetr4J is a Twitter API binding library for the Java language licensed under Apache License 2.0.

Twitter4J includes software from JSON.org to parse JSON response from the Twitter API. You can see the license term at http://www.JSON.org/license.html

readme-libs.txt - this file
twitter4j-core.jar - REST and Search API support
twitter4j-async.jar - Async API support : use with twitter4j-core
twitter4j-media-support.jar - media API support : use with twitter4j-core
twitter4j-stream.jar - Streaming API support : use with twitter4j-core and twitter4j-async
twitter4j-examples.jar - examples : use with twitter4j-core, twitter4j-async and twitter4j-stream
2 changes: 2 additions & 0 deletions readme.txt
@@ -1,5 +1,7 @@
Twittetr4J is a Twitter API binding library for the Java language licensed under Apache License 2.0.

Twitter4J includes software from JSON.org to parse JSON response from the Twitter API. You can see the license term at http://www.JSON.org/license.html

LICENSE.txt - the terms of license of this software
pom.xml - maven parent pom
powered-by-badge - badge
Expand Down
1 change: 1 addition & 0 deletions twitter4j-async/src/test/resources/twitter4j.properties
@@ -0,0 +1 @@
jsonStoreEnabled=true
Expand Up @@ -104,6 +104,8 @@ public interface Configuration extends HttpClientConfiguration

boolean isIncludeEntitiesEnabled();

boolean isJSONStoreEnabled();

boolean isUserStreamRepliesAllEnabled();

String getMediaProvider();
Expand Down
11 changes: 11 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/conf/ConfigurationBase.java
Expand Up @@ -72,6 +72,8 @@ class ConfigurationBase implements Configuration, java.io.Serializable {

private boolean includeEntitiesEnabled;

private boolean jsonStoreEnabled;

private boolean userStreamRepliesAllEnabled;

private String mediaProvider;
Expand Down Expand Up @@ -144,6 +146,7 @@ protected ConfigurationBase() {

setIncludeEntitiesEnbled(false);

setJSONStoreEnabled(false);

setOAuthRequestTokenURL(DEFAULT_OAUTH_REQUEST_TOKEN_URL);
setOAuthAuthorizationURL(DEFAULT_OAUTH_AUTHORIZATION_URL);
Expand Down Expand Up @@ -536,6 +539,14 @@ protected final void setIncludeEntitiesEnbled(boolean enabled) {
this.includeEntitiesEnabled = enabled;
}

public boolean isJSONStoreEnabled() {
return this.jsonStoreEnabled;
}

protected final void setJSONStoreEnabled(boolean enabled) {
this.jsonStoreEnabled = enabled;
}

public boolean isUserStreamRepliesAllEnabled() {
return this.userStreamRepliesAllEnabled;
}
Expand Down
Expand Up @@ -247,6 +247,12 @@ public ConfigurationBuilder setIncludeEntitiesEnabled(boolean enabled) {
return this;
}

public ConfigurationBuilder setJSONStoreEnabled(boolean enabled) {
checkNotBuilt();
configurationBean.setJSONStoreEnabled(enabled);
return this;
}

public ConfigurationBuilder setUserStreamRepliesAllEnabled(boolean enabled) {
checkNotBuilt();
configurationBean.setUserStreamRepliesAllEnabled(enabled);
Expand Down
Expand Up @@ -32,7 +32,6 @@
public final class PropertyConfiguration extends ConfigurationBase implements java.io.Serializable {

public static final String DEBUG = "debug";
public static final String SOURCE = "source";
public static final String HTTP_USER_AGENT = "http.userAgent";
public static final String USER = "user";
public static final String PASSWORD = "password";
Expand Down Expand Up @@ -79,6 +78,7 @@ public final class PropertyConfiguration extends ConfigurationBase implements ja
public static final String ASYNC_DISPATCHER_IMPL = "async.dispatcherImpl";
public static final String INCLUDE_RTS = "includeRTs";
public static final String INCLUDE_ENTITIES = "includeEntities";
public static final String JSON_STORE_ENABLED = "jsonStoreEnabled";
public static final String STREAM_USER_REPLIES_ALL = "stream.user.repliesAll";

public static final String MEDIA_PROVIDER = "media.provider";
Expand Down Expand Up @@ -342,6 +342,9 @@ private void setFieldsWithPrefix(Properties props, String prefix) {
if (notNull(props, prefix, INCLUDE_ENTITIES)) {
setIncludeEntitiesEnbled(getBoolean(props, prefix, INCLUDE_ENTITIES));
}
if (notNull(props, prefix, JSON_STORE_ENABLED)) {
setJSONStoreEnabled(getBoolean(props, prefix, JSON_STORE_ENABLED));
}
if (notNull(props, prefix, STREAM_USER_REPLIES_ALL)) {
setUserStreamRepliesAllEnabled(getBoolean(props, prefix, STREAM_USER_REPLIES_ALL));
}
Expand Down
Expand Up @@ -16,14 +16,15 @@

package twitter4j.internal.json;

import twitter4j.conf.ConfigurationContext;
import twitter4j.json.DataObjectFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* provides public access to package private methods of twitter4j.json.DataObjectFactory class.<br>
* This class is intended to
* This class is not intended to be used by Twitter4J client.
*
* @author Yusuke Yamamoto - yusuke at mac.com
* @since Twitter4J 2.1.7
Expand All @@ -33,6 +34,8 @@ private DataObjectFactoryUtil() {
throw new AssertionError("not intended to be instantiated.");
}

private static final boolean JSON_STORE_ENABLED = ConfigurationContext.getInstance().isJSONStoreEnabled();

private static final Method CLEAR_THREAD_LOCAL_MAP;
private static final Method REGISTER_JSON_OBJECT;

Expand Down Expand Up @@ -60,26 +63,32 @@ private DataObjectFactoryUtil() {
* provides a public access to {DAOFactory#clearThreadLocalMap}
*/
public static void clearThreadLocalMap() {
try {
CLEAR_THREAD_LOCAL_MAP.invoke(null);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
throw new AssertionError(e);
if (JSON_STORE_ENABLED) {
try {
CLEAR_THREAD_LOCAL_MAP.invoke(null);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
throw new AssertionError(e);
}
}
}

/**
* provides a public access to {DAOFactory#registerJSONObject}
*/
public static <T> T registerJSONObject(T key, Object json) {
try {
if (JSON_STORE_ENABLED) {
try {
// Class[] clazz = REGISTER_JSON_OBJECT.getParameterTypes();
return (T) REGISTER_JSON_OBJECT.invoke(null, key, json);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
throw new AssertionError(e);
return (T) REGISTER_JSON_OBJECT.invoke(null, key, json);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
throw new AssertionError(e);
}
} else {
return key;
}
}
}
34 changes: 22 additions & 12 deletions twitter4j-core/src/main/java/twitter4j/json/DataObjectFactory.java
Expand Up @@ -17,6 +17,7 @@
package twitter4j.json;

import twitter4j.*;
import twitter4j.conf.ConfigurationContext;
import twitter4j.internal.org.json.JSONArray;
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;
Expand All @@ -34,6 +35,7 @@ public final class DataObjectFactory {
private DataObjectFactory() {
throw new AssertionError("not intended to be instantiated.");
}
private static final boolean JSON_STORE_ENABLED = ConfigurationContext.getInstance().isJSONStoreEnabled();

private static final Constructor<Status> statusConstructor;
private static final Constructor<User> userConstructor;
Expand Down Expand Up @@ -112,12 +114,12 @@ private DataObjectFactory() {
}
}

private static ThreadLocal<Map> rawJsonMap = new ThreadLocal<Map>() {
private static final ThreadLocal<Map> rawJsonMap = JSON_STORE_ENABLED ? new ThreadLocal<Map>() {
@Override
protected Map initialValue() {
return new HashMap();
}
};
} : null;

/**
* Returns a raw JSON form of the provided object.<br>
Expand All @@ -128,14 +130,18 @@ protected Map initialValue() {
* @since Twitter4J 2.1.7
*/
public static String getRawJSON(Object obj) {
Object json = rawJsonMap.get().get(obj);
if (json instanceof String) {
return (String) json;
} else if (json != null) {
// object must be instance of JSONObject
return json.toString();
} else {
return null;
if (JSON_STORE_ENABLED) {
Object json = rawJsonMap.get().get(obj);
if (json instanceof String) {
return (String) json;
} else if (json != null) {
// object must be instance of JSONObject
return json.toString();
} else {
return null;
}
}else{
throw new IllegalStateException("JSON Store not enabled. See http://twitter4j.org/en/configuration.html#Misc. for the detail.");
}
}

Expand Down Expand Up @@ -562,7 +568,9 @@ public static Object createObject(String rawJSON) throws TwitterException {
* @since Twitter4J 2.1.7
*/
static void clearThreadLocalMap() {
rawJsonMap.get().clear();
if (JSON_STORE_ENABLED) {
rawJsonMap.get().clear();
}
}

/**
Expand All @@ -572,7 +580,9 @@ static void clearThreadLocalMap() {
* @since Twitter4J 2.1.7
*/
static <T> T registerJSONObject(T key, Object json) {
rawJsonMap.get().put(key, json);
if (JSON_STORE_ENABLED) {
rawJsonMap.get().put(key, json);
}
return key;
}
}
1 change: 1 addition & 0 deletions twitter4j-core/src/test/resources/twitter4j.properties
@@ -0,0 +1 @@
jsonStoreEnabled=true
1 change: 1 addition & 0 deletions twitter4j-stream/src/test/resources/twitter4j.properties
@@ -0,0 +1 @@
jsonStoreEnabled=true

0 comments on commit c00b7e4

Please sign in to comment.