Mit untenstehender ServiceMBean soll der Zugriff auf eine XML-Datenbank über xmldb realisiert werden. Meine Frage wäre nun, wie ich an die Collection (Methode getBaseCollection()) komme bzw. wie ein entsprechender Client aussehen muss?
In meinem Client muss ich ja irgendwann mal so was wie
stehen haben. Hiermit sollten sich dann XML-Dateien einfügen lassen, oder?
ServiceMBean:
In meinem Client muss ich ja irgendwann mal so was wie
Code:
Collection col = ???
ServiceMBean:
Code:
import javax.ejb.Remote;
import org.jboss.system.ServiceMBean;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;
@Remote
public interface XmlDbServiceMBean extends ServiceMBean{
public String getDriver();
public void setDriver(String driver);
public String getBaseCollectionURI();
public void setBaseCollectionURI(String baseCollectionURI);
public Collection getBaseCollection() throws XMLDBException;
public void setEncoding(String encode);
public String getEncoding();
}
Code:
import org.jboss.naming.NonSerializableFactory;
import org.jboss.system.ServiceMBeanSupport;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Arrays;
@Stateless
public class XmlDbService extends ServiceMBeanSupport implements
XmlDbServiceMBean {
private String baseCollectionURI;
private String driver;
private Collection baseCollection;
private String encoding = "ISO-8859-1";// "UTF-8";
public void setEncoding(String encode) {
this.encoding = encode;
}
public String getEncoding() {
return this.encoding;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getBaseCollectionURI() {
return baseCollectionURI;
}
public void setBaseCollectionURI(String baseCollectionURI) {
this.baseCollectionURI = baseCollectionURI;
}
public Collection getBaseCollection() throws XMLDBException {
baseCollection = DatabaseManager.getCollection(baseCollectionURI);
return baseCollection;
}
protected void startService() throws Exception {
log = org.jboss.logging.Logger
.getLogger("XmlDbService");
Context context = new InitialContext();
Class c = Class.forName(driver);
Database database = (Database) c.newInstance();
DatabaseManager.registerDatabase(database);
database.setProperty("create-database", "true");
baseCollection = getBaseCollection();
baseCollection.setProperty("encoding", encoding);
log.info("Got base Collection");
NonSerializableFactory.rebind(context, this.getClass().getName(), this);
String[] collections = baseCollection.listChildCollections();
log.info("ChildCollections " + Arrays.asList(collections));
}
protected void stopService() throws Exception {
NonSerializableFactory.unbind(this.getClass().getName());
if (baseCollection != null) {
baseCollection.close();
log.info("Closed base (db) collection");
}
}
public static CollectionManagementService getCollectionManagementService(
Collection parentCollection) throws XMLDBException {
CollectionManagementService service = (CollectionManagementService) parentCollection
.getService("CollectionManagementService", "1.0");
return service;
}
}