PIMDatabasepublic class PIMDatabase extends Object Handles reading and writing PIM data. |
Fields Summary |
---|
private static SecurityToken | classSecurityTokenSecurity token to allow access to implementation APIs | private String | dirName of root dir. | private Hashtable | categoriesMapHashtable of categories. | private final String | fileSepFile separator symbol. | private static final String[] | DIRECTORIESDirectory structure of lists. | String[] | LISTSDefault names of lists. |
Constructors Summary |
---|
public PIMDatabase()Default constructor.
| public PIMDatabase(String inpDir)Constructor for creating a database object for the given path.
this.dir = inpDir;
String fcPrefix = "file:";
if (dir.startsWith(fcPrefix)) { // remove file prefix
dir = dir.substring(fcPrefix.length());
}
FileConnection tmpDir;
boolean tmpCond;
String tmpStr, tmpStr1;
com.sun.midp.io.j2me.file.Protocol conn =
new com.sun.midp.io.j2me.file.Protocol();
tmpDir = (FileConnection)conn.openPrim(classSecurityToken, dir);
tmpCond = tmpDir.exists();
tmpDir.close();
if (!tmpCond) { // Create directories if need
synchronized (this) {
tmpDir = (FileConnection)conn.openPrim
(classSecurityToken, dir);
tmpDir.mkdir();
tmpStr = tmpDir.getURL().substring(fcPrefix.length());
tmpDir.close();
// create list type directories with default lists
for (int i = 0; i < DIRECTORIES.length; i++) {
tmpDir =
(FileConnection)conn.openPrim(classSecurityToken,
tmpStr + DIRECTORIES[i]);
tmpDir.mkdir();
tmpStr1 = tmpDir.getURL().substring(fcPrefix.length());
tmpDir.close();
for (int j = 0; j < LISTS[i].length; j++) {
String listName =
Configuration.getProperty(LISTS[i][j]);
tmpDir = (FileConnection)conn.openPrim
(classSecurityToken, tmpStr1 + listName);
tmpDir.mkdir();
tmpDir.close();
}
}
}
}
|
Methods Summary |
---|
private java.lang.String | categoryKey(int listType, java.lang.String listName)Create the key string by the given PIM list.
return "" + listType + "." + listName;
| public synchronized java.lang.String | commitElement(int listType, java.lang.String listName, java.lang.String key, byte[] data)Commits the PIM item by key.
String ret_v = null;
if (key == null) {
// make a new key
Hashtable keySet = getKeys(listType, listName);
String extension;
switch (listType) {
case PIMBridge.CONTACT_LIST: extension = ".vcf"; break;
default: extension = ".vcs"; break;
}
key = 1 + extension;
int i = 1;
while (keySet.containsKey(i + extension)) {
key = (++i) + extension;
// keep looking for a free file name
}
}
String listDir = getListDir(listType, listName);
FileConnection file = null;
com.sun.midp.io.j2me.file.Protocol conn =
new com.sun.midp.io.j2me.file.Protocol();
try {
file = (FileConnection)conn.openPrim(classSecurityToken,
listDir + fileSep + key);
if (data == null) {
if (file.exists()) {
file.delete();
}
file.close();
} else {
if (!file.exists()) {
file.create();
}
DataOutputStream out = file.openDataOutputStream();
out.write(data);
out.close();
file.close();
ret_v = new String(key);
}
} catch (IOException e) {
if (Logging.TRACE_ENABLED) {
Logging.trace(e, "commitElement: FileConnection problem");
}
throw new PIMException("commitElement: " + e.getMessage());
} finally {
try {
if (file.isOpen()) {
file.close();
}
} catch (IOException e) {
// do nothing
}
}
return ret_v;
| public synchronized java.lang.String | getCategories(int listType, java.lang.String listName)Gets the string of supported categories.
String key = categoryKey(listType, listName);
String categories = (String) categoriesMap.get(key);
com.sun.midp.io.j2me.file.Protocol conn =
new com.sun.midp.io.j2me.file.Protocol();
if (categories == null) {
FileConnection file = null;
try {
file = (FileConnection)conn.openPrim(classSecurityToken,
getListDir(listType, listName) + fileSep +
"categories.txt");
if (!file.exists()) {
file.create();
}
long catSize = file.fileSize();
if (catSize <= 0) { // no categories
categories = "";
} else { // read categories
byte[] buffer = new byte[(int)catSize];
DataInputStream in = file.openDataInputStream();
in.readFully(buffer);
in.close();
categories = new String(buffer);
}
file.close();
categoriesMap.put(key, categories);
} catch (IOException e) {
throw new PIMException("getCategories: " + e.getMessage());
} finally {
try {
if (file.isOpen()) {
file.close();
}
} catch (IOException e) {
throw new PIMException("getCategories: " +
e.getMessage());
}
}
}
return categories;
| public java.lang.String | getDefaultListName(int listType)Gets the default list name for given list type.
return Configuration.getProperty(LISTS[listType - 1][0]);
| public byte[] | getElement(int listType, java.lang.String listName, java.lang.String key)Gets the element by key.
FileConnection file = null;
byte[] ret_v = null;
com.sun.midp.io.j2me.file.Protocol conn =
new com.sun.midp.io.j2me.file.Protocol();
try {
file = (FileConnection)conn.openPrim(classSecurityToken,
getListDir(listType, listName) + fileSep + key,
Connector.READ);
long elemSize = file.fileSize();
if (elemSize <= 0) { // no categories
ret_v = new byte[0];
} else { // read categories
byte[] buffer = new byte[(int)elemSize];
DataInputStream in = file.openDataInputStream();
in.readFully(buffer);
in.close();
ret_v = buffer;
}
file.close();
} catch (IOException e) {
if (Logging.TRACE_ENABLED) {
Logging.trace(e, "getElement: FileConnection problem");
}
throw new PIMException("getElement: " + e.getMessage());
} finally {
try {
if (file.isOpen()) {
file.close();
}
} catch (IOException e) {
// do nothing
}
}
return ret_v;
| public java.util.Hashtable | getKeys(int listType, java.lang.String listName)Gets the key table for given list name.
Hashtable keys = new Hashtable();
Enumeration enListDir = null;
FileConnection tmpDir;
com.sun.midp.io.j2me.file.Protocol conn =
new com.sun.midp.io.j2me.file.Protocol();
try {
tmpDir = (FileConnection)conn.openPrim(classSecurityToken,
getTypeDir(listType) + "/" + listName);
enListDir = tmpDir.list();
tmpDir.close();
} catch (IOException e) {
if (Logging.TRACE_ENABLED) {
Logging.trace(e, "getKeys: FileConnection problem");
}
throw new PIMException("getKeys: " + e.getMessage());
}
while (enListDir.hasMoreElements()) {
String curr_name = (enListDir.nextElement()).toString();
curr_name = curr_name.toLowerCase();
if (curr_name.endsWith(".vcf") || curr_name.endsWith(".vcs")) {
keys.put(curr_name, curr_name);
}
}
return keys;
| private java.lang.String | getListDir(int listType, java.lang.String listName)Gets the path for given list name.
return getTypeDir(listType) + fileSep + listName;
| public java.lang.String[] | getListNames(int listType)Gets the list of names for given list type.
Vector vect_names = new Vector();
Enumeration enListType = null;
FileConnection tmpDir;
com.sun.midp.io.j2me.file.Protocol conn =
new com.sun.midp.io.j2me.file.Protocol();
try {
tmpDir = (FileConnection)conn.openPrim(classSecurityToken,
getTypeDir(listType));
enListType = tmpDir.list();
tmpDir.close();
} catch (IOException e) {
if (Logging.TRACE_ENABLED) {
Logging.trace(e, "getListNames: FileConnection problem");
}
}
while (enListType.hasMoreElements()) {
String curr_name = (enListType.nextElement()).toString();
if (curr_name.endsWith(fileSep)) { // The last symbol is "/"
vect_names.addElement(curr_name.substring(0,
curr_name.length() - 1)); // save the list name
}
}
String[] names = new String[vect_names.size()];
Enumeration dir_names = vect_names.elements();
int i;
for (i = 0; i < names.length; i++) {
names[i] = (dir_names.nextElement()).toString();
}
return names;
| private java.lang.String | getTypeDir(int listType)Gets the path for given list type.
return dir + fileSep + DIRECTORIES[listType - 1];
| public synchronized void | setCategories(int listType, java.lang.String listName, java.lang.String categories)Sets the new categories.
FileConnection file = null;
com.sun.midp.io.j2me.file.Protocol conn =
new com.sun.midp.io.j2me.file.Protocol();
try {
file = (FileConnection)conn.openPrim(classSecurityToken,
getListDir(listType, listName) + fileSep + "categories.txt");
if (!file.exists()) {
file.create();
} else {
file.truncate(0);
}
DataOutputStream out = file.openDataOutputStream();
out.write(categories.getBytes());
out.close();
file.close();
categoriesMap.put(categoryKey(listType, listName), categories);
} catch (IOException e) {
if (Logging.TRACE_ENABLED) {
Logging.trace(e, "setCategories: FileConnection problem");
}
throw new PIMException("setCategories: " + e.getMessage());
} finally {
try {
if (file.isOpen()) {
file.close();
}
} catch (IOException e) {
// do nothing
}
}
|
|