Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add API for use to define custom ini files

  • Loading branch information...
commit 292b46958476a164e7ea6da37eff730cb3b80385 1 parent bea2938
Dale  Harvey authored
4  src/com/couchbase/libcouch/CouchInstaller.java
@@ -185,11 +185,11 @@ private static void installPackage(String pkg, Handler handler, CouchService ser
185 185
 		replace(CouchbaseEmbeddedServer.dataPath() + "/erlang/bin/erl", replacements);
186 186
 		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/lib/couchdb/erlang/lib/couch/ebin/couch.app", replacements);
187 187
 		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/lib/couchdb/erlang/lib/couch/priv/lib/couch_icu_driver.la", replacements);
188  
-		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/bin/couchdb", replacements);
189 188
 		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/bin/couchjs", replacements);
190 189
 		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/bin/couchjs_wrapper", replacements);
  190
+		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/bin/couchdb_wrapper", replacements);
191 191
 		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/etc/couchdb/default.ini", replacements);
192  
-		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/etc/couchdb/default.d/android.default.ini", replacements);
  192
+		replace(CouchbaseEmbeddedServer.dataPath() + "/couchdb/etc/couchdb/android.default.ini", replacements);
193 193
 	}
194 194
 
195 195
 	/*
55  src/com/couchbase/libcouch/CouchService.java
@@ -12,11 +12,12 @@
12 12
 import java.io.StringWriter;
13 13
 import java.net.URL;
14 14
 import java.util.ArrayList;
  15
+import java.util.Arrays;
  16
+import java.util.Collection;
  17
+import java.util.Iterator;
15 18
 import java.util.regex.Matcher;
16 19
 import java.util.regex.Pattern;
17 20
 
18  
-import com.google.ase.Exec;
19  
-
20 21
 import android.app.Service;
21 22
 import android.content.Intent;
22 23
 import android.os.Handler;
@@ -25,9 +26,11 @@
25 26
 import android.os.RemoteException;
26 27
 import android.util.Log;
27 28
 
  29
+import com.google.ase.Exec;
  30
+
28 31
 public class CouchService extends Service {
29 32
 
30  
-	/* 
  33
+	/*
31 34
 	 * Contants used to communnicate between the Couch Service
32 35
 	 * and this thread
33 36
 	 */
@@ -36,7 +39,7 @@
36 39
 	public final static int COMPLETE = 2;
37 40
 	public final static int COUCH_STARTED = 3;
38 41
 	public final static int INSTALLING = 4;
39  
-	
  42
+
40 43
 	/* The url Couch has started on */
41 44
 	public URL url;
42 45
 
@@ -50,7 +53,7 @@
50 53
 	private PrintStream out;
51 54
 	private BufferedReader in;
52 55
 
53  
-	/* 
  56
+	/*
54 57
 	 * Couch runs in a seperate thread, have the communication run
55 58
 	 * through our own handler so the app doesnt need to create a new
56 59
 	 * handler to deal with touching the UI from a different thread
@@ -157,12 +160,33 @@ public void run() {
157 160
 
158 161
 	/* Start the couch process, run in a seperate thread */
