Permalink
Browse files

general code quality improvements

CouchDB sub-process implementation changed to use Process
finally blocks added to properly clean up resources
public non-final/non-static members modified
javadocs added

Change-Id: If2f16b97f1e080189f460575855815256618d5d9
Reviewed-on: http://review.couchbase.org/9390
Reviewed-by: Dale Harvey <daleharvey@arandomurl.com>
Tested-by: Dale Harvey <daleharvey@arandomurl.com>
  • Loading branch information...
1 parent af24b11 commit 602a67e05376205fc414f85199e87026c101a612 @mschoch mschoch committed with Dale Harvey Sep 5, 2011
View
@@ -126,4 +126,16 @@
</zip>
</target>
+ <target name="javadoc">
+ <javadoc
+ destdir="bin/docs"
+ windowtitle="Android Couchbase">
+
+ <packageset dir="src" defaultexcludes="yes">
+ <include name="com/couchbase/android/**"/>
+ </packageset>
+
+ </javadoc>
+ </target>
+
</project>
Binary file not shown.
@@ -22,10 +22,24 @@
public class CouchbaseInstaller {
+ /**
+ * Utility function to return the path to a file where we record the files installed
+ *
+ * @return the path to the index file
+ */
public static String indexFile() {
return CouchbaseMobile.dataPath() + "/installedfiles.index";
}
+ /**
+ * Checks to see if the requested package is already installed, removes old installations
+ * and installs the package if necessary
+ *
+ * @param pkg the identifier of the package to be installed
+ * @param handler the handler connecting the installation thread with the application main thread
+ * @param service service reference to the Couchbase service performing the installation
+ * @throws IOException
+ */
public static void doInstall(String pkg, Handler handler, CouchbaseService service)
throws IOException {
@@ -59,8 +73,13 @@ public static void doInstall(String pkg, Handler handler, CouchbaseService servi
handler.sendMessage(done);
}
- /*
- * This fetches a given package from amazon and tarbombs it to the filsystem
+ /**
+ * This fetches a given package from the assets directory and tarbombs it to the filsystem
+ *
+ * @param pkg the identifier of the package to be installed
+ * @param handler the handler connecting the installation thread with the application main thread
+ * @param service reference to the Couchbase service performing the installation
+ * @throws IOException
*/
private static void installPackage(String pkg, Handler handler, CouchbaseService service)
throws IOException {
@@ -176,7 +195,7 @@ private static void installPackage(String pkg, Handler handler, CouchbaseService
iLOWriter.close();
String[][] replacements = new String[][]{
- {"%app_name%", CouchbaseMobile.appNamespace},
+ {"%app_name%", CouchbaseMobile.getAppNamespace()},
{"%sdk_int%", Integer.toString(android.os.Build.VERSION.SDK_INT)}
};
@@ -193,9 +212,12 @@ private static void installPackage(String pkg, Handler handler, CouchbaseService
replace(CouchbaseMobile.dataPath() + "/couchdb/etc/couchdb/android.default.ini", replacements);
}
- /*
+ /**
* Verifies that requested version of Couchbase is installed by checking for the presence of
* the package files we write upon installation in the data directory of the app.
+ *
+ * @param pkg version string identifying the pckage
+ * @return true if the package is installed, false otherwise
*/
public static boolean checkInstalled(String pkg) {
@@ -207,8 +229,11 @@ public static boolean checkInstalled(String pkg) {
return true;
}
- /*
+ /**
* Recursively delete directory
+ *
+ * @param dir the directory to delete
+ * @return success/failure
*/
public static Boolean deleteDirectory(File dir) {
if (dir.isDirectory()) {
@@ -223,26 +248,31 @@ public static Boolean deleteDirectory(File dir) {
return dir.delete();
}
- static void replace(String fileName, String[][] replacements) {
- try {
- File file = new File(fileName);
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line = "", content = "";
- while((line = reader.readLine()) != null) {
- content += line + "\n";
- }
-
- for (int i = 0; i < replacements.length; i++) {
- content = content.replaceAll(replacements[i][0], replacements[i][1]);
- }
- reader.close();
- FileWriter writer = new FileWriter(fileName);
- writer.write(content);
- writer.close();
- Runtime.getRuntime().exec("chmod 755 " + fileName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
+ /**
+ * Utilty to process a file line by line, replacing strings with values
+ *
+ * @param fileName the name of the file to process
+ * @param replacements array of 2 element string arrays, 0 is string to match, 1 is replacement
+ * @throws IOException
+ */
+ static void replace(String fileName, String[][] replacements)
+ throws IOException {
+ File file = new File(fileName);
+ BufferedReader reader = new BufferedReader(new FileReader(file));
+ String line = "", content = "";
+ while ((line = reader.readLine()) != null) {
+ content += line + "\n";
+ }
+
+ for (int i = 0; i < replacements.length; i++) {
+ content = content
+ .replaceAll(replacements[i][0], replacements[i][1]);
+ }
+ reader.close();
+ FileWriter writer = new FileWriter(fileName);
+ writer.write(content);
+ writer.close();
+ Runtime.getRuntime().exec("chmod 755 " + fileName);
+ }
}
@@ -16,91 +16,136 @@
import android.os.Environment;
import android.os.IBinder;
-/*
+/**
* This is the minimal API for building against Android-Couchbase, its
* main function is to allow developers to start Couchbase and contains
* some utility functions
+ *
*/
public class CouchbaseMobile {
- /* Tag used for log messages */
+ /**
+ * Tag used for log messages
+ */
public final static String TAG = "Couchbase";
- /* The application name (eg: com.dale.fubar) */
- public static String appNamespace;
+ /**
+ * The application name (eg: com.dale.fubar)
+ */
+ private static String appNamespace;
- /* The name of the binary package of Couchbase stored in assets */
+ /**
+ * The name of the binary package of Couchbase stored in assets
+ */
private static String releaseName;
- /*
+ /**
* The default package name of Couchbase binaries, applications are
* recommended to use this default package name as it ensures this library
* was built to support these binaries
*/
private final static String defaultRelease = "couchbase-1.0-dp-ebc0925";
- /*
+ /**
* List of user defined ini files
*/
- public static ArrayList<String> customIniFiles = new ArrayList<String>();
+ private static ArrayList<String> customIniFiles = new ArrayList<String>();
+ /**
+ * Reference to the couchbaseService
+ */
private static ICouchbaseService couchbaseService;
+
+ /**
+ * Reference to the delegate
+ */
private static ICouchbaseDelegate couchbaseDelegate;
+
+ /**
+ * Reference to the Android context
+ */
private static Context ctx;
- /*
+ /**
* A few of the utility functions require some of the same context
* that cannot be gotten automatically, so made this a class to
* store some context for later functions
+ *
+ * @param appCtx the Android context
+ * @param delegate the Couchbase delegate
*/
public CouchbaseMobile(Context appCtx, ICouchbaseDelegate delegate) {
couchbaseDelegate = delegate;
ctx = appCtx;
appNamespace = ctx.getPackageName();
}
- /* The path to this apps internal memory */
+ /**
+ * The path to this apps internal memory
+ *
+ * @return path to apps internal directory
+ */
public static String dataPath() {
return "/data/data/" + CouchbaseMobile.appNamespace;
}
- /* The path to this apps external (sdcard) memory */
+ /**
+ * The path to this apps external (sdcard) memory
+ *
+ * @return path to the apps external directory
+ */
public static String externalPath() {
return Environment.getExternalStorageDirectory() + "/Android/data/" + CouchbaseMobile.appNamespace;
}
- /* Start Couchbase with the default binaries */
+ /**
+ * Start Couchbase with the default binaries
+ *
+ * @return the Couchbase service connection
+ */
public ServiceConnection startCouchbase() {
return startCouchbase(ctx, defaultRelease);
}
- /*
+ /**
* Start Couchbase, this starts Couchbase as an android service, the ServiceConnection
* returned allowed for futher communication (such as install progress / started
- * callbacks), check the ICouchbaseDelegate.aidl and ICouchbaseService.aidl for the definition
- * of these callbacks
+ * callbacks)
+ *
+ * @see ICouchbaseDelegate
+ * @see ICouchbaseService
+ *
+ * @param ctx the Android context
+ * @param release the identifier of the release to use
+ * @return Couchbase service connection
*/
public ServiceConnection startCouchbase(Context ctx, String release) {
releaseName = release;
ctx.bindService(new Intent(ctx, CouchbaseService.class), mConnection, Context.BIND_AUTO_CREATE);
return mConnection;
}
- /*
+ /**
* This will copy a database from the assets folder into the
* couchbase database directory
* NOTE: Databases that use snappy encoding will not currently
* be able to be opened
+ *
+ * @param fileName the name of the data file
+ * @throws IOException
*/
public void installDatabase(String fileName) throws IOException {
File destination = new File(externalPath() + "/db/" + fileName);
copyIffNotExists(fileName, destination);
}
- /*
+ /**
* Copy an .ini file from the assets folder into
* /data/data/com.your.app/user_data directory and
* add it to the list of ini files for couchbase to load
+ *
+ * @param fileName the name of the ini file
+ * @throws IOException
*/
public void copyIniFile(String fileName) throws IOException {
File destination = new File(dataPath() + "/user_data/" + fileName);
@@ -112,6 +157,13 @@ public void copyIniFile(String fileName) throws IOException {
}
}
+ /**
+ * Copy a file from the assets folder to a destination, if it does not already exist
+ *
+ * @param name the name of the file in the assets folder
+ * @param destination destination file
+ * @throws IOException
+ */
private void copyIffNotExists(String name, File destination) throws IOException {
if (!destination.exists()) {
@@ -131,7 +183,25 @@ private void copyIffNotExists(String name, File destination) throws IOException
}
}
- /*
+ /**
+ * Get the custom INI files to be used when starting Couchbase
+ *
+ * @return array of string paths to ini files
+ */
+ public static ArrayList<String> getCustomIniFiles() {
+ return customIniFiles;
+ }
+
+ /**
+ * Get the application namespace
+ *
+ * @return string containing the application namespace
+ */
+ public static String getAppNamespace() {
+ return appNamespace;
+ }
+
+ /**
* This holds the connection to the Couchbase Service
*/
private final static ServiceConnection mConnection = new ServiceConnection() {
Oops, something went wrong.

0 comments on commit 602a67e

Please sign in to comment.