001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and iPlanet
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.wireless.taglibs.commprefs;
015:
016: import com.sun.portal.log.common.PortalLogger;
017: import com.sun.ssoadapter.config.Configuration;
018:
019: import java.util.Arrays;
020: import java.util.List;
021: import java.util.ArrayList;
022: import java.util.Map;
023: import java.util.Vector;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: public class SSOConfigurationBean extends SaveableBean {
028:
029: /**
030: * A SSOConfigurationBean is a named bean backed by an ssoadapter Configuration, that keeps track of whether is has been updated
031: * since construction. The name is not stored in the map.
032: */
033:
034: private static Logger logger = PortalLogger
035: .getLogger(SSOConfigurationBean.class);
036: protected Configuration configuration = null;
037: private String name = null;
038: private String oldName = null; // for rename handling
039:
040: public SSOConfigurationBean() {
041: configuration = new Configuration();
042: }
043:
044: public SSOConfigurationBean(String name, Configuration configuration) {
045: this .name = name;
046: this .configuration = configuration;
047: }
048:
049: public Configuration getConfiguration() {
050: return configuration;
051: }
052:
053: public String getName() {
054: return name;
055: }
056:
057: public void setName(String name) {
058: if (this .name != null)
059: setOldName(this .name);
060: this .name = name;
061: configuration.setConfigurationName(name);
062: wasUpdated = true;
063: }
064:
065: public String getOldName() {
066: return oldName;
067: }
068:
069: public void setOldName(String oldName) {
070: this .oldName = oldName;
071: }
072:
073: /**
074: * Returns empty string if absent.
075: * @param property
076: * @return
077: */
078: public String getProperty(String property) {
079: String value = "";
080: if (property != null) {
081: value = configuration.getProperty(property, "");
082: }
083: return value;
084: }
085:
086: /**
087: * Only set if String value is non-null or empty string, and has changed.
088: * @param property
089: * @param value
090: */
091: public void setProperty(String property, String value) {
092: if ((value != null) && (!"".equals(value))
093: && (!value.equals(configuration.getProperty(property)))) {
094: configuration.setProperty(property, value);
095: wasUpdated = true;
096: }
097: }
098:
099: /**
100: * Get property array as a list.
101: * @param property
102: * @return
103: */
104: public List getPropertyList(String property) {
105: List valueList = new ArrayList();
106: try {
107: Object[] values = configuration.getPropertyArray(property);
108: if (values != null)
109: valueList = Arrays.asList(values);
110: } catch (Exception e) {
111: logger.log(Level.WARNING, "PSMA_CSPWTP0012", property);
112: }
113: return valueList;
114: }
115:
116: /**
117: * Add a String value to the values list:
118: * '*CLEAR*' or null - clears all values
119: * non-null value - add the value to the list
120: * @param property
121: * @param value
122: */
123: public void addValueToList(String property, String value) {
124: if (configuration == null) {
125: logger.log(Level.WARNING, "PSMA_CSPWTP0010", this );
126: return;
127: }
128: String[] valuesArray = new String[0];
129: try {
130: if ((value == null) || (value.equals("*CLEAR*"))) {
131: configuration.setPropertyArray(property, valuesArray);
132: } else {
133: // non-null: add to list
134: Vector values = new Vector(getPropertyList(property));
135: if (values == null) {
136: values = new Vector();
137: }
138: values.add(value);
139: valuesArray = (String[]) values.toArray(new String[0]);
140: configuration.setPropertyArray(property, valuesArray);
141: }
142: wasUpdated = true;
143: } catch (Exception e) {
144: logger.log(Level.WARNING, "PSMA_CSPWTP0011", e);
145: }
146: }
147:
148: /**
149: * Set property array as a list of non-null Strings.
150: * @param property
151: * @param values
152: */
153: public void setPropertyList(String property, List values) {
154: if (values == null)
155: return;
156: String[] valuesArray = (String[]) values.toArray(new String[0]);
157: configuration.setPropertyArray(property, valuesArray);
158: }
159:
160: public Map getProperties() {
161: return configuration.getHashMap();
162: }
163:
164: public boolean isDelete() {
165: return delete;
166: }
167:
168: public void setDelete(boolean delete) {
169: this .delete = delete;
170: }
171:
172: /**
173: * The configuration has been saved since the last update.
174: */
175: public void upToDate() {
176: super .upToDate();
177: // just deleted old config after rename, so oldName no longer useful
178: this.oldName = null;
179: }
180:
181: }
|