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