[jboss-cvs] jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/service ...
Norman Richards
norman.richards at jboss.com
Wed Nov 1 13:14:22 EST 2006
User: nrichards
Date: 06/11/01 13:14:22
Added: jbossas/j2ee/examples/src/main/org/jboss/book/security/service
DistributedCacheService.java
DistributedCacheServiceMBean.java
SecurityConfig.java SecurityConfigMBean.java
Log:
modified for j2ee guide
Revision Changes Path
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/service/DistributedCacheService.java
Index: DistributedCacheService.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.book.security.service;
import java.lang.reflect.Method;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.Vector;
import javax.naming.InitialContext;
import javax.naming.Name;
import org.jboss.ha.framework.interfaces.DistributedState;
import org.jboss.ha.framework.interfaces.DistributedState.DSListenerEx;
import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.ha.framework.interfaces.HAPartition.HAMembershipListener;
import org.jboss.ha.framework.server.util.DistributedTimedCachePolicy;
import org.jboss.naming.NonSerializableFactory;
import org.jboss.system.ServiceMBeanSupport;
/** The cluster parition membership monitor and distributed cache
test mbean implementation
@author Scott.Stark at jboss.org
@version $Revision: 1.1 $
*/
public class DistributedCacheService extends ServiceMBeanSupport
implements DistributedCacheServiceMBean, HAMembershipListener, DSListenerEx
{
private String jndiName;
private String partitionName = "Default";
private HAPartition partition;
private DistributedState map;
private DistributedTimedCachePolicy cache;
private int cacheTimeout = 1800;
// --- Begin ServiceMBeanSupport overriden methods
protected void startService() throws Exception
{
InitialContext ctx = new InitialContext();
String partitionJndiName = "/HAPartition/" + partitionName;
partition = (HAPartition) ctx.lookup(partitionJndiName);
map = partition.getDistributedStateService();
cache = new DistributedTimedCachePolicy(jndiName, partitionName, cacheTimeout);
cache.create();
cache.start();
// Bind the cache into JNDI
Name name = ctx.getNameParser("").parse(jndiName);
NonSerializableFactory.rebind(name, cache, true);
// Register as a listener of cache changes
map.registerDSListenerEx(jndiName, this);
log.info("Registered as DMListener for category="+jndiName);
// Register as a listener of cluster membership changes
partition.registerMembershipListener(this);
log.info("Registered as MembershipListener");
}
protected void stopService() throws Exception
{
cache.stop();
map.unregisterDSListenerEx(jndiName, this);
partition.unregisterMembershipListener(this);
}
// --- End ServiceMBeanSupport overriden methods
// --- Begin DistributedCacheServiceMBean interface methods
public String getPartitionName()
{
return partitionName;
}
public void setPartitionName(String name)
{
this.partitionName = name;
}
public String getCacheJndiName()
{
return jndiName;
}
public void setCacheJndiName(String name)
{
this.jndiName = name;
}
public int getCacheTimeout()
{
return cacheTimeout;
}
public void setCacheTimeout(int timeoutSecs)
{
this.cacheTimeout = timeoutSecs;
}
public Vector getClusterNodes()
{
Vector view = null;
try
{
InitialContext ctx = new InitialContext();
String jndiName = "/HAPartition/" + partitionName;
HAPartition partition = (HAPartition) ctx.lookup(jndiName);
view = partition.getCurrentView();
}
catch(Exception e)
{
log.error("Failed to access HAPartition state", e);
}
return view;
}
public Object get(String key)
{
return cache.get(key);
}
public void set(String key, String value)
{
cache.insert(key, value);
}
// --- End DistributedCacheServiceMBean interface methods
// --- Begin HAMembershipListener interface methods
/** Called when a new partition topology occurs.
* @param deadMembers A list of nodes that have died since the previous view
* @param newMembers A list of nodes that have joined the partition since
* the previous view
* @param allMembers A list of nodes that built the current view
*/
public void membershipChanged(Vector deadMembers, Vector newMembers, Vector allMembers)
{
log.info("DeadMembers: size="+deadMembers.size());
for(int m = 0; m < deadMembers.size(); m ++)
{
AddressPort addrInfo = getMemberAddress(deadMembers.get(m));
log.info(addrInfo);
}
log.info("NewMembers: size="+newMembers.size());
for(int m = 0; m < newMembers.size(); m ++)
{
AddressPort addrInfo = getMemberAddress(newMembers.get(m));
log.info(addrInfo);
}
log.info("AllMembers: size="+allMembers.size());
for(int m = 0; m < allMembers.size(); m ++)
{
AddressPort addrInfo = getMemberAddress(allMembers.get(m));
log.info(addrInfo);
}
}
// --- End HAMembershipListener interface methods
// --- Begin DMListener interface methods
public void valueHasChanged(String category, Serializable key,
Serializable value, boolean locallyModified)
{
log.info("valueHasChanged, category="+category+", key="+key
+", value="+value+", locallyModified="+locallyModified);
}
public void keyHasBeenRemoved (String category, Serializable key,
Serializable value, boolean locallyModified)
{
log.info("keyHasBeenRemoved, category="+category+", key="+key
+", value="+value+", locallyModified="+locallyModified);
}
// --- End DMListener interface methods
/** Use reflection to access the address InetAddress and port if they exist
* in the Address implementation
*/
private AddressPort getMemberAddress(Object addr)
{
AddressPort info = null;
try
{
Class[] parameterTypes = {};
Object[] args = {};
Method getIpAddress = addr.getClass().getMethod("getIpAddress", parameterTypes);
InetAddress inetAddr = (InetAddress) getIpAddress.invoke(addr, args);
Method getPort = addr.getClass().getMethod("getPort", parameterTypes);
Integer port = (Integer) getPort.invoke(addr, args);
info = new AddressPort(inetAddr, port);
}
catch(Exception e)
{
// Try to access the address info from the getName -> addr:port string
try
{
Class[] parameterTypes = {};
Object[] args = {};
Method getName = addr.getClass().getMethod("getName", parameterTypes);
String id = (String) getName.invoke(addr, args);
String addrString = id;
Integer port = null;
int colon = id.indexOf(':');
if( colon > 0 )
{
addrString = id.substring(0, colon);
port = Integer.valueOf(id.substring(colon+1));
}
InetAddress inetAddr = InetAddress.getByName(addrString);
info = new AddressPort(inetAddr, port);
}
catch(Exception e2)
{
log.warn("Failed to obtain InetAddress/port from addr: "+addr, e);
}
}
return info;
}
static class AddressPort
{
InetAddress addr;
Integer port;
AddressPort(InetAddress addr, Integer port)
{
this.addr = addr;
this.port = port;
}
public String toString()
{
return "{host("+addr+"), port("+port+")}";
}
}
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/service/DistributedCacheServiceMBean.java
Index: DistributedCacheServiceMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.book.security.service;
import java.util.Vector;
import org.jboss.system.Service;
/** A utility mbean that monitors membership of a cluster parition and
* establishes a DistributedTimedCachePolicy
* @author Scott.Stark at jboss.org
* @version $Revision: 1.1 $
*/
public interface DistributedCacheServiceMBean extends Service
{
/** Get the cluster parition name the mbean is monitoring
*/
public String getPartitionName();
/** Set the cluster parition name the mbean is monitoring
*/
public void setPartitionName(String name);
/** Get the JNDI name under which the CachePolicy is bound into JNDI
*/
public String getCacheJndiName();
/** Set the JNDI name under which the CachePolicy is bound into JNDI
*/
public void setCacheJndiName(String name);
/** Get the cache timeout (seconds)
*/
public int getCacheTimeout();
/** Set the cache timeout (seconds)
*/
public void setCacheTimeout(int timeoutSecs);
/** Get the current cluster parition membership info
*@return a Vector of org.javagroups.Address implementations, for example,
*org.javagroups.stack.IpAddress
*/
public Vector getClusterNodes();
/** Get a value from the cache policy
*/
public Object get(String key);
/** Set a value in the cache policy
*/
public void set(String key, String value);
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/service/SecurityConfig.java
Index: SecurityConfig.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.book.security.service;
import java.net.URL;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.jboss.mx.util.MBeanProxy;
import org.jboss.security.auth.login.XMLLoginConfigMBean;
import org.jboss.system.ServiceMBeanSupport;
/** A security config mbean that loads an xml login configuration and
pushes a XMLLoginConfig instance onto the the config stack managed by
the SecurityConfigName mbean(default=jboss.security:service=XMLLoginConfig).
@author Scott.Stark at jboss.org
@version $Revision: 1.1 $
*/
public class SecurityConfig extends ServiceMBeanSupport
implements SecurityConfigMBean
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
private String authConf = "login-config.xml";
private String[] loadedDomains;
private ObjectName mainSecurityConfig;
private XMLLoginConfigMBean config;
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
public SecurityConfig()
{
setSecurityConfigName("jboss.security:service=XMLLoginConfig");
}
public String getName()
{
return "JAAS Login Config";
}
public String getSecurityConfigName()
{
return mainSecurityConfig.toString();
}
public void setSecurityConfigName(String objectName)
{
try
{
mainSecurityConfig = new ObjectName(objectName);
}
catch(Exception e)
{
log.error("Failed to create ObjectName", e);
}
}
/** Get the resource path to the JAAS login configuration file to use.
*/
public String getAuthConfig()
{
return authConf;
}
/** Set the resource path to the JAAS login configuration file to use.
The default is "login-config.xml".
*/
public void setAuthConfig(String authConf)
{
this.authConf = authConf;
}
/** Start the service. This entails locating the AuthConfig resource and
* then loading this into the XMLLoginConfigMBean given by the
* SecurityConfigName mbean.
*/
protected void startService() throws Exception
{
// Look for the authConf as resource
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL loginConfig = loader.getResource(authConf);
if( loginConfig != null )
{
log.info("Using JAAS AuthConfig: "+loginConfig.toExternalForm());
config = (XMLLoginConfigMBean) MBeanProxy.get(XMLLoginConfigMBean.class,
mainSecurityConfig, server);
loadedDomains = config.loadConfig(loginConfig);
}
else
{
log.warn("No AuthConfig resource found");
}
}
protected void stopService() throws Exception
{
if( loadedDomains != null )
config.removeConfigs(loadedDomains);
}
}
1.1 date: 2006/11/01 18:14:22; author: nrichards; state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/service/SecurityConfigMBean.java
Index: SecurityConfigMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.book.security.service;
import org.jboss.system.ServiceMBean;
/** An mbean interface for a config service that pushes an xml based
javax.security.auth.login.Configuration onto the config stack managed by
the mbean whose name is given by the SecurityConfigName attribute.
@see org.jboss.security.plugins.SecurityConfigMBean
@author Scott.Stark at jboss.org
@version $Revision: 1.1 $
*/
public interface SecurityConfigMBean extends ServiceMBean
{
/** Get the classpath resource name of the security configuration file */
public String getAuthConfig();
/** Set the classpath resource name of the security configuration file */
public void setAuthConfig(String configURL);
/** Get the name of the SecurityConfig mbean whose pushLoginConfig and
popLoginConfig ops will be used to install and remove the xml login config*/
public String getSecurityConfigName();
/** Set the name of the SecurityConfig mbean whose pushLoginConfig and
popLoginConfig ops will be used to install and remove the xml login config*/
public void setSecurityConfigName(String objectName);
}
More information about the jboss-cvs-commits
mailing list