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 javax.naming.NamingException;
49 import javax.naming.NameNotFoundException;
50 import javax.naming.NamingEnumeration;
51 import javax.naming.directory.SearchControls;
52 import javax.naming.directory.DirContext;
53 import javax.naming.directory.SearchResult;
54
55 import org.xml.sax.DocumentHandler;
56 import org.xml.sax.SAXException;
57 import org.castor.core.util.Messages;
58 import org.exolab.castor.dsml.Consumer;
59 import org.exolab.castor.dsml.Exporter;
60 import org.exolab.castor.dsml.SearchDescriptor;
61 import org.exolab.castor.dsml.ImportExportException;
62
63
64
65
66
67 public class JNDIExporter extends Exporter {
68 private DirContext _ctx;
69
70 public JNDIExporter(final DirContext ctx) {
71 _ctx = ctx;
72 }
73
74 protected Consumer createConsumer() {
75 return new JNDIConsumer();
76 }
77
78 public void export(final DocumentHandler docHandler, final boolean serverSchema,
79 final boolean importPolicy) throws ImportExportException {
80 NamingEnumeration<SearchResult> enumeration;
81 String filter;
82 JNDIProducer producer;
83 SearchControls searchCtrl;
84
85 if (getSearchDescriptor() == null) {
86 throw new IllegalStateException(Messages.message("dsml.searchDescriptorRequired"));
87 }
88
89 filter = getSearchDescriptor().getFilter();
90 if (filter == null) {
91 filter = "()";
92 }
93 try {
94 searchCtrl = new SearchControls();
95 searchCtrl.setReturningAttributes(getSearchDescriptor().getReturnAttrs());
96 switch (getSearchDescriptor().getScope()) {
97 case SearchDescriptor.Scope.BASE:
98 searchCtrl.setSearchScope(SearchControls.OBJECT_SCOPE);
99 break;
100 case SearchDescriptor.Scope.ONE_LEVEL:
101 searchCtrl.setSearchScope(SearchControls.ONELEVEL_SCOPE);
102 break;
103 case SearchDescriptor.Scope.SUB_TREE:
104 searchCtrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
105 break;
106 default:
107 break;
108 }
109 enumeration = _ctx.search(getSearchDescriptor().getBaseDN(), filter, searchCtrl);
110 } catch (NameNotFoundException except) {
111 enumeration = null;
112 } catch (NamingException except) {
113 throw new ImportExportException(except);
114 }
115
116 try {
117 producer = new JNDIProducer(docHandler, false);
118 producer.startDocument();
119 if (enumeration != null) {
120 producer.produce(enumeration);
121 }
122 if (importPolicy && getImportDescriptor() != null) {
123 producer.produce(getImportDescriptor());
124 }
125 producer.endDocument();
126 } catch (SAXException except) {
127 throw new ImportExportException(except);
128 }
129 }
130 }