summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/request/RequestRepository.java
blob: 92c162ca8a701d9b33117cb0e32700f740a569cc (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.request;

import java.math.BigInteger;
import java.util.Enumeration;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.dbs.EDBException;
import com.netscape.certsrv.dbs.IDBSSession;
import com.netscape.certsrv.dbs.IDBSubsystem;
import com.netscape.certsrv.dbs.Modification;
import com.netscape.certsrv.dbs.ModificationSet;
import com.netscape.certsrv.dbs.repository.IRepositoryRecord;
import com.netscape.certsrv.request.IRequestQueue;
import com.netscape.cmscore.dbs.Repository;
import com.netscape.cmscore.dbs.RepositoryRecord;

/**
 * TODO: what does this class provide beyond the Repository
 * base class??
 * <p>
 * 
 * @author thayes
 * @version $Revision$ $Date$
 */
class RequestRepository
        extends Repository {

    IDBSubsystem mDB = null;
    IRequestQueue mRequestQueue = null;

    /**
     * Create a request repository that uses the LDAP database
     * <p>
     * 
     * @param name
     *            the name of the repository. This String is used to
     *            construct the DN for the repository's LDAP entry.
     * @param db
     *            the LDAP database system.
     */
    public RequestRepository(String name, int increment, IDBSubsystem db)
            throws EDBException {
        super(db, increment, "ou=" + name + ",ou=requests," + db.getBaseDN());

        CMS.debug("RequestRepository: constructor 1");
        mBaseDN = "ou=" + name + ",ou=requests," + db.getBaseDN();

        // Let RequestRecord class register its
        // database mapping and object mapping values
        RequestRecord.register(db);
        mDB = db;
    }

    public RequestRepository(String name, int increment, IDBSubsystem db, IRequestQueue requestQueue)
            throws EDBException {
        super(db, increment, "ou=" + name + ",ou=requests," + db.getBaseDN());

        CMS.debug("RequestRepository: constructor2.");
        mRequestQueue = requestQueue;
        mBaseDN = "ou=" + name + ",ou=requests," + db.getBaseDN();

        // Let RequestRecord class register its
        // database mapping and object mapping values
        RequestRecord.register(db);
        mDB = db;
    }

    /**
     * get the LDAP base DN for this repository. This
     * value can be used by the request queue to create the
     * name for the request records themselves.
     * <p>
     * 
     * @return
     *         the LDAP base DN.
     */
    public String getBaseDN() {
        return mBaseDN;
    }

    /**
     * Resets serial number.
     */
    public void resetSerialNumber(BigInteger serial) throws EBaseException {
        setTheSerialNumber(serial);
    }

    /**
     * Removes all objects with this repository.
     */
    public void removeAllObjects() throws EBaseException {
        IDBSSession s = mDB.createSession();
        try {
            Enumeration e = s.search(getBaseDN(),
                               "(" + RequestRecord.ATTR_REQUEST_ID + "=*)");
            while (e.hasMoreElements()) {
                RequestRecord r = (RequestRecord) e.nextElement();
                String name = "cn" + "=" +
                        r.getRequestId().toString() + "," + getBaseDN();
                s.delete(name);
            }
        } finally {
            if (s != null)
                s.close();
        }
    }

    public BigInteger getLastSerialNumberInRange(BigInteger min, BigInteger max) {

        CMS.debug("RequestRepository: in getLastSerialNumberInRange: min " + min + " max " + max);

        CMS.debug("RequestRepository: mRequestQueue " + mRequestQueue);

        BigInteger ret = null;

        if (mRequestQueue == null) {

            CMS.debug("RequestRepository:  mRequestQueue is null.");

        } else {

            CMS.debug("RequestRepository: about to call mRequestQueue.getLastRequestIdInRange");
            ret = mRequestQueue.getLastRequestIdInRange(min, max);

        }

        return ret;

    }

    /**
     * the LDAP base DN for this repository
     */
    protected String mBaseDN;

    public String getPublishingStatus() {
        RepositoryRecord record = null;
        Object obj = null;
        IDBSSession dbs = null;
        String status = null;

        try {
            dbs = mDB.createSession();
            obj = dbs.read(mBaseDN);
        } catch (Exception e) {
            CMS.debug("RequestRepository:  getPublishingStatus:  Error: " + e);
            CMS.debugStackTrace();
        } finally {
            // Close session - ignoring errors (UTIL)
            if (dbs != null) {
                try {
                    dbs.close();
                } catch (Exception ex) {
                    CMS.debug("RequestRepository:  getPublishingStatus:  Error: " + ex);
                }
            }
        }

        if (obj != null || (obj instanceof RepositoryRecord)) {
            record = (RepositoryRecord) obj;
            status = record.getPublishingStatus();
        } else {
            CMS.debug("RequestRepository:  obj is NOT instanceof RepositoryRecord");
        }
        CMS.debug("RequestRepository:  getPublishingStatus  mBaseDN: " + mBaseDN +
                  "  status: " + ((status != null) ? status : "null"));

        return status;
    }

    public void setPublishingStatus(String status) {
        IDBSSession dbs = null;

        CMS.debug("RequestRepository:  setPublishingStatus  mBaseDN: " + mBaseDN + "  status: " + status);
        ModificationSet mods = new ModificationSet();

        if (status != null && status.length() > 0) {
            mods.add(IRepositoryRecord.ATTR_PUB_STATUS,
                    Modification.MOD_REPLACE, status);

            try {
                dbs = mDB.createSession();
                dbs.modify(mBaseDN, mods);
            } catch (Exception e) {
                CMS.debug("RequestRepository:  setPublishingStatus:  Error: " + e);
                CMS.debugStackTrace();
            } finally {
                // Close session - ignoring errors (UTIL)
                if (dbs != null) {
                    try {
                        dbs.close();
                    } catch (Exception ex) {
                        CMS.debug("RequestRepository:  setPublishingStatus:  Error: " + ex);
                    }
                }
            }
        }
    }
}