159 162
 	void startCouch() {
160  
-		
  163
+
  164
+		String path = CouchbaseEmbeddedServer.dataPath();
  165
+
  166
+		String cmd = path + "/couchdb/bin/couchdb_wrapper";
  167
+
  168
+		String[] plainArgs = {
  169
+				"+Bd", "-noinput", "sasl", "errlog_type", "all", "+K", "true",
  170
+				"-env", "ERL_LIBS", path + "/couchdb/lib/couchdb/erlang/lib", "-couch_ini",
  171
+				path + "/couchdb/etc/couchdb/default.ini",
  172
+				path + "/couchdb/etc/couchdb/local.ini",
  173
+				path + "/couchdb/etc/couchdb/android.default.ini"};
  174
+
  175
+		ArrayList<String> args = new ArrayList<String>(Arrays.asList(plainArgs));
  176
+		for (String iniPath : CouchbaseEmbeddedServer.customIniFiles) {
  177
+			args.add(iniPath);
  178
+		}
  179
+
  180
+		args.add(path + "/couchdb/etc/couchdb/overrides.ini -s couch");
  181
+
161 182
 		String shell = "/system/bin/sh";
162  
-		String couchbin = CouchbaseEmbeddedServer.dataPath() + "/couchdb/bin/couchdb";
163  
-		
  183
+		String couchbin = join(args, " ");
  184
+
  185
+		Log.v(CouchbaseEmbeddedServer.TAG, "couchbin");
  186
+		Log.v(CouchbaseEmbeddedServer.TAG, couchbin);
  187
+
164 188
 		int[] pidbuffer = new int[1];
165  
-		final FileDescriptor fd = Exec.createSubprocess(shell, couchbin, "", pidbuffer);
  189
+		final FileDescriptor fd = Exec.createSubprocess(shell, cmd, couchbin, pidbuffer);
166 190
 		pid = pidbuffer[0];
167 191
 		out = new PrintStream(new FileOutputStream(fd), true);
168 192
 		in = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
@@ -210,4 +234,17 @@ private void stop() {
210 234
 		}
211 235
 		return links;
212 236
 	}
  237
+
  238
+	public static String join(Collection<String> s, String delimiter) {
  239
+		StringBuffer buffer = new StringBuffer();
  240
+		Iterator<String> iter = s.iterator();
  241
+		while (iter.hasNext()) {
  242
+			buffer.append(iter.next());
  243
+			if (iter.hasNext()) {
  244
+				buffer.append(delimiter);
  245
+			}
  246
+		}
  247
+		return buffer.toString();
  248
+	}
  249
+
213 250
 }
68  src/com/couchbase/libcouch/CouchbaseEmbeddedServer.java
... ...
@@ -1,10 +1,12 @@
1 1
 package com.couchbase.libcouch;
2 2
 
3 3
 import java.io.File;
  4
+import java.io.FileNotFoundException;
4 5
 import java.io.FileOutputStream;
5 6
 import java.io.IOException;
6 7
 import java.io.InputStream;
7 8
 import java.io.OutputStream;
  9
+import java.util.ArrayList;
8 10
 
9 11
 import android.content.ComponentName;
10 12
 import android.content.Context;
@@ -27,32 +29,37 @@
27 29
 
28 30
 	/* The application name (eg: com.dale.fubar) */
29 31
 	public static String appNamespace;
30  
-	
  32
+
31 33
 	/* The name of the binary package of Couchbase stored in assets */
32 34
 	private static String releaseName;
33 35
 
34  
-	/* 
35  
-	 * The default package name of couchdb binaries, applications are 
  36
+	/*
  37
+	 * The default package name of couchdb binaries, applications are
36 38
 	 * recommended to use this default package name as it ensures this library
37 39
 	 * was built to support these binaries
38 40
 	 */
39  
-	private final static String defaultRelease = "couchbase-test-60d0c5d";
  41
+	private final static String defaultRelease = "couchbase-1.0-dp-3ecef7a";
  42
+
  43
+	/*
  44
+	 * List of user defined ini files
  45
+	 */
  46
+	public static ArrayList<String> customIniFiles = new ArrayList<String>();
40 47
 
41 48
 	private static ICouchService couchService;
42 49
 	private static ICouchClient couchClient;
43 50
 	private static Context ctx;
44 51
 
45  
-	/* 
  52
+	/*
46 53
 	 * A few of the utility functions require some of the same context
47  
-	 * that cannot be gotten automatically, so made this a class to 
  54
+	 * that cannot be gotten automatically, so made this a class to
48 55
 	 * store some context for later functions
49 56
 	 */
