1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.exolab.castor.dsml.jndi;
47
48 import java.util.Vector;
49 import java.util.Enumeration;
50 import javax.naming.NamingException;
51 import javax.naming.NameNotFoundException;
52 import javax.naming.NamingEnumeration;
53 import javax.naming.directory.Attribute;
54 import javax.naming.directory.Attributes;
55 import javax.naming.directory.ModificationItem;
56 import javax.naming.directory.DirContext;
57 import javax.naming.directory.SearchResult;
58 import org.exolab.castor.dsml.Consumer;
59 import org.exolab.castor.dsml.ImportEventListener;
60 import org.exolab.castor.dsml.Importer;
61 import org.exolab.castor.dsml.ImportDescriptor;
62 import org.exolab.castor.dsml.ImportExportException;
63
64
65
66
67
68 public class JNDIImporter extends Importer {
69 private DirContext _ctx;
70
71 public JNDIImporter(final DirContext ctx) {
72 _ctx = ctx;
73 }
74
75 protected Consumer createConsumer() {
76 return new JNDIConsumer();
77 }
78
79 public void importEntry(final SearchResult result, final int policy) throws NamingException {
80 Attributes existing;
81 Attributes attrSet;
82 Attribute attr;
83 NamingEnumeration<? extends Attribute> enumeration;
84
85 if (result.getAttributes().size() == 0) {
86 if ((policy & ImportDescriptor.Policy.DELETE_EMPTY) != 0) {
87 try {
88 _ctx.lookup(result.getName());
89 _ctx.unbind(result.getName());
90 notify(result.getName(), ImportEventListener.DELETED);
91 } catch (NameNotFoundException except) {
92
93 notify(result.getName(), ImportEventListener.IGNORED);
94 }
95 } else {
96 notify(result.getName(), ImportEventListener.IGNORED);
97 }
98 } else {
99 try {
100 existing = _ctx.getAttributes(result.getName());
101
102 Vector<ModificationItem> modifs = new Vector<ModificationItem>();
103 attrSet = result.getAttributes();
104 enumeration = attrSet.getAll();
105 while (enumeration.hasMore()) {
106 attr = enumeration.next();
107 if (existing.get(attr.getID()) != null) {
108 if ((policy & ImportDescriptor.Policy.NEW_ATTRIBUTE_ONLY) == 0) {
109 if (attr.size() > 0) {
110 modifs.addElement(new ModificationItem(
111 DirContext.REPLACE_ATTRIBUTE, attr));
112 } else {
113 modifs.addElement(new ModificationItem(
114 DirContext.REMOVE_ATTRIBUTE, attr));
115 }
116 }
117 } else {
118 if ((policy & ImportDescriptor.Policy.UPDATE_ONLY) == 0) {
119 if (attr.size() > 0) {
120 modifs.addElement(new ModificationItem(
121 DirContext.ADD_ATTRIBUTE, attr));
122 }
123 }
124 }
125 }
126 if ((policy & ImportDescriptor.Policy.REPLACE_ATTRIBUTE) != 0) {
127 enumeration = existing.getAll();
128 while (enumeration.hasMore()) {
129 attr = enumeration.next();
130 if (attrSet.get(attr.getID()) == null) {
131 modifs.addElement(new ModificationItem(
132 DirContext.REMOVE_ATTRIBUTE, attr));
133 }
134 }
135 }
136 if (modifs.size() > 0) {
137 ModificationItem[] array;
138
139 array = new ModificationItem[modifs.size()];
140 modifs.copyInto(array);
141 _ctx.modifyAttributes(result.getName(), array);
142 notify(result.getName(), ImportEventListener.REFRESHED);
143 } else {
144 notify(result.getName(), ImportEventListener.IGNORED);
145 }
146 } catch (NameNotFoundException except) {
147
148 if ((policy & ImportDescriptor.Policy.REFRESH_ONLY) == 0) {
149 _ctx.bind(result.getName(), null, result.getAttributes());
150 notify(result.getName(), ImportEventListener.CREATED);
151 } else {
152 notify(result.getName(), ImportEventListener.IGNORED);
153 }
154 }
155 }
156 }
157
158 public void importEntries(final NamingEnumeration<SearchResult> results)
159 throws NamingException {
160 if (getImportDescriptor() == null) {
161 setImportDescriptor(new ImportDescriptor());
162 }
163 while (results.hasMore()) {
164 SearchResult result = results.next();
165 importEntry(result, getImportDescriptor().getPolicy(result.getName()));
166 }
167 }
168
169 public void importEntries(final Enumeration<SearchResult> results)
170 throws ImportExportException {
171 if (getImportDescriptor() == null) {
172 setImportDescriptor(new ImportDescriptor());
173 }
174 try {
175 while (results.hasMoreElements()) {
176 SearchResult result = results.nextElement();
177 importEntry(result, getImportDescriptor().getPolicy(result.getName()));
178 }
179 } catch (NamingException except) {
180 throw new ImportExportException(except);
181 }
182 }
183 }