001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.monitoring;
006:
007: import com.sun.portal.log.common.PortalLogger;
008: import com.sun.portal.monitoring.security.SaslConfiguration;
009: import com.sun.portal.monitoring.security.SecurityContext;
010: import com.sun.portal.monitoring.security.SecurityException;
011: import com.sun.portal.monitoring.security.SslConfiguration;
012: import com.sun.portal.monitoring.security.User;
013: import com.sun.portal.monitoring.security.sasl.SaslContext;
014: import com.sun.portal.monitoring.security.ssl.SslContext;
015: import com.sun.portal.monitoring.utilities.PropertyHelper;
016: import com.sun.portal.monitoring.utilities.UtilityException;
017:
018: import java.io.File;
019: import java.net.InetAddress;
020: import java.net.UnknownHostException;
021: import java.security.Provider;
022: import java.security.Security;
023: import java.util.Properties;
024: import java.util.logging.Level;
025: import java.util.logging.LogRecord;
026: import java.util.logging.Logger;
027:
028: public class MonitoringConfiguration {
029: private static final Logger logger = PortalLogger
030: .getLogger(MonitoringConfiguration.class);
031:
032: private static LogRecord getLogRecord(Level level, String message,
033: Object[] parameters, Throwable t) {
034: LogRecord result = new LogRecord(level, message);
035: result.setLoggerName(logger.getName());
036: result.setParameters(parameters);
037: result.setThrown(t);
038: return result;
039: }
040:
041: public MonitoringConfiguration() {
042: this (null);
043: }
044:
045: public MonitoringConfiguration(PropertyHelper propertyHelper) {
046: this .propertyHelper = propertyHelper;
047: if (propertyHelper == null) {
048: this .propertyHelper = new PropertyHelper(null);
049: }
050: }
051:
052: private PropertyHelper propertyHelper;
053:
054: protected static void configure(MonitoringContext monitoringContext)
055: throws MonitoringException {
056: User user = new User(monitoringContext.getPropertyHelper());
057: user.setName(monitoringContext.getUserName());
058: user.setPassword(monitoringContext.getUserPassword()
059: .toCharArray());
060:
061: SaslConfiguration saslConfiguration = new SaslConfiguration(
062: monitoringContext.getSaslContext());
063: try {
064: saslConfiguration.createUserPassword(user);
065: } catch (SecurityException se) {
066: throw new MonitoringException(se);
067: }
068:
069: SslConfiguration sslConfiguration = new SslConfiguration(
070: monitoringContext.getSecurityContext());
071: try {
072: sslConfiguration.createKeyStore();
073: } catch (SecurityException se) {
074: throw new MonitoringException(se);
075: }
076:
077: try {
078: sslConfiguration.createTrustStore();
079: } catch (SecurityException se) {
080: throw new MonitoringException(se);
081: }
082: }
083:
084: protected static void unConfigure(
085: MonitoringContext monitoringContext) {
086: SaslConfiguration saslConfiguration = new SaslConfiguration(
087: monitoringContext.getSaslContext());
088: saslConfiguration.deleteUserPassword();
089:
090: SslConfiguration sslConfiguration = new SslConfiguration(
091: monitoringContext.getSecurityContext());
092: sslConfiguration.deleteKeyStore();
093: sslConfiguration.deleteTrustStore();
094: }
095:
096: protected String getConfigurationDirectory(
097: PropertyHelper propertyHelper) {
098: return propertyHelper
099: .getProperty(
100: MonitoringContext.class.getName(),
101: MonitoringContext.PROPERTY_SUFFIX_CONFIGURATION_DIRECTORY,
102: CURRENT_DIRECTORY);
103: }
104:
105: protected String getSecurityDirectory(PropertyHelper propertyHelper) {
106: return getConfigurationDirectory(propertyHelper)
107: + File.separator + SECURITY_DIRECTORY;
108: }
109:
110: protected String getMode(PropertyHelper propertyHelper) {
111: return propertyHelper.getProperty(MonitoringContext.class
112: .getName(),
113: MonitoringContext.PROPERTY_SUFFIX_SUBSYSTEM_MODE,
114: MonitoringContext.SUBSYSTEM_MODE);
115: }
116:
117: public MonitoringContext getMonitoringContext() {
118: if (monitoringContext == null) {
119: monitoringContext = new MonitoringContext(propertyHelper);
120:
121: try {
122: propertyHelper.setProperty(SecurityContext.class
123: .getName(),
124: SecurityContext.PROPERTY_SUFFIX_CERTIFICATE_CN,
125: InetAddress.getLocalHost()
126: .getCanonicalHostName());
127: } catch (UnknownHostException e) {
128: if (logger.isLoggable(Level.SEVERE)) {
129: logger.log(getLogRecord(Level.SEVERE,
130: "PSMN_CSPM0001", new Object[] { e
131: .getLocalizedMessage() }, e));
132: }
133: }
134:
135: propertyHelper.setProperty(SslContext.class.getName(),
136: SslContext.PROPERTY_SUFFIX_HOST_NAME, "localhost");
137: propertyHelper
138: .setProperty(
139: SaslContext.class.getName(),
140: SaslContext.PROPERTY_SUFFIX_USER_PASSWORD_DIRECTORY,
141: getSecurityDirectory(propertyHelper));
142: propertyHelper.setProperty(SecurityContext.class.getName(),
143: SecurityContext.PROPERTY_SUFFIX_DIRECTORY,
144: getSecurityDirectory(propertyHelper));
145: propertyHelper
146: .setProperty(
147: MonitoringContext.class.getName(),
148: MonitoringContext.PROPERTY_SUFFIX_CUSTOM_PROPERTIES_DIRECTORY,
149: getConfigurationDirectory(propertyHelper));
150:
151: monitoringContext.gearUp();
152: }
153:
154: return monitoringContext;
155: }
156:
157: public void setMonitoringContext(MonitoringContext monitoringContext) {
158: this .monitoringContext = monitoringContext;
159: }
160:
161: private static final String PROVIDER_NAME_SUN_JCE = "SunJCE";
162: private static final String PROVIDER_CLASS_NAME_SUN_JCE = "com.sun.crypto.provider.SunJCE";
163: private static final String PROVIDER_NAME_SUN_JSSE = "SunJSSE";
164: private static final String PROVIDER_CLASS_NAME_SUN_JSSE = "com.sun.net.ssl.internal.ssl.Provider";
165:
166: private Provider getProvider(String providerClassName) {
167: try {
168: Class clazz = getClass().getClassLoader().loadClass(
169: providerClassName);
170: return (Provider) clazz.newInstance();
171: } catch (ClassNotFoundException e) {
172: if (logger.isLoggable(Level.SEVERE)) {
173: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
174: new Object[] { e.getLocalizedMessage() }, e));
175: }
176: } catch (IllegalAccessException e) {
177: if (logger.isLoggable(Level.SEVERE)) {
178: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
179: new Object[] { e.getLocalizedMessage() }, e));
180: }
181: } catch (InstantiationException e) {
182: if (logger.isLoggable(Level.SEVERE)) {
183: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
184: new Object[] { e.getLocalizedMessage() }, e));
185: }
186: } catch (RuntimeException e) {
187: if (logger.isLoggable(Level.SEVERE)) {
188: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
189: new Object[] { e.getLocalizedMessage() }, e));
190: }
191: throw e;
192: } catch (Throwable t) {
193: if (logger.isLoggable(Level.SEVERE)) {
194: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
195: new Object[] { t.getLocalizedMessage() }, t));
196: }
197: throw new RuntimeException(t);
198: }
199:
200: return null;
201: }
202:
203: private void gearUpSecurityProvider(String providerName,
204: String providerClassName) {
205: Provider provider = Security.getProvider(providerName);
206: if (provider == null) {
207: provider = getProvider(providerClassName);
208: if (provider != null) {
209: Security.addProvider(provider);
210: }
211: }
212: }
213:
214: private void gearUpSecurityProviders(
215: MonitoringContext monitoringContext) {
216: Properties properties = monitoringContext.getPropertyHelper()
217: .getProperties();
218:
219: if (properties
220: .getProperty(SubsystemProperties.SECRET_KEY_ALGORITHM_PROVIDER_NAME) == null) {
221: gearUpSecurityProvider(PROVIDER_NAME_SUN_JCE,
222: PROVIDER_CLASS_NAME_SUN_JCE);
223: } else {
224: gearUpSecurityProvider(
225: properties
226: .getProperty(SubsystemProperties.SECRET_KEY_ALGORITHM_PROVIDER_NAME),
227: properties
228: .getProperty(SubsystemProperties.SECRET_KEY_ALGORITHM_PROVIDER_CLASS_NAME));
229: }
230:
231: if (properties
232: .getProperty(SubsystemProperties.CIPHER_TRANSFORMATION_PROVIDER_NAME) == null) {
233: gearUpSecurityProvider(PROVIDER_NAME_SUN_JCE,
234: PROVIDER_CLASS_NAME_SUN_JCE);
235: } else {
236: gearUpSecurityProvider(
237: properties
238: .getProperty(SubsystemProperties.CIPHER_TRANSFORMATION_PROVIDER_NAME),
239: properties
240: .getProperty(SubsystemProperties.CIPHER_TRANSFORMATION_PROVIDER_CLASS_NAME));
241: }
242:
243: if (properties
244: .getProperty(SubsystemProperties.JSSE_PROVIDER_NAME) == null) {
245: gearUpSecurityProvider(PROVIDER_NAME_SUN_JSSE,
246: PROVIDER_CLASS_NAME_SUN_JSSE);
247: } else {
248: gearUpSecurityProvider(
249: properties
250: .getProperty(SubsystemProperties.JSSE_PROVIDER_NAME),
251: properties
252: .getProperty(SubsystemProperties.JSSE_PROVIDER_CLASS_NAME));
253: }
254: }
255:
256: public void setUp() throws MonitoringException {
257: MonitoringContext monitoringContext = getMonitoringContext();
258:
259: gearUpSecurityProviders(monitoringContext);
260:
261: File file = new File(getSecurityDirectory(monitoringContext
262: .getPropertyHelper()));
263: if (!file.exists()) {
264: file.mkdirs();
265:
266: MonitoringConfiguration.configure(monitoringContext);
267: }
268:
269: monitoringContext.gearUpConnectorContext();
270: }
271:
272: public void tearDown() throws MonitoringException {
273: MonitoringContext monitoringContext = getMonitoringContext();
274:
275: if (getMode(monitoringContext.getPropertyHelper()).equals(
276: MonitoringContext.SUBSYSTEM_MODE)) {
277: MonitoringConfiguration.unConfigure(monitoringContext);
278:
279: File file = new File(getSecurityDirectory(monitoringContext
280: .getPropertyHelper()));
281: if (file.exists()) {
282: file.delete();
283: }
284: }
285: }
286:
287: public static void main(String[] args) throws MonitoringException {
288: new MonitoringConfiguration().process(args);
289: }
290:
291: public void process(String[] args) throws MonitoringException {
292: MonitoringConfiguration monitoringConfiguration = null;
293:
294: try {
295: if (args.length == 0) {
296: monitoringConfiguration = new MonitoringConfiguration();
297: monitoringConfiguration.setUp();
298: } else if (args.length == 1) {
299: PropertyHelper propertyHelper = new PropertyHelper(null);
300: try {
301: propertyHelper.loadProperties(args[0]);
302: } catch (UtilityException e) {
303: throw new MonitoringException(e);
304: }
305:
306: monitoringConfiguration = new MonitoringConfiguration(
307: propertyHelper);
308: monitoringConfiguration.setUp();
309: } else if (args.length == 2) {
310: PropertyHelper propertyHelper = new PropertyHelper(null);
311: try {
312: propertyHelper.loadProperties(args[0]);
313: } catch (UtilityException e) {
314: throw new MonitoringException(e);
315: }
316:
317: monitoringConfiguration = new MonitoringConfiguration(
318: propertyHelper);
319: if (args[1].equals("setUp")) {
320: monitoringConfiguration.setUp();
321: } else if (args[1].equals("tearDown")) {
322: monitoringConfiguration.tearDown();
323: }
324: }
325: } finally {
326: if (monitoringConfiguration != null) {
327: monitoringConfiguration.getMonitoringContext()
328: .getPropertyHelper().printProperties();
329: }
330: }
331: }
332:
333: private static String CURRENT_DIRECTORY = ".";
334: private static String SECURITY_DIRECTORY = "security";
335: private MonitoringContext monitoringContext;
336: }
|