View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  
37  package org.exolab.castor.dsml.mozilla;
38  
39  
40  import java.util.Enumeration;
41  import org.xml.sax.DocumentHandler;
42  import org.xml.sax.SAXException;
43  import netscape.ldap.LDAPException;
44  import netscape.ldap.LDAPConnection;
45  import netscape.ldap.LDAPSchema;
46  import netscape.ldap.LDAPv2;
47  
48  import org.castor.core.util.Messages;
49  import org.exolab.castor.dsml.Consumer;
50  import org.exolab.castor.dsml.Exporter;
51  import org.exolab.castor.dsml.SearchDescriptor;
52  import org.exolab.castor.dsml.ImportExportException;
53  
54  
55  /**
56   *
57   *
58   * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
59   * @version $Revision$ $Date: 2006-04-10 16:39:24 -0600 (Mon, 10 Apr 2006) $
60   */
61  public class MozillaExporter extends Exporter {
62  
63  
64    private LDAPConnection _conn;
65  
66  
67    public MozillaExporter(LDAPConnection conn) {
68      _conn = conn;
69    }
70  
71  
72    protected Consumer createConsumer() {
73      return new MozillaConsumer();
74    }
75  
76  
77    public void export(DocumentHandler docHandler, boolean serverSchema, boolean importPolicy)
78        throws ImportExportException {
79      Enumeration enumeration;
80      String[] attrs;
81      MozillaProducer producer;
82      LDAPSchema schema;
83      int scope;
84  
85      if (getSearchDescriptor() == null)
86        throw new IllegalStateException(Messages.message("dsml.searchDescriptorRequired"));
87  
88      attrs = getSearchDescriptor().getReturnAttrs();
89      try {
90        scope = getSearchDescriptor().getScope();
91        switch (scope) {
92          case SearchDescriptor.Scope.ONE_LEVEL:
93            scope = LDAPv2.SCOPE_ONE;
94            break;
95          case SearchDescriptor.Scope.BASE:
96            scope = LDAPv2.SCOPE_BASE;
97            break;
98          case SearchDescriptor.Scope.SUB_TREE:
99            scope = LDAPv2.SCOPE_SUB;
100           break;
101       }
102       enumeration = _conn.search(getSearchDescriptor().getBaseDN(), scope,
103           getSearchDescriptor().getFilter(), attrs, false);
104     } catch (LDAPException except) {
105       // Object does not exist, was not removed, ignore.
106       // Anything else, we must complain.
107       if (except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT)
108         throw new ImportExportException(except);
109       enumeration = null;
110     }
111 
112     try {
113       producer = new MozillaProducer(docHandler, false);
114       producer.startDocument();
115       if (serverSchema) {
116         schema = new LDAPSchema();
117         schema.fetchSchema(_conn);
118         producer.produce(schema);
119       }
120       if (enumeration != null)
121         producer.produce(enumeration);
122       if (importPolicy && getImportDescriptor() != null)
123         producer.produce(getImportDescriptor());
124       producer.endDocument();
125     } catch (SAXException except) {
126       throw new ImportExportException(except);
127     } catch (LDAPException except) {
128       throw new ImportExportException(except);
129     }
130   }
131 
132 
133 
134 }
135