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 package org.exolab.castor.dsml.mozilla;
38
39
40 import java.util.Enumeration;
41 import netscape.ldap.LDAPConnection;
42 import netscape.ldap.LDAPEntry;
43 import netscape.ldap.LDAPException;
44 import netscape.ldap.LDAPModification;
45 import netscape.ldap.LDAPModificationSet;
46 import netscape.ldap.LDAPAttribute;
47 import netscape.ldap.LDAPAttributeSet;
48 import org.exolab.castor.dsml.Consumer;
49 import org.exolab.castor.dsml.ImportEventListener;
50 import org.exolab.castor.dsml.Importer;
51 import org.exolab.castor.dsml.ImportDescriptor;
52 import org.exolab.castor.dsml.ImportExportException;
53
54
55
56
57
58
59
60
61 public class MozillaImporter extends Importer {
62
63
64 private LDAPConnection _conn;
65
66
67 public MozillaImporter(LDAPConnection conn) {
68 _conn = conn;
69 }
70
71
72 protected Consumer createConsumer() {
73 return new MozillaConsumer();
74 }
75
76
77 public void importEntry(LDAPEntry entry, int policy) throws LDAPException {
78 LDAPEntry existing;
79 LDAPModificationSet modifs;
80 LDAPAttributeSet attrSet;
81 LDAPAttribute attr;
82 int i;
83 Enumeration enumeration;
84
85 if (entry.getAttributeSet() == null || entry.getAttributeSet().size() == 0) {
86
87 if ((policy & ImportDescriptor.Policy.DELETE_EMPTY) != 0) {
88 try {
89 _conn.read(entry.getDN());
90 _conn.delete(entry.getDN());
91 notify(entry.getDN(), ImportEventListener.DELETED);
92 } catch (LDAPException except) {
93
94
95 if (except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT)
96 throw except;
97 notify(entry.getDN(), ImportEventListener.IGNORED);
98 }
99 } else {
100 notify(entry.getDN(), ImportEventListener.IGNORED);
101 }
102
103 } else {
104
105 try {
106 existing = _conn.read(entry.getDN());
107
108 modifs = new LDAPModificationSet();
109 attrSet = entry.getAttributeSet();
110 for (i = 0; i < attrSet.size(); ++i) {
111 attr = attrSet.elementAt(i);
112 if (existing.getAttributeSet().getAttribute(attr.getName()) != null) {
113 if ((policy & ImportDescriptor.Policy.NEW_ATTRIBUTE_ONLY) == 0) {
114 if (attr.size() > 0) {
115 modifs.add(LDAPModification.REPLACE, attr);
116 } else {
117 modifs.add(LDAPModification.DELETE, attr);
118 }
119 }
120 } else {
121 if ((policy & ImportDescriptor.Policy.UPDATE_ONLY) == 0) {
122 if (attr.size() > 0) {
123 modifs.add(LDAPModification.ADD, attr);
124 }
125 }
126 }
127 }
128 if ((policy & ImportDescriptor.Policy.REPLACE_ATTRIBUTE) != 0) {
129 enumeration = existing.getAttributeSet().getAttributes();
130 while (enumeration.hasMoreElements()) {
131 attr = (LDAPAttribute) enumeration.nextElement();
132 if (entry.getAttribute(attr.getName()) == null) {
133 modifs.add(LDAPModification.DELETE, attr);
134 }
135 }
136 }
137 if (modifs.size() > 0) {
138 _conn.modify(entry.getDN(), modifs);
139 notify(entry.getDN(), ImportEventListener.REFRESHED);
140 } else {
141 notify(entry.getDN(), ImportEventListener.IGNORED);
142 }
143 } catch (LDAPException except) {
144
145
146 if (except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT)
147 throw except;
148 if ((policy & ImportDescriptor.Policy.REFRESH_ONLY) == 0) {
149 _conn.add(entry);
150 notify(entry.getDN(), ImportEventListener.CREATED);
151 } else {
152 notify(entry.getDN(), ImportEventListener.IGNORED);
153 }
154 }
155 }
156 }
157
158
159 public void importEntries(Enumeration entries) throws ImportExportException {
160 LDAPEntry entry;
161
162 if (getImportDescriptor() == null)
163 setImportDescriptor(new ImportDescriptor());
164 try {
165 while (entries.hasMoreElements()) {
166 entry = (LDAPEntry) entries.nextElement();
167 importEntry(entry, getImportDescriptor().getPolicy(entry.getDN()));
168 }
169 } catch (LDAPException except) {
170 throw new ImportExportException(except);
171 }
172 }
173
174
175 }