-
Notifications
You must be signed in to change notification settings - Fork 90
/
Manager.java
1080 lines (953 loc) · 40.5 KB
/
Manager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2016 Couchbase, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.couchbase.lite;
import com.couchbase.lite.auth.Authorizer;
import com.couchbase.lite.auth.FacebookAuthorizer;
import com.couchbase.lite.auth.PersonaAuthorizer;
import com.couchbase.lite.internal.InterfaceAudience;
import com.couchbase.lite.replicator.Replication;
import com.couchbase.lite.support.FileDirUtils;
import com.couchbase.lite.support.HttpClientFactory;
import com.couchbase.lite.support.Version;
import com.couchbase.lite.util.Log;
import com.couchbase.lite.util.StreamUtils;
import com.couchbase.lite.util.Utils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Top-level CouchbaseLite object; manages a collection of databases as a CouchDB server does.
*/
public final class Manager {
public static final String PRODUCT_NAME = "CouchbaseLite";
protected static final String kV1DBExtension = ".cblite"; // Couchbase Lite 1.0
protected static final String kDBExtension = ".cblite2"; // Couchbase Lite 1.2 or later (for iOS 1.1 or later)
public static final ManagerOptions DEFAULT_OPTIONS = new ManagerOptions();
public static final String LEGAL_CHARACTERS = "[^a-z]{1,}[^a-z0-9_$()/+-]*$";
public static String USER_AGENT = null;
public static final String SQLITE_STORAGE = "SQLite";
public static final String FORESTDB_STORAGE = "ForestDB";
// NOTE: Jackson is thread-safe http://wiki.fasterxml.com/JacksonFAQThreadSafety
// NOTE: Jackson automatically close input resource after parsing the conent.
// Need to disable it. https://github.com/FasterXML/jackson-databind/issues/697
private static final ObjectMapper mapper = new ObjectMapper()
.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
private ManagerOptions options;
private File directoryFile;
/*package*/ Object lockDatabases = new Object();
private Map<String, Database> databases;
private Map<String, Object> encryptionKeys;
private List<Replication> replications;
private ScheduledExecutorService workExecutor;
private HttpClientFactory defaultHttpClientFactory;
private Context context;
private String storageType;
///////////////////////////////////////////////////////////////////////////
// APIs
// https://github.com/couchbaselabs/couchbase-lite-api/blob/master/gen/md/Database.md
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Constructors
///////////////////////////////////////////////////////////////////////////
/**
* Constructor
*
* @throws UnsupportedOperationException - not currently supported
* @exclude
*/
@InterfaceAudience.Public
public Manager() {
final String detailMessage = "Parameterless constructor is not a valid API call on Android. " +
" Pure java version coming soon.";
throw new UnsupportedOperationException(detailMessage);
}
/**
* Constructor
*
* @throws java.lang.SecurityException - Runtime exception that can be thrown by File.mkdirs()
*/
@InterfaceAudience.Public
public Manager(Context context, ManagerOptions options) throws IOException {
Log.i(Database.TAG, "### %s ###", getFullVersionInfo());
this.context = context;
this.directoryFile = context.getFilesDir();
this.options = (options != null) ? options : DEFAULT_OPTIONS;
this.databases = new HashMap<String, Database>();
this.encryptionKeys = new HashMap<String, Object>();
this.replications = new ArrayList<Replication>();
if (!directoryFile.exists()) {
directoryFile.mkdirs();
}
if (!directoryFile.isDirectory()) {
throw new IOException(String.format(Locale.ENGLISH, "Unable to create directory for: %s", directoryFile));
}
upgradeOldDatabaseFiles(directoryFile);
// this must be a single threaded executor due to contract w/ Replication object
// which must run on either:
// - a shared single threaded executor
// - its own single threaded executor
workExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "CBLManagerWorkExecutor");
}
});
}
///////////////////////////////////////////////////////////////////////////
// Constants
///////////////////////////////////////////////////////////////////////////
public static final String VERSION = Version.VERSION;
///////////////////////////////////////////////////////////////////////////
// Class Members - Properties
///////////////////////////////////////////////////////////////////////////
/**
* Get shared instance
*
* @throws UnsupportedOperationException - not currently supported
* @exclude
*/
@InterfaceAudience.Public
public static Manager getSharedInstance() {
final String detailMessage = "getSharedInstance() is not a valid API call on Android. " +
" Pure java version coming soon";
throw new UnsupportedOperationException(detailMessage);
}
/**
* Get the default storage type.
* @return Storage type.
*/
@InterfaceAudience.Public
public String getStorageType() {
return storageType;
}
/**
* Set default storage engine type for newly-created databases.
* There are two options, "SQLite" (the default) or "ForestDB".
* @param storageType
*/
@InterfaceAudience.Public
public void setStorageType(String storageType) {
this.storageType = storageType;
}
///////////////////////////////////////////////////////////////////////////
// Class Members - Methods
///////////////////////////////////////////////////////////////////////////
/**
* Enable logging for a particular tag / loglevel combo
*
* @param tag Used to identify the source of a log message. It usually identifies
* the class or activity where the log call occurs.
* @param logLevel The loglevel to enable. Anything matching this loglevel
* or having a more urgent loglevel will be emitted. Eg, Log.VERBOSE.
*/
public static void enableLogging(String tag, int logLevel) {
Log.enableLogging(tag, logLevel);
}
/**
* Returns YES if the given name is a valid database name.
* (Only the characters in "abcdefghijklmnopqrstuvwxyz0123456789_$()+-/" are allowed.)
*/
@InterfaceAudience.Public
public static boolean isValidDatabaseName(String databaseName) {
if (databaseName.length() > 0 && databaseName.length() < 240 &&
containsOnlyLegalCharacters(databaseName) &&
Character.isLowerCase(databaseName.charAt(0))) {
return true;
}
return databaseName.equals(Replication.REPLICATOR_DATABASE_NAME);
}
///////////////////////////////////////////////////////////////////////////
// Instance Members - Properties
///////////////////////////////////////////////////////////////////////////
/**
* An array of the names of all existing databases.
*/
@InterfaceAudience.Public
public List<String> getAllDatabaseNames() {
String[] databaseFiles = directoryFile.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
if (filename.endsWith(Manager.kDBExtension)) {
return true;
}
return false;
}
});
List<String> result = new ArrayList<String>();
for (String databaseFile : databaseFiles) {
String trimmed = databaseFile.substring(0, databaseFile.length() - Manager.kDBExtension.length());
String replaced = trimmed.replace(':', '/');
result.add(replaced);
}
Collections.sort(result);
return Collections.unmodifiableList(result);
}
/**
* The root directory of this manager (as specified at initialization time.)
*/
@InterfaceAudience.Public
public File getDirectory() {
return directoryFile;
}
///////////////////////////////////////////////////////////////////////////
// Instance Members - Methods
///////////////////////////////////////////////////////////////////////////
/**
* Releases all resources used by the Manager instance and closes all its databases.
*/
@InterfaceAudience.Public
public void close() {
synchronized (lockDatabases) {
Log.d(Database.TAG, "Closing " + this);
// Close all database:
// Snapshot of the current open database to avoid concurrent modification as
// the database will be forgotten (removed from the databases map) when it is closed:
Database[] openDbs = databases.values().toArray(new Database[databases.size()]);
for (Database database : openDbs)
database.close();
databases.clear();
// Stop reachability:
context.getNetworkReachabilityManager().stopListening();
// Shutdown ScheduledExecutorService:
if (workExecutor != null && !workExecutor.isShutdown())
Utils.shutdownAndAwaitTermination(workExecutor);
Log.d(Database.TAG, "Closed " + this);
}
}
/**
* <p>
* Returns the database with the given name, or creates it if it doesn't exist.
* Multiple calls with the same name will return the same {@link Database} instance.
* <p/>
* <p>
* This is equivalent to calling {@link #openDatabase(String, DatabaseOptions)}
* with a default set of options with the `Create` flag set.
* </p>
* <p>
* NOTE: Database names may not contain capital letters.
* </p>
*/
@InterfaceAudience.Public
public Database getDatabase(String name) throws CouchbaseLiteException {
DatabaseOptions options = getDefaultOptions(name);
options.setCreate(true);
return openDatabase(name, options);
}
/**
* <p>
* Returns the database with the given name, or null if it doesn't exist.
* Multiple calls with the same name will return the same {@link Database} instance.
* <p/>
* <p>
* This is equivalent to calling {@link #openDatabase(String, DatabaseOptions)}
* with a default set of options.
* </p>
*/
@InterfaceAudience.Public
public Database getExistingDatabase(String name) throws CouchbaseLiteException {
DatabaseOptions options = getDefaultOptions(name);
return openDatabase(name, options);
}
/**
* Returns the database with the given name. If the database is not yet open, the options given
* will be applied; if it's already open, the options are ignored.
* Multiple calls with the same name will return the same {@link Database} instance.
* @param name The name of the database. May NOT contain capital letters!
* @param options Options to use when opening, such as the encryption key; if null, a default
* set of options will be used.
* @return The database instance.
* @throws CouchbaseLiteException thrown when there is an error.
*/
@InterfaceAudience.Public
public Database openDatabase(String name, DatabaseOptions options)
throws CouchbaseLiteException {
if (options == null)
options = getDefaultOptions(name);
Database db = getDatabase(name, !options.isCreate());
if (db != null && !db.isOpen()) {
db.open(options);
registerEncryptionKey(options.getEncryptionKey(), name);
}
return db;
}
/**
* This method has been superseded by {@link #openDatabase(String, DatabaseOptions)}.
*
* Registers an encryption key for a database. This must be called _before_ opening an encrypted
* database, or before creating a database that's to be encrypted.
* If the key is incorrect (or no key is given for an encrypted database), the subsequent call
* to open the database will fail with an error with code 401.
* To use this API, the database storage engine must support encryption, and the
* ManagerOptions.EnableStorageEncryption property must be set to true. Otherwise opening
* the database will fail with an error.
* @param keyOrPassword The encryption key in the form of an String (a password) or an
* byte[] object exactly 32 bytes in length (a raw AES key.)
* If a string is given, it will be internally converted to a raw key
* using 64,000 rounds of PBKDF2 hashing.
* A null value is legal, and clears a previously-registered key.
* @param databaseName The name of the database.
* @return True if the key can be used, False if it's not in a legal form
* (e.g. key as a byte[] is not 32 bytes in length.)
*/
@InterfaceAudience.Public
public boolean registerEncryptionKey(Object keyOrPassword, String databaseName) {
if (databaseName == null)
return false;
if (keyOrPassword != null) {
encryptionKeys.put(databaseName, keyOrPassword);
} else
encryptionKeys.remove(databaseName);
return true;
}
/**
* Replaces or installs a database from a file.
* <p/>
* This is primarily used to install a canned database
* on first launch of an app, in which case you should first check .exists to avoid replacing the
* database if it exists already. The canned database would have been copied into your app bundle
* at build time. This property is deprecated for the new .cblite2 database file. If the database
* file is a directory and has the .cblite2 extension,
* use -replaceDatabaseNamed:withDatabaseDir:error: instead.
*
* @param databaseName The name of the target Database to replace or create.
* @param databaseStream InputStream on the source Database file.
* @param attachmentStreams Map of the associated source Attachments, or null if there are no attachments.
* The Map key is the name of the attachment, the map value is an InputStream for
* the attachment contents. If you wish to control the order that the attachments
* will be processed, use a LinkedHashMap, SortedMap or similar and the iteration order
* will be honoured.
*/
@InterfaceAudience.Public
public void replaceDatabase(String databaseName,
InputStream databaseStream,
Map<String, InputStream> attachmentStreams)
throws CouchbaseLiteException {
replaceDatabase(databaseName, databaseStream,
attachmentStreams == null ? null : attachmentStreams.entrySet().iterator());
}
/**
* Replaces or installs a database from a file.
*
* This is primarily used to install a canned database
* on first launch of an app, in which case you should first check .exists to avoid replacing the
* database if it exists already. The canned database would have been copied into your app bundle
* at build time. If the database file is not a directory and has the .cblite extension,
* use -replaceDatabaseNamed:withDatabaseFile:withAttachments:error: instead.
*
* @param databaseName The name of the database to replace.
* @param databaseDir Path of the database directory that should replace it.
* @return YES if the database was copied, NO if an error occurred.
*/
@InterfaceAudience.Public
public boolean replaceDatabase(String databaseName, String databaseDir) {
Database db = getDatabase(databaseName, false);
if(db == null)
return false;
File dir = new File(databaseDir);
if(!dir.exists()){
Log.w(Database.TAG, "Database file doesn't exist at path : %s", databaseDir);
return false;
}
if (!dir.isDirectory()) {
Log.w(Database.TAG, "Database file is not a directory. " +
"Use -replaceDatabaseNamed:withDatabaseFilewithAttachments:error: instead.");
return false;
}
File destDir = new File(db.getPath());
File srcDir = new File(databaseDir);
if(destDir.exists()) {
if (!FileDirUtils.deleteRecursive(destDir)) {
Log.w(Database.TAG, "Failed to delete file/directly: " + destDir);
return false;
}
}
try {
FileDirUtils.copyFolder(srcDir, destDir);
} catch (IOException e) {
Log.w(Database.TAG, "Failed to copy directly from " + srcDir + " to " + destDir, e);
return false;
}
try {
db.open();
} catch (CouchbaseLiteException e) {
Log.w(Database.TAG, "Failed to open database", e);
return false;
}
/* TODO: Currently Java implementation is different from iOS, needs to catch up.
if(!db.saveLocalUUIDInLocalCheckpointDocument()){
Log.w(Database.TAG, "Failed to replace UUIDs");
return false;
}
*/
if(!db.replaceUUIDs()){
Log.w(Database.TAG, "Failed to replace UUIDs");
db.close();
return false;
}
// close so app can (re)open db with its preferred options:
db.close();
return true;
}
///////////////////////////////////////////////////////////////////////////
// End of APIs
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Public but Not API
///////////////////////////////////////////////////////////////////////////
@InterfaceAudience.Private
DatabaseOptions getDefaultOptions(String databaseName) {
DatabaseOptions options = new DatabaseOptions();
options.setEncryptionKey(encryptionKeys.get(databaseName));
return options;
}
/**
* @exclude
*/
@InterfaceAudience.Private
public static ObjectMapper getObjectMapper() {
return mapper;
}
/**
* @exclude
*/
@InterfaceAudience.Private
public HttpClientFactory getDefaultHttpClientFactory() {
return defaultHttpClientFactory;
}
/**
* @exclude
*/
@InterfaceAudience.Private
public void setDefaultHttpClientFactory(HttpClientFactory defaultHttpClientFactory) {
this.defaultHttpClientFactory = defaultHttpClientFactory;
}
/**
* @exclude
*/
@InterfaceAudience.Private
public Collection<Database> allOpenDatabases() {
synchronized (lockDatabases) {
return databases.values();
}
}
/**
* @exclude
*/
@InterfaceAudience.Private
public Map<String, Object> getEncryptionKeys() {
return Collections.unmodifiableMap(encryptionKeys) ;
}
/**
* Asynchronously dispatches a callback to run on a background thread. The callback will be passed
* Database instance. There is not currently a known reason to use it, it may not make
* sense on the Android API, but it was added for the purpose of having a consistent API with iOS.
*
* @exclude
*/
@InterfaceAudience.Private
public Future runAsync(String databaseName, final AsyncTask function) throws CouchbaseLiteException {
final Database database = getDatabase(databaseName);
return runAsync(new Runnable() {
@Override
public void run() {
function.run(database);
}
});
}
/**
* Instantiates a database but doesn't open the file yet.
* in CBLManager.m
* - (CBLDatabase*) _databaseNamed: (NSString*)name
* mustExist: (BOOL)mustExist
* error: (NSError**)outError
*
* @exclude
*/
@InterfaceAudience.Private
public Database getDatabase(String name, boolean mustExist) {
synchronized (lockDatabases) {
if (options.isReadOnly())
mustExist = true;
Database db = databases.get(name);
if (db == null) {
if (!isValidDatabaseName(name))
throw new IllegalArgumentException("Invalid database name: " + name);
String path = pathForDatabaseNamed(name);
if (path == null)
return null;
db = new Database(path, name, this, options.isReadOnly());
if (mustExist && !db.exists()) {
Log.i(Database.TAG, "mustExist is true and db (%s) does not exist", name);
return null;
}
db.setName(name);
databases.put(name, db);
}
Log.v(Log.TAG_DATABASE, "getDatabase() %s %s", this, db);
return db;
}
}
/**
* @exclude
*/
@InterfaceAudience.Private
public Replication getReplicator(Map<String, Object> properties) throws CouchbaseLiteException {
Map<String, Object> remoteMap;
Map<String, Object> sourceMap = parseSourceOrTarget(properties, "source");
Map<String, Object> targetMap = parseSourceOrTarget(properties, "target");
String source = (String) sourceMap.get("url");
String target = (String) targetMap.get("url");
Boolean createTargetBoolean = (Boolean) properties.get("create_target");
boolean createTarget = (createTargetBoolean != null && createTargetBoolean.booleanValue());
Boolean continuousBoolean = (Boolean) properties.get("continuous");
boolean continuous = (continuousBoolean != null && continuousBoolean.booleanValue());
Boolean cancelBoolean = (Boolean) properties.get("cancel");
boolean cancel = (cancelBoolean != null && cancelBoolean.booleanValue());
// Map the 'source' and 'target' JSON params to a local database and remote URL:
if (source == null || target == null) {
throw new CouchbaseLiteException("Source and target are both null", new Status(Status.BAD_REQUEST));
}
boolean push = false;
Database db = null;
String remoteStr = null;
if (Manager.isValidDatabaseName(source)) {
db = getExistingDatabase(source);
remoteStr = target;
push = true;
remoteMap = targetMap;
} else {
remoteStr = source;
if (createTarget && !cancel) {
boolean mustExist = false;
db = getDatabase(target, mustExist);
db.open();
} else {
db = getExistingDatabase(target);
}
if (db == null) {
throw new CouchbaseLiteException("Database is null", new Status(Status.NOT_FOUND));
}
remoteMap = sourceMap;
}
// Can't specify both a filter and doc IDs
if (properties.get("filter") != null && properties.get("doc_ids") != null)
throw new CouchbaseLiteException("Can't specify both a filter and doc IDs",
new Status(Status.BAD_REQUEST));
URL remote = null;
try {
remote = new URL(remoteStr);
} catch (MalformedURLException e) {
throw new CouchbaseLiteException("Malformed remote url: " + remoteStr,
new Status(Status.BAD_REQUEST));
}
if (remote == null) {
throw new CouchbaseLiteException("Remote URL is null: " + remoteStr,
new Status(Status.BAD_REQUEST));
}
Authorizer authorizer = null;
Map<String, Object> authMap = (Map<String, Object>) remoteMap.get("auth");
if (authMap != null) {
Map<String, Object> persona = (Map<String, Object>) authMap.get("persona");
if (persona != null) {
String email = (String) persona.get("email");
authorizer = new PersonaAuthorizer(email);
}
Map<String, Object> facebook = (Map<String, Object>) authMap.get("facebook");
if (facebook != null) {
String email = (String) facebook.get("email");
authorizer = new FacebookAuthorizer(email);
}
authorizer.setRemoteURL(remote);
authorizer.setLocalUUID(db.publicUUID());
}
Replication repl;
if (push)
repl = db.createPushReplication(remote);
else
repl = db.createPullReplication(remote);
repl.setContinuous(continuous);
if (authorizer != null) {
repl.setAuthenticator(authorizer);
}
Map<String, Object> headers = null;
if (remoteMap != null) {
headers = (Map) remoteMap.get("headers");
}
if (headers != null && !headers.isEmpty()) {
repl.setHeaders(headers);
}
String filterName = (String) properties.get("filter");
if (filterName != null) {
repl.setFilter(filterName);
Map<String, Object> filterParams = (Map<String, Object>) properties.get("query_params");
if (filterParams != null) {
repl.setFilterParams(filterParams);
}
}
// docIDs
if(properties.get("doc_ids") != null) {
if(properties.get("doc_ids") instanceof List){
List<String> docIds = (List<String>)properties.get("doc_ids");
repl.setDocIds(docIds);
}
}
String remoteUUID = (String) properties.get("remoteUUID");
if (remoteUUID != null) {
repl.setRemoteUUID(remoteUUID);
}
if (push) {
repl.setCreateTarget(createTarget);
}
Replication activeReplicator = db.findActiveReplicator(repl);
return activeReplicator != null ? activeReplicator : repl;
}
/**
* @exclude
*/
@InterfaceAudience.Private
public ScheduledExecutorService getWorkExecutor() {
return workExecutor;
}
/**
* @exclude
*/
@InterfaceAudience.Private
public Context getContext() {
return context;
}
/**
* @exclude
*/
@InterfaceAudience.Private
public int getExecutorThreadPoolSize() {
return this.options.getExecutorThreadPoolSize();
}
///////////////////////////////////////////////////////////////////////////
// Internal (protected or private) Methods
///////////////////////////////////////////////////////////////////////////
@InterfaceAudience.Private
private void replaceDatabase(String databaseName,
InputStream databaseStream,
Iterator<Map.Entry<String, InputStream>> attachmentStreams)
throws CouchbaseLiteException {
try {
Database db = getDatabase(databaseName, false);
String dstDbPath = FileDirUtils.getPathWithoutExt(db.getPath()) + kV1DBExtension;
String dstAttsPath = FileDirUtils.getPathWithoutExt(dstDbPath) + " attachments";
OutputStream destStream = new FileOutputStream(new File(dstDbPath));
StreamUtils.copyStream(databaseStream, destStream);
File attachmentsFile = new File(dstAttsPath);
FileDirUtils.deleteRecursive(attachmentsFile);
if (!attachmentsFile.exists()) {
attachmentsFile.mkdirs();
}
if (attachmentStreams != null) {
StreamUtils.copyStreamsToFolder(attachmentStreams, attachmentsFile);
}
if (!upgradeV1Database(databaseName, dstDbPath)) {
throw new CouchbaseLiteException(Status.INTERNAL_SERVER_ERROR);
}
db.open();
db.replaceUUIDs();
} catch (FileNotFoundException e) {
Log.e(Database.TAG, "Error replacing the database: %s", e, databaseName);
throw new CouchbaseLiteException(Status.INTERNAL_SERVER_ERROR);
} catch (IOException e) {
Log.e(Database.TAG, "Error replacing the database: %s", e, databaseName);
throw new CouchbaseLiteException(Status.INTERNAL_SERVER_ERROR);
}
}
/**
* @exclude
*/
@InterfaceAudience.Private
private static boolean containsOnlyLegalCharacters(String databaseName) {
Pattern p = Pattern.compile("^[abcdefghijklmnopqrstuvwxyz0123456789_$()+-/]+$");
Matcher matcher = p.matcher(databaseName);
return matcher.matches();
}
/**
* Scan my dir for SQLite-based databases from Couchbase Lite 1.0 and upgrade them:
* <p/>
* in CBLManager.m
* - (void) upgradeOldDatabaseFiles
*
* @exclude
*/
@InterfaceAudience.Private
private void upgradeOldDatabaseFiles(File dir) throws IOException {
if (dir == null) throw new IllegalArgumentException("dir argument is null.");
if (!dir.exists() || !dir.isDirectory() || !dir.canRead() || !dir.canWrite())
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"dir[%s] might not exist, not be a directory, or not have read/write permission.", dir));
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return name.endsWith(kV1DBExtension);
}
});
// https://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles(java.io.FilenameFilter)
// returns: Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
if (files == null)
throw new IOException(String.format(Locale.ENGLISH,
"Error in File.listFiles(): dir[%s]: dir might not be directory, or i/o error occurs.", dir));
for (File file : files) {
String filename = file.getName();
String name = nameOfDatabaseAtPath(filename);
String oldDbPath = new File(dir, filename).getAbsolutePath();
if (!upgradeDatabase(name, oldDbPath, true))
throw new RuntimeException("Database upgrade failed for: " + name);
}
}
/**
* in CBLManager.m
* - (BOOL) upgradeDatabaseNamed: (NSString*)name
* atPath: (NSString*)dbPath
* error: (NSError**)outError
*
* NOTE: upgradeDatabase() method is called if the old database exists.
*/
private boolean upgradeDatabase(String name, String dbPath, boolean close) {
Log.v(Log.TAG_DATABASE, "CouchbaseLite: Upgrading database (%s) at %s ...", name, dbPath);
// if db with name already exists, not need to migrate. Simply remove old database
Database db = getDatabase(name, false);
if (!db.exists() && !name.equals("_replicator")) {
// temporary database name for new db schema
String tempName = name + ".tmp";
// Create and open new CBLDatabase with temporary name:
Database tmpDB = getDatabase(tempName, false);
if (tmpDB == null) {
Log.w(Log.TAG_DATABASE,
"Upgrade failed: Creating new db failed: %s", tempName);
return false;
}
// upgradeDatabase() is called only if dbPath (old db version) exists. So presence of
// temporary db indicates that previous upgrade crashed midway.
if (tmpDB.exists()) {
Log.v(Log.TAG_DATABASE, "Previous upgrade probably crashed midway. dbPath: " + dbPath);
// rollback from temporary database to old database
// NOTE: Deleting temporary db is not enough because `DatabaseUpgrade.importData()`
// move attachment files instead of copy files.
DatabaseUpgrade upgrader = new DatabaseUpgrade(this, tmpDB, dbPath);
upgrader.backOut();
// temporary db is deleted by previous operation, reopen the temporary db.
tmpDB = getDatabase(tempName, false);
if (tmpDB == null) {
Log.w(Log.TAG_DATABASE, "Upgrade failed: Creating new db failed: %s", tempName);
return false;
}
}
if (tmpDB.exists()) {
// the temporary db should not exist. Just double check
Log.w(Log.TAG_DATABASE,
"Upgrade failed: Failed to delete already existing db: %s", tempName);
return false;
}
// Upgrade the old database into the new one:
DatabaseUpgrade upgrader = new DatabaseUpgrade(this, tmpDB, dbPath);
if (!upgrader.importData()) {
upgrader.backOut();
return false;
}
// close temporary database
tmpDB.close();
// rename temporary database name to new name
File tmpPath = new File(pathForDatabaseNamed(tempName));
File newPath = new File(pathForDatabaseNamed(name));
if (!tmpPath.renameTo(newPath)) {
Log.w(Log.TAG_DATABASE,
"Upgrade failed: Failed to rename db folder from temporary name: %s -> %s",
tmpPath, newPath);
upgrader = new DatabaseUpgrade(this, getDatabase(tempName, false), dbPath);
upgrader.backOut();
return false;
}
// reopen db with name
db = getDatabase(name, false);
if (!db.exists()) {
Log.w(Log.TAG_DATABASE,
"Upgrade failed: Failed to open the database after migrate: %s", name);
return false;
}
}
// close db if necessary
if (close)
db.close();
// Remove old database file and its SQLite side files:
moveSQLiteDbFiles(dbPath, null);
if (dbPath.endsWith(kV1DBExtension)) {
String oldAttachmentsName = FileDirUtils.getDatabaseNameFromPath(dbPath) + " attachments";
File oldAttachmentsDir = new File(directoryFile, oldAttachmentsName);
if (oldAttachmentsDir.exists())
FileDirUtils.deleteRecursive(oldAttachmentsDir);
}
Log.v(Log.TAG_DATABASE, " ...success!");
return true;
}
private boolean upgradeV1Database(String name, String dbPath) {
if (dbPath.endsWith(kV1DBExtension)) {
return upgradeDatabase(name, dbPath, false);
} else {
// Gracefully skipping the upgrade:
Log.w(Log.TAG_DATABASE, "Upgrade skipped: Database file extension is not %s", kDBExtension);
return true;
}
}
private static void moveSQLiteDbFiles(String oldDbPath, String newDbPath) {
for (String suffix : Arrays.asList("", "-wal", "-shm", "-journal")) {
File oldFile = new File(oldDbPath + suffix);
if (!oldFile.exists())
continue;
if (newDbPath != null)
oldFile.renameTo(new File(newDbPath + suffix));
else
oldFile.delete();
}
}
/**
* @exclude
*/
@InterfaceAudience.Private
protected Future runAsync(Runnable runnable) {
synchronized (workExecutor) {
if (!workExecutor.isShutdown()) {
return workExecutor.submit(runnable);
} else {
return null;
}
}
}
/**
* in CBLManager.m
* - (NSString*) pathForDatabaseNamed: (NSString*)name
*
* @exclude
*/
@InterfaceAudience.Private
private static String nameOfDatabaseAtPath(String path) {
String name = FileDirUtils.getDatabaseNameFromPath(path);
return isWindows() ? name.replace('/', '.') : name.replace('/', ':');
}
/**
* in CBLManager.m
* - (NSString*) pathForDatabaseNamed: (NSString*)name
*
* @exclude
*/
@InterfaceAudience.Private
private String pathForDatabaseNamed(String name) {
if ((name == null) || (name.length() == 0) || Pattern.matches(LEGAL_CHARACTERS, name))
return null;
// NOTE: CouchDB allows forward slash as part of database name.
// However, ':' is illegal character on Windows platform.
// For Windows, substitute with period '.'
name = isWindows() ? name.replace('/', '.') : name.replace('/', ':');
String result = directoryFile.getPath() + File.separator + name + Manager.kDBExtension;
return result;
}
/**
* @exclude
*/
@InterfaceAudience.Private
private static Map<String, Object> parseSourceOrTarget(Map<String, Object> properties, String key) {