001: package com.sun.portal.ubt.admin.mbeans;
002:
003: import com.sun.portal.admin.server.mbeans.PSResource;
004: import com.sun.portal.admin.common.context.PSConfigContext;
005: import com.sun.portal.admin.common.context.PortalDomainContext;
006: import com.sun.portal.admin.common.PSMBeanException;
007: import com.sun.portal.log.common.PortalLogger;
008: import com.sun.portal.log.common.EnhancedProperties;
009:
010: import java.util.Hashtable;
011: import java.util.List;
012: import java.util.Properties;
013: import java.util.Enumeration;
014: import java.util.Set;
015: import java.util.HashSet;
016: import java.util.logging.Logger;
017: import java.util.logging.Level;
018: import java.util.logging.LogRecord;
019: import java.io.*;
020: import java.lang.reflect.Field;
021: import com.sun.portal.ubt.report.client.UBTConstantsClient;
022:
023: /**
024: *
025: */
026: public class UBTSettings extends PSResource implements UBTSettingsMBean {
027: private static Set excludeSet = new HashSet();
028: private String psPortalID;
029: private static Logger logger = PortalLogger
030: .getLogger(UBTSettings.class);
031: private static String UBT_CONFIG_FILE = "UBTConfig.properties";
032: static {
033: Field[] fields = UBTConstantsClient.class.getFields();
034:
035: try {
036: for (int i = 0; i < fields.length; i++)
037: excludeSet.add(fields[i].get(UBTConstantsClient.class));
038: } catch (IllegalAccessException e) {
039: //dropthrough!
040: }
041: }
042:
043: public void init(PSConfigContext cc, PortalDomainContext pdc,
044: List path) {
045: super .init(cc, pdc, path);
046: psPortalID = (String) path.get(1);
047: }
048:
049: public Hashtable getUBTSettings() throws Exception {
050: return (Hashtable) getUBTConfigProperties();
051: }
052:
053: public void setUBTSettings(Hashtable values) throws Exception {
054: EnhancedProperties props = getUBTConfigEnhancedProperties();
055: updateProperties(props, values);
056: FileOutputStream fos = null;
057: try {
058: fos = new FileOutputStream(new File(getUBTConfigFileName()));
059: props.store(fos, null);
060: } finally {
061: if (fos != null) {
062: fos.close();
063: }
064: }
065: }
066:
067: private Properties getUBTConfigProperties() throws PSMBeanException {
068: File ubtPropertyFile = new File(getUBTConfigFileName());
069: Properties ubtProperties = new Properties();
070: FileInputStream fis = null;
071: try {
072: fis = new FileInputStream(ubtPropertyFile);
073: ubtProperties.load(fis);
074: } catch (Exception e) {
075: if (logger.isLoggable(Level.SEVERE)) {
076: LogRecord record = new LogRecord(Level.SEVERE,
077: "PSUB_CSPUAM0003");
078: record.setParameters(new String[] { getID() });
079: record.setThrown(e);
080: record.setLoggerName(logger.getName());
081: logger.log(record);
082: }
083:
084: throw new PSMBeanException(
085: "admin.error.ubt.reading.ubt.properties", e
086: .getMessage(), e, new Object[] { getID() });
087: } finally {
088: if (fis != null) {
089: try {
090: fis.close();
091: } catch (IOException e) {
092: //drop through
093: }
094: }
095: }
096:
097: return ubtProperties;
098:
099: }
100:
101: private String getUBTConfigFileName() {
102: String dataPath = cc.getPSDataDir();
103: String ubtPropertyFilePath = dataPath + fs + "portals" + fs
104: + psPortalID + fs + "config" + fs + UBT_CONFIG_FILE;
105: return ubtPropertyFilePath;
106: }
107:
108: private EnhancedProperties getUBTConfigEnhancedProperties()
109: throws PSMBeanException {
110: File ubtPropertyFile = new File(getUBTConfigFileName());
111: EnhancedProperties ubtProperties = new EnhancedProperties();
112: BufferedInputStream bis = null;
113: try {
114: bis = new BufferedInputStream(new FileInputStream(
115: ubtPropertyFile));
116: ubtProperties.load(bis);
117: } catch (Exception e) {
118: if (logger.isLoggable(Level.SEVERE)) {
119: LogRecord record = new LogRecord(Level.SEVERE,
120: "PSUB_CSPUAM0003");
121: record.setParameters(new String[] { getID() });
122: record.setThrown(e);
123: record.setLoggerName(logger.getName());
124: logger.log(record);
125: }
126:
127: throw new PSMBeanException(
128: "admin.error.ubt.reading.ubt.properties", e
129: .getMessage(), e, new Object[] { getID() });
130: } finally {
131: if (bis != null) {
132: try {
133: bis.close();
134: } catch (IOException e) {
135: //drop through
136: }
137: }
138: }
139:
140: return ubtProperties;
141: }
142:
143: private void updateProperties(EnhancedProperties props, Hashtable newValues){
144: removeExcludeList(props);
145: Enumeration enum = newValues.keys();
146: while(enum.hasMoreElements()){
147: String key = (String)enum.nextElement();
148: props.put(key, newValues.get(key));
149: }
150: }
151:
152: private void removeExcludeList(EnhancedProperties props) {
153: Enumeration enum = props.keys();
154: while (enum.hasMoreElements()){
155: String key = (String)enum.nextElement();
156: if (!excludeSet.contains(key)){
157: props.remove(key);
158: }
159: }
160: }
161: }
|