50 57
 	public CouchbaseEmbeddedServer(Context appCtx, ICouchClient client) {
51 58
 		couchClient = client;
52 59
 		ctx = appCtx;
53  
-		appNamespace = ctx.getPackageName();		
54  
-	}	
55  
-	
  60
+		appNamespace = ctx.getPackageName();
  61
+	}
  62
+
56 63
 	/* The path to this apps internal memory */
57 64
 	public static String dataPath() {
58 65
 		return "/data/data/" + CouchbaseEmbeddedServer.appNamespace;
@@ -61,21 +68,21 @@ public static String dataPath() {
61 68
 	/* The path to this apps external (sdcard) memory */
62 69
 	public static String externalPath() {
63 70
 		return Environment.getExternalStorageDirectory() + "/Android/data/" + CouchbaseEmbeddedServer.appNamespace;
64  
-	}	
  71
+	}
65 72
 
66 73
 	/* Start Couchbase with the default binaries */
67 74
 	public ServiceConnection startCouchbase() {
68 75
 		return startCouchbase(ctx, defaultRelease);
69 76
 	}
70 77
 
71  
-	/* 
  78
+	/*
72 79
 	 * Start Couchbase, this starts Couchbase as an android service, the ServiceConnection
73  
-	 * returned allowed for futher communication (such as install progress / started 
  80
+	 * returned allowed for futher communication (such as install progress / started
74 81
 	 * callbacks), check the ICouchClient.aidl and ICouchServer.aidl for the definition
75 82
 	 * of these callbacks
76 83
 	 */
77 84
 	public ServiceConnection startCouchbase(Context ctx, String release) {
78  
-		releaseName = release;		
  85
+		releaseName = release;
79 86
 		ctx.bindService(new Intent(ctx, CouchService.class), mConnection, Context.BIND_AUTO_CREATE);
80 87
 		return mConnection;
81 88
 	}
@@ -87,15 +94,34 @@ public ServiceConnection startCouchbase(Context ctx, String release) {
87 94
 	 * be able to be opened
88 95
 	 */
89 96
 	public void installDatabase(String fileName) throws IOException {
90  
-		File db = new File(externalPath() + "/db/" + fileName);
91  
-		if (!db.exists()) {
92  
-			
93  
-			// Ensure db directory exists
94  
-			(new File(externalPath() + "/db/")).mkdirs();
95  
-			
  97
+		File destination = new File(externalPath() + "/db/" + fileName);
  98
+		copyIffNotExists(fileName, destination);
  99
+	}
  100
+
  101
+	/*
  102
+	 * Copy an .ini file from the assets folder into
  103
+	 * /data/data/com.your.app/user_data directory and
  104
+	 * add it to the list of ini files for couch to load
  105
+	 */
  106
+	public void copyIniFile(String fileName) throws IOException {
  107
+		File destination = new File(dataPath() + "/user_data/" + fileName);
  108
+		try {
  109
+			copyIffNotExists(fileName, destination);
  110
+			customIniFiles.add(destination.getAbsolutePath());
  111
+		} catch(FileNotFoundException e) {
  112
+			throw e;
  113
+		}
  114
+	}
  115
+
  116
+	private void copyIffNotExists(String name, File destination) throws IOException  {
  117
+		if (!destination.exists()) {
  118
+
  119
+			// Ensure directory exists
  120
+			(new File(destination.getParent())).mkdirs();
  121
+
96 122
 			AssetManager assetManager = ctx.getAssets();
97  
-			InputStream in = assetManager.open(fileName);
98  
-			OutputStream out = new FileOutputStream(db);
  123
+			InputStream in = assetManager.open(name);
  124
+			OutputStream out = new FileOutputStream(destination);
99 125
 			byte[] buffer = new byte[1024];
100 126
 			int read;
101 127
 			while((read = in.read(buffer)) != -1){

0 notes on commit 292b469

Please sign in to comment.
Something went wrong with that request. Please try again.