From 1ef3a0176cc870d3cb26a77652b7ae9fd79584ce Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Thu, 5 Jun 2014 17:48:53 +0200 Subject: [PATCH] Added map/pool component (in progress) for OwnCloudClient instances and removed internal dependencies on OwnCloudClientFactory#createOwnCloudClient(ACCOUNT, ...) methods --- .../android/lib/common/OwnCloudClient.java | 52 +++++++------ .../android/lib/common/OwnCloudClientMap.java | 76 +++++++++++++++++++ .../common/operations/RemoteOperation.java | 54 ++++++++++++- 3 files changed, 156 insertions(+), 26 deletions(-) create mode 100644 src/com/owncloud/android/lib/common/OwnCloudClientMap.java diff --git a/src/com/owncloud/android/lib/common/OwnCloudClient.java b/src/com/owncloud/android/lib/common/OwnCloudClient.java index e47de84b..86a259f3 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClient.java @@ -126,30 +126,36 @@ public class OwnCloudClient extends HttpClient { Log.e(TAG + " #" + mInstanceNumber, "BASE URL: " + mUri); Log.e(TAG + " #" + mInstanceNumber, "WebDAV URL: " + mWebdavUri); - getParams().setAuthenticationPreemptive(false); + if (accessToken != null && accessToken.length() > 0) { - mSsoSessionCookie = accessToken; - mCredentials = null; - - Uri serverUri = (mUri != null)? mUri : mWebdavUri; - // TODO refactoring the mess of URIs - - String[] cookies = mSsoSessionCookie.split(";"); - if (cookies.length > 0) { - //Cookie[] cookies = new Cookie[cookiesStr.length]; - for (int i=0; i 0) { + //Cookie[] cookies = new Cookie[cookiesStr.length]; + for (int i=0; i mClients = + new java.util.concurrent.ConcurrentHashMap(); + + public static synchronized OwnCloudClient getClientFor(Account account, Context context) + throws OperationCanceledException, AuthenticatorException, + AccountNotFoundException, IOException { + + OwnCloudClient client = mClients.get(account); + if (client == null) { + client = OwnCloudClientFactory.createOwnCloudClient( + account, + context.getApplicationContext()); + mClients.putIfAbsent(account.name, client); + } + return client; + } + + + public static synchronized OwnCloudClient removeClientFor(Account account) { + return mClients.remove(account.name); + } + + + public static synchronized void clearPool() { + mClients.clear(); + } + +} diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 9d453090..859dac0e 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -30,6 +30,7 @@ import org.apache.commons.httpclient.Credentials; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; +import com.owncloud.android.lib.common.OwnCloudClientMap; import com.owncloud.android.lib.common.network.BearerCredentials; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; @@ -105,7 +106,7 @@ public abstract class RemoteOperation implements Runnable { mAccount = account; mContext = context.getApplicationContext(); try { - mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext); + mClient = OwnCloudClientMap.getClientFor(mAccount, mContext); } catch (Exception e) { Log.e(TAG, "Error while trying to access to " + mAccount.name, e); return new RemoteOperationResult(e); @@ -135,12 +136,17 @@ public abstract class RemoteOperation implements Runnable { * * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}. * + * @deprecated This method will be removed in version 1.0. + * Use {@link #execute(Account, Context, OnRemoteOperationListener, Handler)} + * instead. + * * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation. * @param context Android context for the component calling the method. * @param listener Listener to be notified about the execution of the operation. * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called. * @return Thread were the remote operation is executed. */ + @Deprecated public Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) { if (account == null) throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); @@ -161,6 +167,42 @@ public abstract class RemoteOperation implements Runnable { } + /** + * Asynchronously executes the remote operation + * + * This method should be used whenever an ownCloud account is available, + * instead of {@link #execute(OwnCloudClient, OnRemoteOperationListener, Handler))}. + * + * @param account ownCloud account in remote ownCloud server to reach during the + * execution of the operation. + * @param context Android context for the component calling the method. + * @param listener Listener to be notified about the execution of the operation. + * @param listenerHandler Handler associated to the thread where the methods of the listener + * objects must be called. + * @return Thread were the remote operation is executed. + */ + public Thread execute(Account account, Context context, OnRemoteOperationListener listener, + Handler listenerHandler) { + + if (account == null) + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); + if (context == null) + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context"); + mAccount = account; + mContext = context.getApplicationContext(); + mCallerActivity = null; + mClient = null; // the client instance will be created from mAccount and mContext in the runnerThread to create below + + mListener = listener; + + mListenerHandler = listenerHandler; + + Thread runnerThread = new Thread(this); + runnerThread.start(); + return runnerThread; + } + + /** * Asynchronously executes the remote operation * @@ -205,10 +247,13 @@ public abstract class RemoteOperation implements Runnable { try{ if (mClient == null) { if (mAccount != null && mContext != null) { + /** DEPRECATED BLOCK - will be removed at version 1.0 */ if (mCallerActivity != null) { - mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext, mCallerActivity); + mClient = OwnCloudClientFactory.createOwnCloudClient( + mAccount, mContext, mCallerActivity); } else { - mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext); + /** EOF DEPRECATED */ + mClient = OwnCloudClientMap.getClientFor(mAccount, mContext); } } else { throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account"); @@ -228,6 +273,8 @@ public abstract class RemoteOperation implements Runnable { result = run(mClient); repeat = false; + /** DEPRECATED BLOCK - will be removed at version 1.0 ; don't trust in this code + * to trigger authentication update */ if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() && // (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) { (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) { @@ -251,6 +298,7 @@ public abstract class RemoteOperation implements Runnable { result = null; } } + /** EOF DEPRECATED BLOCK **/ } while (repeat); final RemoteOperationResult resultToSend = result;