modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingProperties.java
| 300 ++++++++++
modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingReader.java
| 201 ++++++
modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/setup/ApacheTestSetup.java
| 2
modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/upgrade/UpgradeTestBase.java
| 2
modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/util/ApacheDeploymentUtil.java
| 4
modules/test-utils/src/main/java/org/rhq/test/TokenReplacingProperties.java
| 300 ----------
modules/test-utils/src/main/java/org/rhq/test/TokenReplacingReader.java
| 201 ------
7 files changed, 505 insertions(+), 505 deletions(-)
New commits:
commit bb955e4a5ccf2f15803dddaf741fe496d1cf044b
Author: John Sanda <jsanda(a)redhat.com>
Date: Wed Mar 27 21:36:13 2013 -0400
moving TokenReplacingProperties and TokenReplacingReader for use in production code
diff --git
a/modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingProperties.java
b/modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingProperties.java
new file mode 100644
index 0000000..12b36e1
--- /dev/null
+++ b/modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingProperties.java
@@ -0,0 +1,300 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2011 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.core.util;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+
+/**
+ * This map is basically an extension of the {@link Properties} class that can resolve
the references
+ * to values of other keys inside the values.
+ * <p>
+ * I.e., if the map is initialized with the following mappings:
+ * <p>
+ * <code>
+ * name => world <br />
+ * hello => Hello ${name}!
+ * </code>
+ * <p>
+ * then the call to:
+ * <p>
+ * <code>
+ * get("hello")
+ * </code>
+ * <p>
+ * will return:
+ * <code>
+ * "Hello world!"
+ * </code>
+ * <p>
+ * To access and modify the underlying unprocessed values, one can use the
"raw" counterparts of the standard
+ * map methods (e.g. instead of {@link #get(Object)}, use {@link #getRaw(Object)},
etc.).
+ *
+ * @author Lukas Krejci
+ */
+public class TokenReplacingProperties extends HashMap<String, String> {
+ private static final long serialVersionUID = 1L;
+
+ private Map<String, String> wrapped;
+ private Map<Object, String> resolved = new HashMap<Object, String>();
+
+ private class Entry implements Map.Entry<String, String> {
+ private Map.Entry<String, String> wrapped;
+ private boolean process;
+
+ public Entry(Map.Entry<String, String> wrapped, boolean process) {
+ this.wrapped = wrapped;
+ this.process = process;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+
+ if (!(obj instanceof Entry)) {
+ return false;
+ }
+
+ Entry other = (Entry) obj;
+
+ String key = wrapped.getKey();
+ String otherKey = other.getKey();
+ String value = getValue();
+ String otherValue = other.getValue();
+
+ return (key == null ? otherKey == null : key.equals(otherKey)) &&
+ (value == null ? otherValue == null : value.equals(otherValue));
+ }
+
+ public String getKey() {
+ return wrapped.getKey();
+ }
+
+ public String getValue() {
+ if (process) {
+ return get(wrapped.getKey());
+ } else {
+ return wrapped.getValue();
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ String key = wrapped.getKey();
+ String value = getValue();
+ return (key == null ? 0 : key.hashCode()) ^
+ (value == null ? 0 : value.hashCode());
+ }
+
+ public String setValue(String value) {
+ resolved.remove(wrapped.getKey());
+ return wrapped.setValue(value);
+ }
+
+ @Override
+ public String toString() {
+ return wrapped.toString();
+ }
+ }
+
+ public TokenReplacingProperties(Map<String, String> wrapped) {
+ this.wrapped = wrapped;
+ }
+
+ @SuppressWarnings("unchecked")
+ public TokenReplacingProperties(Properties properties) {
+ @SuppressWarnings("rawtypes")
+ Map map = properties;
+ this.wrapped = (Map<String, String>) map;
+ }
+
+ @Override
+ public String get(Object key) {
+ if (resolved.containsKey(key)) {
+ return resolved.get(key);
+ }
+
+ String rawValue = getRaw(key);
+
+ if (rawValue == null) {
+ return null;
+ }
+
+ String ret = readAll(new TokenReplacingReader(new
StringReader(rawValue.toString()), this));
+
+ resolved.put(key, ret);
+
+ return ret;
+ }
+
+ public String getRaw(Object key) {
+ return wrapped.get(key);
+ }
+
+ @Override
+ public String put(String key, String value) {
+ resolved.remove(key);
+ return wrapped.put(key, value);
+ }
+
+ @Override
+ public void putAll(Map<? extends String, ? extends String> m) {
+ for(String key : m.keySet()) {
+ resolved.remove(key);
+ }
+ wrapped.putAll(m);
+ }
+
+ public void putAll(Properties properties) {
+ for(String propName : properties.stringPropertyNames()) {
+ put(propName, properties.getProperty(propName));
+ }
+ }
+
+ @Override
+ public void clear() {
+ wrapped.clear();
+ resolved.clear();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return wrapped.containsKey(key);
+ }
+
+ @Override
+ public Set<String> keySet() {
+ return wrapped.keySet();
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ for(String key : keySet()) {
+ String thisVal = get(key);
+ if (thisVal == null) {
+ if (value == null) {
+ return true;
+ }
+ } else {
+ if (thisVal.equals(value)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Checks whether this map contains the unprocessed value.
+ *
+ * @param value
+ * @return
+ */
+ public boolean containsRawValue(Object value) {
+ return wrapped.containsValue(value);
+ }
+
+ /**
+ * The returned set <b>IS NOT</b> backed by this map
+ * (unlike in the default map implementations).
+ * <p>
+ * The {@link java.util.Map.Entry#setValue(Object)} method
+ * does modify this map though.
+ */
+ @Override
+ public Set<Map.Entry<String, String>> entrySet() {
+ Set<Map.Entry<String, String>> ret = new
HashSet<Map.Entry<String, String>>();
+ for(Map.Entry<String, String> entry : wrapped.entrySet()) {
+ ret.add(new Entry(entry, true));
+ }
+
+ return ret;
+ }
+
+ public Set<Map.Entry<String, String>> getRawEntrySet() {
+ Set<Map.Entry<String, String>> ret = new
HashSet<Map.Entry<String, String>>();
+ for(Map.Entry<String, String> entry : wrapped.entrySet()) {
+ ret.add(new Entry(entry, false));
+ }
+
+ return ret;
+ }
+
+ @Override
+ public String remove(Object key) {
+ resolved.remove(key);
+ return wrapped.remove(key).toString();
+ }
+
+ @Override
+ public int size() {
+ return wrapped.size();
+ }
+
+ /**
+ * Unlike in the default implementation the collection returned
+ * from this method <b>IS NOT</b> backed by this map.
+ */
+ @Override
+ public Collection<String> values() {
+ List<String> ret = new ArrayList<String>();
+ for(String key : keySet()) {
+ ret.add(get(key));
+ }
+
+ return ret;
+ }
+
+ public Collection<String> getRawValues() {
+ List<String> ret = new ArrayList<String>();
+ for(String key : keySet()) {
+ ret.add(wrapped.get(key));
+ }
+
+ return ret;
+ }
+
+ private String readAll(Reader rdr) {
+ int in = -1;
+ StringBuilder bld = new StringBuilder();
+ try {
+ while ((in = rdr.read()) != -1) {
+ bld.append((char) in);
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException("Exception while reading a
string.", e);
+ }
+
+ return bld.toString();
+ }
+}
\ No newline at end of file
diff --git a/modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingReader.java
b/modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingReader.java
new file mode 100644
index 0000000..3303b76
--- /dev/null
+++ b/modules/core/util/src/main/java/org/rhq/core/util/TokenReplacingReader.java
@@ -0,0 +1,201 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2011 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.core.util;
+
+import java.io.IOException;
+import java.io.PushbackReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.CharBuffer;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Copied from
http://tutorials.jenkov.com/java-howto/replace-strings-in-streams-arrays-...
+ * with fixes to {@link #read(char[], int, int)} and added support for escaping.
+ *
+ * @author Lukas Krejci
+ */
+public class TokenReplacingReader extends Reader {
+
+ private PushbackReader pushbackReader = null;
+ private Map<String, String> tokens = null;
+ private StringBuilder tokenNameBuffer = new StringBuilder();
+ private String tokenValue = null;
+ private int tokenValueIndex = 0;
+ private boolean escaping = false;
+ private Deque<String> activeTokens;
+ private Map<String, String> resolvedTokens;
+
+ public TokenReplacingReader(Reader source, Map<String, String> tokens) {
+ this.pushbackReader = new PushbackReader(source, 2);
+ this.tokens = tokens;
+ this.activeTokens = new ArrayDeque<String>();
+ this.resolvedTokens = new HashMap<String, String>();
+ }
+
+ protected TokenReplacingReader(String source, Map<String, String> tokens,
Deque<String> activeTokens, Map<String, String> resolvedTokens) {
+ pushbackReader = new PushbackReader(new StringReader(source));
+ this.tokens = tokens;
+ this.activeTokens = activeTokens;
+ this.resolvedTokens = resolvedTokens;
+ }
+
+ public int read(CharBuffer target) throws IOException {
+ throw new RuntimeException("Operation Not Supported");
+ }
+
+ public int read() throws IOException {
+ if (this.tokenValue != null) {
+ if (this.tokenValueIndex < this.tokenValue.length()) {
+ return this.tokenValue.charAt(this.tokenValueIndex++);
+ }
+ if (this.tokenValueIndex == this.tokenValue.length()) {
+ this.tokenValue = null;
+ this.tokenValueIndex = 0;
+ }
+ }
+
+ int data = this.pushbackReader.read();
+
+ if (escaping) {
+ escaping = false;
+ return data;
+ }
+
+ if (data == '\\') {
+ escaping = true;
+ return data;
+ }
+
+ if (data != '$')
+ return data;
+
+ data = this.pushbackReader.read();
+ if (data != '{') {
+ this.pushbackReader.unread(data);
+ return '$';
+ }
+ this.tokenNameBuffer.delete(0, this.tokenNameBuffer.length());
+
+ data = this.pushbackReader.read();
+ while (data != '}') {
+ this.tokenNameBuffer.append((char) data);
+ data = this.pushbackReader.read();
+ }
+
+ String tokenName = tokenNameBuffer.toString();
+
+ if (resolvedTokens.containsKey(tokenName)) {
+ tokenValue = resolvedTokens.get(tokenName);
+ } else {
+ tokenValue = resolveToken(tokenName);
+ }
+
+ tokenValueIndex = 0;
+
+ if (!this.tokenValue.isEmpty()) {
+ return this.tokenValue.charAt(this.tokenValueIndex++);
+ } else {
+ return read();
+ }
+ }
+
+ public int read(char cbuf[]) throws IOException {
+ return read(cbuf, 0, cbuf.length);
+ }
+
+ public int read(char cbuf[], int off, int len) throws IOException {
+ int i = 0;
+ for (; i < len; i++) {
+ int nextChar = read();
+ if (nextChar == -1) {
+ if (i == 0) {
+ i = -1;
+ }
+ break;
+ }
+ cbuf[off + i] = (char) nextChar;
+ }
+ return i;
+ }
+
+ public void close() throws IOException {
+ this.pushbackReader.close();
+ }
+
+ public long skip(long n) throws IOException {
+ throw new UnsupportedOperationException("skip() not supported on
TokenReplacingReader.");
+ }
+
+ public boolean ready() throws IOException {
+ return this.pushbackReader.ready();
+ }
+
+ public boolean markSupported() {
+ return false;
+ }
+
+ public void mark(int readAheadLimit) throws IOException {
+ throw new IOException("mark() not supported on
TokenReplacingReader.");
+ }
+
+ public void reset() throws IOException {
+ throw new IOException("reset() not supported on
TokenReplacingReader.");
+ }
+
+ private String readAll(Reader r) throws IOException {
+ int c;
+ StringBuilder bld = new StringBuilder();
+ while((c = r.read()) >= 0) {
+ bld.append((char)c);
+ }
+
+ return bld.toString();
+ }
+
+ private String resolveToken(String tokenName) throws IOException {
+ if (activeTokens.contains(tokenName)) {
+ throw new IllegalArgumentException("Token '" + tokenName +
"' (indirectly) contains reference to itself in its value.");
+ }
+
+ activeTokens.push(tokenName);
+
+ String tokenValue = tokens.get(tokenName);
+
+ if (tokenValue != null) {
+ if (tokenValue.contains("${")) {
+ TokenReplacingReader childReader = new TokenReplacingReader(tokenValue,
tokens, activeTokens, resolvedTokens);
+
+ tokenValue = readAll(childReader);
+ }
+ } else {
+ tokenValue = "${" + tokenName + "}";
+ }
+
+ resolvedTokens.put(tokenName, tokenValue);
+
+ activeTokens.pop();
+
+ return tokenValue;
+ }
+}
diff --git
a/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/setup/ApacheTestSetup.java
b/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/setup/ApacheTestSetup.java
index e0363af..908f7d7 100644
---
a/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/setup/ApacheTestSetup.java
+++
b/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/setup/ApacheTestSetup.java
@@ -60,7 +60,7 @@ import org.rhq.plugins.apache.util.HttpdAddressUtility;
import org.rhq.plugins.apache.util.ResourceTypes;
import org.rhq.plugins.apache.util.VHostSpec;
import org.rhq.test.ObjectCollectionSerializer;
-import org.rhq.test.TokenReplacingReader;
+import org.rhq.core.util.TokenReplacingReader;
import org.rhq.test.pc.PluginContainerTest;
public class ApacheTestSetup {
diff --git
a/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/upgrade/UpgradeTestBase.java
b/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/upgrade/UpgradeTestBase.java
index 719a3dd..7c4eb1e 100644
---
a/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/upgrade/UpgradeTestBase.java
+++
b/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/upgrade/UpgradeTestBase.java
@@ -66,7 +66,7 @@ import
org.rhq.plugins.apache.util.ApacheDeploymentUtil.DeploymentConfig;
import org.rhq.plugins.apache.util.ResourceTypes;
import org.rhq.plugins.apache.util.VHostSpec;
import org.rhq.plugins.apache.util.VirtualHostLegacyResourceKeyUtil;
-import org.rhq.test.TokenReplacingReader;
+import org.rhq.core.util.TokenReplacingReader;
import org.rhq.test.pc.PluginContainerTest;
diff --git
a/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/util/ApacheDeploymentUtil.java
b/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/util/ApacheDeploymentUtil.java
index d5a7e45..c7f176c 100644
---
a/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/util/ApacheDeploymentUtil.java
+++
b/modules/integration-tests/apache-plugin-test/src/test/java/org/rhq/plugins/apache/util/ApacheDeploymentUtil.java
@@ -44,8 +44,8 @@ import org.rhq.core.util.file.FileUtil;
import org.rhq.core.util.stream.StreamUtil;
import org.rhq.plugins.apache.util.HttpdAddressUtility.Address;
import org.rhq.test.PortScout;
-import org.rhq.test.TokenReplacingProperties;
-import org.rhq.test.TokenReplacingReader;
+import org.rhq.core.util.TokenReplacingProperties;
+import org.rhq.core.util.TokenReplacingReader;
/**
*
diff --git a/modules/test-utils/src/main/java/org/rhq/test/TokenReplacingProperties.java
b/modules/test-utils/src/main/java/org/rhq/test/TokenReplacingProperties.java
deleted file mode 100644
index 5708ee3..0000000
--- a/modules/test-utils/src/main/java/org/rhq/test/TokenReplacingProperties.java
+++ /dev/null
@@ -1,300 +0,0 @@
-/*
- * RHQ Management Platform
- * Copyright (C) 2005-2011 Red Hat, Inc.
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
-package org.rhq.test;
-
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
-
-/**
- * This map is basically an extension of the {@link Properties} class that can resolve
the references
- * to values of other keys inside the values.
- * <p>
- * I.e., if the map is initialized with the following mappings:
- * <p>
- * <code>
- * name => world <br />
- * hello => Hello ${name}!
- * </code>
- * <p>
- * then the call to:
- * <p>
- * <code>
- * get("hello")
- * </code>
- * <p>
- * will return:
- * <code>
- * "Hello world!"
- * </code>
- * <p>
- * To access and modify the underlying unprocessed values, one can use the
"raw" counterparts of the standard
- * map methods (e.g. instead of {@link #get(Object)}, use {@link #getRaw(Object)},
etc.).
- *
- * @author Lukas Krejci
- */
-public class TokenReplacingProperties extends HashMap<String, String> {
- private static final long serialVersionUID = 1L;
-
- private Map<String, String> wrapped;
- private Map<Object, String> resolved = new HashMap<Object, String>();
-
- private class Entry implements Map.Entry<String, String> {
- private Map.Entry<String, String> wrapped;
- private boolean process;
-
- public Entry(Map.Entry<String, String> wrapped, boolean process) {
- this.wrapped = wrapped;
- this.process = process;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
-
- if (!(obj instanceof Entry)) {
- return false;
- }
-
- Entry other = (Entry) obj;
-
- String key = wrapped.getKey();
- String otherKey = other.getKey();
- String value = getValue();
- String otherValue = other.getValue();
-
- return (key == null ? otherKey == null : key.equals(otherKey)) &&
- (value == null ? otherValue == null : value.equals(otherValue));
- }
-
- public String getKey() {
- return wrapped.getKey();
- }
-
- public String getValue() {
- if (process) {
- return get(wrapped.getKey());
- } else {
- return wrapped.getValue();
- }
- }
-
- @Override
- public int hashCode() {
- String key = wrapped.getKey();
- String value = getValue();
- return (key == null ? 0 : key.hashCode()) ^
- (value == null ? 0 : value.hashCode());
- }
-
- public String setValue(String value) {
- resolved.remove(wrapped.getKey());
- return wrapped.setValue(value);
- }
-
- @Override
- public String toString() {
- return wrapped.toString();
- }
- }
-
- public TokenReplacingProperties(Map<String, String> wrapped) {
- this.wrapped = wrapped;
- }
-
- @SuppressWarnings("unchecked")
- public TokenReplacingProperties(Properties properties) {
- @SuppressWarnings("rawtypes")
- Map map = properties;
- this.wrapped = (Map<String, String>) map;
- }
-
- @Override
- public String get(Object key) {
- if (resolved.containsKey(key)) {
- return resolved.get(key);
- }
-
- String rawValue = getRaw(key);
-
- if (rawValue == null) {
- return null;
- }
-
- String ret = readAll(new TokenReplacingReader(new
StringReader(rawValue.toString()), this));
-
- resolved.put(key, ret);
-
- return ret;
- }
-
- public String getRaw(Object key) {
- return wrapped.get(key);
- }
-
- @Override
- public String put(String key, String value) {
- resolved.remove(key);
- return wrapped.put(key, value);
- }
-
- @Override
- public void putAll(Map<? extends String, ? extends String> m) {
- for(String key : m.keySet()) {
- resolved.remove(key);
- }
- wrapped.putAll(m);
- }
-
- public void putAll(Properties properties) {
- for(String propName : properties.stringPropertyNames()) {
- put(propName, properties.getProperty(propName));
- }
- }
-
- @Override
- public void clear() {
- wrapped.clear();
- resolved.clear();
- }
-
- @Override
- public boolean containsKey(Object key) {
- return wrapped.containsKey(key);
- }
-
- @Override
- public Set<String> keySet() {
- return wrapped.keySet();
- }
-
- @Override
- public boolean containsValue(Object value) {
- for(String key : keySet()) {
- String thisVal = get(key);
- if (thisVal == null) {
- if (value == null) {
- return true;
- }
- } else {
- if (thisVal.equals(value)) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- /**
- * Checks whether this map contains the unprocessed value.
- *
- * @param value
- * @return
- */
- public boolean containsRawValue(Object value) {
- return wrapped.containsValue(value);
- }
-
- /**
- * The returned set <b>IS NOT</b> backed by this map
- * (unlike in the default map implementations).
- * <p>
- * The {@link java.util.Map.Entry#setValue(Object)} method
- * does modify this map though.
- */
- @Override
- public Set<Map.Entry<String, String>> entrySet() {
- Set<Map.Entry<String, String>> ret = new
HashSet<Map.Entry<String, String>>();
- for(Map.Entry<String, String> entry : wrapped.entrySet()) {
- ret.add(new Entry(entry, true));
- }
-
- return ret;
- }
-
- public Set<Map.Entry<String, String>> getRawEntrySet() {
- Set<Map.Entry<String, String>> ret = new
HashSet<Map.Entry<String, String>>();
- for(Map.Entry<String, String> entry : wrapped.entrySet()) {
- ret.add(new Entry(entry, false));
- }
-
- return ret;
- }
-
- @Override
- public String remove(Object key) {
- resolved.remove(key);
- return wrapped.remove(key).toString();
- }
-
- @Override
- public int size() {
- return wrapped.size();
- }
-
- /**
- * Unlike in the default implementation the collection returned
- * from this method <b>IS NOT</b> backed by this map.
- */
- @Override
- public Collection<String> values() {
- List<String> ret = new ArrayList<String>();
- for(String key : keySet()) {
- ret.add(get(key));
- }
-
- return ret;
- }
-
- public Collection<String> getRawValues() {
- List<String> ret = new ArrayList<String>();
- for(String key : keySet()) {
- ret.add(wrapped.get(key));
- }
-
- return ret;
- }
-
- private String readAll(Reader rdr) {
- int in = -1;
- StringBuilder bld = new StringBuilder();
- try {
- while ((in = rdr.read()) != -1) {
- bld.append((char) in);
- }
- } catch (IOException e) {
- throw new IllegalStateException("Exception while reading a
string.", e);
- }
-
- return bld.toString();
- }
-}
\ No newline at end of file
diff --git a/modules/test-utils/src/main/java/org/rhq/test/TokenReplacingReader.java
b/modules/test-utils/src/main/java/org/rhq/test/TokenReplacingReader.java
deleted file mode 100644
index bc5aef6..0000000
--- a/modules/test-utils/src/main/java/org/rhq/test/TokenReplacingReader.java
+++ /dev/null
@@ -1,201 +0,0 @@
-/*
- * RHQ Management Platform
- * Copyright (C) 2005-2011 Red Hat, Inc.
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
-package org.rhq.test;
-
-import java.io.IOException;
-import java.io.PushbackReader;
-import java.io.Reader;
-import java.io.StringReader;
-import java.nio.CharBuffer;
-import java.util.ArrayDeque;
-import java.util.Deque;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Copied from
http://tutorials.jenkov.com/java-howto/replace-strings-in-streams-arrays-...
- * with fixes to {@link #read(char[], int, int)} and added support for escaping.
- *
- * @author Lukas Krejci
- */
-public class TokenReplacingReader extends Reader {
-
- private PushbackReader pushbackReader = null;
- private Map<String, String> tokens = null;
- private StringBuilder tokenNameBuffer = new StringBuilder();
- private String tokenValue = null;
- private int tokenValueIndex = 0;
- private boolean escaping = false;
- private Deque<String> activeTokens;
- private Map<String, String> resolvedTokens;
-
- public TokenReplacingReader(Reader source, Map<String, String> tokens) {
- this.pushbackReader = new PushbackReader(source, 2);
- this.tokens = tokens;
- this.activeTokens = new ArrayDeque<String>();
- this.resolvedTokens = new HashMap<String, String>();
- }
-
- protected TokenReplacingReader(String source, Map<String, String> tokens,
Deque<String> activeTokens, Map<String, String> resolvedTokens) {
- pushbackReader = new PushbackReader(new StringReader(source));
- this.tokens = tokens;
- this.activeTokens = activeTokens;
- this.resolvedTokens = resolvedTokens;
- }
-
- public int read(CharBuffer target) throws IOException {
- throw new RuntimeException("Operation Not Supported");
- }
-
- public int read() throws IOException {
- if (this.tokenValue != null) {
- if (this.tokenValueIndex < this.tokenValue.length()) {
- return this.tokenValue.charAt(this.tokenValueIndex++);
- }
- if (this.tokenValueIndex == this.tokenValue.length()) {
- this.tokenValue = null;
- this.tokenValueIndex = 0;
- }
- }
-
- int data = this.pushbackReader.read();
-
- if (escaping) {
- escaping = false;
- return data;
- }
-
- if (data == '\\') {
- escaping = true;
- return data;
- }
-
- if (data != '$')
- return data;
-
- data = this.pushbackReader.read();
- if (data != '{') {
- this.pushbackReader.unread(data);
- return '$';
- }
- this.tokenNameBuffer.delete(0, this.tokenNameBuffer.length());
-
- data = this.pushbackReader.read();
- while (data != '}') {
- this.tokenNameBuffer.append((char) data);
- data = this.pushbackReader.read();
- }
-
- String tokenName = tokenNameBuffer.toString();
-
- if (resolvedTokens.containsKey(tokenName)) {
- tokenValue = resolvedTokens.get(tokenName);
- } else {
- tokenValue = resolveToken(tokenName);
- }
-
- tokenValueIndex = 0;
-
- if (!this.tokenValue.isEmpty()) {
- return this.tokenValue.charAt(this.tokenValueIndex++);
- } else {
- return read();
- }
- }
-
- public int read(char cbuf[]) throws IOException {
- return read(cbuf, 0, cbuf.length);
- }
-
- public int read(char cbuf[], int off, int len) throws IOException {
- int i = 0;
- for (; i < len; i++) {
- int nextChar = read();
- if (nextChar == -1) {
- if (i == 0) {
- i = -1;
- }
- break;
- }
- cbuf[off + i] = (char) nextChar;
- }
- return i;
- }
-
- public void close() throws IOException {
- this.pushbackReader.close();
- }
-
- public long skip(long n) throws IOException {
- throw new UnsupportedOperationException("skip() not supported on
TokenReplacingReader.");
- }
-
- public boolean ready() throws IOException {
- return this.pushbackReader.ready();
- }
-
- public boolean markSupported() {
- return false;
- }
-
- public void mark(int readAheadLimit) throws IOException {
- throw new IOException("mark() not supported on
TokenReplacingReader.");
- }
-
- public void reset() throws IOException {
- throw new IOException("reset() not supported on
TokenReplacingReader.");
- }
-
- private String readAll(Reader r) throws IOException {
- int c;
- StringBuilder bld = new StringBuilder();
- while((c = r.read()) >= 0) {
- bld.append((char)c);
- }
-
- return bld.toString();
- }
-
- private String resolveToken(String tokenName) throws IOException {
- if (activeTokens.contains(tokenName)) {
- throw new IllegalArgumentException("Token '" + tokenName +
"' (indirectly) contains reference to itself in its value.");
- }
-
- activeTokens.push(tokenName);
-
- String tokenValue = tokens.get(tokenName);
-
- if (tokenValue != null) {
- if (tokenValue.contains("${")) {
- TokenReplacingReader childReader = new TokenReplacingReader(tokenValue,
tokens, activeTokens, resolvedTokens);
-
- tokenValue = readAll(childReader);
- }
- } else {
- tokenValue = "${" + tokenName + "}";
- }
-
- resolvedTokens.put(tokenName, tokenValue);
-
- activeTokens.pop();
-
- return tokenValue;
- }
-}