| Method from org.jboss.ha.jndi.DetachedHANamingService Detail: |
protected void createService() throws Exception {
if (this.clusterPartition == null)
{
throw new IllegalStateException("HAPartition property must be set before starting HAJNDI service");
}
if (this.cache == null)
{
throw new IllegalStateException("ClusteredCache property must be set before starting HAJNDI service");
}
this.log.debug("Initializing HAJNDI server on partition: " + this.clusterPartition.getPartitionName());
// Start HAJNDI service
this.theServer = new HAJNDI(this.clusterPartition, this.cache);
// Build the Naming interface method map
Map< Long, Method > map = new HashMap< Long, Method >(13);
Method[] methods = Naming.class.getMethods();
for (Method method: methods)
{
Long hash = new Long(MarshalledInvocation.calculateHash(method));
map.put(hash, method);
}
this.marshalledInvocationMapping = Collections.unmodifiableMap(map);
// share instance for in-vm discovery
NamingContext.setHANamingServerForPartition(this.clusterPartition.getPartitionName(), this.theServer);
}
|
protected void destroyService() throws Exception {
this.log.debug("Destroying the HAJNDI service");
// server may be null if service failed on startup
if (this.theServer != null)
{
this.theServer.destroy();
}
}
|
protected HAPartition findHAPartitionWithName(String name) {
HAPartition result = null;
// Class name match does not work with the AOP proxy :(
// QueryExp classEQ = Query.eq(Query.classattr(),
// Query.value(ClusterPartition.class.getName()));
QueryExp matchName = Query.match(Query.attr("Name"),
Query.value("ClusterPartition"));
QueryExp matchPartitionName = Query.match(Query.attr("PartitionName"),
Query.value(name));
QueryExp exp = Query.and(matchName, matchPartitionName);
Set< ? > mbeans = this.getServer().queryMBeans(null, exp);
if (mbeans != null && mbeans.size() > 0)
{
for (Object mbean: mbeans)
{
ObjectInstance inst = (ObjectInstance) mbean;
try
{
ClusterPartitionMBean cp =
(ClusterPartitionMBean) MBeanProxyExt.create(
ClusterPartitionMBean.class,
inst.getObjectName(),
this.getServer());
result = cp.getHAPartition();
break;
}
catch (Exception e)
{
// Ignore
}
}
}
return result;
}
|
public String getAutoDiscoveryAddress() {
return this.adGroupAddress;
}
|
public String getAutoDiscoveryBindAddress() {
return (this.discoveryBindAddress != null) ? this.discoveryBindAddress.getHostAddress() : null;
}
|
public int getAutoDiscoveryGroup() {
return this.adGroupPort;
}
|
public int getAutoDiscoveryTTL() {
return this.autoDiscoveryTTL;
}
|
public int getBacklog() {
return this.backlog;
}
|
public String getBindAddress() {
String address = null;
if (this.bindAddress != null)
{
address = this.bindAddress.getHostAddress();
}
return address;
}
|
public Cache getClusteredCache() {
return this.cache;
}
|
public boolean getDiscoveryDisabled() {
return this.discoveryDisabled;
}
|
public HAPartition getHAPartition() {
return this.clusterPartition;
}
|
public Map getMethodMap() {
return this.marshalledInvocationMapping;
}
Expose the Naming service interface mapping as a read-only attribute |
protected Naming getNamingProxy() throws Exception {
return (Naming) this.server.getAttribute(this.proxyFactory, "Proxy");
}
Get the Naming proxy for the transport. This version looks up the
proxyFactory service Proxy attribute. Subclasses can override this to set
the proxy another way. |
public String getPartitionName() {
return this.clusterPartition.getPartitionName();
}
|
public int getPort() {
return this.port;
}
|
public ObjectName getProxyFactoryObjectName() {
return this.proxyFactory;
}
|
protected void initBootstrapListener() {
// Start listener
try
{
// Get the default ServerSocketFactory is one was not specified
if (this.jnpServerSocketFactory == null)
{
this.jnpServerSocketFactory = ServerSocketFactory.getDefault();
}
this.bootstrapSocket = this.jnpServerSocketFactory.createServerSocket(this.port, this.backlog, this.bindAddress);
// If an anonymous port was specified get the actual port used
if (this.port == 0)
{
this.port = this.bootstrapSocket.getLocalPort();
}
String msg = "Started HAJNDI bootstrap; jnpPort=" + this.port
+ ", backlog=" + this.backlog + ", bindAddress=" + this.bindAddress;
this.log.info(msg);
}
catch (IOException e)
{
this.log.error("Could not start HAJNDI bootstrap listener on port " + this.port, e);
}
if (this.lookupPool == null)
{
this.lookupPool = new BasicThreadPool("HANamingBootstrap Pool");
}
AcceptHandler handler = new AcceptHandler();
this.lookupPool.run(handler);
}
Bring up the bootstrap lookup port for obtaining the naming service proxy |
public Object invoke(Invocation invocation) throws Exception {
// Set the method hash to Method mapping
if (invocation instanceof MarshalledInvocation)
{
MarshalledInvocation mi = (MarshalledInvocation) invocation;
mi.setMethodMap(this.marshalledInvocationMapping);
}
// Invoke the Naming method via reflection
Method method = invocation.getMethod();
Object[] args = invocation.getArguments();
Object value = null;
try
{
value = method.invoke(this.theServer, args);
}
catch (InvocationTargetException e)
{
Throwable t = e.getTargetException();
if (t instanceof Exception)
{
throw (Exception) t;
}
throw new UndeclaredThrowableException(t, method.toString());
}
return value;
}
Expose the Naming service via JMX to invokers. |
public void setAutoDiscoveryAddress(String adAddress) {
this.adGroupAddress = adAddress;
}
|
public void setAutoDiscoveryBindAddress(String address) throws UnknownHostException {
this.discoveryBindAddress = InetAddress.getByName(address);
}
|
public void setAutoDiscoveryGroup(int adGroup) {
this.adGroupPort = adGroup;
}
|
public void setAutoDiscoveryTTL(int ttl) {
this.autoDiscoveryTTL = ttl;
}
|
public void setBacklog(int backlog) {
if (backlog < = 0)
{
backlog = 50;
}
this.backlog = backlog;
}
|
public void setBindAddress(String host) throws UnknownHostException {
this.bindAddress = InetAddress.getByName(host);
}
|
public void setClusteredCache(Cache cache) {
this.cache = cache;
}
|
public void setDiscoveryDisabled(boolean disable) {
this.discoveryDisabled = disable;
}
|
public void setHAPartition(HAPartition clusterPartition) {
this.clusterPartition = clusterPartition;
}
|
public void setJNPServerSocketFactory(String factoryClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
this.jnpServerSocketFactoryName = factoryClassName;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class< ? > clazz = loader.loadClass(this.jnpServerSocketFactoryName);
this.jnpServerSocketFactory = (ServerSocketFactory) clazz.newInstance();
}
|
public void setLookupPool(BasicThreadPoolMBean poolMBean) {
this.lookupPool = poolMBean.getInstance();
}
|
public void setPort(int p) {
this.port = p;
}
|
public void setProxyFactoryObjectName(ObjectName proxyFactory) {
this.proxyFactory = proxyFactory;
}
|
protected void startService() throws Exception {
this.log.debug("Obtaining the HAJNDI transport proxy");
this.stub = this.getNamingProxy();
this.theServer.setHAStub(this.stub);
if (this.port >= 0)
{
this.log.debug("Starting HAJNDI bootstrap listener");
this.initBootstrapListener();
}
// Automatic Discovery for unconfigured clients
if (this.adGroupAddress != null && this.discoveryDisabled == false)
{
try
{
this.autoDiscovery = new AutomaticDiscovery();
this.autoDiscovery.start();
this.lookupPool.run(this.autoDiscovery);
}
catch (Exception e)
{
this.log.warn("Failed to start AutomaticDiscovery", e);
}
}
this.log.debug("initializing HAJNDI");
this.theServer.init();
}
|
protected void stopService() throws Exception {
// un-share instance for in-vm discovery
NamingContext.removeHANamingServerForPartition(this.clusterPartition.getPartitionName());
// Stop listener
ServerSocket s = this.bootstrapSocket;
this.bootstrapSocket = null;
if (s != null)
{
this.log.debug("Closing the HAJNDI bootstrap listener");
s.close();
}
// Stop HAJNDI service
this.log.debug("Stopping the HAJNDI service");
this.theServer.stop();
this.log.debug("Stopping AutomaticDiscovery");
if (this.autoDiscovery != null && this.discoveryDisabled == false)
{
this.autoDiscovery.stop();
}
}
|