# JMX und ServiceMBean - Zugriff mittels Client



## tonick (4. Jul 2011)

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

```
Collection col = ???
```
stehen haben. Hiermit sollten sich dann XML-Dateien einfügen lassen, oder?

ServiceMBean:

```
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();
}
```


```
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;
	}
}
```


----------



## tonick (4. Jul 2011)

Soweit bin ich mittlerweile:


```
public static void main(String args[]) throws Exception {

		JMXServiceURL u = new JMXServiceURL(
				"service:jmx:rmi://127.0.0.1/jndi/rmi://127.0.0.1:1090/jmxconnector");
		JMXConnector c = JMXConnectorFactory.connect(u);
		MBeanServerConnection server = c.getMBeanServerConnection();

		ObjectName name = new ObjectName("eXist:type=Service,category=Database,name=XMLDB");
		System.out.println(server.getAttribute(name, "BaseCollectionURI"));
	}
```

Wie komme ich an die BaseCollection? Ist das überhaupt der richtige Weg?


----------



## tonick (5. Jul 2011)

damit es bei anderen schneller geht:


```
Context ctx = new InitialContext();
XmlDbServiceMBean xmlDbService = (XmlDbServiceMBean) ctx.lookup(XmlDbService.class.getName());
Collection baseCol = xmlDbService.getBaseCollection();
```

eigentlich logisch ...

local reicht mir erstmal, wie man remote drankommt weiß ich nicht


----------

