~ubuntu-branches/ubuntu/utopic/389-admin-console/utopic

« back to all changes in this revision

Viewing changes to src/com/netscape/management/admserv/panel/CGIDataModel.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2011-10-12 11:05:09 UTC
  • Revision ID: package-import@ubuntu.com-20111012110509-ybd1jr5xeat2ug1i
Tags: upstream-1.1.8
ImportĀ upstreamĀ versionĀ 1.1.8

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
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; version 2
 
9
 * of the License.
 
10
 * 
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 * 
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 * 
 
20
 * END COPYRIGHT BLOCK **/
 
21
package com.netscape.management.admserv.panel;
 
22
 
 
23
import java.util.*;
 
24
import java.net.*;
 
25
import java.text.MessageFormat;
 
26
import com.netscape.management.client.util.*;
 
27
import com.netscape.management.client.comm.HttpChannel;
 
28
import com.netscape.management.client.console.ConsoleInfo;
 
29
import com.netscape.management.admserv.config.*;
 
30
 
 
31
/**
 
32
  *
 
33
  * @version 0.1 11/28/97
 
34
  * @author miodrag@netscape.com
 
35
  */
 
36
public abstract class CGIDataModel implements IConfigDataModel {
 
37
 
 
38
    static ResourceSet _resource;
 
39
    static String _i18nProgressLoading, _i18nProgressSaving,
 
40
    _i18nLoadFailed, _i18nSaveFailed;
 
41
 
 
42
    static {
 
43
        _resource = new ResourceSet("com.netscape.management.admserv.panel.panel");
 
44
        _i18nProgressLoading = _resource.getString("adminop","ProgressLoading");
 
45
        _i18nProgressSaving = _resource.getString("adminop","ProgressSaving");
 
46
        _i18nLoadFailed = _resource.getString("adminop","LoadFailed");
 
47
        _i18nSaveFailed = _resource.getString("adminop","SaveFailed");
 
48
    }
 
49
 
 
50
    protected Hashtable _data, _origData;
 
51
    protected ConsoleInfo _consoleInfo;
 
52
    protected boolean _loaded, _saved, _modified;
 
53
    protected String _taskURL;
 
54
    protected String _adminURL;
 
55
 
 
56
    protected java.awt.Component _dialogParent; // for info messages
 
57
 
 
58
    public CGIDataModel(ConsoleInfo consoleInfo, String taskURL) {
 
59
        _consoleInfo = consoleInfo;
 
60
        _taskURL = taskURL;
 
61
        _adminURL = _consoleInfo.getAdminURL() + _taskURL;
 
62
    }
 
63
 
 
64
    static public String getServerName(ConsoleInfo ci) {
 
65
        String server = (String) ci.get("SERVER_NAME");
 
66
        if (server == null) {
 
67
            Debug.println("CGIDataModel.getServerName: SERVER_NAME not found in console info");
 
68
            return "unknown";
 
69
        }
 
70
        return server;
 
71
    }
 
72
 
 
73
    public String getModelName() {
 
74
        return getServerName(_consoleInfo);
 
75
    }
 
76
 
 
77
 
 
78
    public ConsoleInfo getConsoleInfo() {
 
79
        return _consoleInfo;
 
80
    }
 
81
 
 
82
    public Enumeration getAttributes() {
 
83
        return _data.keys();
 
84
    }
 
85
 
 
86
    /**
 
87
      * Implements IConfigDataModel.isLoaded()
 
88
     */
 
89
    public boolean isLoaded() {
 
90
        return _loaded;
 
91
    }
 
92
 
 
93
    /**
 
94
      * Implements IConfigDataModel.load()
 
95
     */
 
96
    public void load() throws RemoteRequestException {
 
97
        if (ActionMonitorPanel.getActiveInstance() != null) {
 
98
            MessageFormat msg = new MessageFormat(_i18nProgressLoading);
 
99
            Object[] msgArgs = { "" }; //{ getModelName() };
 
100
            ActionMonitorPanel.getActiveInstance().setStatusText(
 
101
                    msg.format(msgArgs));
 
102
        }
 
103
        _data = getConfiguration(_adminURL, getCGIParamsForGetOp());
 
104
        _loaded = ! _data.isEmpty();
 
105
        if (!_loaded) {
 
106
            throw new RemoteRequestException(_adminURL, _i18nLoadFailed);
 
107
        }
 
108
        _origData = cloneStringHashtable(_data, null);
 
109
    }
 
110
 
 
111
    /**
 
112
      * Implements IConfigDataModel.isLoaded()
 
113
     */
 
114
    public boolean isModified() {
 
115
        //Debug.println(getClass().getName() + " modified=" + _modified);
 
116
        return _modified;
 
117
    }
 
118
 
 
119
 
 
120
    /**
 
121
      * Implements IConfigDataModel.save()
 
122
     */
 
123
    public void save() throws RemoteRequestException {
 
124
        if (ActionMonitorPanel.getActiveInstance() != null) {
 
125
            MessageFormat msg = new MessageFormat(_i18nProgressSaving);
 
126
            Object[] msgArgs = { getModelName()};
 
127
            ActionMonitorPanel.getActiveInstance().setStatusText(
 
128
                    msg.format(msgArgs));
 
129
        }
 
130
 
 
131
        try {
 
132
            setConfiguration(_adminURL, getCGIParamsForSetOp());
 
133
            if (!_saved) {
 
134
                throw new RemoteRequestException(_adminURL,
 
135
                        _i18nSaveFailed);
 
136
            }
 
137
        } catch (RemoteRequestException e) {
 
138
            _data.clear();
 
139
            _data = cloneStringHashtable(_origData, null);
 
140
            throw e;
 
141
        }
 
142
        _origData.clear();
 
143
        _origData = cloneStringHashtable(_data, null);
 
144
        _modified = false;
 
145
    }
 
146
 
 
147
    /**
 
148
      * Implements IConfigDataModel.getAttribute()
 
149
     */
 
150
    public String getAttribute(String attr) {
 
151
        return (String)_data.get(attr);
 
152
    }
 
153
 
 
154
    /**
 
155
      * Implements IConfigDataModel.setAttribute()
 
156
     */
 
157
 
 
158
    public void setAttribute(String attr,
 
159
            String val) throws ValidationException {
 
160
        String curVal = getAttribute(attr);
 
161
        if (!_modified) {
 
162
            _modified = (val == null) ? (curVal != null) :
 
163
                    !(val.equals(curVal));
 
164
        }
 
165
        //Debug.println(attr + " curVal="+curVal+" val="+val+"modified="+_modified);
 
166
        _data.put(attr, val);
 
167
    }
 
168
 
 
169
 
 
170
    public String toURLformat(Hashtable args) {
 
171
        String p = "";
 
172
        Enumeration e = args.keys();
 
173
        while (e.hasMoreElements()) {
 
174
            String name = (String) e.nextElement();
 
175
            String value = (String) args.get(name);
 
176
 
 
177
            p += name + "=" + URLByteEncoder.encodeUTF8(value) +
 
178
                    (e.hasMoreElements() ? "&":"");
 
179
        }
 
180
        return p;
 
181
    }
 
182
 
 
183
    public static Hashtable cloneStringHashtable(Hashtable tbl,
 
184
            String renamePrefix) {
 
185
        Hashtable out = new Hashtable();
 
186
        for (Enumeration e = tbl.keys(); e.hasMoreElements();) {
 
187
            String key = (String) e.nextElement();
 
188
            String val = (String) tbl.get(key);
 
189
 
 
190
            if (renamePrefix == null) {
 
191
                out.put(new String(key), new String(val));
 
192
            } else {
 
193
                out.put(renamePrefix + key, new String(val));
 
194
            }
 
195
        }
 
196
        return out;
 
197
    }
 
198
 
 
199
 
 
200
    public static Vector commaStringToVector(String s) {
 
201
        Vector v = new Vector();
 
202
        if (s != null) {
 
203
            StringTokenizer st = new StringTokenizer(s, ",");
 
204
            while (st.hasMoreTokens()) {
 
205
                v.addElement(st.nextToken());
 
206
            }
 
207
        }
 
208
        return v;
 
209
    }
 
210
 
 
211
    public static String vectorToCommaString(Vector v) {
 
212
        String s = "";
 
213
        Enumeration e = v.elements();
 
214
        while (e.hasMoreElements()) {
 
215
            s = s + e.nextElement();
 
216
            if (e.hasMoreElements())
 
217
                s = s + ",";
 
218
        }
 
219
        return s;
 
220
    }
 
221
 
 
222
    public java.awt.Component getDialogParent() {
 
223
        return _dialogParent;
 
224
    }
 
225
 
 
226
    public void setDialogParent (java.awt.Component comp) {
 
227
        _dialogParent = comp;
 
228
    }
 
229
 
 
230
    /**
 
231
      * CGI arguments used in getConfiguration()
 
232
     */
 
233
    public abstract String getCGIParamsForGetOp();
 
234
 
 
235
    /**
 
236
     * CGI arguments used in setConfiguration()
 
237
    */
 
238
    public abstract String getCGIParamsForSetOp();
 
239
 
 
240
    protected Hashtable getConfiguration(String adminURL,
 
241
            String args) throws RemoteRequestException {
 
242
        AdmTask task = null;
 
243
        try {
 
244
            task = new AdmTask(new URL(adminURL),
 
245
                    _consoleInfo.getAuthenticationDN(),
 
246
                    _consoleInfo.getAuthenticationPassword());
 
247
        } catch (MalformedURLException e) {
 
248
            Debug.println("CGIDataModel.getConfiguration "+e);
 
249
            throw new RemoteRequestException(e);
 
250
        }
 
251
 
 
252
        //task.trace();
 
253
        if (args != null) {
 
254
            //task.setArguments(HttpChannel._encode(args));
 
255
            task.setArguments(args);
 
256
        }
 
257
        task.exec();
 
258
 
 
259
        int status = task.getStatus();
 
260
        _loaded = (status == 0);
 
261
        Debug.println("CGIDataModel.getConfiguration(): called URL " +
 
262
                      adminURL + " "+status);
 
263
 
 
264
        AdminOperation.processAdmTaskStatus(adminURL, task, _consoleInfo);
 
265
 
 
266
        if (task != null) {
 
267
            //Debug.printHashtable("config", task.getResult());
 
268
            return task.getResult();
 
269
        }
 
270
 
 
271
        return new Hashtable();
 
272
 
 
273
    }
 
274
 
 
275
    protected void setConfiguration(String adminURL,
 
276
            String args) throws RemoteRequestException {
 
277
        AdmTask task = null;
 
278
        try {
 
279
            task = new AdmTask(new URL(adminURL),
 
280
                    _consoleInfo.getAuthenticationDN(),
 
281
                    _consoleInfo.getAuthenticationPassword());
 
282
        } catch (MalformedURLException e) {
 
283
            Debug.println("CGIDataModel.setConfiguration "+e);
 
284
            throw new RemoteRequestException(e);
 
285
        }
 
286
 
 
287
        //task.trace();
 
288
        if (args != null) {
 
289
            task.setArguments(args);
 
290
        }
 
291
        task.exec();
 
292
 
 
293
        int status = task.getStatus();
 
294
        Debug.println(adminURL + " "+status);
 
295
        _saved = (status == 0);
 
296
        AdminOperation.processAdmTaskStatus(adminURL, task, _consoleInfo);
 
297
    }
 
298
}