~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/panel/ConfigEntry.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation version 2 of the License.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 * END COPYRIGHT BLOCK **/
 
19
/* ConfigEntry.java - class that implements a standalone 
 
20
                      configuration entry
 
21
 */
 
22
 
 
23
package com.netscape.admin.dirserv.panel;
 
24
 
 
25
import netscape.ldap.*;
 
26
import java.util.*;
 
27
 
 
28
public class ConfigEntry {
 
29
    public ConfigEntry (String dn, LDAPConnection ldc){
 
30
        _configDN = dn;
 
31
        _ldc = ldc;
 
32
    }
 
33
 
 
34
    public void addAttribute (String attrName, DSEntry dsEntry){
 
35
        _attrTable.put (attrName, dsEntry);    
 
36
    }
 
37
 
 
38
    public void setAddAttributes (/*LDAPAttributeSet*/LDAPAttribute[] attrSet){
 
39
        _addAttrs = attrSet;
 
40
    }
 
41
 
 
42
    public void read () throws LDAPException{
 
43
        int i;
 
44
        int size;
 
45
        String [] attrs;
 
46
        Enumeration keys;
 
47
        String name;
 
48
        DSEntry dsEntry;
 
49
        LDAPAttribute lda;
 
50
        LDAPEntry entry = null;
 
51
 
 
52
        /* get the list of attributes we are interested in */
 
53
        size = _attrTable.size ();
 
54
        if (size == 0)
 
55
        {
 
56
            /* ONREPL print warning */
 
57
            return;
 
58
        }
 
59
 
 
60
        attrs = new String [size];
 
61
 
 
62
        keys = _attrTable.keys ();
 
63
        i = 0;
 
64
        while (keys.hasMoreElements()) {
 
65
            attrs [i] = (String)keys.nextElement();
 
66
            i ++;
 
67
        }
 
68
        
 
69
        /* read the entry */
 
70
        try {
 
71
            entry = _ldc.read (_configDN, attrs);
 
72
        } catch (LDAPException lde){
 
73
            if (lde.getLDAPResultCode() != lde.NO_SUCH_OBJECT)
 
74
                throw lde;
 
75
        }
 
76
 
 
77
        _configEntry = entry;
 
78
        
 
79
        /* update correspondent form fields */
 
80
        keys = _attrTable.keys ();
 
81
        while (keys.hasMoreElements()) {
 
82
            name = (String)keys.nextElement ();
 
83
            dsEntry = (DSEntry)_attrTable.get (name);
 
84
            if (entry != null){
 
85
                lda = entry.getAttribute (name);    
 
86
                dsEntry.remoteToLocal(lda.getStringValues());
 
87
            }
 
88
            else{
 
89
                dsEntry.remoteToLocal("");                
 
90
            }
 
91
            dsEntry.show ();        
 
92
        }
 
93
    }
 
94
 
 
95
    public void write () throws LDAPException{
 
96
        /* we are adding new entry */
 
97
        if (_configEntry == null)
 
98
            _configEntry = addEntry ();
 
99
        else
 
100
            modifyEntry ();
 
101
    }
 
102
 
 
103
    public void delete () throws LDAPException{
 
104
        /* delete entry */
 
105
        _ldc.delete (_configDN);
 
106
    }
 
107
 
 
108
    private LDAPEntry addEntry () throws LDAPException{
 
109
        LDAPEntry entry;
 
110
        LDAPAttribute attr;
 
111
        LDAPAttributeSet attrs;
 
112
        Enumeration entries;
 
113
        Enumeration keys;
 
114
        DSEntry dsEntry;
 
115
        String name;
 
116
        String[] values;
 
117
 
 
118
        /* populate entry */
 
119
        if (_addAttrs != null)
 
120
            attrs = new LDAPAttributeSet (_addAttrs);
 
121
        else
 
122
            attrs = new LDAPAttributeSet ();
 
123
 
 
124
        keys = _attrTable.keys ();
 
125
        while (keys.hasMoreElements ()){
 
126
            name = (String)(keys.nextElement ());
 
127
            dsEntry = (DSEntry)_attrTable.get (name);
 
128
            dsEntry.store ();
 
129
            values = dsEntry.localToRemote ();
 
130
            attr = new LDAPAttribute (name, values);
 
131
            attrs.add (attr);
 
132
        }        
 
133
 
 
134
        entry = new LDAPEntry (_configDN, attrs);
 
135
 
 
136
        try{        
 
137
            _ldc.add (entry);
 
138
        }catch (LDAPException lde){
 
139
            entry = null; /* help garbage collection */
 
140
            throw (lde);
 
141
        }
 
142
        
 
143
        /* reset DSEntries */
 
144
        entries = _attrTable.elements ();
 
145
        while (entries.hasMoreElements ()){
 
146
            dsEntry = (DSEntry)entries.nextElement ();
 
147
            dsEntry.reset ();
 
148
        }
 
149
 
 
150
        return entry;
 
151
        
 
152
    }
 
153
   
 
154
    private void modifyEntry () throws LDAPException{
 
155
        LDAPModificationSet mods;
 
156
        LDAPAttribute       attr;
 
157
        Enumeration         keys;
 
158
        Enumeration         entries;
 
159
        String              name;
 
160
        DSEntry             dsEntry;
 
161
        String[]            values;
 
162
 
 
163
        mods = new LDAPModificationSet ();
 
164
 
 
165
        keys = _attrTable.keys ();
 
166
        while (keys.hasMoreElements ()){
 
167
            name = (String)(keys.nextElement ());
 
168
            dsEntry = (DSEntry)_attrTable.get (name);
 
169
            dsEntry.store ();
 
170
            if (dsEntry.getDirty()){
 
171
                values = dsEntry.localToRemote ();
 
172
                attr = new LDAPAttribute (name, values);
 
173
                mods.add(LDAPModification.REPLACE, attr);    
 
174
            }
 
175
        }              
 
176
        
 
177
        try{
 
178
            _ldc.modify (_configDN, mods);
 
179
        }catch (LDAPException lde){
 
180
            throw lde;
 
181
        }
 
182
 
 
183
        /* reset DSEntries */
 
184
        entries = _attrTable.elements ();
 
185
        while (entries.hasMoreElements ()){
 
186
            dsEntry = (DSEntry)entries.nextElement ();
 
187
            dsEntry.reset ();
 
188
        }
 
189
    }
 
190
 
 
191
    /* private data */
 
192
    private LDAPEntry _configEntry = null;
 
193
    private String _configDN;    
 
194
    private LDAPConnection _ldc;
 
195
    /* attributes that should be part of the entry when it added as
 
196
       opposed to modified, like object class
 
197
     */
 
198
    private /*LDAPAttributeSet*/ LDAPAttribute[] _addAttrs = null;     
 
199
    private Hashtable _attrTable = new Hashtable();
 
200
}