001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: ParameterWrapper.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.task;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.parameters.Parameters;
028: import org.apache.lenya.util.NamespaceMap;
029: import org.apache.log4j.Logger;
030:
031: /**
032: * Parameter wrapper class
033: * @deprecated Use the usecase framework instead.
034: */
035: public abstract class ParameterWrapper {
036:
037: private static Logger log = Logger
038: .getLogger(ParameterWrapper.class);
039: private NamespaceMap parameters;
040:
041: /**
042: * Returns the un-prefixed parameters.
043: * @return A map.
044: */
045: public Map getMap() {
046: return this .parameters.getMap();
047: }
048:
049: /**
050: * Ctor.
051: * @param prefixedParameters The prefixed parameters to wrap.
052: */
053: public ParameterWrapper(Map prefixedParameters) {
054: this .parameters = new NamespaceMap(prefixedParameters,
055: getPrefix());
056: }
057:
058: /**
059: * Returns the namespace prefix.
060: * @return A string.
061: */
062: public abstract String getPrefix();
063:
064: /**
065: * Adds a key-value pair. If the value is null, no pair is added.
066: * @param key The key.
067: * @param value The value.
068: */
069: public void put(String key, String value) {
070: if (value != null) {
071: log.debug("Setting parameter: [" + key + "] = [" + value
072: + "]");
073: this .parameters.put(key, value);
074: } else {
075: log.debug("Not setting parameter: [" + key + "] = ["
076: + value + "]");
077: }
078: }
079:
080: /**
081: * Returns the value for a key.
082: * @param key The key.
083: * @return The value.
084: */
085: public String get(String key) {
086: return (String) this .parameters.get(key);
087: }
088:
089: /**
090: * Returns the required keys.
091: * @return A string array.
092: */
093: protected abstract String[] getRequiredKeys();
094:
095: /**
096: * Checks if this parameters object contains all necessary parameters.
097: * @return A boolean value.
098: */
099: public boolean isComplete() {
100: boolean complete = true;
101: Map parameterMap = getMap();
102: String[] requiredKeys = getRequiredKeys();
103: int i = 0;
104: while (complete && i < requiredKeys.length) {
105: log.debug("Checking parameter: [" + requiredKeys[i] + "]");
106: complete = complete
107: && parameterMap.containsKey(requiredKeys[i]);
108: log.debug("OK: [" + complete + "]");
109: i++;
110: }
111: return complete;
112: }
113:
114: /**
115: * Returns the missing parameters parameters.
116: * @return A string array.
117: */
118: public String[] getMissingKeys() {
119: String[] requiredKeys = getRequiredKeys();
120: Map parameterMap = getMap();
121: List keyList = new ArrayList();
122: for (int i = 0; i < requiredKeys.length; i++) {
123: if (!parameterMap.containsKey(requiredKeys[i])) {
124: keyList.add(requiredKeys[i]);
125: }
126: }
127: return (String[]) keyList.toArray(new String[keyList.size()]);
128: }
129:
130: /**
131: * Parameterizes this wrapper with un-prefixed parameters.
132: * @param _parameters A parameters object.
133: */
134: public void parameterize(Parameters _parameters) {
135: String[] keys = _parameters.getNames();
136: for (int i = 0; i < keys.length; i++) {
137: put(keys[i], _parameters.getParameter(keys[i], null));
138: }
139: }
140:
141: }
|