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 package org.exolab.castor.dsml.jndi;
37
38 import java.util.ArrayList;
39 import java.util.Enumeration;
40 import java.util.List;
41 import javax.naming.NamingException;
42 import javax.naming.NameNotFoundException;
43 import javax.naming.NamingEnumeration;
44 import javax.naming.directory.Attribute;
45 import javax.naming.directory.Attributes;
46 import javax.naming.directory.ModificationItem;
47 import javax.naming.directory.DirContext;
48 import javax.naming.directory.SearchResult;
49 import org.exolab.castor.dsml.Consumer;
50 import org.exolab.castor.dsml.ImportEventListener;
51 import org.exolab.castor.dsml.Importer;
52 import org.exolab.castor.dsml.ImportDescriptor;
53 import org.exolab.castor.dsml.ImportExportException;
54
55
56
57
58
59 public class JNDIImporter extends Importer {
60 private DirContext _ctx;
61
62 public JNDIImporter(final DirContext ctx) {
63 _ctx = ctx;
64 }
65
66 protected Consumer createConsumer() {
67 return new JNDIConsumer();
68 }
69
70 public void importEntry(final SearchResult result, final int policy) throws NamingException {
71 Attributes existing;
72 Attributes attrSet;
73 Attribute attr;
74 NamingEnumeration<? extends Attribute> enumeration;
75
76 if (result.getAttributes().size() == 0) {
77 if ((policy & ImportDescriptor.Policy.DELETE_EMPTY) != 0) {
78 try {
79 _ctx.lookup(result.getName());
80 _ctx.unbind(result.getName());
81 notify(result.getName(), ImportEventListener.DELETED);
82 } catch (NameNotFoundException except) {
83
84 notify(result.getName(), ImportEventListener.IGNORED);
85 }
86 } else {
87 notify(result.getName(), ImportEventListener.IGNORED);
88 }
89 } else {
90 try {
91 existing = _ctx.getAttributes(result.getName());
92
93 List<ModificationItem> modifs = new ArrayList<ModificationItem>();
94 attrSet = result.getAttributes();
95 enumeration = attrSet.getAll();
96 while (enumeration.hasMore()) {
97 attr = enumeration.next();
98 if (existing.get(attr.getID()) != null) {
99 if ((policy & ImportDescriptor.Policy.NEW_ATTRIBUTE_ONLY) == 0) {
100 if (attr.size() > 0) {
101 modifs.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr));
102 } else {
103 modifs.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr));
104 }
105 }
106 } else {
107 if ((policy & ImportDescriptor.Policy.UPDATE_ONLY) == 0) {
108 if (attr.size() > 0) {
109 modifs.add(new ModificationItem(DirContext.ADD_ATTRIBUTE, attr));
110 }
111 }
112 }
113 }
114 if ((policy & ImportDescriptor.Policy.REPLACE_ATTRIBUTE) != 0) {
115 enumeration = existing.getAll();
116 while (enumeration.hasMore()) {
117 attr = enumeration.next();
118 if (attrSet.get(attr.getID()) == null) {
119 modifs.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr));
120 }
121 }
122 }
123 if (!modifs.isEmpty()) {
124 ModificationItem[] array = modifs.toArray(new ModificationItem[modifs.size()]);
125 _ctx.modifyAttributes(result.getName(), array);
126 notify(result.getName(), ImportEventListener.REFRESHED);
127 } else {
128 notify(result.getName(), ImportEventListener.IGNORED);
129 }
130 } catch (NameNotFoundException except) {
131
132 if ((policy & ImportDescriptor.Policy.REFRESH_ONLY) == 0) {
133 _ctx.bind(result.getName(), null, result.getAttributes());
134 notify(result.getName(), ImportEventListener.CREATED);
135 } else {
136 notify(result.getName(), ImportEventListener.IGNORED);
137 }
138 }
139 }
140 }
141
142 public void importEntries(final NamingEnumeration<SearchResult> results) throws NamingException {
143 if (getImportDescriptor() == null) {
144 setImportDescriptor(new ImportDescriptor());
145 }
146 while (results.hasMore()) {
147 SearchResult result = results.next();
148 importEntry(result, getImportDescriptor().getPolicy(result.getName()));
149 }
150 }
151
152 public void importEntries(final Enumeration<SearchResult> results) throws ImportExportException {
153 if (getImportDescriptor() == null) {
154 setImportDescriptor(new ImportDescriptor());
155 }
156 try {
157 while (results.hasMoreElements()) {
158 SearchResult result = results.nextElement();
159 importEntry(result, getImportDescriptor().getPolicy(result.getName()));
160 }
161 } catch (NamingException except) {
162 throw new ImportExportException(except);
163 }
164 }
165 }