summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java
blob: 9a9d4bd4fc17ad0a0e2e47bcbd39b584d78f7cb1 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// --- 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.logging;


import java.io.*;
import java.util.*;
import java.text.MessageFormat;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.logging.*;
import com.netscape.certsrv.apps.CMS;
import com.netscape.cmscore.util.Debug;


/**
 * A class represents a log subsystem.
 * <P>
 * 
 * @author thomask
 * @author mzhao
 * @version $Revision$, $Date$
 */
public class LogSubsystem implements ILogSubsystem {

    private static LogSubsystem mInstance = new LogSubsystem();
    private static ILogQueue mLogQueue = LogQueue.getLogQueue();
    private IConfigStore mConfig = null;

    public static final String PROP_LOGGING = "log";

    public static final String ID = "log";

    public static final String PROP_CLASS = "class";
    public static final String PROP_IMPL = "impl";
    public static final String PROP_PLUGIN = "pluginName";
    public static final String PROP_INSTANCE = "instance";

    public Hashtable mLogPlugins = new Hashtable();
    public Hashtable mLogInsts = new Hashtable();

    /**
     * Constructs a log subsystem.
     */
    private LogSubsystem() {
    }

    public String getId() {
        return ID;
    }

    public void setId(String id) throws EBaseException {
        throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_OPERATION"));
    }

    /**
     * Initializes the log subsystem.
     * <P>
     *
     * @param owner owner of this subsystem
     * @param config configuration store
     */
    public void init(ISubsystem owner, IConfigStore config)
        throws EBaseException {
        mConfig = config;
        mLogQueue.init();

        // load log plugin implementation
        IConfigStore c = config.getSubStore(PROP_IMPL);
        Enumeration mImpls = c.getSubStoreNames();

        while (mImpls.hasMoreElements()) {
            String id = (String) mImpls.nextElement();
            String pluginPath = c.getString(id + "." + PROP_CLASS);
            LogPlugin plugin = new LogPlugin(id, pluginPath);

            mLogPlugins.put(id, plugin);
        }
        if (Debug.ON)
            Debug.trace("loaded logger plugins");

            // load log instances
        c = config.getSubStore(PROP_INSTANCE);
        Enumeration instances = c.getSubStoreNames();

        while (instances.hasMoreElements()) {
            String insName = (String) instances.nextElement();
            String implName = c.getString(insName + "." + 
                    PROP_PLUGIN);
            LogPlugin plugin =
                (LogPlugin) mLogPlugins.get(implName);

            if (plugin == null) { 
                throw new EBaseException(implName);
            }
            String className = plugin.getClassPath();
            // Instantiate and init the log listener.
            ILogEventListener logInst = null;

            try {
                logInst = (ILogEventListener)
                        Class.forName(className).newInstance();
                IConfigStore pConfig = 
                    c.getSubStore(insName);

                logInst.init(this, pConfig);
                // for view from console

            } catch (ClassNotFoundException e) {
                String errMsg = "LogSubsystem:: init()-" + e.toString();

                throw new EBaseException(insName + ":Failed to instantiate class " + className);
            } catch (IllegalAccessException e) {
                String errMsg = "LogSubsystem:: init()-" + e.toString();

                throw new EBaseException(insName + ":Failed to instantiate class " + className);
            } catch (InstantiationException e) {
                String errMsg = "LogSubsystem:: init()-" + e.toString();

                throw new EBaseException(insName + ":Failed to instantiate class " + className);
            } catch (Throwable e) {
                e.printStackTrace();
                throw new EBaseException(insName + ":Failed to instantiate class " + className + " error: " + e.getMessage());
            }

            if (logInst == null) {
                throw new EBaseException("Failed to instantiate class " + className);
            }

            if (insName == null) {
                throw new EBaseException("Failed to instantiate class " + insName);
            }

            // add log instance to list.
            mLogInsts.put(insName, logInst);
            if (Debug.ON)
                Debug.trace("loaded log instance " + insName + " impl " + implName);
        }

    }

    public void startup() throws EBaseException {
        Debug.trace("entering LogSubsystem.startup()");
        Enumeration enum1 = mLogInsts.keys();

        while (enum1.hasMoreElements()) {
            String instName = (String) enum1.nextElement();

            Debug.trace("about to call inst=" + instName + " in LogSubsystem.startup()");
            ILogEventListener inst = (ILogEventListener)
                mLogInsts.get(instName);

            inst.startup();
        }
    }

    /**
     * Stops this subsystem.
     * <P>
     */
    public void shutdown() {
        mLogQueue.shutdown();
    }

    /**
     * Returns the root configuration storage of this system.
     * <P>
     *
     * @return configuration store of this subsystem
     */
    public IConfigStore getConfigStore() {
        return mConfig;
    }

    /**
     * Retrieves singleton: the LogSubsystem.
     */
    public static LogSubsystem getInstance() {
        return mInstance;
    }

    /**
     * Retrieves LogQueue.
     */
    public static ILogQueue getLogQueue() {
        return mLogQueue;
    }

    public String getLogPluginName(ILogEventListener log) {
        IConfigStore cs = log.getConfigStore();

        try {
            return cs.getString("pluginName", "");
        } catch (EBaseException e) {
            return "";
        }
    }

    /**
     * Retrieve log instance by it's name
     */
    public ILogEventListener getLogInstance(String insName) {
        return (ILogEventListener) mLogInsts.get(insName);
    }

    public Hashtable getLogPlugins() {
        return mLogPlugins;
    }

    public Hashtable getLogInsts() {
        return mLogInsts;
    }

    public Vector getLogDefaultParams(String implName) throws
            ELogException {
        // is this a registered implname?
        LogPlugin plugin = (LogPlugin)
            mLogPlugins.get(implName);

        if (plugin == null) {
            throw new ELogException(implName);
        }
			
        // a temporary instance
        ILogEventListener LogInst = null;
        String className = plugin.getClassPath();

        try {
            LogInst = (ILogEventListener)
                    Class.forName(className).newInstance();
            Vector v = LogInst.getDefaultParams();

            return v;
        } catch (InstantiationException e) {
            throw new ELogException(
                    CMS.getUserMessage("CMS_LOG_LOAD_CLASS_FAIL", className));
        } catch (ClassNotFoundException e) {
            throw new ELogException(
                    CMS.getUserMessage("CMS_LOG_LOAD_CLASS_FAIL", className));
        } catch (IllegalAccessException e) {
            throw new ELogException(
                    CMS.getUserMessage("CMS_LOG_LOAD_CLASS_FAIL", className));
        }
    }

    public Vector getLogInstanceParams(String insName) throws
            ELogException {
        ILogEventListener logInst = getLogInstance(insName);

        if (logInst == null) {
            return null;
        }
        Vector v = logInst.getInstanceParams();

        return v;
    }
}