33.6. The JoramDistributionService

The only remaining piece to describe is the JoramDistributionService itself. Here it is. As mentioned, we do not use topics in our system; adding code to handle topic permission and binding would be completely straightforward.

package com.nimblefish.sdk.jonas;

import org.objectweb.jonas.service.Service;
import org.objectweb.jonas.service.ServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Enumeration;
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

import javax.naming.Context;
import javax.jms.Destination;

import fr.dyade.aaa.joram.admin.AdminItf;

/**
* This class implements the JOnAS service interface and performs
* JOnAS-startup-time configuration actions relating to distributed JORAM servers.
*
* It uses a properties file named "joramdist.properties" to configure its activity;
* this configuration file must be in $JONAS_BASE/conf (which is part of the JOnAS
* classpath, and hence is reachable as a classloader resource from this class.)
* This of course can be changed at your discretion.
*
* See http://jonas.objectweb.org/current/doc/Services.html
*
* Written by Rob Jellinghaus (robj at nimblefish dot com) on 11 February 2004.
* Thanks very much to Frederic Maistre (frederic.maistre at objectweb dot org)
* for his indispensable and voluminous help.
* This file is hereby placed into the public domain for use by JOnAS users at their
* sole discretion; please include this comment text in your uses of this code.
*/
public class JoramDistributionService implements Service {
private static Log log = LogFactory.getLog(JoramDistributionService.class);

private boolean createAnonUser = false;
private int joramPort = -1;
private int joramInstance = -1;
private String joramBindHost = null;
private String[] joramBindQueues = null;

public void init(Context context) throws ServiceException {
log.info("JoramDistributionService initializing");
try {
InputStream propStream = JoramDistributionService.class.getClassLoader(!
.getResourceAsStream("joramdist.properties");
Properties joramProperties = null;
joramProperties = new Properties();
joramProperties.load(propStream);
Enumeration props2 = joramProperties.propertyNames();
while (props2.hasMoreElements()) {
String s = (String) props2.nextElement();
log.info("joram.properties property: "+s+": "+joramProperties.getProperty(s));
}

if (joramProperties.containsKey("joram.createanonuser")
&& joramProperties.get("joram.createanonuser").equals("true")) {
createAnonUser = true;
}

if (joramProperties.containsKey("joram.port")) {
joramPort = Integer.parseInt(joramProperties.getProperty("joram.port"));
}

if (joramProperties.containsKey("joram.instance")) {
  joramInstance = Integer.parseInt(joramProperties.getProperty("joram.instance"));
}

if (joramProperties.containsKey("joram.bindremotehost")) {
joramBindHost = joramProperties.getProperty("joram.bindremotehost");
}

if (joramProperties.containsKey("joram.bindremotequeues")) {
joramBindQueues = joramProperties.getProperty("joram.bindremotequeues").split(",");
}

} catch (IOException e) {
throw new ServiceException("Could not initialize JoramDistributionService", e);
}

}

public void start() throws ServiceException {
started = true;

if (joramPort == -1 && joramInstance == -1)  {
log.info("No joram.port and/or joram.instance defined; performing no JORAM configuration.");
return;
}

try {

if (joramPort != -1) {
AdminItf admin = new fr.dyade.aaa.joram.admin.AdminImpl();
admin.connect("localhost", joramPort, "root", "root", 60);

if (createAnonUser) {
log.info("Creating JORAM anonymous user on localhost:"+joramPort+
" for instance "+joramInstance+"...");
admin.createUser("anonymous", "anonymous", joramInstance);
}

log.info("Created JORAM anonymous user.");

if (joramBindHost != null && joramBindQueues != null) {
log.info("Looking up JNDI queues from rmi://"+joramBindHost+":1099");
javax.naming.Context jndiCtx;

java.util.Hashtable env = new java.util.Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://"+joramBindHost+":1099");
jndiCtx = new javax.naming.InitialContext(env);

Object[] remoteTopics = new Object[joramBindQueues.length];
for (int i = 0; i < joramBindQueues.length; i++) {
String joramBindQueue = joramBindQueues[i];
remoteTopics[i] = jndiCtx.lookup(joramBindQueue);
log.debug("Got queue "+joramBindQueue+": "+remoteTopics[i]);

// open up all topics to everyone
admin.setFreeReading((Destination)remoteTopics[i]);
admin.setFreeWriting((Destination)remoteTopics[i]);
}

// if we are on local host, don't rebind
if (!joramBindHost.equals("localhost")) {
env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
jndiCtx = new javax.naming.InitialContext(env);

           for (int i = 0; i < joramBindQueues.length; i++) {
jndiCtx.bind(joramBindQueues[i], remoteTopics[i]);
log.debug("Bound "+joramBindQueues[i]+" in localhost context");
}
}
}

// Disconnecting the administrator.
admin.disconnect();
}

log.info("Completed JoramDistributionService startup successfully.");
} catch (Exception e) {
throw new ServiceException("Could not start JoramDistributionService", e);
}
}

private boolean started = false;
private String name;

public void stop() throws ServiceException {
started = false;
log.info("JoramDistributionService stopped");
}

public boolean isStarted() {
return started;
}

public void ! getName(String s) {
name = s;
}

public String getName() {
return name;
}
}

This needs to be built with a simple task that just compiles the class and places it in a JAR file, which then must be placed in the $JONAS_ROOT/lib/ext or $JONAS_BASE/lib/ext directories on each server before launching JOnAS.