1 24 package fr.dyade.aaa.jndi2.impl; 25 26 import fr.dyade.aaa.util.*; 27 import java.util.*; 28 import java.io.*; 29 import javax.naming.*; 30 31 import org.objectweb.util.monolog.api.BasicLevel; 32 import org.objectweb.util.monolog.api.Logger; 33 34 public class NamingContext implements Serializable, Cloneable { 35 36 private NamingContextId id; 37 38 private Object ownerId; 39 40 private Vector records; 41 42 public NamingContext(NamingContextId id, 43 Object ownerId) { 44 this.id = id; 45 this.ownerId = ownerId; 46 records = new Vector(); 47 } 48 49 public final NamingContextId getId() { 50 return id; 51 } 52 53 public final Object getOwnerId() { 54 return ownerId; 55 } 56 57 public void setOwnerId(Object ownerId) { 58 this.ownerId = ownerId; 59 } 60 61 public Record getRecord(String name) { 62 for (int i = 0; i < records.size(); i++) { 63 Record r = (Record)records.elementAt(i); 64 if (r.getName().equals(name)) return r; 65 } 66 return null; 67 } 68 69 public void addRecord(Record record) { 70 records.addElement(record); 71 } 72 73 public boolean removeRecord(String name) { 74 for (int i = 0; i < records.size(); i++) { 75 Record r = (Record)records.elementAt(i); 76 if (r.getName().equals(name)) { 77 records.removeElementAt(i); 78 return true; 79 } 80 } 81 return false; 82 } 83 84 public int size() { 85 return records.size(); 86 } 87 88 public NameClassPair[] getNameClassPairs() { 89 NameClassPair[] res = new NameClassPair[records.size()]; 90 for (int i = 0; i < records.size(); i++) { 91 Record r = (Record)records.elementAt(i); 92 if (r instanceof ObjectRecord) { 93 ObjectRecord or = (ObjectRecord)r; 94 res[i] = new NameClassPair( 95 or.getName(), 96 getClassName(or.getObject()), 97 true); 98 } else if (r instanceof ContextRecord) { 99 res[i] = new NameClassPair( 100 r.getName(), 101 Context.class.getName(), 102 true); 103 } 104 } 105 return res; 106 } 107 108 public Binding[] getBindings() { 109 Binding[] res = new Binding[records.size()]; 110 for (int i = 0; i < records.size(); i++) { 111 Record r = (Record)records.elementAt(i); 112 if (r instanceof ObjectRecord) { 113 ObjectRecord or = (ObjectRecord)r; 114 res[i] = new Binding( 115 or.getName(), 116 getClassName(or.getObject()), 117 or.getObject(), 118 true); 119 } else if (r instanceof ContextRecord) { 120 res[i] = new Binding( 121 r.getName(), 122 Context.class.getName(), 123 null, 124 true); 125 } 126 } 127 return res; 128 } 129 130 private static String getClassName(Object obj) { 131 if (obj instanceof Reference) { 132 Reference ref = (Reference)obj; 133 return ref.getClassName(); 134 } else { 135 return obj.getClass().getName(); 136 } 137 } 138 139 public Object clone() { 140 try { 141 NamingContext clone = 142 (NamingContext)super.clone(); 143 clone.records = (Vector)records.clone(); 144 return clone; 146 } catch (CloneNotSupportedException exc) { 147 return null; 148 } 149 } 150 151 public String toString() { 152 StringBuffer buf = new StringBuffer (); 153 buf.append('(' + super.toString()); 154 buf.append(",id=" + id); 155 buf.append(",ownerId=" + ownerId); 156 buf.append(",records="); 157 Strings.toString(buf, records); 158 buf.append(')'); 159 return buf.toString(); 160 } 161 } 162 | Popular Tags |