0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: Config.java 3634 2007-01-08 21:42:24Z gbevin $
0007: */
0008: package com.uwyn.rife.config;
0009:
0010: import com.uwyn.rife.config.exceptions.*;
0011: import com.uwyn.rife.tools.*;
0012: import java.util.*;
0013:
0014: import com.uwyn.rife.rep.Participant;
0015: import com.uwyn.rife.rep.Rep;
0016: import com.uwyn.rife.resources.ResourceFinder;
0017: import com.uwyn.rife.selector.XmlSelectorResolver;
0018: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
0019: import com.uwyn.rife.tools.exceptions.SerializationUtilsErrorException;
0020: import com.uwyn.rife.xml.exceptions.XmlErrorException;
0021: import java.io.File;
0022: import java.io.Serializable;
0023: import java.io.UnsupportedEncodingException;
0024: import java.net.URL;
0025: import java.net.URLDecoder;
0026: import java.util.logging.Logger;
0027: import java.util.prefs.BackingStoreException;
0028: import java.util.prefs.Preferences;
0029:
0030: public class Config implements Cloneable {
0031: public final static String DEFAULT_PARTICIPANT_NAME = "ParticipantConfig";
0032:
0033: public final static String PARAMETER_PREFERENCES_USER = "CONFIG_PREFERENCES_USER";
0034: public final static String PARAMETER_PREFERENCES_SYSTEM = "CONFIG_PREFERENCES_SYSTEM";
0035:
0036: private HashMap<String, String> mParameters = null;
0037: private ArrayList<String> mFinalParameters = null;
0038: private HashMap<String, ArrayList<String>> mLists = null;
0039: private ArrayList<String> mFinalLists = null;
0040: private String mXmlPath = null;
0041: private ResourceFinder mResourceFinder = null;
0042:
0043: public Config() {
0044: mParameters = new HashMap<String, String>();
0045: mFinalParameters = new ArrayList<String>();
0046: mLists = new HashMap<String, ArrayList<String>>();
0047: mFinalLists = new ArrayList<String>();
0048: }
0049:
0050: public Config(String configSource, ResourceFinder resourceFinder)
0051: throws ConfigErrorException {
0052: this (configSource, resourceFinder, null, null, null, null);
0053: }
0054:
0055: Config(String configSource, ResourceFinder resourceFinder,
0056: HashMap<String, String> parameters,
0057: ArrayList<String> finalParameters,
0058: HashMap<String, ArrayList<String>> lists,
0059: ArrayList<String> finalLists) throws ConfigErrorException {
0060: if (null == configSource)
0061: throw new IllegalArgumentException(
0062: "configSource can't be null.");
0063: if (0 == configSource.length())
0064: throw new IllegalArgumentException(
0065: "configSource can't be empty.");
0066: if (null == resourceFinder)
0067: throw new IllegalArgumentException(
0068: "resourceFinder can't be null.");
0069:
0070: mResourceFinder = resourceFinder;
0071:
0072: String config_resolved = XmlSelectorResolver.resolve(
0073: configSource, resourceFinder, "rep/config-");
0074: if (null == config_resolved) {
0075: throw new ConfigsourceNotFoundException(configSource);
0076: }
0077:
0078: URL config_resource = resourceFinder
0079: .getResource(config_resolved);
0080: if (null == config_resource) {
0081: throw new ConfigsourceNotFoundException(configSource,
0082: config_resolved);
0083: }
0084:
0085: mXmlPath = config_resolved;
0086:
0087: Xml2Config xml_config = new Xml2Config(parameters,
0088: finalParameters, lists, finalLists);
0089: initialize(xml_config);
0090: xml_config = null;
0091: }
0092:
0093: public String getXmlPath() {
0094: return mXmlPath;
0095: }
0096:
0097: private boolean isPreferencesParameter(String parameter) {
0098: if (null == parameter) {
0099: return false;
0100: }
0101:
0102: return parameter.equals(PARAMETER_PREFERENCES_SYSTEM)
0103: || parameter.equals(PARAMETER_PREFERENCES_USER);
0104:
0105: }
0106:
0107: public void setPreferencesNode(Preferences node) {
0108: if (null == node)
0109: throw new IllegalArgumentException("node can't be null.");
0110:
0111: if (node.isUserNode()) {
0112: setParameter(PARAMETER_PREFERENCES_USER, node
0113: .absolutePath());
0114: removeParameter(PARAMETER_PREFERENCES_SYSTEM);
0115: } else {
0116: removeParameter(PARAMETER_PREFERENCES_USER);
0117: setParameter(PARAMETER_PREFERENCES_SYSTEM, node
0118: .absolutePath());
0119: }
0120: }
0121:
0122: public boolean hasPreferencesNode() {
0123: return mParameters.containsKey(PARAMETER_PREFERENCES_USER)
0124: || mParameters
0125: .containsKey(PARAMETER_PREFERENCES_SYSTEM);
0126: }
0127:
0128: public Preferences getPreferencesNode() {
0129: if (mParameters.containsKey(PARAMETER_PREFERENCES_USER)) {
0130: return Preferences.userRoot().node(
0131: getString(PARAMETER_PREFERENCES_USER));
0132: }
0133:
0134: if (mParameters.containsKey(PARAMETER_PREFERENCES_SYSTEM)) {
0135: return Preferences.systemRoot().node(
0136: getString(PARAMETER_PREFERENCES_SYSTEM));
0137: }
0138:
0139: return null;
0140: }
0141:
0142: public static boolean hasRepInstance() {
0143: return Rep.hasParticipant(DEFAULT_PARTICIPANT_NAME);
0144: }
0145:
0146: public static Config getRepInstance() {
0147: Participant participant = Rep
0148: .getParticipant(DEFAULT_PARTICIPANT_NAME);
0149: if (null == participant) {
0150: return null;
0151: }
0152:
0153: return (Config) participant.getObject();
0154: }
0155:
0156: public boolean hasParameter(String parameter) {
0157: if (null == parameter)
0158: throw new IllegalArgumentException(
0159: "parameter can't be null.");
0160: if (0 == parameter.length())
0161: throw new IllegalArgumentException(
0162: "parameter can't be empty.");
0163:
0164: if (mParameters.containsKey(parameter)) {
0165: return true;
0166: }
0167:
0168: if (!isPreferencesParameter(parameter) && hasPreferencesNode()) {
0169: Preferences preferences = getPreferencesNode();
0170:
0171: if (preferences != null
0172: && preferences.get(parameter, null) != null) {
0173: return true;
0174: }
0175: }
0176:
0177: return false;
0178: }
0179:
0180: public boolean isFinalParameter(String parameter) {
0181: if (null == parameter)
0182: throw new IllegalArgumentException(
0183: "parameter can't be null.");
0184: if (0 == parameter.length())
0185: throw new IllegalArgumentException(
0186: "parameter can't be empty.");
0187:
0188: return mFinalParameters.contains(parameter);
0189: }
0190:
0191: public int countParameters() {
0192: int result = mParameters.size();
0193:
0194: if (hasPreferencesNode()) {
0195: Preferences preferences = getPreferencesNode();
0196:
0197: if (preferences != null) {
0198: try {
0199: String keys[] = preferences.keys();
0200:
0201: for (String key : keys) {
0202: if (!mParameters.containsKey(key)) {
0203: result++;
0204: }
0205: }
0206: } catch (BackingStoreException e) {
0207: // that's ok, don't handle the preferences node
0208: }
0209: }
0210: }
0211:
0212: return result;
0213: }
0214:
0215: public String getString(String parameter) {
0216: if (null == parameter)
0217: throw new IllegalArgumentException(
0218: "parameter can't be null.");
0219: if (0 == parameter.length())
0220: throw new IllegalArgumentException(
0221: "parameter can't be empty.");
0222:
0223: return getString(parameter, null);
0224: }
0225:
0226: public String getString(String parameter, String defaultValue) {
0227: if (null == parameter)
0228: throw new IllegalArgumentException(
0229: "parameter can't be null.");
0230: if (0 == parameter.length())
0231: throw new IllegalArgumentException(
0232: "parameter can't be empty.");
0233:
0234: String result = null;
0235:
0236: synchronized (this ) {
0237: if (!mFinalParameters.contains(parameter)
0238: && !isPreferencesParameter(parameter)
0239: && hasPreferencesNode()) {
0240: Preferences preferences = getPreferencesNode();
0241:
0242: if (preferences != null) {
0243: result = preferences.get(parameter, null);
0244: }
0245: }
0246:
0247: if (null == result && null != mParameters) {
0248: result = mParameters.get(parameter);
0249: }
0250: }
0251:
0252: if (null == result) {
0253: result = defaultValue;
0254: }
0255:
0256: return result;
0257: }
0258:
0259: public boolean getBool(String parameter) {
0260: if (null == parameter)
0261: throw new IllegalArgumentException(
0262: "parameter can't be null.");
0263: if (0 == parameter.length())
0264: throw new IllegalArgumentException(
0265: "parameter can't be empty.");
0266:
0267: return getBool(parameter, false);
0268: }
0269:
0270: public boolean getBool(String parameter, boolean defaultValue) {
0271: if (null == parameter)
0272: throw new IllegalArgumentException(
0273: "parameter can't be null.");
0274: if (0 == parameter.length())
0275: throw new IllegalArgumentException(
0276: "parameter can't be empty.");
0277:
0278: String string_parameter = getString(parameter);
0279: if (null != string_parameter) {
0280: return StringUtils.convertToBoolean(string_parameter);
0281: } else {
0282: return defaultValue;
0283: }
0284: }
0285:
0286: public int getChar(String parameter) {
0287: if (null == parameter)
0288: throw new IllegalArgumentException(
0289: "parameter can't be null.");
0290: if (0 == parameter.length())
0291: throw new IllegalArgumentException(
0292: "parameter can't be empty.");
0293:
0294: return getChar(parameter, (char) 0);
0295: }
0296:
0297: public int getChar(String parameter, char defaultValue) {
0298: if (null == parameter)
0299: throw new IllegalArgumentException(
0300: "parameter can't be null.");
0301: if (0 == parameter.length())
0302: throw new IllegalArgumentException(
0303: "parameter can't be empty.");
0304:
0305: String parameter_string_value = getString(parameter);
0306:
0307: if (null != parameter_string_value
0308: && parameter_string_value.length() > 0) {
0309: return parameter_string_value.charAt(0);
0310: } else {
0311: return defaultValue;
0312: }
0313: }
0314:
0315: public int getInt(String parameter) {
0316: if (null == parameter)
0317: throw new IllegalArgumentException(
0318: "parameter can't be null.");
0319: if (0 == parameter.length())
0320: throw new IllegalArgumentException(
0321: "parameter can't be empty.");
0322:
0323: return getInt(parameter, 0);
0324: }
0325:
0326: public int getInt(String parameter, int defaultValue) {
0327: if (null == parameter)
0328: throw new IllegalArgumentException(
0329: "parameter can't be null.");
0330: if (0 == parameter.length())
0331: throw new IllegalArgumentException(
0332: "parameter can't be empty.");
0333:
0334: String string_parameter = getString(parameter);
0335:
0336: if (null != string_parameter) {
0337: try {
0338: return Integer.parseInt(string_parameter);
0339: } catch (NumberFormatException e) {
0340: return defaultValue;
0341: }
0342: } else {
0343: return defaultValue;
0344: }
0345: }
0346:
0347: public long getLong(String parameter) {
0348: if (null == parameter)
0349: throw new IllegalArgumentException(
0350: "parameter can't be null.");
0351: if (0 == parameter.length())
0352: throw new IllegalArgumentException(
0353: "parameter can't be empty.");
0354:
0355: return getLong(parameter, 0L);
0356: }
0357:
0358: public long getLong(String parameter, long defaultValue) {
0359: if (null == parameter)
0360: throw new IllegalArgumentException(
0361: "parameter can't be null.");
0362: if (0 == parameter.length())
0363: throw new IllegalArgumentException(
0364: "parameter can't be empty.");
0365:
0366: String string_parameter = getString(parameter);
0367:
0368: if (null != string_parameter) {
0369: try {
0370: return Long.parseLong(string_parameter);
0371: } catch (NumberFormatException e) {
0372: return defaultValue;
0373: }
0374: } else {
0375: return defaultValue;
0376: }
0377: }
0378:
0379: public float getFloat(String parameter) {
0380: if (null == parameter)
0381: throw new IllegalArgumentException(
0382: "parameter can't be null.");
0383: if (0 == parameter.length())
0384: throw new IllegalArgumentException(
0385: "parameter can't be empty.");
0386:
0387: return getFloat(parameter, 0F);
0388: }
0389:
0390: public float getFloat(String parameter, float defaultValue) {
0391: if (null == parameter)
0392: throw new IllegalArgumentException(
0393: "parameter can't be null.");
0394: if (0 == parameter.length())
0395: throw new IllegalArgumentException(
0396: "parameter can't be empty.");
0397:
0398: String string_parameter = getString(parameter);
0399:
0400: if (null != string_parameter) {
0401: try {
0402: return Float.parseFloat(string_parameter);
0403: } catch (NumberFormatException e) {
0404: return defaultValue;
0405: }
0406: } else {
0407: return defaultValue;
0408: }
0409: }
0410:
0411: public double getDouble(String parameter) {
0412: if (null == parameter)
0413: throw new IllegalArgumentException(
0414: "parameter can't be null.");
0415: if (0 == parameter.length())
0416: throw new IllegalArgumentException(
0417: "parameter can't be empty.");
0418:
0419: return getDouble(parameter, 0D);
0420: }
0421:
0422: public double getDouble(String parameter, double defaultValue) {
0423: if (null == parameter)
0424: throw new IllegalArgumentException(
0425: "parameter can't be null.");
0426: if (0 == parameter.length())
0427: throw new IllegalArgumentException(
0428: "parameter can't be empty.");
0429:
0430: String string_parameter = getString(parameter);
0431:
0432: if (null != string_parameter) {
0433: try {
0434: return Double.parseDouble(string_parameter);
0435: } catch (NumberFormatException e) {
0436: return defaultValue;
0437: }
0438: } else {
0439: return defaultValue;
0440: }
0441: }
0442:
0443: public <TargetType extends Serializable> TargetType getSerializable(
0444: String parameter) {
0445: if (null == parameter)
0446: throw new IllegalArgumentException(
0447: "parameter can't be null.");
0448: if (0 == parameter.length())
0449: throw new IllegalArgumentException(
0450: "parameter can't be empty.");
0451:
0452: return (TargetType) getSerializable(parameter, null);
0453: }
0454:
0455: public <TargetType extends Serializable> TargetType getSerializable(
0456: String parameter, TargetType defaultValue) {
0457: if (null == parameter)
0458: throw new IllegalArgumentException(
0459: "parameter can't be null.");
0460: if (0 == parameter.length())
0461: throw new IllegalArgumentException(
0462: "parameter can't be empty.");
0463:
0464: String value = getString(parameter);
0465: if (null != value) {
0466: try {
0467: return (TargetType) SerializationUtils
0468: .deserializeFromString(value);
0469: } catch (SerializationUtilsErrorException e) {
0470: return defaultValue;
0471: }
0472: } else {
0473: return defaultValue;
0474: }
0475: }
0476:
0477: public void setFinalParameter(String parameter, boolean isFinal) {
0478: if (null == parameter)
0479: throw new IllegalArgumentException(
0480: "parameter can't be null.");
0481: if (0 == parameter.length())
0482: throw new IllegalArgumentException(
0483: "parameter can't be empty.");
0484:
0485: if (isFinal && !mFinalParameters.contains(parameter)) {
0486: mFinalParameters.add(parameter);
0487: } else {
0488: mFinalParameters.remove(parameter);
0489: }
0490: }
0491:
0492: public void setParameter(String parameter, String value) {
0493: if (null == parameter)
0494: throw new IllegalArgumentException(
0495: "parameter can't be null.");
0496: if (0 == parameter.length())
0497: throw new IllegalArgumentException(
0498: "parameter can't be empty.");
0499: if (null == value)
0500: throw new IllegalArgumentException("value can't be null.");
0501:
0502: synchronized (this ) {
0503: if (!mFinalParameters.contains(parameter)) {
0504: mParameters.put(parameter, value);
0505:
0506: if (!isPreferencesParameter(parameter)
0507: && hasPreferencesNode()) {
0508: Preferences preferences = getPreferencesNode();
0509:
0510: if (preferences != null
0511: && preferences.get(parameter, null) != null) {
0512: preferences.put(parameter, value);
0513: }
0514: }
0515:
0516: // Set certain core parameters direct as system properties
0517: // to prevent deadlocks.
0518: // These parameters will not be picked up by the system through
0519: // the default configuration participant, but they will already
0520: // be set to the last value they got assigned to in a Config
0521: // instance
0522: if (RifeConfig.Engine.PARAM_ELEMENT_AUTO_RELOAD
0523: .equals(parameter)
0524: || RifeConfig.Engine.PARAM_ELEMENT_GENERATION_PATH
0525: .equals(parameter)) {
0526: System.getProperties().put(parameter, value);
0527: }
0528: }
0529: }
0530: }
0531:
0532: public void setParameter(String parameter, boolean value) {
0533: setParameter(parameter, String.valueOf(value));
0534: }
0535:
0536: public void setParameter(String parameter, char value) {
0537: setParameter(parameter, String.valueOf(value));
0538: }
0539:
0540: public void setParameter(String parameter, int value) {
0541: setParameter(parameter, String.valueOf(value));
0542: }
0543:
0544: public void setParameter(String parameter, long value) {
0545: setParameter(parameter, String.valueOf(value));
0546: }
0547:
0548: public void setParameter(String parameter, float value) {
0549: setParameter(parameter, String.valueOf(value));
0550: }
0551:
0552: public void setParameter(String parameter, double value) {
0553: setParameter(parameter, String.valueOf(value));
0554: }
0555:
0556: public void setParameter(String parameter, Serializable value)
0557: throws ConfigErrorException {
0558: try {
0559: setParameter(parameter, SerializationUtils
0560: .serializeToString(value));
0561: } catch (SerializationUtilsErrorException e) {
0562: throw new ConfigErrorException(e);
0563: }
0564: }
0565:
0566: public void removeParameter(String parameter) {
0567: if (null == parameter)
0568: throw new IllegalArgumentException(
0569: "parameter can't be null.");
0570: if (0 == parameter.length())
0571: throw new IllegalArgumentException(
0572: "parameter can't be empty.");
0573:
0574: if (mFinalParameters.contains(parameter)) {
0575: return;
0576: }
0577:
0578: synchronized (this ) {
0579: mParameters.remove(parameter);
0580:
0581: if (!isPreferencesParameter(parameter)
0582: && hasPreferencesNode()) {
0583: Preferences preferences = getPreferencesNode();
0584:
0585: if (preferences != null) {
0586: preferences.remove(parameter);
0587: }
0588: }
0589: }
0590: }
0591:
0592: public boolean isFinalList(String list) {
0593: if (null == list)
0594: throw new IllegalArgumentException("list can't be null.");
0595: if (0 == list.length())
0596: throw new IllegalArgumentException("list can't be empty.");
0597:
0598: return mFinalLists.contains(list);
0599: }
0600:
0601: public Collection<String> getStringItems(String list) {
0602: if (null == list)
0603: throw new IllegalArgumentException("list can't be null.");
0604: if (0 == list.length())
0605: throw new IllegalArgumentException("list can't be empty.");
0606:
0607: synchronized (this ) {
0608: Collection<String> list_items = null;
0609:
0610: if (!mFinalLists.contains(list) && hasPreferencesNode()) {
0611: Preferences preferences = getPreferencesNode();
0612:
0613: if (preferences != null) {
0614: Preferences list_preferences = preferences
0615: .node(list);
0616:
0617: if (list_preferences != null) {
0618: try {
0619: String[] string_array = list_preferences
0620: .keys();
0621: if (string_array != null
0622: && string_array.length > 0) {
0623: int[] int_array = new int[string_array.length];
0624: int counter = 0;
0625: for (String item_string : string_array) {
0626: int_array[counter++] = Integer
0627: .parseInt(item_string);
0628: }
0629: Arrays.sort(int_array);
0630:
0631: list_items = new ArrayList<String>(
0632: int_array.length);
0633: for (int item_int : int_array) {
0634: list_items.add(list_preferences
0635: .get(String
0636: .valueOf(item_int),
0637: null));
0638: }
0639: }
0640: } catch (BackingStoreException e) {
0641: // that's ok, don't handle the preferences node
0642: }
0643: }
0644: }
0645: }
0646:
0647: if (null == list_items) {
0648: list_items = mLists.get(list);
0649: }
0650:
0651: return list_items;
0652: }
0653: }
0654:
0655: public Collection<Boolean> getBoolItems(String list) {
0656: if (null == list)
0657: throw new IllegalArgumentException("list can't be null.");
0658: if (0 == list.length())
0659: throw new IllegalArgumentException("list can't be empty.");
0660:
0661: Collection<String> list_items = getStringItems(list);
0662: ArrayList<Boolean> result = new ArrayList<Boolean>(list_items
0663: .size());
0664: for (String item : list_items) {
0665: if (item.equalsIgnoreCase("true")
0666: || item.equalsIgnoreCase("t")
0667: || item.equalsIgnoreCase("1")) {
0668: result.add(Boolean.valueOf(true));
0669: } else {
0670: result.add(Boolean.valueOf(false));
0671: }
0672: }
0673:
0674: return result;
0675: }
0676:
0677: public Collection<Character> getCharItems(String list) {
0678: if (null == list)
0679: throw new IllegalArgumentException("list can't be null.");
0680: if (0 == list.length())
0681: throw new IllegalArgumentException("list can't be empty.");
0682:
0683: Collection<String> list_items = getStringItems(list);
0684: ArrayList<Character> result = new ArrayList<Character>(
0685: list_items.size());
0686: for (String item : list_items) {
0687: if (null != item && item.length() > 0) {
0688: result.add(new Character(item.charAt(0)));
0689: }
0690: }
0691:
0692: return result;
0693: }
0694:
0695: public Collection<Integer> getIntItems(String list) {
0696: if (null == list)
0697: throw new IllegalArgumentException("list can't be null.");
0698: if (0 == list.length())
0699: throw new IllegalArgumentException("list can't be empty.");
0700:
0701: Collection<String> list_items = getStringItems(list);
0702: ArrayList<Integer> result = new ArrayList<Integer>(list_items
0703: .size());
0704: for (String item : list_items) {
0705: if (null != item) {
0706: try {
0707: result.add(Integer.parseInt(item));
0708: } catch (NumberFormatException e) {
0709: continue;
0710: }
0711: }
0712: }
0713:
0714: return result;
0715: }
0716:
0717: public Collection<Long> getLongItems(String list) {
0718: if (null == list)
0719: throw new IllegalArgumentException("list can't be null.");
0720: if (0 == list.length())
0721: throw new IllegalArgumentException("list can't be empty.");
0722:
0723: Collection<String> list_items = getStringItems(list);
0724: ArrayList<Long> result = new ArrayList<Long>(list_items.size());
0725: for (String item : list_items) {
0726: if (null != item) {
0727: try {
0728: result.add(Long.parseLong(item));
0729: } catch (NumberFormatException e) {
0730: continue;
0731: }
0732: }
0733: }
0734:
0735: return result;
0736: }
0737:
0738: public Collection<Float> getFloatItems(String list) {
0739: if (null == list)
0740: throw new IllegalArgumentException("list can't be null.");
0741: if (0 == list.length())
0742: throw new IllegalArgumentException("list can't be empty.");
0743:
0744: Collection<String> list_items = getStringItems(list);
0745: ArrayList<Float> result = new ArrayList<Float>(list_items
0746: .size());
0747: for (String item : list_items) {
0748: if (null != item) {
0749: try {
0750: result.add(Float.parseFloat(item));
0751: } catch (NumberFormatException e) {
0752: continue;
0753: }
0754: }
0755: }
0756:
0757: return result;
0758: }
0759:
0760: public Collection<Double> getDoubleItems(String list) {
0761: if (null == list)
0762: throw new IllegalArgumentException("list can't be null.");
0763: if (0 == list.length())
0764: throw new IllegalArgumentException("list can't be empty.");
0765:
0766: Collection<String> list_items = getStringItems(list);
0767: ArrayList<Double> result = new ArrayList<Double>(list_items
0768: .size());
0769: for (String item : list_items) {
0770: if (null != item) {
0771: try {
0772: result.add(Double.parseDouble(item));
0773: } catch (NumberFormatException e) {
0774: continue;
0775: }
0776: }
0777: }
0778:
0779: return result;
0780: }
0781:
0782: public <TargetType extends Serializable> Collection<TargetType> getSerializableItems(
0783: String list) {
0784: if (null == list)
0785: throw new IllegalArgumentException("list can't be null.");
0786: if (0 == list.length())
0787: throw new IllegalArgumentException("list can't be empty.");
0788:
0789: Collection<String> list_items = getStringItems(list);
0790: ArrayList<TargetType> result = new ArrayList<TargetType>(
0791: list_items.size());
0792: for (String item : list_items) {
0793: try {
0794: result.add((TargetType) SerializationUtils
0795: .deserializeFromString(item));
0796: } catch (SerializationUtilsErrorException e) {
0797: continue;
0798: }
0799: }
0800:
0801: return result;
0802: }
0803:
0804: public boolean hasList(String list) {
0805: if (null == list)
0806: throw new IllegalArgumentException("list can't be null.");
0807: if (0 == list.length())
0808: throw new IllegalArgumentException("list can't be empty.");
0809:
0810: if (mLists.containsKey(list)) {
0811: return true;
0812: }
0813:
0814: if (hasPreferencesNode()) {
0815: Preferences preferences = getPreferencesNode();
0816:
0817: if (preferences != null) {
0818: try {
0819: String[] list_names_array = preferences
0820: .childrenNames();
0821:
0822: if (list_names_array != null) {
0823: List<String> list_names = Arrays
0824: .asList(list_names_array);
0825:
0826: return list_names.contains(list);
0827: }
0828: } catch (BackingStoreException e) {
0829: // that's ok, don't handle the preferences node
0830: }
0831: }
0832: }
0833:
0834: return false;
0835: }
0836:
0837: public int countLists() {
0838: int result = mLists.size();
0839:
0840: if (hasPreferencesNode()) {
0841: Preferences preferences = getPreferencesNode();
0842:
0843: if (preferences != null) {
0844: try {
0845: String[] list_names = preferences.childrenNames();
0846:
0847: if (list_names != null) {
0848: for (String list_name : list_names) {
0849: if (!mLists.containsKey(list_name)) {
0850: result++;
0851: }
0852: }
0853: }
0854: } catch (BackingStoreException e) {
0855: // that's ok, don't handle the preferences node
0856: }
0857: }
0858: }
0859:
0860: return result;
0861: }
0862:
0863: public void addListItem(String list, String item) {
0864: if (null == list)
0865: throw new IllegalArgumentException("list can't be null.");
0866: if (0 == list.length())
0867: throw new IllegalArgumentException("list can't be empty.");
0868: if (null == item)
0869: throw new IllegalArgumentException("item can't be null.");
0870:
0871: if (mFinalLists.contains(list)) {
0872: return;
0873: }
0874:
0875: synchronized (this ) {
0876: ArrayList<String> list_items = null;
0877:
0878: if (hasPreferencesNode()) {
0879: Preferences preferences = getPreferencesNode();
0880:
0881: if (preferences != null) {
0882: Preferences list_preferences = preferences
0883: .node(list);
0884:
0885: if (list_preferences != null) {
0886: try {
0887: String[] string_array = list_preferences
0888: .keys();
0889: if (string_array != null) {
0890: int[] int_array = new int[string_array.length];
0891: int counter = 0;
0892: for (String item_string : string_array) {
0893: int_array[counter++] = Integer
0894: .parseInt(item_string);
0895: }
0896: Arrays.sort(int_array);
0897:
0898: list_items = new ArrayList<String>(
0899: int_array.length);
0900: for (int item_int : int_array) {
0901: list_items.add(list_preferences
0902: .get(String
0903: .valueOf(item_int),
0904: null));
0905: }
0906: }
0907:
0908: list_preferences
0909: .put(
0910: String
0911: .valueOf(string_array.length),
0912: item);
0913: list_items.add(item);
0914: } catch (BackingStoreException e) {
0915: // that's ok, don't handle the preferences node
0916: }
0917: }
0918: }
0919: }
0920:
0921: if (list_items != null) {
0922: mLists.put(list, list_items);
0923: } else {
0924: if (mLists.containsKey(list)) {
0925: list_items = mLists.get(list);
0926: list_items.add(item);
0927: } else {
0928: list_items = new ArrayList<String>();
0929: mLists.put(list, list_items);
0930:
0931: list_items.add(item);
0932: }
0933: }
0934: }
0935: }
0936:
0937: public void addListItem(String list, boolean item) {
0938: addListItem(list, String.valueOf(item));
0939: }
0940:
0941: public void addListItem(String list, char item) {
0942: addListItem(list, String.valueOf(item));
0943: }
0944:
0945: public void addListItem(String list, int item) {
0946: addListItem(list, String.valueOf(item));
0947: }
0948:
0949: public void addListItem(String list, long item) {
0950: addListItem(list, String.valueOf(item));
0951: }
0952:
0953: public void addListItem(String list, float item) {
0954: addListItem(list, String.valueOf(item));
0955: }
0956:
0957: public void addListItem(String list, double item) {
0958: addListItem(list, String.valueOf(item));
0959: }
0960:
0961: public void addListItem(String list, Serializable item)
0962: throws ConfigErrorException {
0963: try {
0964: addListItem(list, SerializationUtils
0965: .serializeToString(item));
0966: } catch (SerializationUtilsErrorException e) {
0967: throw new ConfigErrorException(e);
0968: }
0969: }
0970:
0971: public void clearList(String list) {
0972: if (null == list)
0973: throw new IllegalArgumentException("list can't be null.");
0974: if (0 == list.length())
0975: throw new IllegalArgumentException("list can't be empty.");
0976:
0977: if (mFinalLists.contains(list)) {
0978: return;
0979: }
0980:
0981: synchronized (this ) {
0982: if (hasPreferencesNode()) {
0983: Preferences preferences = getPreferencesNode();
0984:
0985: if (preferences != null) {
0986: Preferences list_preferences = preferences
0987: .node(list);
0988:
0989: if (list_preferences != null) {
0990: try {
0991: list_preferences.clear();
0992: } catch (BackingStoreException e) {
0993: // that's ok, don't handle the preferences node
0994: }
0995: }
0996: }
0997: }
0998:
0999: if (mLists.containsKey(list)) {
1000: mLists.put(list, new ArrayList<String>());
1001: }
1002: }
1003: }
1004:
1005: public void removeList(String list) {
1006: if (null == list)
1007: throw new IllegalArgumentException("list can't be null.");
1008: if (0 == list.length())
1009: throw new IllegalArgumentException("list can't be empty.");
1010:
1011: if (mFinalLists.contains(list)) {
1012: return;
1013: }
1014:
1015: synchronized (this ) {
1016: if (hasPreferencesNode()) {
1017: Preferences preferences = getPreferencesNode();
1018:
1019: if (preferences != null) {
1020: Preferences list_preferences = preferences
1021: .node(list);
1022:
1023: if (list_preferences != null) {
1024: try {
1025: list_preferences.removeNode();
1026: } catch (BackingStoreException e) {
1027: // that's ok, don't handle the preferences node
1028: }
1029: }
1030: }
1031: }
1032:
1033: mLists.remove(list);
1034: }
1035: }
1036:
1037: public void setFinalList(String list, boolean isFinal) {
1038: if (null == list)
1039: throw new IllegalArgumentException("list can't be null.");
1040: if (0 == list.length())
1041: throw new IllegalArgumentException("list can't be empty.");
1042:
1043: if (isFinal && !mFinalLists.contains(list)) {
1044: mFinalLists.add(list);
1045: } else {
1046: mFinalLists.remove(list);
1047: }
1048: }
1049:
1050: private void initialize(Xml2Config xmlConfig)
1051: throws ConfigErrorException {
1052: try {
1053: xmlConfig.processXml(mXmlPath, mResourceFinder);
1054: synchronized (this ) {
1055: mParameters = xmlConfig.getParameters();
1056: mFinalParameters = xmlConfig.getFinalParameters();
1057: mLists = xmlConfig.getLists();
1058: mFinalLists = xmlConfig.getFinalLists();
1059:
1060: // Set certain core parameters direct as system properties
1061: // to prevent deadlocks.
1062: // These parameters will not be picked up by the system through
1063: // the default configuration participant, but they will already
1064: // be set to the last value they got assigned to in a Config
1065: // instance
1066: if (mParameters
1067: .containsKey(RifeConfig.Engine.PARAM_ELEMENT_AUTO_RELOAD)) {
1068: System
1069: .getProperties()
1070: .put(
1071: RifeConfig.Engine.PARAM_ELEMENT_AUTO_RELOAD,
1072: mParameters
1073: .get(RifeConfig.Engine.PARAM_ELEMENT_AUTO_RELOAD));
1074: }
1075: if (mParameters
1076: .containsKey(RifeConfig.Engine.PARAM_ELEMENT_GENERATION_PATH)) {
1077: System
1078: .getProperties()
1079: .put(
1080: RifeConfig.Engine.PARAM_ELEMENT_GENERATION_PATH,
1081: mParameters
1082: .get(RifeConfig.Engine.PARAM_ELEMENT_GENERATION_PATH));
1083: }
1084: }
1085: } catch (XmlErrorException e) {
1086: throw new InitializationErrorException(mXmlPath, e);
1087: }
1088: }
1089:
1090: Map<String, String> getParameters() {
1091: return mParameters;
1092: }
1093:
1094: List<String> getFinalParameters() {
1095: return mFinalParameters;
1096: }
1097:
1098: Map<String, ArrayList<String>> getLists() {
1099: return mLists;
1100: }
1101:
1102: List<String> getFinalLists() {
1103: return mFinalLists;
1104: }
1105:
1106: public String toXml() {
1107: StringBuilder xml_output = new StringBuilder();
1108: xml_output.append("<config>\n");
1109:
1110: ArrayList<String> list_keys_arraylist = new ArrayList<String>();
1111: for (String list_key : mLists.keySet()) {
1112: list_keys_arraylist.add(list_key);
1113: }
1114:
1115: (new SortListComparables()).sort(list_keys_arraylist);
1116:
1117: for (String list_key : list_keys_arraylist) {
1118: xml_output.append("\t<list name=\"");
1119: xml_output.append(StringUtils.encodeXml(list_key));
1120: if (mFinalLists.contains(list_key)) {
1121: xml_output.append("\" final=\"true");
1122: }
1123: xml_output.append("\">\n");
1124:
1125: ArrayList<String> list_items = mLists.get(list_key);
1126: for (String list_item : list_items) {
1127: xml_output.append("\t\t<item>").append(
1128: StringUtils.encodeXml(list_item)).append(
1129: "</item>\n");
1130: }
1131:
1132: xml_output.append("\t</list>\n");
1133: }
1134:
1135: ArrayList<String> parameter_keys_arraylist = new ArrayList<String>();
1136: for (String parameter_key : mParameters.keySet()) {
1137: parameter_keys_arraylist.add(parameter_key);
1138: }
1139:
1140: (new SortListComparables()).sort(parameter_keys_arraylist);
1141:
1142: for (String parameter_key : parameter_keys_arraylist) {
1143: xml_output.append("\t<param name=\"");
1144: xml_output.append(StringUtils.encodeXml(parameter_key));
1145: if (mFinalParameters.contains(parameter_key)) {
1146: xml_output.append("\" final=\"true");
1147: }
1148: xml_output.append("\">");
1149: xml_output.append(StringUtils.encodeXml(mParameters
1150: .get(parameter_key)));
1151: xml_output.append("</param>\n");
1152: }
1153:
1154: xml_output.append("</config>\n");
1155:
1156: return xml_output.toString();
1157: }
1158:
1159: public void storeToXml() throws ConfigErrorException {
1160: String xmlpath = null;
1161: URL xmlpath_resource = null;
1162:
1163: xmlpath = getXmlPath();
1164: if (null == xmlpath) {
1165: throw new MissingXmlPathException();
1166: }
1167:
1168: xmlpath_resource = mResourceFinder.getResource(xmlpath);
1169: if (null == xmlpath_resource) {
1170: throw new CantFindXmlPathException(xmlpath);
1171: }
1172:
1173: try {
1174: storeToXml(new File(URLDecoder.decode(xmlpath_resource
1175: .getPath(), "ISO-8859-1")));
1176: } catch (UnsupportedEncodingException e) {
1177: // should never fail, it's a standard encoding
1178: }
1179: }
1180:
1181: public synchronized void storeToXml(File destination)
1182: throws ConfigErrorException {
1183: if (null == destination)
1184: throw new IllegalArgumentException(
1185: "destination can't be null");
1186:
1187: if (destination.exists() && !destination.canWrite()) {
1188: throw new CantWriteToDestinationException(destination);
1189: }
1190:
1191: StringBuilder content = new StringBuilder(
1192: "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
1193: content
1194: .append("<!DOCTYPE config SYSTEM \"/dtd/config.dtd\">\n");
1195: content.append(toXml());
1196: try {
1197: FileUtils.writeString(content.toString(), destination);
1198: } catch (FileUtilsErrorException e) {
1199: throw new StoreXmlErrorException(destination, e);
1200: }
1201:
1202: }
1203:
1204: public void storeToPreferences() throws ConfigErrorException {
1205: if (!hasPreferencesNode()) {
1206: throw new MissingPreferencesUserNodeException();
1207: }
1208:
1209: storeToPreferences(getPreferencesNode());
1210: }
1211:
1212: public synchronized void storeToPreferences(Preferences preferences)
1213: throws ConfigErrorException {
1214: if (null == preferences)
1215: throw new IllegalArgumentException(
1216: "destination can't be null");
1217:
1218: synchronized (preferences) {
1219: Preferences list_node = null;
1220:
1221: for (String list_key : mLists.keySet()) {
1222: if (mFinalLists.contains(list_key)) {
1223: continue;
1224: }
1225:
1226: list_node = preferences.node(list_key);
1227:
1228: int counter = 0;
1229: ArrayList<String> list_items = mLists.get(list_key);
1230: for (String list_item : list_items) {
1231: list_node.put(String.valueOf(counter++), list_item);
1232: }
1233: }
1234:
1235: for (String parameter_key : mParameters.keySet()) {
1236: if (parameter_key.equals(PARAMETER_PREFERENCES_SYSTEM)
1237: || parameter_key
1238: .equals(PARAMETER_PREFERENCES_USER)
1239: || mFinalParameters.contains(parameter_key)) {
1240: continue;
1241: }
1242:
1243: preferences.put(parameter_key, mParameters
1244: .get(parameter_key));
1245: }
1246:
1247: try {
1248: preferences.flush();
1249: } catch (BackingStoreException e) {
1250: throw new StorePreferencesErrorException(preferences, e);
1251: }
1252: }
1253: }
1254:
1255: public Config clone() {
1256: Config new_config = null;
1257: try {
1258: new_config = (Config) super .clone();
1259: } catch (CloneNotSupportedException e) {
1260: ///CLOVER:OFF
1261: // this should never happen
1262: Logger.getLogger("com.uwyn.rife.config").severe(
1263: ExceptionUtils.getExceptionStackTrace(e));
1264: ///CLOVER:ON
1265: }
1266:
1267: try {
1268: new_config.mParameters = ObjectUtils.deepClone(mParameters);
1269: new_config.mFinalParameters = ObjectUtils
1270: .deepClone(mFinalParameters);
1271: new_config.mLists = ObjectUtils.deepClone(mLists);
1272: new_config.mFinalLists = ObjectUtils.deepClone(mFinalLists);
1273: } catch (CloneNotSupportedException e) {
1274: ///CLOVER:OFF
1275: // this should never happen
1276: Logger.getLogger("com.uwyn.rife.config").severe(
1277: ExceptionUtils.getExceptionStackTrace(e));
1278: ///CLOVER:ON
1279: }
1280:
1281: return new_config;
1282: }
1283: }
|