summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/extensions/CMSExtensionsMap.java
blob: aceb6f1bfa359befed22cec4bccc1df00669d573 (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
// --- 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.extensions;


import java.util.Hashtable;
import java.util.Enumeration;

import netscape.security.x509.Extension;
import netscape.security.util.ObjectIdentifier;

import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.ISubsystem;
import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.extensions.*;


/** 
 * Loads extension classes from configuration file and return 
 * for a given extension name or OID.
 */
public class CMSExtensionsMap implements ISubsystem {
    public static String ID = "extensions";

    private static CMSExtensionsMap mInstance = new CMSExtensionsMap();

    public static final CMSExtensionsMap getInstance() {
        return mInstance;
    }

    private CMSExtensionsMap() {
    }

    private static final String PROP_CLASS = "class";

    private Hashtable mName2Ext = new Hashtable();
    private Hashtable mOID2Ext = new Hashtable();
    private ISubsystem mOwner = null;
    private IConfigStore mConfig = null;

    /**
     * Create extensions from configuration store.
     * @param config the configuration store.
     */
    public void init(ISubsystem owner, IConfigStore config) 
        throws EBaseException {
        mOwner = owner;
        mConfig = config;

        Enumeration sstores = mConfig.getSubStoreNames();

        while (sstores.hasMoreElements()) {
            String name = (String) sstores.nextElement();
            IConfigStore c = mConfig.getSubStore(name);

            String className = c.getString(PROP_CLASS);
            ICMSExtension ext = null;

            try {
                ext = (ICMSExtension) Class.forName(className).newInstance();
                ext.init(this, c);
                addExt(ext);
            } catch (ClassNotFoundException e) {
                throw new EExtensionsException(
                        CMS.getUserMessage("CMS_EXTENSION_CLASS_NOT_FOUND", className));
            } catch (IllegalAccessException e) {
                throw new EExtensionsException(
                        CMS.getUserMessage("CMS_EXTENSION_INSTANTIATE_ERROR",
                        className, e.toString()));
            } catch (InstantiationException e) {
                throw new EExtensionsException(
                        CMS.getUserMessage("CMS_EXTENSION_INSTANTIATE_ERROR",
                        className, e.toString()));
            } catch (ClassCastException e) {
                throw new EExtensionsException(
                        CMS.getUserMessage("CMS_EXTENSION_INVALID_IMPL", className));
            }
        }
    }

    public void addExt(ICMSExtension ext) throws EBaseException {
        String name = ext.getName();
        ObjectIdentifier oid = ext.getOID();

        if (name == null || oid == null) {
            throw new EExtensionsException(
                    CMS.getUserMessage("CMS_EXTENSION_INCORRECT_IMPL",
                    ext.getClass().getName()));
        }
        mName2Ext.put(name, ext);
        mOID2Ext.put(oid.toString(), ext);
    }

    /**
     * startup - does nothing.
     */
    public void startup() throws EBaseException {
    }

    /**
     * shutdown - does nothing.
     */
    public void shutdown() {
    }

    /**
     * Get configuration store. 
     */
    public IConfigStore getConfigStore() {
        return mConfig;
    }

    /**
     * Returns subsystem ID 
     */
    public String getId() {
        return ID;
    }

    /**
     * sets subsystem ID 
     */
    public void setId(String Id) {
    }

    /**
     * Get the extension class by name.
     * @param name name of the extension
     * @return the extension class. 
     */
    public ICMSExtension getByName(String name) {
        return (ICMSExtension) mName2Ext.get(name);
    }

    /**
     * Get the extension class by its OID.
     * @param oid - the OID of the extension.
     * @return the extension class.
     */
    public ICMSExtension getByOID(ObjectIdentifier oid) {
        return (ICMSExtension) mOID2Ext.get(oid.toString());
    }
}