User: user57
Date: 01/05/03 15:39:55
Modified: src/main/org/jboss/ejb/plugins/jrmp/interfaces
InitialContextHandle.java
Added: src/main/org/jboss/ejb/plugins/jrmp/interfaces
DefaultInitialContextHandle.java
InitialContextHandleFactory.java
PropertiesInitialContextHandle.java
Log:
o Modified InitialContextHandle.create() to use an InitialContextHandleFactory
instance, which is pluggable via properties.
o Created a PropertiesInitialContextHandle, which provides the old behavior
of reading from a properties file.
o Added DefaultInitialContextHandle, which currently assumes the usage of
the default jnp naming server and asks the Naming service for the port
number when constructing the provider url.
o Removed configuration properties and configuration file for handles. The
default behavior will provide handles that remember with no extra
configuration.
Revision Changes Path
1.2 +62 -79
jboss/src/main/org/jboss/ejb/plugins/jrmp/interfaces/InitialContextHandle.java
Index: InitialContextHandle.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/jrmp/interfaces/InitialContextHandle.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- InitialContextHandle.java 2001/05/02 03:04:14 1.1
+++ InitialContextHandle.java 2001/05/03 22:39:54 1.2
@@ -6,98 +6,94 @@
*/
package org.jboss.ejb.plugins.jrmp.interfaces;
-import java.util.Hashtable;
-import java.util.Properties;
import java.io.Serializable;
-import java.io.InputStream;
-import java.net.URL;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
- * A simple handle to facilitate serialization of an initial context.
+ * Provides the interface for creating new handles instances and
+ * for getting a reference to the initial context.
*
* @author Jason Dillon <a
href="mailto:[EMAIL PROTECTED]"><[EMAIL PROTECTED]></a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
-public class InitialContextHandle
+public abstract class InitialContextHandle
implements Serializable
{
/** Serial Version Identifier. */
- private static final long serialVersionUID = 5716858030389936723L;
-
- /**
- * The property name, which is a URL spec of a properties file to read
- */
- public static final String ENV_PROPERTIES =
- InitialContextHandle.class.getName() + ".environment";
+ private static final long serialVersionUID = 8271304971930101243L;
- /** The single instance. */
- private static InitialContextHandle instance = null;
+ /** The factory for producing handles. */
+ private static InitialContextHandleFactory factory = null;
/**
- * Factory method for producting state objects.
- *
- * @return A state object.
+ * Lookup the class of the factory that will be used to construct
+ * new handle instances.
*/
- public static InitialContextHandle create() {
- //
- // This should do the right thing *most* of the time with respect
- // to multi-threadded access. If there is a concurrency problem
- // then more than one object will be created, but they should be
- // the same, so rather than sync lets just let that happen.
- //
- if (instance == null) {
- instance = new InitialContextHandle();
- }
+ private static Class getFactoryType() {
+ String propname = InitialContextHandle.class.getName() + ".factory";
+ String classname = System.getProperty(propname, null);
+
+ Class type;
+ if (classname != null) {
+ try {
+ type = Class.forName(classname);
+ }
+ catch (ClassNotFoundException e) {
+ throw new RuntimeException
+ ("invalid factory class name: " + classname);
+ }
- return instance;
+ // check if the given class is the correct type before
+ // attempting to construct a new object
+ if (! InitialContextHandleFactory.class.isAssignableFrom(type)) {
+ throw new RuntimeException("does not implement: " +
+ InitialContextHandleFactory.class);
+ }
+ }
+ else {
+ type = DefaultInitialContextHandle.Factory.class;
+ }
+
+ return type;
}
- /** The InitialContext enviroment (or null if unable to determine) */
- private Hashtable env;
-
/**
- * Construct a <tt>InitialContextHandle</tt>.
+ * Construct a new factory if one has not been created yet.
*/
- public InitialContextHandle() {
- // save the current enviroment
- env = getEnvironment();
- }
+ private static synchronized void createFactory() {
+ if (factory != null) return;
+
+ // get the type of factory that will be used
+ Class type = getFactoryType();
+ // create a new instance
+ try {
+ factory = (InitialContextHandleFactory)type.newInstance();
+ }
+ catch (Exception e) {
+ if (e instanceof RuntimeException)
+ throw (RuntimeException)e;
+
+ // should really use a nesting exception here to preserve
+ // the target throwables detail.
+ throw new RuntimeException("failed to construct factory: " + e);
+ }
+ }
+
/**
- * Get the environment table suitable for passing to an initial context
- * or null if we should use a vanilla one.
+ * Factory method for producting state objects.
*
- * <p>Checks for a non-null value for the system property
- * {@link #ENV_PROPERTIES}. If it finds one, it assumes that
- * it is a url specification which can be used to populate a
- * properties table.
+ * @return A state object.
*/
- private Hashtable getEnvironment() {
- String spec = System.getProperty(ENV_PROPERTIES);
-
- try {
- if (spec != null) {
- URL url = new URL(spec);
- InputStream input = url.openStream();
- Properties props;
-
- try {
- props = new Properties();
- props.load(input);
- }
- finally {
- input.close();
- }
-
- return props;
- }
+ public static InitialContextHandle create() {
+ // lazy initialize the factory
+ if (factory == null) {
+ createFactory();
}
- catch (Exception ignore) {}
- return null;
+ return factory.create();
}
/**
@@ -107,18 +103,5 @@
*
* @throws NamingException Failed to create <tt>InitialContext</tt>.
*/
- public InitialContext getInitialContext() throws NamingException {
- InitialContext ctx;
-
- // if the environment is not null, then use it else
- // assume there is a system property set.
- if (env != null) {
- ctx = new InitialContext(env);
- }
- else {
- ctx = new InitialContext();
- }
-
- return ctx;
- }
+ public abstract InitialContext getInitialContext() throws NamingException;
}
1.1
jboss/src/main/org/jboss/ejb/plugins/jrmp/interfaces/DefaultInitialContextHandle.java
Index: DefaultInitialContextHandle.java
===================================================================
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ejb.plugins.jrmp.interfaces;
import java.util.Hashtable;
import java.util.List;
import java.io.Serializable;
import java.net.InetAddress;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import org.jboss.naming.NamingServiceMBean;
/**
* The default implementaion of {@link InitialContextHandle}.
*
* <p>Attempts to discover the requried enviroment properties by inspecting
* the current system. This implementation is currently specific to
* the <tt>org.jnp.*</tt> naming classes.
*
* @author Jason Dillon <a
href="mailto:[EMAIL PROTECTED]"><[EMAIL PROTECTED]></a>
* @version $Revision: 1.1 $
*/
public final class DefaultInitialContextHandle
extends InitialContextHandle
{
/** Serial Version Identifier. */
private static final long serialVersionUID = -7164179332281875677L;
/** The InitialContext enviroment (or null if unable to determine) */
private Hashtable env;
/**
* Construct a <tt>DefaultInitialContextHandle</tt>.
*/
public DefaultInitialContextHandle() {
// save the current enviroment
env = getEnvironment();
}
/**
* Discover the URL of the local naming server.
*/
private String getUrl() throws Exception {
// discover the hostname to use
String hostname = System.getProperty("java.rmi.server.hostname");
if (hostname == null) {
hostname = InetAddress.getLocalHost().getHostName();
}
// get the port to use
List list = MBeanServerFactory.findMBeanServer(null);
if (list.size() == 0) {
throw new RuntimeException("no MBean servers found");
}
// for now just assume that is the first one
MBeanServer server = (MBeanServer)list.get(0);
// ask the naming service for the port
Integer port = (Integer)
server.invoke(new ObjectName(NamingServiceMBean.OBJECT_NAME),
"getPort",
new Object[] {},
new String[] {});
// construct a new url
return hostname + ":" + port;
}
/**
* Get the environment table suitable for passing to an initial context
* or null if we should use a vanilla one.
*/
private Hashtable getEnvironment() {
Hashtable map = new Hashtable();
try {
map.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
map.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
map.put(Context.PROVIDER_URL, getUrl());
return map;
}
catch (Exception e) {
throw new RuntimeException
("failed to discover environment properties: " + e);
}
}
/**
* Get the initial context for this handle.
*
* @return <tt>InitialContext</tt>.
*
* @throws NamingException Failed to create <tt>InitialContext</tt>.
*/
public InitialContext getInitialContext() throws NamingException {
InitialContext ctx;
// if the environment is not null, then use it else
// assume there is a system property set.
if (env != null) {
ctx = new InitialContext(env);
}
else {
ctx = new InitialContext();
}
return ctx;
}
/**
* A factory for producing {@link DefaultInitialContextHandle}
* instance objects.
*
* <p>Only one instance is created to help minimize the overhead
* required to inspect the running configuration.
*/
public static final class Factory
implements InitialContextHandleFactory
{
/** The single handle instace. */
private InitialContextHandle instance;
/**
* Creates an initial context handle suitable creating an initial
* context for the current virtual machine.
*
* @return An initial context handle suitable for the current vm.
*/
public InitialContextHandle create() {
// only synchronize once during initialization
if (instance == null) {
synchronized (this) {
if (instance == null) {
instance = new DefaultInitialContextHandle();
}
}
}
return instance;
}
}
}
1.1
jboss/src/main/org/jboss/ejb/plugins/jrmp/interfaces/InitialContextHandleFactory.java
Index: InitialContextHandleFactory.java
===================================================================
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ejb.plugins.jrmp.interfaces;
/**
* A factory for producing {@link InitialContextHandle} objects.
*
* @author Jason Dillon <a
href="mailto:[EMAIL PROTECTED]"><[EMAIL PROTECTED]></a>
* @version $Revision: 1.1 $
*/
public interface InitialContextHandleFactory
{
/**
* Creates an initial context handle suitable creating an initial
* context for the current virtual machine.
*
* @return An initial context handle suitable for the current vm.
*/
InitialContextHandle create();
}
1.1
jboss/src/main/org/jboss/ejb/plugins/jrmp/interfaces/PropertiesInitialContextHandle.java
Index: PropertiesInitialContextHandle.java
===================================================================
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ejb.plugins.jrmp.interfaces;
import java.util.Hashtable;
import java.util.Properties;
import java.io.Serializable;
import java.io.InputStream;
import java.net.URL;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* An implementation of {@link InitialContextHandle} that reads a properties
* file from a configured URL for the environment.
*
* @author Jason Dillon <a
href="mailto:[EMAIL PROTECTED]"><[EMAIL PROTECTED]></a>
* @version $Revision: 1.1 $
*/
public final class PropertiesInitialContextHandle
extends InitialContextHandle
{
/** Serial Version Identifier. */
private static final long serialVersionUID = -7458882217772107915L;
/** The InitialContext enviroment (or null if unable to determine) */
private Hashtable env;
/**
* Construct a <tt>PropertiesInitialContextHandle</tt>.
*/
public PropertiesInitialContextHandle() {
// save the current enviroment
env = getEnvironment();
}
/**
* Get the environment table suitable for passing to an initial context
* or null if we should use a vanilla one.
*/
private Hashtable getEnvironment() {
String propname =
PropertiesInitialContextHandle.class.getName() +
".environment";
String spec = System.getProperty(propname, null);
if (spec == null)
return null;
try {
URL url = new URL(spec);
InputStream input = url.openStream();
Properties props;
try {
props = new Properties();
props.load(input);
}
finally {
input.close();
}
return props;
}
catch (Exception e) {
throw new RuntimeException
("failed to load environment properties: " + e);
}
}
/**
* Get the initial context for this handle.
*
* @return <tt>InitialContext</tt>.
*
* @throws NamingException Failed to create <tt>InitialContext</tt>.
*/
public InitialContext getInitialContext() throws NamingException {
InitialContext ctx;
// if the environment is not null, then use it else
// assume there is a system property set.
if (env != null) {
ctx = new InitialContext(env);
}
else {
ctx = new InitialContext();
}
return ctx;
}
/**
* A factory for producing {@link PropertiesInitialContextHandle}
* instance objects.
*
* <p>Only one instance is created to help minimize the overhead
* required to inspect the running configuration.
*/
public static final class Factory
implements InitialContextHandleFactory
{
/** The single handle instace. */
private InitialContextHandle instance;
/**
* Creates an initial context handle suitable creating an initial
* context for the current virtual machine.
*
* @return An initial context handle suitable for the current vm.
*/
public InitialContextHandle create() {
// only synchronize once during initialization
if (instance == null) {
synchronized (this) {
if (instance == null) {
instance = new PropertiesInitialContextHandle();
}
}
}
return instance;
}
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development