You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@apache.org on 2002/11/05 20:03:14 UTC
cvs commit: jakarta-tomcat-connectors/naming/src/org/apache/naming/core ServerBinding.java NamingContextEnumeration.java
costin 2002/11/05 11:03:14
Modified: naming/src/org/apache/naming/core
NamingContextEnumeration.java
Added: naming/src/org/apache/naming/core ServerBinding.java
Log:
Delay the evaluation of getClassName and getValue until it is
needed ( in most cases it won't ).
Also allow any kind of enumerations ( including Strings ) to be used.
Revision Changes Path
1.3 +49 -24 jakarta-tomcat-connectors/naming/src/org/apache/naming/core/NamingContextEnumeration.java
Index: NamingContextEnumeration.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-connectors/naming/src/org/apache/naming/core/NamingContextEnumeration.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NamingContextEnumeration.java 8 Oct 2002 18:46:42 -0000 1.2
+++ NamingContextEnumeration.java 5 Nov 2002 19:03:14 -0000 1.3
@@ -63,43 +63,50 @@
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
-import javax.naming.NamingException;
-import javax.naming.NamingEnumeration;
-import javax.naming.NameClassPair;
+import javax.naming.*;
/**
* Naming enumeration implementation.
*
+ * TODO: implement 'throw exceptions on close' part of the spec.
+ * TODO: implement recycling ( for example on close )
+ *
* @author Remy Maucherat
- * @version $Revision$ $Date$
+ * @author Costin Manolache
*/
-
public class NamingContextEnumeration
implements NamingEnumeration
{
-
-
- // ----------------------------------------------------------- Constructors
-
-
- public NamingContextEnumeration(Vector entries) {
- enum = entries.elements();
- }
-
-
- public NamingContextEnumeration(Enumeration enum) {
+ /** Constructor.
+ *
+ * @param enum base enumeration. Elements can be Strings, NameClassPair,
+ * Bindings or Entries, we'll provide the wrapping if needed. For String
+ * the Class and value will be lazy-loaded.
+ *
+ * @param ctx. The context where this enum belongs. Used to lazy-eval
+ * the class and value
+ *
+ * @param bindings If true, we'll wrap things as Binding ( true for
+ * listBindings, false for list ).
+ */
+ public NamingContextEnumeration( Enumeration enum, Context ctx,
+ boolean bindings )
+ {
+ this.ctx = ctx;
+ this.bindings=bindings;
this.enum = enum;
}
-
// -------------------------------------------------------------- Variables
-
+ // return bindings instead of NameClassPair
+ protected boolean bindings;
/**
* Underlying enumeration.
*/
protected Enumeration enum;
+ protected Context ctx;
// --------------------------------------------------------- Public Methods
@@ -108,7 +115,8 @@
* Retrieves the next element in the enumeration.
*/
public Object next()
- throws NamingException {
+ throws NamingException
+ {
return nextElement();
}
@@ -117,7 +125,8 @@
* Determines whether there are any more elements in the enumeration.
*/
public boolean hasMore()
- throws NamingException {
+ throws NamingException
+ {
return enum.hasMoreElements();
}
@@ -126,7 +135,9 @@
* Closes this enumeration.
*/
public void close()
- throws NamingException {
+ throws NamingException
+ {
+ // XXX all exceptions should be thrown on close ( AFAIK )
}
@@ -134,10 +145,24 @@
return enum.hasMoreElements();
}
-
public Object nextElement() {
- NamingEntry entry = (NamingEntry) enum.nextElement();
- return new NameClassPair(entry.name, entry.value.getClass().getName());
+ Object next=enum.nextElement();
+ if( next instanceof NamingEntry ) {
+ NamingEntry entry = (NamingEntry) next;
+ return new ServerBinding(entry.name, ctx, true);
+ } else if( next instanceof NameClassPair ) {
+ NameClassPair ncp=(NameClassPair)next;
+ if( bindings )
+ return new ServerBinding(ncp.getName(), ctx, true);
+ return next;
+ } else if( next instanceof Binding ) {
+ return next;
+ } else if( next instanceof String ) {
+ String name=(String)next;
+ return new ServerBinding( name, ctx, true );
+ }
+ return null;
}
+
}
1.1 jakarta-tomcat-connectors/naming/src/org/apache/naming/core/ServerBinding.java
Index: ServerBinding.java
===================================================================
package org.apache.naming.core;
import javax.naming.*;
/** This is used for both NameClass and Binding.
* Lazy eval - so the extra methods in Binding don't affect us.
* For most contexts we'll deal getting the class name is as expensive
* as getting the object. In addition most operations will only use the name.
*
*/
public class ServerBinding extends Binding {
public ServerBinding( String name, Context ctx, boolean isRelative ) {
super( name, null );
this.ctx=ctx;
this.name = name;
this.isRel=isRelative;
}
public void recycle() {
}
private Context ctx;
private Object boundObj;
private String name;
private boolean isRel = true;
private String className;
private void lookup() {
try {
boundObj=ctx.lookup(name);
} catch( NamingException ex ) {
ex.printStackTrace();
}
}
public String getClassName() {
if( className!=null ) return className;
if( boundObj==null ) lookup();
if( boundObj!=null )
className=boundObj.getClass().getName();
return className;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setClassName(String name) {
this.className = name;
}
public boolean isRelative() {
return isRel;
}
public void setRelative(boolean r) {
isRel = r;
}
/**
* Generates the string representation of this name/class pair.
* The string representation consists of the name and class name separated
* by a colon (':').
* The contents of this string is useful
* for debugging and is not meant to be interpreted programmatically.
*
* @return The string representation of this name/class pair.
*/
public String toString() {
return (isRelative() ? "" : "(not relative)") + getName() + ": " +
getClassName();
}
public Object getObject() {
if( boundObj!=null )
return boundObj;
lookup();
return boundObj;
}
public void setObject(Object obj) {
boundObj = obj;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>