showery wrote:
yes, I just want to startup it from my application. Please send it to me if you have an example.For what it's worth, here's my XMLDBUtil class. You must do XMLDBUtil.initializeDatabase() first, then you can add, remove, etc, collections and documents. I'm just not sure about index creation at the mo.
thank you very much.
showery
EXT-Brannan, Pat wrote:
Do you want to run it embedded or start a no-kidding network server? The documentation for using it as an embedded server is reasonably good, but I could send you and example if you want.
-----Original Message----- From: showery [mailto:[EMAIL PROTECTED] Sent: Thursday, January 15, 2004 8:55 AM To: [email protected] Subject: how to startup the xindice server in a java program
Hi,
I am a beginner in Xindice and not a skilled programmer in java. I am trying to write something like a Xindice GUI.The first problem which I encounterd is how to startup the Xindice server in the java program but not in a command line. In javadoc I find a class named Shutdown, but nothing about the startup of the server. and in startup.bat, they use the class org.apache.xindice.server.Xindice. but that is a abstract class. I don't know how it worked.
How could I startup the xindice server in a java program not in a command line?
Any suggestions would be appreciated.
showery
Regards, Upayavira
package com.xxxxxx.util;
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.List;
import org.apache.xindice.client.xmldb.embed.DatabaseImpl; import org.apache.xindice.client.xmldb.services.CollectionManager; import org.apache.xindice.xml.dom.DocumentImpl; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xmldb.api.DatabaseManager; import org.xmldb.api.base.Collection; import org.xmldb.api.base.Resource; import org.xmldb.api.base.XMLDBException; import org.xmldb.api.modules.CollectionManagementService;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a>
* @version CVS $Id: XMLDBUtil.java,v 1.3 2004/01/14 22:22:38 upayavira Exp $
*/
public class XMLDBUtil {
private static final String dbPath = "xmldb:xindice-embed:///db/"; public static final String XMLDBAPIVERSION = "1.0";
public static void initializeDatabase() throws Exception {
DatabaseManager.registerDatabase(new DatabaseImpl());
}public static void deleteCollection(String base, String name, List report) throws XMLDBException {
Collection collection = null;
try {
collection = DatabaseManager.getCollection(dbPath + base + "/" + name);
} catch (XMLDBException e) {
report.add("Collection does not exist - not deleted: " + base + "/" + name);
return;
}
if (collection != null) {
Collection parentCollection = DatabaseManager.getCollection(dbPath + base);
CollectionManagementService service =
(CollectionManagementService) parentCollection.getService("CollectionManagementService", "1.0");
service.removeCollection(name);
report.add("Removed collection " + base + "/" + name);
} else {
report.add("Collection does not exist - not deleted: " + base + "/" + name);
}
}
public static Collection createCollection(String base, String name, List report) throws XMLDBException {
Collection parentCollection = DatabaseManager.getCollection(dbPath + base);
CollectionManagementService service =
(CollectionManagementService) parentCollection.getService("CollectionManagementService", "1.0");
Collection collection = service.createCollection(name);
report.add("Added collection "+ base + "/" + name);
return collection;
}
public static void addResources(String rootPath, Collection collection, String resourcePath, List report) throws Exception{
String[] resources = new File(rootPath, resourcePath).list();
if (resources == null) {
report.add("No resources in folder: " + rootPath + "/" + resourcePath);
return;
}
for (int i=0; i<resources.length; i++) {
String resource = (String) resources[i];
if (resource.endsWith(".xml")) {
String content = getFileContent(rootPath + "/" + resourcePath, resource);
addResource(collection, resource, content, report);
}
}
}
public static void addResource(Collection collection, String name, String content, List report) throws XMLDBException {
try {
Resource resource = collection.createResource(name, "XMLResource");
resource.setContent(content);
collection.storeResource(resource);
report.add("Added document " + name);
} catch (XMLDBException e) {
report.add("Failed to add document " + name + ":" + e.getMessage());
}
}
private static String getFileContent(String path, String filename) throws FileNotFoundException, IOException {
StringBuffer b = new StringBuffer();
BufferedReader reader = new BufferedReader(new FileReader(new File(path, filename)));
String line;
while ((line = reader.readLine())!=null) {
b.append(line);
}
reader.close();
return b.toString();
}
public static void createIndex(Collection collection, String name, String pattern, List report) throws XMLDBException {
CollectionManager manager = (CollectionManager) collection.getService("CollectionManager", XMLDBAPIVERSION);
Document doc = new DocumentImpl();
Element idxEle = doc.createElement("index");
idxEle.setAttribute("class", "org.apache.xindice.core.indexer.ValueIndexer");
idxEle.setAttribute("name", name);
idxEle.setAttribute("pattern", pattern);
doc.appendChild(idxEle);
manager.createIndexer(doc);
report.add("Created index named " + name + " for " + pattern);
}
}
