You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2016/08/09 19:54:03 UTC
[30/51] [partial] incubator-juneau git commit: Rename project
directories.
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/ConfigUtils.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/ConfigUtils.java b/juneau-core/src/main/java/org/apache/juneau/ini/ConfigUtils.java
new file mode 100644
index 0000000..190cb97
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ini/ConfigUtils.java
@@ -0,0 +1,94 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.ini;
+
+/**
+ * Internal utility methods.
+ *
+ * @author James Bognar (james.bognar@salesforce.com)
+ */
+public class ConfigUtils {
+
+ static final String getSectionName(String key) {
+ int i = key.indexOf('/');
+ if (i == -1)
+ return "default";
+ return key.substring(0, i);
+ }
+
+ static final String getSectionKey(String key) {
+ int i = key.indexOf('/');
+ if (i == -1)
+ return key;
+ return key.substring(i+1);
+ }
+
+ static final String getFullKey(String section, String key) {
+ if (section.equals("default"))
+ return key;
+ return section + '/' + key;
+ }
+
+ static final boolean isComment(String line) {
+ for (int i = 0; i < line.length(); i++) {
+ char c = line.charAt(i);
+ if (! Character.isWhitespace(c))
+ return c == '#';
+ }
+ return false;
+ }
+
+ static final boolean isAssignment(String line) {
+ int S1 = 1; // Looking for char;
+ int S2 = 2; // Found char, looking for whitespace or =
+ int S3 = 3; // Found whitespace, looking for =
+ int state = S1;
+ for (int i = 0; i < line.length(); i++) {
+ char c = line.charAt(i);
+ if (state == S1) {
+ if (! Character.isWhitespace(c))
+ state = S2;
+ } else if (state == S2) {
+ if (c == '=')
+ return true;
+ if (Character.isWhitespace(c))
+ state = S3;
+ } else if (state == S3) {
+ if (c == '=')
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static final boolean isSection(String line) {
+ int S1 = 1; // Looking for [;
+ int S2 = 2; // Found [, looking for ]
+ int state = S1;
+ for (int i = 0; i < line.length(); i++) {
+ char c = line.charAt(i);
+ if (state == S1) {
+ if (! Character.isWhitespace(c)) {
+ if (c == '[')
+ state = S2;
+ else
+ return false;
+ }
+ } else if (state == S2) {
+ if (c == ']')
+ return true;
+ }
+ }
+ return false;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/Encoder.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/Encoder.java b/juneau-core/src/main/java/org/apache/juneau/ini/Encoder.java
new file mode 100644
index 0000000..768400f
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ini/Encoder.java
@@ -0,0 +1,39 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.ini;
+
+/**
+ * API for defining a string encoding/decoding mechanism for entries in {@link ConfigFile}.
+ *
+ * @author James Bognar (james.bognar@salesforce.com)
+ */
+public interface Encoder {
+
+ /**
+ * Encode a string.
+ *
+ * @param fieldName The field name being encoded.
+ * @param in The unencoded input string.
+ * @return The encoded output string.
+ */
+ public String encode(String fieldName, String in);
+
+ /**
+ * Decode a string.
+ *
+ * @param fieldName The field name being decoded.
+ * @param in The encoded input string.
+ * @return The decoded output string.
+ */
+ public String decode(String fieldName, String in);
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/EntryListener.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/EntryListener.java b/juneau-core/src/main/java/org/apache/juneau/ini/EntryListener.java
new file mode 100644
index 0000000..157f736
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ini/EntryListener.java
@@ -0,0 +1,48 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.ini;
+
+import java.util.*;
+
+
+/**
+ * Listener that can be used to listen for change events for a specific entry in a config file.
+ * <p>
+ * Use the {@link ConfigFile#addListener(ConfigFileListener)} method to register listeners.
+ */
+public class EntryListener extends ConfigFileListener {
+
+ private String fullKey;
+
+ /**
+ * Constructor.
+ *
+ * @param fullKey The key in the config file to listen for changes on.
+ */
+ public EntryListener(String fullKey) {
+ this.fullKey = fullKey;
+ }
+
+ @Override /* ConfigFileListener */
+ public void onChange(ConfigFile cf, Set<String> changes) {
+ if (changes.contains(fullKey))
+ onChange(cf);
+ }
+
+ /**
+ * Signifies that the config file entry changed.
+ *
+ * @param cf The config file being changed.
+ */
+ public void onChange(ConfigFile cf) {}
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/Section.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/Section.java b/juneau-core/src/main/java/org/apache/juneau/ini/Section.java
new file mode 100644
index 0000000..56821e7
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ini/Section.java
@@ -0,0 +1,568 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.ini;
+
+import static org.apache.juneau.ini.ConfigFileFormat.*;
+import static org.apache.juneau.ini.ConfigUtils.*;
+
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.locks.*;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.internal.*;
+
+/**
+ * Defines a section in a config file.
+ */
+public class Section implements Map<String,String> {
+
+ private ConfigFileImpl configFile;
+ String name; // The config section name, or "default" if the default section. Never null.
+
+ // The data structures that make up this object.
+ // These must be kept synchronized.
+ private LinkedList<String> lines = new LinkedList<String>();
+ private List<String> headerComments = new LinkedList<String>();
+ private Map<String,String> entries;
+
+ private ReadWriteLock lock = new ReentrantReadWriteLock();
+ private boolean readOnly;
+
+ /**
+ * Constructor.
+ */
+ public Section() {
+ this.entries = new LinkedHashMap<String,String>();
+ }
+
+ /**
+ * Constructor with predefined contents.
+ *
+ * @param contents Predefined contents to copy into this section.
+ */
+ public Section(Map<String,String> contents) {
+ this.entries = new LinkedHashMap<String,String>(contents);
+ }
+
+ Section setReadOnly() {
+ // This method is only called once from ConfigFileImpl constructor.
+ this.readOnly = true;
+ this.entries = Collections.unmodifiableMap(entries);
+ return this;
+ }
+
+ /**
+ * Sets the config file that this section belongs to.
+ *
+ * @param configFile The config file that this section belongs to.
+ * @return This object (for method chaining).
+ */
+ @ParentProperty
+ public Section setParent(ConfigFileImpl configFile) {
+ this.configFile = configFile;
+ return this;
+ }
+
+ /**
+ * Sets the section name
+ *
+ * @param name The section name.
+ * @return This object (for method chaining).
+ */
+ @NameProperty
+ public Section setName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ //--------------------------------------------------------------------------------
+ // Map methods
+ //--------------------------------------------------------------------------------
+
+ @Override /* Map */
+ public void clear() {
+ Set<String> changes = createChanges();
+ writeLock();
+ try {
+ if (changes != null)
+ for (String k : keySet())
+ changes.add(getFullKey(name, k));
+ entries.clear();
+ lines.clear();
+ headerComments.clear();
+ } finally {
+ writeUnlock();
+ }
+ signalChanges(changes);
+ }
+
+ @Override /* Map */
+ public boolean containsKey(Object key) {
+ return entries.containsKey(key);
+ }
+
+ @Override /* Map */
+ public boolean containsValue(Object value) {
+ return entries.containsValue(value);
+ }
+
+ @Override /* Map */
+ public Set<Map.Entry<String,String>> entrySet() {
+
+ // We need to create our own set so that entries are removed correctly.
+ return new AbstractSet<Map.Entry<String,String>>() {
+ @Override /* Set */
+ public Iterator<Map.Entry<String,String>> iterator() {
+ return new Iterator<Map.Entry<String,String>>() {
+ Iterator<Map.Entry<String,String>> i = entries.entrySet().iterator();
+ Map.Entry<String,String> i2;
+
+ @Override /* Iterator */
+ public boolean hasNext() {
+ return i.hasNext();
+ }
+
+ @Override /* Iterator */
+ public Map.Entry<String,String> next() {
+ i2 = i.next();
+ return i2;
+ }
+
+ @Override /* Iterator */
+ public void remove() {
+ Set<String> changes = createChanges();
+ String key = i2.getKey(), val = i2.getValue();
+ addChange(changes, key, val, null);
+ writeLock();
+ try {
+ i.remove();
+ removeLine(key);
+ } finally {
+ writeUnlock();
+ }
+ signalChanges(changes);
+ }
+ };
+ }
+
+ @Override /* Set */
+ public int size() {
+ return entries.size();
+ }
+ };
+ }
+
+ @Override /* Map */
+ public String get(Object key) {
+ String s = entries.get(key);
+ return s;
+ }
+
+ @Override /* Map */
+ public boolean isEmpty() {
+ return entries.isEmpty();
+ }
+
+ @Override /* Map */
+ public Set<String> keySet() {
+
+ // We need to create our own set so that sections are removed correctly.
+ return new AbstractSet<String>() {
+ @Override /* Set */
+ public Iterator<String> iterator() {
+ return new Iterator<String>() {
+ Iterator<String> i = entries.keySet().iterator();
+ String i2;
+
+ @Override /* Iterator */
+ public boolean hasNext() {
+ return i.hasNext();
+ }
+
+ @Override /* Iterator */
+ public String next() {
+ i2 = i.next();
+ return i2;
+ }
+
+ @Override /* Iterator */
+ public void remove() {
+ Set<String> changes = createChanges();
+ String key = i2;
+ String val = entries.get(key);
+ addChange(changes, key, val, null);
+ writeLock();
+ try {
+ i.remove();
+ removeLine(key);
+ } finally {
+ writeUnlock();
+ }
+ signalChanges(changes);
+ }
+ };
+ }
+
+ @Override /* Set */
+ public int size() {
+ return entries.size();
+ }
+ };
+ }
+
+ @Override /* Map */
+ public String put(String key, String value) {
+ return put(key, value, false);
+ }
+
+ /**
+ * Sets the specified value in this section.
+ * @param key The section key.
+ * @param value The new value.
+ * @param encoded Whether this value should be encoded during save.
+ * @return The previous value.
+ */
+ public String put(String key, String value, boolean encoded) {
+ Set<String> changes = createChanges();
+ String s = put(key, value, encoded, changes);
+ signalChanges(changes);
+ return s;
+ }
+
+ String put(String key, String value, boolean encoded, Set<String> changes) {
+ writeLock();
+ try {
+ addLine(key, encoded);
+ String prev = entries.put(key, value);
+ addChange(changes, key, prev, value);
+ return prev;
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ @Override /* Map */
+ public void putAll(Map<? extends String,? extends String> map) {
+ Set<String> changes = createChanges();
+ for (Map.Entry<? extends String,? extends String> e : map.entrySet())
+ put(e.getKey(), e.getValue(), false, changes);
+ signalChanges(changes);
+ }
+
+ @Override /* Map */
+ public String remove(Object key) {
+ Set<String> changes = createChanges();
+ String old = remove(key, changes);
+ signalChanges(changes);
+ return old;
+ }
+
+ String remove(Object key, Set<String> changes) {
+ writeLock();
+ try {
+ String prev = entries.remove(key);
+ addChange(changes, key.toString(), prev, null);
+ removeLine(key.toString());
+ return prev;
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ private void removeLine(String key) {
+ for (Iterator<String> i = lines.iterator(); i.hasNext();) {
+ String k = i.next();
+ if (k.startsWith("*") || k.startsWith(">")) {
+ if (k.substring(1).equals(key)) {
+ i.remove();
+ break;
+ }
+ }
+ }
+ }
+
+ @Override /* Map */
+ public int size() {
+ return entries.size();
+ }
+
+ @Override /* Map */
+ public Collection<String> values() {
+ return Collections.unmodifiableCollection(entries.values());
+ }
+
+ //--------------------------------------------------------------------------------
+ // API methods
+ //--------------------------------------------------------------------------------
+
+ /**
+ * Returns <jk>true</jk> if the specified entry is encoded.
+ *
+ * @param key The key.
+ * @return <jk>true</jk> if the specified entry is encoded.
+ */
+ public boolean isEncoded(String key) {
+ readLock();
+ try {
+ for (String s : lines)
+ if (s.length() > 1)
+ if (s.substring(1).equals(key))
+ return s.charAt(0) == '*';
+ return false;
+ } finally {
+ readUnlock();
+ }
+ }
+
+ /**
+ * Adds header comments to this section.
+ * @see ConfigFile#addHeaderComments(String, String...) for a description.
+ * @param comments The comment lines to add to this section.
+ * @return This object (for method chaining).
+ */
+ public Section addHeaderComments(List<String> comments) {
+ writeLock();
+ try {
+ for (String c : comments) {
+ if (c == null)
+ c = "";
+ if (! c.startsWith("#"))
+ c = "#" + c;
+ this.headerComments.add(c);
+ }
+ return this;
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ /**
+ * Removes all header comments from this section.
+ */
+ public void clearHeaderComments() {
+ writeLock();
+ try {
+ this.headerComments.clear();
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ /**
+ * Serialize this section.
+ * @param out What to serialize to.
+ * @param format The format (e.g. INI, BATCH, SHELL).
+ */
+ public void writeTo(PrintWriter out, ConfigFileFormat format) {
+ readLock();
+ try {
+ if (format == INI) {
+ for (String s : headerComments)
+ out.append(s).println();
+ if (! name.equals("default"))
+ out.append('[').append(name).append(']').println();
+ for (String l : lines) {
+ char c = (l.length() > 0 ? l.charAt(0) : 0);
+ if (c == '>' || c == '*'){
+ boolean encode = c == '*';
+ String key = l.substring(1);
+ String val = entries.get(key);
+ if (val.indexOf('\n') != -1)
+ val = val.replaceAll("(\\r?\\n)", "$1\t");
+ if (val.indexOf('=') != -1)
+ val = val.replace("=", "\\u003D");
+ if (val.indexOf('#') != -1)
+ val = val.replace("#", "\\u0023");
+ out.append(key);
+ if (encode)
+ out.append('*');
+ out.append(" = ");
+ if (encode)
+ out.append('{').append(configFile.getEncoder().encode(key, val)).append('}');
+ else
+ out.append(val);
+ out.println();
+ } else {
+ out.append(l).println();
+ }
+ }
+
+ } else if (format == BATCH) {
+ String section = name.replaceAll("\\.\\/", "_");
+ for (String l : headerComments) {
+ l = trimComment(l);
+ if (! l.isEmpty())
+ out.append("rem ").append(l);
+ out.println();
+ }
+ for (String l : lines) {
+ char c = (l.length() > 0 ? l.charAt(0) : 0);
+ if (c == '>' || c == '*') {
+ String key = l.substring(1);
+ String val = entries.get(key);
+ out.append("set ");
+ if (! name.equals("default"))
+ out.append(section).append('_');
+ out.append(key.replaceAll("\\.\\/", "_")).append(" = ").append(val).println();
+ } else {
+ l = trimComment(l);
+ if (! l.isEmpty())
+ out.append("rem ").append(l);
+ out.println();
+ }
+ }
+
+ } else if (format == SHELL) {
+ String section = name.replaceAll("\\.\\/", "_");
+ for (String l : headerComments) {
+ l = trimComment(l);
+ if (! l.isEmpty())
+ out.append("# ").append(l);
+ out.println();
+ }
+ for (String l : lines) {
+ char c = (l.length() > 0 ? l.charAt(0) : 0);
+ if (c == '>' || c == '*'){
+ String key = l.substring(1);
+ String val = entries.get(key).replaceAll("\\\\", "\\\\\\\\");
+ out.append("export ");
+ if (! name.equals("default"))
+ out.append(section).append('_');
+ out.append(key.replaceAll("\\.\\/", "_")).append('=').append('"').append(val).append('"').println();
+ } else {
+ l = trimComment(l);
+ if (! l.isEmpty())
+ out.append("# ").append(l);
+ out.println();
+ }
+ }
+ }
+ } finally {
+ readUnlock();
+ }
+ }
+
+ //--------------------------------------------------------------------------------
+ // Protected methods used by ConfigFile
+ //--------------------------------------------------------------------------------
+
+ /*
+ * Add lines to this section.
+ */
+ Section addLines(Set<String> changes, String...l) {
+ writeLock();
+ try {
+ if (l == null)
+ l = new String[0];
+ for (int i = 0; i < l.length; i++) {
+ String line = l[i];
+ if (line == null)
+ line = "";
+ if (isComment(line))
+ this.lines.add(line);
+ else if (isAssignment(line)) {
+ // Key/value pairs are stored as either ">key" or "*key";
+ String key = StringUtils.replaceUnicodeSequences(line.substring(0, line.indexOf('=')).trim());
+ String val = StringUtils.replaceUnicodeSequences(line.substring(line.indexOf('=')+1).trim());
+ boolean encoded = key.length() > 1 && key.endsWith("*");
+ if (encoded) {
+ key = key.substring(0, key.lastIndexOf('*'));
+ String v = val.toString().trim();
+ if (v.startsWith("{") && v.endsWith("}"))
+ val = configFile.getEncoder().decode(key, v.substring(1, v.length()-1));
+ else
+ configFile.setHasBeenModified();
+ }
+ if (containsKey(key)) {
+ entries.remove(key);
+ lines.remove('*' + key);
+ lines.remove('>' + key);
+ }
+ lines.add((encoded ? '*' : '>') + key);
+ addChange(changes, key, entries.put(key, val), val);
+ } else {
+ this.lines.add(line);
+ }
+ }
+ return this;
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ /*
+ * Remove all "#*" lines at the end of this section so they can
+ * be associated with the next section.
+ */
+ List<String> removeTrailingComments() {
+ LinkedList<String> l = new LinkedList<String>();
+ while ((! lines.isEmpty()) && lines.getLast().startsWith("#"))
+ l.addFirst(lines.removeLast());
+ return l;
+ }
+
+ //--------------------------------------------------------------------------------
+ // Private methods
+ //--------------------------------------------------------------------------------
+
+ private void addLine(String key, boolean encoded) {
+ for (Iterator<String> i = lines.iterator(); i.hasNext();) {
+ String k = i.next();
+ if ((k.startsWith("*") || k.startsWith(">")) && k.substring(1).equals(key)) {
+ if (k.startsWith("*") && encoded || k.startsWith(">") && ! encoded)
+ return;
+ i.remove();
+ }
+ }
+ lines.add((encoded ? "*" : ">") + key);
+ }
+
+ private void readLock() {
+ lock.readLock().lock();
+ }
+
+ private void readUnlock() {
+ lock.readLock().unlock();
+ }
+
+ private void writeLock() {
+ if (readOnly)
+ throw new UnsupportedOperationException("Cannot modify read-only ConfigFile.");
+ lock.writeLock().lock();
+ }
+
+ private void writeUnlock() {
+ lock.writeLock().unlock();
+ }
+
+ private String trimComment(String s) {
+ return s.replaceAll("^\\s*\\#\\s*", "").trim();
+ }
+
+ private Set<String> createChanges() {
+ return (configFile != null && configFile.getListeners().size() > 0 ? new LinkedHashSet<String>() : null);
+ }
+
+ private void signalChanges(Set<String> changes) {
+ if (changes != null && ! changes.isEmpty())
+ for (ConfigFileListener l : configFile.getListeners())
+ l.onChange(configFile, changes);
+ }
+
+ private void addChange(Set<String> changes, String key, String oldVal, String newVal) {
+ if (changes != null)
+ if (! StringUtils.isEquals(oldVal, newVal))
+ changes.add(getFullKey(name, key));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/SectionListener.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/SectionListener.java b/juneau-core/src/main/java/org/apache/juneau/ini/SectionListener.java
new file mode 100644
index 0000000..83f0c9e
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ini/SectionListener.java
@@ -0,0 +1,63 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.ini;
+
+import java.util.*;
+
+import org.apache.juneau.internal.*;
+
+
+/**
+ * Listener that can be used to listen for change events for a specific section in a config file.
+ * <p>
+ * Use the {@link ConfigFile#addListener(ConfigFileListener)} method to register listeners.
+ */
+public class SectionListener extends ConfigFileListener {
+
+ private boolean isDefault;
+ private String prefix;
+
+ /**
+ * Constructor.
+ *
+ * @param section The name of the section in the config file to listen to.
+ */
+ public SectionListener(String section) {
+ isDefault = StringUtils.isEmpty(section);
+ prefix = isDefault ? null : (section + '/');
+ }
+
+ @Override /* ConfigFileListener */
+ public void onChange(ConfigFile cf, Set<String> changes) {
+ for (String c : changes) {
+ if (isDefault) {
+ if (c.indexOf('/') == -1) {
+ onChange(cf);
+ return;
+ }
+ } else {
+ if (c.startsWith(prefix)) {
+ onChange(cf);
+ return;
+ }
+ }
+ }
+ }
+
+ /**
+ * Signifies that the config file entry changed.
+ *
+ * @param cf The config file being modified.
+ */
+ public void onChange(ConfigFile cf) {}
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/XorEncoder.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/XorEncoder.java b/juneau-core/src/main/java/org/apache/juneau/ini/XorEncoder.java
new file mode 100644
index 0000000..42874e2
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ini/XorEncoder.java
@@ -0,0 +1,50 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.ini;
+
+import org.apache.juneau.internal.*;
+
+/**
+ * Simply XOR+Base64 encoder for obscuring passwords and other sensitive data in INI config files.
+ * <p>
+ * This is not intended to be used as strong encryption.
+ *
+ * @author James Bognar (james.bognar@salesforce.com)
+ */
+public final class XorEncoder implements Encoder {
+
+ /** Reusable XOR-Encoder instance. */
+ public static final XorEncoder INSTANCE = new XorEncoder();
+
+ private static final String key = System.getProperty("org.apache.juneau.ini.XorEncoder.key", "nuy7og796Vh6G9O6bG230SHK0cc8QYkH"); // The super-duper-secret key
+
+ @Override /* Encoder */
+ public String encode(String fieldName, String in) {
+ byte[] b = in.getBytes(IOUtils.UTF8);
+ for (int i = 0; i < b.length; i++) {
+ int j = i % key.length();
+ b[i] = (byte)(b[i] ^ key.charAt(j));
+ }
+ return StringUtils.base64Encode(b);
+ }
+
+ @Override /* Encoder */
+ public String decode(String fieldName, String in) {
+ byte[] b = StringUtils.base64Decode(in);
+ for (int i = 0; i < b.length; i++) {
+ int j = i % key.length();
+ b[i] = (byte)(b[i] ^ key.charAt(j));
+ }
+ return new String(b, IOUtils.UTF8);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config1.png
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config1.png b/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config1.png
new file mode 100644
index 0000000..531f280
Binary files /dev/null and b/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config1.png differ
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config2.png
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config2.png b/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config2.png
new file mode 100644
index 0000000..7f5a4b3
Binary files /dev/null and b/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config2.png differ
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config3.png
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config3.png b/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config3.png
new file mode 100644
index 0000000..749da14
Binary files /dev/null and b/juneau-core/src/main/java/org/apache/juneau/ini/doc-files/config3.png differ
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/ini/package.html
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ini/package.html b/juneau-core/src/main/java/org/apache/juneau/ini/package.html
new file mode 100644
index 0000000..b8b3509
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ini/package.html
@@ -0,0 +1,650 @@
+<!DOCTYPE HTML>
+<!--
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ *
+ ***************************************************************************************************************************/
+ -->
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <style type="text/css">
+ /* For viewing in Page Designer */
+ @IMPORT url("../../../../../../javadoc.css");
+
+ /* For viewing in REST interface */
+ @IMPORT url("../htdocs/javadoc.css");
+ body {
+ margin: 20px;
+ }
+ </style>
+ <script>
+ /* Replace all @code and @link tags. */
+ window.onload = function() {
+ document.body.innerHTML = document.body.innerHTML.replace(/\{\@code ([^\}]+)\}/g, '<code>$1</code>');
+ document.body.innerHTML = document.body.innerHTML.replace(/\{\@link (([^\}]+)\.)?([^\.\}]+)\}/g, '<code>$3</code>');
+ }
+ </script>
+</head>
+<body>
+<p>INI file support</p>
+
+<script>
+ function toggle(x) {
+ var div = x.nextSibling;
+ while (div != null && div.nodeType != 1)
+ div = div.nextSibling;
+ if (div != null) {
+ var d = div.style.display;
+ if (d == 'block' || d == '') {
+ div.style.display = 'none';
+ x.className += " closed";
+ } else {
+ div.style.display = 'block';
+ x.className = x.className.replace(/(?:^|\s)closed(?!\S)/g , '' );
+ }
+ }
+ }
+</script>
+
+<a id='TOC'></a><h5 class='toc'>Table of Contents</h5>
+<ol class='toc'>
+ <li><p><a class='doclink' href='#Overview'>Overview</a></p>
+ <li><p><a class='doclink' href='#Variables'>Variables</a></p>
+ <li><p><a class='doclink' href='#Encoded'>Encoded Entries</a></p>
+ <li><p><a class='doclink' href='#Listeners'>Listeners</a></p>
+ <li><p><a class='doclink' href='#CommandLine'>Command Line API</a></p>
+ <li><p><a class='doclink' href='#Serializing'>Serializing Config Files</a></p>
+ <li><p><a class='doclink' href='#Merging'>Merging Config Files</a></p>
+</ol>
+
+<!-- ======================================================================================================== -->
+<a id="Overview"></a>
+<h2 class='topic' onclick='toggle(this)'>1 - Overview</h2>
+<div class='topic'>
+ <p>
+ The {@link org.apache.juneau.ini.ConfigMgr} and {@link org.apache.juneau.ini.ConfigFile} classes
+ implement an API for working with INI-style configuration files such as the following:
+ </p>
+ <p class='bcode'>
+ <cc>#--------------------------</cc>
+ <cc># Default section</cc>
+ <cc>#--------------------------</cc>
+ <ck>key1</ck> = <cv>1</cv>
+ <ck>key2</ck> = <cv>true</cv>
+ <ck>key3</ck> = <cv>1,2,3</cv>
+ <ck>key4</ck> = <cv>http://foo</cv>
+
+ <cc>#--------------------------</cc>
+ <cc># A comment about Section 1</cc>
+ <cc>#--------------------------</cc>
+ <cs>[Section1]</cs>
+ <ck>key1</ck> = <cv>2</cv>
+ <ck>key2</ck> = <cv>false</cv>
+ <ck>key3</ck> = <cv>4,5,6</cv>
+ <ck>key4</ck> = <cv>http://bar</cv>
+ </p>
+
+ <p>
+ The {@link org.apache.juneau.ini.ConfigMgr} class is used to instantiate instances of
+ {@link org.apache.juneau.ini.ConfigFile} which can then be used to retrieve config file values through either <js>"key"</js> or <js>"Section/key"</js> identifiers.
+ </p>
+
+ <p class='bcode'>
+ <jk>int</jk> key1;
+ <jk>boolean</jk> key2;
+ <jk>int</jk>[] key3;
+ URL key4;
+
+ <jc>// Get our config file using the default config manager</jc>
+ ConfigFile f = ConfigMgr.<jsf>DEFAULT</jsf>.getConfig(<js>"C:/temp/MyConfig.cfg"</js>);
+
+ <jc>// Read values from default section</jc>
+ key1 = f.getInt(<js>"key1"</js>);
+ key2 = f.getBoolean(<js>"key2"</js>);
+ key3 = f.getObject(<jk>int</jk>[].<jk>class</jk>, <js>"key3"</js>);
+ key4 = f.getObject(URL.<jk>class</jk>, <js>"key4"</js>);
+
+ <jc>// Read values from Section #1</jc>
+ key1 = f.getInt(<js>"Section1/key1"</js>);
+ key2 = f.getBoolean(<js>"Section1/key2"</js>);
+ key3 = f.getObject(<jk>int</jk>[].<jk>class</jk>, <js>"Section1/key3"</js>);
+ key4 = f.getObject(URL.<jk>class</jk>, <js>"Section1/key4"</js>);
+ </p>
+
+ <p>
+ The interface also allows config files to be constructed programmatically...
+ </p>
+
+ <p class='bcode'>
+ <jc>// Construct the sample INI file programmatically</jc>
+ ConfigFile f = ConfigMgr.<jsf>DEFAULT</jsf>.getConfig(<js>"C:/temp/MyConfig.cfg"</js>, <jk>true</jk>)
+ .addLines(<jk>null</jk>, <jc>// The default 'null' section</jc>
+ <js>"# Default section"</js>, <jc>// A regular comment</jc>
+ <js>"key1 = 1"</js>, <jc>// A numeric entry</jc>
+ <js>"key2 = true"</js>, <jc>// A boolean entry</jc>
+ <js>"key3 = 1,2,3"</js>, <jc>// An array entry</jc>
+ <js>"key4 = http://foo"</js>, <jc>// A POJO entry</jc>
+ <js>""</js>) <jc>// A blank line</jc>
+ .addHeaderComments(<js>"Section1"</js>, <jc>// The 'Section1' section</jc>
+ <js>"A comment about Section 1"</js>) <jc>// A header comment</jc>
+ .addLines(<js>"Section1"</js>, <jc>// The 'Section1' section</jc>
+ <js>"key1 = 2"</js>, <jc>// A numeric entry</jc>
+ <js>"key2 = false"</js>, <jc>// A boolean entry</jc>
+ <js>"key3 = 4,5,6"</js>, <jc>// An array entry</jc>
+ <js>"key4 = http://bar"</js>) <jc>// A POJO entry</jc>
+ .save(); <jc>// Save to MyConfig.cfg</jc>
+ </p>
+
+ <p>
+ The following is equivalent, except uses {@link org.apache.juneau.ini.ConfigFile#put(String,Object)} to set values.
+ Note how we're setting values as POJOs which will be automatically converted to strings when persisted to disk.
+ <p class='bcode'>
+ <jc>// Construct the sample INI file programmatically</jc>
+ ConfigFile f = ConfigMgr.<jsf>DEFAULT</jsf>.getConfig(<js>"C:/temp/MyConfig.cfg"</js>, <jk>true</jk>)
+ .addLines(<jk>null</jk>,
+ <js>"# Default section"</js>)
+ .addHeaderComments(<js>"Section1"</js>,
+ <js>"A comment about Section 1"</js>);
+ cf.put(<js>"key1"</js>, 1);
+ cf.put(<js>"key2"</js>, <jk>true</jk>);
+ cf.put(<js>"key3"</js>, <jk>new int</jk>[]{1,2,3});
+ cf.put(<js>"key4"</js>, <jk>new</jk> URL(<js>"http://foo"</js>));
+ cf.put(<js>"Section1/key1"</js>, 2);
+ cf.put(<js>"Section1/key2"</js>, <jk>false</jk>);
+ cf.put(<js>"Section1/key3"</js>, <jk>new int</jk>[]{4,5,6});
+ cf.put(<js>"Section1/key4"</js>, <jk>new</jk> URL(<js>"http://bar"</js>));
+ cf.save();
+ </p>
+ <p>
+ Refer to {@link org.apache.juneau.ini.ConfigFile#put(String,Object,boolean)} for a description of
+ formats for various data types.
+ </p>
+ <p>
+ Various convenience getter methods are provided for retrieving different data types:
+ </p>
+ <p class='bcode'>
+ <jc>// Strings with default values</jc>
+ <jc>// key1 = foobar</jc>
+ String key1 = cf.getString(<js>"key1"</js>);
+
+ <jc>// Numbers</jc>
+ <jc>// key2 = 123</jc>
+ <jk>float</jk> key2 = cf.getObject(<jk>float</jk>.<jk>class</jk>, <js>"key2"</js>);
+
+ <jc>// Booleans</jc>
+ <jc>// key3 = true</jc>
+ <jk>boolean</jk> key3 = cf.getBoolean(<js>"key3"</js>);
+
+ <jc>// Objects convertable to and from strings using the JSON serializer and parser</jc>
+ <jc>// key4 = http://foo</jc>
+ URL key4 = cf.getObject(URL.<jk>class</jk>, <js>"key4"</js>);
+
+ <jc>// Arrays of strings</jc>
+ <jc>// key5 = foo, bar</jc>
+ String[] key5 = cf.getStringArray(<js>"key5"</js>);
+
+ <jc>// Arrays of objects</jc>
+ <jc>// key6 = http://foo,http://bar</jc>
+ URL[] key6 = cf.getObject(URL[].<jk>class</jk>, <js>"key6"</js>);
+
+ <jc>// Arrays of primitives</jc>
+ <jc>// key7 = 1,2,3</jc>
+ <jk>int</jk>[] key7 = cf.getObject(<jk>int</jk>[].<jk>class</jk>, <js>"key7"</js>);
+
+ <jc>// Enums</jc>
+ <jc>// key8 = MINUTES</jc>
+ TimeUnit key8 = cf.getObject(TimeUnit.<jk>class</jk>, <js>"key8"</js>);
+
+ <jc>// Beans</jc>
+ <jc>// key9 = {name:'John Smith', addresses:[{street:'101 Main St', city:'Anywhere', state:'TX'}]}</jc>
+ Person key9 = cf.getObject(Person.<jk>class</jk>, <js>"key9"</js>);
+
+ <jc>// Generic Maps</jc>
+ <jc>// key10 = {foo:'bar', baz:123}</jc>
+ Map key10 = cf.getObject(ObjectMap.<jk>class</jk>, <js>"key10"</js>);
+ </p>
+</div>
+
+<!-- ======================================================================================================== -->
+<a id="Variables"></a>
+<h2 class='topic' onclick='toggle(this)'>2 - Variables</h2>
+<div class='topic'>
+ <p>
+ Config files can contain variables that get resolved dynamically using the {@link org.apache.juneau.svl.VarResolver} API.<br>
+ </p>
+ <p>
+ Resolving config files can be retrieved through the following methods:
+ <ul class='spaced-list'>
+ <li>{@link org.apache.juneau.ini.ConfigFile#getResolving()} - Returns a config file that resolves a default set of variables.
+ <li>{@link org.apache.juneau.ini.ConfigFile#getResolving(VarResolver)} - Returns a config file that resolves a custom set of variables.
+ </ul>
+ </p>
+ <p>
+ The default {@link org.apache.juneau.ini.ConfigFile#getResolving()} method returns a config file that resolves the following
+ variables:
+ </p>
+ <ul class='spaced-list'>
+ <li><code>$S{key}</code>, <code>$S{key,default}</code> - System properties.
+ <li><code>$E{key}</code>, <code>$E{key,default}</code> - Environment variables.
+ <li><code>$C{key}</code>, <code>$C{key,default}</code> - Values in this configuration file.
+ </ul>
+ <p>
+ <h6 class='topic'>Examples:</h6>
+ <p class='bcode'>
+ <cc>#--------------------------</cc>
+ <cc># Examples </cc>
+ <cc>#--------------------------</cc>
+ <cs>[MyProperties]</cs>
+ <ck>javaHome</ck> = <cv>$S{java.home}</cv>
+ <ck>path</ck> = <cv>$E{PATH}</cv>
+ <ck>customMessage</ck> = <cv>Java home is $C{MyProperties/javaHome} and the environment path is $C{MyProperties/path}.</cv>
+ </p>
+ <p>
+ Resolving config files (and any config files retrieved through the same <code>ConfigMgr</code> that point to the same physical file)
+ share the same underlying config files in memory.
+ This allows changes in one instance of the config file to be reflected in all.
+ </p>
+ <p>
+ Support for variables is extensible. You can add support for your own variables by implementing custom
+ {@link org.apache.juneau.svl.VarResolver VarResolvers}.<br>
+ For example, the microservice <code>Resource</code> class provides access to config files that
+ can contain any of the following variables:
+ </p>
+ <ul>
+ <li><code>$C</code> - Config variables.
+ <li><code>$S</code> - System properties.
+ <li><code>$E</code> - Environment variables.
+ <li><code>$I</code> - Servlet init parameters.
+ <li><code>$ARG</code> - JVM command-line arguments.
+ <li><code>$MF</code> - Main jar manifest file entries.
+ <li><code>$L</code> - Localized strings.
+ <li><code>$A</code> - HTTP request attributes.
+ <li><code>$P</code> - HTTP request URL parameters.
+ <li><code>$R</code> - HTTP request variables.
+ <li><code>$UE</code> - URL-encoding function.
+ </ul>
+</div>
+
+<!-- ======================================================================================================== -->
+<a id="Encoded"></a>
+<h2 class='topic' onclick='toggle(this)'>3 - Encoded Entries</h2>
+<div class='topic'>
+ <p>
+ If a config file contains sensitive information such as passwords, those values can be
+ marked for encoding by appending <js>'*'</js> to the end of the key name.<br>
+ If a marked and unencoded value is detected in the file during load, it will be encoded and saved immediately.
+ </p>
+ <p>
+ For example, the following password is marked for encoding....
+ </p>
+ <p class='bcode'>
+ <cs>[MyHost]</cs>
+ <ck>url</ck> = <cv>http://localhost:9080/foo</cv>
+ <ck>user</ck> = <cv>me</cv>
+ <ck>password*</ck> = <cv>mypassword</cv>
+ </p>
+ <p>
+ After initial loading, the file contents will contain an encoded value...
+ </p>
+ <p class='bcode'>
+ <cs>[MyHost]</cs>
+ <ck>url</ck> = <cv>http://localhost:9080/foo</cv>
+ <ck>user</ck> = <cv>me</cv>
+ <ck>password*</ck> = <cv>{AwwJVhwUQFZEMg==}</cv>
+ </p>
+ <p>
+ The default encoder is {@link org.apache.juneau.ini.XorEncoder} which is a simple XOR+Base64 encoder.<br>
+ If desired, custom encoder can be used by implementing the {@link org.apache.juneau.ini.Encoder}
+ interface and creating your own <code>ConfigMgr</code> using the {@link org.apache.juneau.ini.ConfigMgr#ConfigMgr(boolean,Encoder,WriterSerializer,ReaderParser,Charset,String[])}
+ constructor.
+ </p>
+</div>
+
+<!-- ======================================================================================================== -->
+<a id="Listeners"></a>
+<h2 class='topic' onclick='toggle(this)'>4 - Listeners</h2>
+<div class='topic'>
+ <p>
+ The following method is provided for listening to changes made on config files:
+ </p>
+ <p>
+ {@link org.apache.juneau.ini.ConfigFile#addListener(ConfigFileListener)}.
+ </p>
+ <p>
+ Subclasses are provided for listening for different kinds of events:
+ </p>
+ <ul class='spaced-list'>
+ <li>{@link org.apache.juneau.ini.ConfigFileListener} - Config file is saved, loaded, or modified.
+ <li>{@link org.apache.juneau.ini.SectionListener} - One or more entries in a section are modified.
+ <li>{@link org.apache.juneau.ini.EntryListener} - An individual entry is modified.
+ </ul>
+ <h6 class="topic">Example:</h6>
+ <p class='bcode'>
+ <jc>// Get our config file using the default config manager</jc>
+ ConfigFile f = ConfigMgr.<jsf>DEFAULT</jsf>.getConfig(<js>"C:/temp/MyConfig.cfg"</js>);
+
+ <jc>// Add a listener for an entry</jc>
+ f.addListener(
+ <jk>new</jk> EntryListener(<js>"Section1/key1"</js>) {
+ <ja>@Override</ja>
+ <jk>public void</jk> onChange(ConfigFile cf) {
+ System.<jsf>err</jsf>.println(<js>"Entry changed! New value is "</js> + cf.getString(<js>"Section1/key1"</js>));
+ }
+ }
+ );
+ </p>
+ <p>
+ Note that since {@link org.apache.juneau.ini.ConfigFile} instances for the same physical files are shared in {@link org.apache.juneau.ini.ConfigMgr}, a change made
+ in once instance of a config file will trigger all listeners defined on that physical file.
+ </p>
+</div>
+
+<!-- ======================================================================================================== -->
+<a id="CommandLine"></a>
+<h2 class='topic' onclick='toggle(this)'>5 - Command Line API</h2>
+<div class='topic'>
+ <p>
+ The {@link org.apache.juneau.ini.ConfigMgr} class contains a {@link org.apache.juneau.ini.ConfigMgr#main(String[])}
+ method that can be used to work with config files through a command-line prompt.<br>
+ This is invoked as a normal Java command:
+ </p>
+ <p class='bcode'>
+ java -jar juneau.jar org.apache.juneau.ini.ConfigMgr [args]
+ </p>
+ <p>
+ Arguments can be any of the following...
+ <ul class='spaced-list'>
+ <li>No arguments<br>
+ Prints usage message.
+ <li><code>createBatchEnvFile -configfile <configFile> -envfile <batchFile> [-verbose]</code><br>
+ Creates a batch file that will set each config file entry as an environment variable.<br>
+ Characters in the keys that are not valid as environment variable names (e.g. <js>'/'</js> and <js>'.'</js>)
+ will be converted to underscores.
+ <li><code>createShellEnvFile -configFile <configFile> -envFile <configFile> [-verbose]</code>
+ Creates a shell script that will set each config file entry as an environment variable.<br>
+ Characters in the keys that are not valid as environment variable names (e.g. <js>'/'</js> and <js>'.'</js>)
+ will be converted to underscores.
+ <li><code>setVals -configFile <configFile> -vals [var1=val1 [var2=val2...]] [-verbose]</code>
+ Sets values in config files.
+ </ul>
+ </p>
+ <p>
+ For example, the following command will create the file <code>'MyConfig.bat'</code> from the contents of the file <code>'MyConfig.cfg'</code>.
+ </p>
+ <p class='bcode'>
+ java org.apache.juneau.ini.ConfigMgr createBatchEnvFile -configfile C:\foo\MyConfig.cfg -batchfile C:\foo\MyConfig.bat
+ </p>
+</div>
+
+<!-- ======================================================================================================== -->
+<a id="Serializing"></a>
+<h2 class='topic' onclick='toggle(this)'>6 - Serializing Config Files</h2>
+<div class='topic'>
+ <p>
+ Instances of {@link org.apache.juneau.ini.ConfigFile} are POJOs that can be serialized to and parsed from
+ all supported Juneau languages.
+ </p>
+ <p>
+ The <code>org.apache.juneau.microservice.resources.ConfigResource</code> is a predefined REST interface that
+ allows access to the config file used by a microservice.<br>
+ The <code>com.ibm.team.juneau.samples</code> project is a microservice that includes this resource
+ at <code>http://localhost:10000/sample/config</code>.<br>
+ The sample microservice uses the following config file <code>juneau-samples.cfg</code>:
+ </p>
+ <p class='bcode'>
+ <cc>#================================================================================
+ # Basic configuration file for SaaS microservices
+ # Subprojects can use this as a starting point.
+ #================================================================================</cc>
+
+ <cc>#================================================================================
+ # REST settings
+ #================================================================================</cc>
+ <cs>[REST]</cs>
+
+ <cc># The HTTP port number to use.
+ # Default is Rest-Port setting in manifest file, or 8000.</cc>
+ <ck>port</ck> = <cv>10000</cv>
+
+ <cc># A JSON map of servlet paths to servlet classes.
+ # Example:
+ # resourceMap = {'/*':'com.ibm.MyServlet'}
+ # Either resourceMap or resources must be specified.</cc>
+ <ck>resourceMap</ck> =
+
+ <cc># A comma-delimited list of names of classes that extend from Servlet.
+ # Resource paths are pulled from @RestResource.path() annotation, or
+ # "/*" if annotation not specified.
+ # Example:
+ # resources = com.ibm.MyServlet
+ # Default is Rest-Resources in manifest file.
+ # Either resourceMap or resources must be specified.</cc>
+ <ck>resources</ck> =
+
+ <cc># The context root of the Jetty server.
+ # Default is Rest-ContextPath in manifest file, or "/".</cc>
+ <ck>contextPath</ck> =
+
+ <cc># Authentication: NONE, BASIC.</cc>
+ <ck>authType</ck> = <cv>NONE</cv>
+
+ <cc># The BASIC auth username.
+ # Default is Rest-LoginUser in manifest file.</cc>
+ <ck>loginUser</ck> =
+
+ <cc># The BASIC auth password.
+ # Default is Rest-LoginPassword in manifest file.</cc>
+ <ck>loginPassword</ck> =
+
+ <cc># The BASIC auth realm.
+ # Default is Rest-AuthRealm in manifest file.</cc>
+ <ck>authRealm</ck> =
+
+ <cc># Stylesheet to use for HTML views.
+ # The default options are:
+ # - styles/juneau.css
+ # - styles/devops.css
+ # Other stylesheets can be referenced relative to the servlet package or working
+ # directory.</cc>
+ <ck>stylesheet</ck> = <cv>styles/devops.css</cv>
+
+ <cc># What to do when the config file is saved.
+ # Possible values:
+ # NOTHING - Don't do anything.
+ # RESTART_SERVER - Restart the Jetty server.
+ # RESTART_SERVICE - Shutdown and exit with code '3'.</cc>
+ <ck>saveConfigAction</ck> = <cv>RESTART_SERVER</cv>
+
+ <cc># Enable SSL support.</cc>
+ <ck>useSsl</ck> = false
+
+ <cc>#================================================================================
+ # Bean properties on the org.eclipse.jetty.util.ssl.SslSocketFactory class
+ #--------------------------------------------------------------------------------
+ # Ignored if REST/useSsl is false.
+ #================================================================================</cc>
+ <cs>[REST-SslContextFactory]</cs>
+ <ck>keyStorePath</ck> = <cv>client_keystore.jks</cv>
+ <ck>keyStorePassword*</ck> = <cv>{HRAaRQoT}</cv>
+ <ck>excludeCipherSuites</ck> = <cv>TLS_DHE.*, TLS_EDH.*</cv>
+ <ck>excludeProtocols</ck> = <cv>SSLv3</cv>
+ <ck>allowRenegotiate</ck> = <cv>false</cv>
+
+ <cc>#================================================================================
+ # Logger settings
+ # See FileHandler Java class for details.
+ #================================================================================</cc>
+ <cs>[Logging]</cs>
+
+ <cc># The directory where to create the log file.
+ # Default is "."</cc>
+ <ck>logDir</ck> = <cv>logs</cv>
+
+ <cc># The name of the log file to create for the main logger.
+ # The logDir and logFile make up the pattern that's passed to the FileHandler
+ # constructor.
+ # If value is not specified, then logging to a file will not be set up.</cc>
+ <ck>logFile</ck> = <cv>microservice.%g.log</cv>
+
+ <cc># Whether to append to the existing log file or create a new one.
+ # Default is false.</cc>
+ <ck>append</ck> =
+
+ <cc># The SimpleDateFormat format to use for dates.
+ # Default is "yyyy.MM.dd hh:mm:ss".</cc>
+ <ck>dateFormat</ck> =
+
+ <cc># The log message format.
+ # The value can contain any of the following variables:
+ # {date} - The date, formatted per dateFormat.
+ # {class} - The class name.
+ # {method} - The method name.
+ # {logger} - The logger name.
+ # {level} - The log level name.
+ # {msg} - The log message.
+ # {threadid} - The thread ID.
+ # {exception} - The localized exception message.
+ # Default is "[{date} {level}] {msg}%n".</cc>
+ <ck>format</ck> =
+
+ <cc># The maximum log file size.
+ # Suffixes available for numbers.
+ # See ConfigFile.getInt(String,int) for details.
+ # Default is 1M.</cc>
+ <ck>limit</ck> = <cv>10M</cv>
+
+ <cc># Max number of log files.
+ # Default is 1.</cc>
+ <ck>count</ck> = <cv>5</cv>
+
+ <cc># Default log levels.
+ # Keys are logger names.
+ # Values are serialized Level POJOs.</cc>
+ <ck>levels</ck> = <cv>{ org.apache.juneau:'INFO' }</cv>
+
+ <cc># Only print unique stack traces once and then refer to them by a simple 8 character hash identifier.
+ # Useful for preventing log files from filling up with duplicate stack traces.
+ # Default is false.</cc>
+ <ck>useStackTraceHashes</ck> = <cv>true</cv>
+
+ <cc># The default level for the console logger.
+ # Default is WARNING.</cc>
+ <ck>consoleLevel</ck> =
+
+ <cc>#================================================================================
+ # System properties
+ #--------------------------------------------------------------------------------
+ # These are arbitrary system properties that are set during startup.
+ #================================================================================</cc>
+ <cs>[SystemProperties]</cs>
+
+ <cc># Configure Jetty for StdErrLog Logging</cc>
+ <ck>org.eclipse.jetty.util.log.class</ck> = <cv>org.eclipse.jetty.util.log.StrErrLog</cv>
+
+ <cc># Jetty logging level</cc>
+ <ck>org.eclipse.jetty.LEVEL</ck> = <cv>WARN</cv>
+ </p>
+ <p>
+ The config file looks deceivingly simple.
+ However, it should be noticed that the config file is a VERY powerful feature with many capabilities including:
+ </p>
+ <p>
+ When you point your browser to this resource, you'll notice that the contents of the config file
+ are being serialized to HTML as a POJO:
+ </p>
+ <img class='bordered' src="doc-files/config1.png">
+ <p>
+ Likewise, the config file can also be serialized as any of the supported languages such as JSON:
+ </p>
+ <img class='bordered' src="doc-files/config2.png">
+ <p>
+ The code for implementing this page could not be any simpler, since it simply returns the config
+ file returned by the <code>RestServlet.getConfig()</code> method.
+ </p>
+ <p class='bcode'>
+ <jd>/**
+ * [GET /] - Show contents of config file.
+ *
+ * <ja>@return</ja> The config file.
+ * <ja>@throws</ja> Exception
+ */</jd>
+ <ja>@RestMethod</ja>(name=<js>"GET"</js>, path=<js>"/"</js>, description=<js>"Show contents of config file."</js>)
+ <jk>public</jk> ConfigFile getConfigContents() <jk>throws</jk> Exception {
+ <jk>return</jk> getConfig();
+ }
+ </p>
+ <p>
+ The edit page takes you to an editor that allows you to modify the contents of the config file:
+ </p>
+ <img class='bordered' src="doc-files/config3.png">
+ <p>
+ This latter page uses the {@link org.apache.juneau.ini.ConfigFile#toString()} method to retrieve the
+ contents of the config file in INI format.
+ </p>
+ <p>
+ Since config files are serializable, that mean they can also be retrieved through the <code>RestClient</code> API.
+ </p>
+ <p class='bcode'>
+ <jc>// Create a new REST client with JSON support</jc>
+ RestClient c = <jk>new</jk> RestClient(JsonSerializer.<jk>class</jk>, JsonParser.<jk>class</jk>);
+
+ <jc>// Retrieve config file through REST interface</jc>
+ ConfigFile cf = c.doGet(<js>"http://localhost:10000/sample/config"</js>).getResponse(ConfigFileImpl.<jk>class</jk>);
+ </p>
+</div>
+
+<!-- ======================================================================================================== -->
+<a id="Merging"></a>
+<h2 class='topic' onclick='toggle(this)'>7 - Merging Config Files</h2>
+<div class='topic'>
+ <p>
+ In the previous example, an edit page was shown that allows you to edit config files through
+ a REST interface.<br>
+ Note that if only a single entry is modified in the config file, we only want to trigger
+ listeners for that change, not trigger all listeners.<br>
+ This is where the {@link org.apache.juneau.ini.ConfigFile#merge(ConfigFile)} method comes into play.<br>
+ This method will copy the contents of one config file over to another config file, but only
+ trigger listeners when the values are different.
+ </p>
+ <p>
+ The edit page is implemented with this method which is a simple PUT with the contents of
+ the new INI file as the body of the HTTP request:
+ </p>
+ <p class='bcode'>
+ <jd>/**
+ * [PUT /] - Sets contents of config file.
+ *
+ * <ja>@param</ja> contents The new contents of the config file.
+ * <ja>@return</ja> The new config file contents.
+ * <ja>@throws</ja> Exception
+ */</jd>
+ <ja>@RestMethod</ja>(name=<js>"PUT"</js>, path=<js>"/"</js>,
+ description=<js>"Sets contents of config file."</js>,
+ input={
+ <ja>@Var</ja>(category=<jsf>CONTENT</jsf>, description=<js>"New contents in INI file format."</js>)
+ }
+ )
+ <jk>public</jk> ConfigFile setConfigContents(<ja>@Content</ja> Reader contents) <jk>throws</jk> Exception {
+
+ <jc>// Create a new in-memory config file based on the contents of the HTTP request.</jc>
+ ConfigFile cf2 = ConfigMgr.<jsf>DEFAULT</jsf>.create().load(contents);
+
+ <jc>// Merge the in-memory config file into the existing config file and save it.
+ // Then return the modified config file to be parsed as a POJO.</jc>
+ <jk>return</jk> getConfig().merge(cf2).save();
+ }
+ </p>
+</div>
+
+</body>
+</html>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/internal/ArrayUtils.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/internal/ArrayUtils.java b/juneau-core/src/main/java/org/apache/juneau/internal/ArrayUtils.java
new file mode 100644
index 0000000..8a1b931
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/internal/ArrayUtils.java
@@ -0,0 +1,278 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.internal;
+
+import static org.apache.juneau.internal.ThrowableUtils.*;
+
+import java.lang.reflect.*;
+import java.util.*;
+
+/**
+ * Quick and dirty utilities for working with arrays.
+ *
+ * @author James Bognar (james.bognar@salesforce.com)
+ */
+public final class ArrayUtils {
+
+ /**
+ * Appends one or more elements to an array.
+ *
+ * @param <T> The element type.
+ * @param array The array to append to.
+ * @param newElements The new elements to append to the array.
+ * @return A new array with the specified elements appended.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T[] append(T[] array, T...newElements) {
+ if (array == null)
+ return newElements;
+ if (newElements.length == 0)
+ return array;
+ T[] a = (T[])Array.newInstance(array.getClass().getComponentType(), array.length + newElements.length);
+ for (int i = 0; i < array.length; i++)
+ a[i] = array[i];
+ for (int i = 0; i < newElements.length; i++)
+ a[i+array.length] = newElements[i];
+ return a;
+ }
+
+ /**
+ * Appends one or more elements to an array.
+ *
+ * @param <T> The element type.
+ * @param array The array to append to.
+ * @param newElements The new elements to append to the array.
+ * @return A new array with the specified elements appended.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T[] append(T[] array, Collection<T> newElements) {
+ assertFieldNotNull(array, "array");
+ if (newElements.size() == 0)
+ return array;
+ T[] a = (T[])Array.newInstance(array.getClass().getComponentType(), array.length + newElements.size());
+ for (int i = 0; i < array.length; i++)
+ a[i] = array[i];
+ int l = array.length;
+ for (T t : newElements)
+ a[l++] = t;
+ return a;
+ }
+
+ /**
+ * Combine an arbitrary number of arrays into a single array.
+ *
+ * @param arrays Collection of arrays to combine.
+ * @return A new combined array, or <jk>null</jk> if all arrays are <jk>null</jk>.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T[] combine(T[]...arrays) {
+ assertFieldNotNull(arrays, "arrays");
+ int l = 0;
+ T[] a1 = null;
+ for (T[] a : arrays) {
+ if (a1 == null && a != null)
+ a1 = a;
+ l += (a == null ? 0 : a.length);
+ }
+ if (a1 == null)
+ return null;
+ T[] a = (T[])Array.newInstance(a1.getClass().getComponentType(), l);
+ int i = 0;
+ for (T[] aa : arrays)
+ if (aa != null)
+ for (T t : aa)
+ a[i++] = t;
+ return a;
+ }
+
+ /**
+ * Creates a new array with reversed entries.
+ *
+ * @param <T> The class type of the array.
+ * @param array The array to reverse.
+ * @return A new array with reversed entries, or <jk>null</jk> if the array was <jk>null</jk>.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T[] reverse(T[] array) {
+ assertFieldNotNull(array, "array");
+ Class<T> c = (Class<T>)array.getClass().getComponentType();
+ T[] a2 = (T[])Array.newInstance(c, array.length);
+ for (int i = 0; i < array.length; i++)
+ a2[a2.length-i-1] = array[i];
+ return a2;
+ }
+
+ /**
+ * Converts the specified array to a <code>Set</code>.
+ * <p>
+ * The order of the entries in the set are the same as the array.
+ *
+ * @param <T> The entry type of the array.
+ * @param array The array being wrapped in a <code>Set</code> interface.
+ * @return The new set.
+ */
+ public static <T> Set<T> asSet(final T[] array) {
+ assertFieldNotNull(array, "array");
+ return new AbstractSet<T>() {
+
+ @Override /* Set */
+ public Iterator<T> iterator() {
+ return new Iterator<T>() {
+ int i = 0;
+
+ @Override /* Iterator */
+ public boolean hasNext() {
+ return i < array.length;
+ }
+
+ @Override /* Iterator */
+ public T next() {
+ if (i >= array.length)
+ throw new NoSuchElementException();
+ T t = array[i];
+ i++;
+ return t;
+ }
+
+ @Override /* Iterator */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ @Override /* Set */
+ public int size() {
+ return array.length;
+ }
+ };
+ }
+
+ /**
+ * Returns an iterator against an array.
+ * This works with any array type (e.g. <code>String[]</code>, <code>Object[]</code>, <code><jk>int</jk>[]</code>, etc...).
+ *
+ * @param array The array to create an iterator over.
+ * @return An iterator over the specified array.
+ */
+ public static Iterator<Object> iterator(final Object array) {
+ return new Iterator<Object>() {
+ int i = 0;
+ int length = array == null ? 0 : Array.getLength(array);
+
+ @Override /* Iterator */
+ public boolean hasNext() {
+ return i < length;
+ }
+
+ @Override /* Iterator */
+ public Object next() {
+ if (i >= length)
+ throw new NoSuchElementException();
+ return Array.get(array, i++);
+ }
+
+ @Override /* Iterator */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ /**
+ * Converts the specified collection to an array.
+ * Works on both object and primitive arrays.
+ *
+ * @param c The collection to convert to an array.
+ * @param componentType The component type of the collection.
+ * @return A new array.
+ */
+ public static <T> Object toArray(Collection<T> c, Class<T> componentType) {
+ Object a = Array.newInstance(componentType, c.size());
+ Iterator<T> it = c.iterator();
+ int i = 0;
+ while (it.hasNext())
+ Array.set(a, i++, it.next());
+ return a;
+ }
+
+ /**
+ * Copies the specified array into the specified list.
+ * Works on both object and primitive arrays.
+ *
+ * @param array The array to copy into a list.
+ * @param list The list to copy the values into.
+ */
+ @SuppressWarnings({"unchecked","rawtypes"})
+ public static void copyToList(Object array, List list) {
+ if (array != null) {
+ int length = Array.getLength(array);
+ for (int i = 0; i < length; i++)
+ list.add(Array.get(array, i));
+ }
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified array contains the specified element
+ * using the {@link Object#equals(Object)} method.
+ *
+ * @param element The element to check for.
+ * @param array The array to check.
+ * @return <jk>true</jk> if the specified array contains the specified element,
+ * <jk>false</jk> if the array or element is <jk>null</jk>.
+ */
+ public static <T> boolean contains(T element, T[] array) {
+ return indexOf(element, array) != -1;
+ }
+
+ /**
+ * Returns the index position of the element in the specified array
+ * using the {@link Object#equals(Object)} method.
+ *
+ * @param element The element to check for.
+ * @param array The array to check.
+ * @return The index position of the element in the specified array, or
+ * <code>-1</code> if the array doesn't contain the element, or the array or element is <jk>null</jk>.
+ */
+ public static <T> int indexOf(T element, T[] array) {
+ if (element == null)
+ return -1;
+ if (array == null)
+ return -1;
+ for (int i = 0; i < array.length; i++)
+ if (element.equals(array[i]))
+ return i;
+ return -1;
+ }
+
+ /**
+ * Converts a primitive wrapper array (e.g. <code>Integer[]</code>) to a primitive array (e.g. <code><jk>int</jk>[]</code>).
+ *
+ * @param o The array to convert. Must be a primitive wrapper array.
+ * @return A new array.
+ * @throws IllegalArgumentException If object is not a wrapper object array.
+ */
+ public static Object toPrimitiveArray(Object o) {
+ Class<?> c = o.getClass();
+ if (! c.isArray())
+ throw new IllegalArgumentException("Cannot pass non-array objects to toPrimitiveArray()");
+ int l = Array.getLength(o);
+ Class<?> tc = ClassUtils.getPrimitiveForWrapper(c.getComponentType());
+ if (tc == null)
+ throw new IllegalArgumentException("Array type is not a primitive wrapper array.");
+ Object a = Array.newInstance(tc, l);
+ for (int i = 0; i < l; i++)
+ Array.set(a, i, Array.get(o, i));
+ return a;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/internal/AsciiSet.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/internal/AsciiSet.java b/juneau-core/src/main/java/org/apache/juneau/internal/AsciiSet.java
new file mode 100644
index 0000000..4ccb5fe
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/internal/AsciiSet.java
@@ -0,0 +1,59 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.internal;
+
+/**
+ * Stores a set of ASCII characters for quick lookup.
+ *
+ * @author James Bognar (james.bognar@salesforce.com)
+ */
+public final class AsciiSet {
+ final boolean[] store = new boolean[128];
+
+ /**
+ * Constructor.
+ *
+ * @param chars The characters to keep in this store.
+ */
+ public AsciiSet(String chars) {
+ for (int i = 0; i < chars.length(); i++) {
+ char c = chars.charAt(i);
+ if (c < 128)
+ store[c] = true;
+ }
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified character is in this store.
+ *
+ * @param c The character to check.
+ * @return <jk>true</jk> if the specified character is in this store.
+ */
+ public boolean contains(char c) {
+ if (c > 127)
+ return false;
+ return store[c];
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified character is in this store.
+ *
+ * @param c The character to check.
+ * @return <jk>true</jk> if the specified character is in this store.
+ */
+ public boolean contains(int c) {
+ if (c < 0 || c > 127)
+ return false;
+ return store[c];
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayCache.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayCache.java b/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayCache.java
new file mode 100644
index 0000000..9747902
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayCache.java
@@ -0,0 +1,106 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.internal;
+
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+/**
+ * A utility class for caching byte arrays in memory so that duplicate arrays can be reused.
+ * <p>
+ *
+ * @author James Bognar (james.bognar@salesforce.com)
+ */
+public class ByteArrayCache {
+
+ /**
+ * Default global byte array cache.
+ * Note that this can't ever get garbage collected so don't add really large arrays!
+ */
+ public static final ByteArrayCache DEFAULT = new ByteArrayCache();
+
+ private final ConcurrentHashMap<ByteArray,byte[]> cache = new ConcurrentHashMap<ByteArray,byte[]>();
+
+ /**
+ * Add the specified byte array to this cache.
+ *
+ * @param contents The byte array to add to this cache.
+ * @return Either the same byte array or a previously cached byte array depending on whether the byte array
+ * already exists in the cache.
+ */
+ public byte[] cache(byte[] contents) {
+ if (contents == null)
+ return null;
+ ByteArray ba = new ByteArray(contents);
+ cache.putIfAbsent(ba, ba.contents);
+ return cache.get(ba);
+ }
+
+ /**
+ * Add the specified input stream to this cache.
+ *
+ * @param contents The input stream whose contents are to be added to this cache.
+ * @return Either the same byte array or a previously cached byte array depending on whether the byte array
+ * already exists in the cache.
+ * @throws IOException
+ */
+ public byte[] cache(InputStream contents) throws IOException {
+ if (contents == null)
+ return null;
+ ByteArray ba = new ByteArray(IOUtils.readBytes(contents, 1024));
+ cache.putIfAbsent(ba, ba.contents);
+ return cache.get(ba);
+ }
+
+ /**
+ * Returns the number of byte arrays in this cache.
+ *
+ * @return The number of byte arrays in this cache.
+ */
+ public int size() {
+ return cache.size();
+ }
+
+ private static class ByteArray {
+ private int hashCode;
+ private byte[] contents;
+
+ private ByteArray(byte[] contents) {
+ this.contents = contents;
+ int multiplier = 1;
+ for (int i = 0; i < contents.length; i++) {
+ hashCode += contents[i] * multiplier;
+ int shifted = multiplier << 5;
+ multiplier = shifted - multiplier;
+ }
+ }
+
+ @Override /* Object */
+ public int hashCode() {
+ if (hashCode == 0) {
+ }
+ return hashCode;
+ }
+
+ @Override /* Object */
+ public boolean equals(Object o) {
+ if (o instanceof ByteArray) {
+ ByteArray ba = (ByteArray)o;
+ if (ba.hashCode == hashCode)
+ return Arrays.equals(ba.contents, contents);
+ }
+ return false;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayInOutStream.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayInOutStream.java b/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayInOutStream.java
new file mode 100644
index 0000000..d104c77
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/internal/ByteArrayInOutStream.java
@@ -0,0 +1,32 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.internal;
+
+import java.io.*;
+
+/**
+ * Subclass of a ByteArrayOutputStream that avoids a byte array copy when reading from an input stream.
+ * <p>
+ * @author James Bognar (james.bognar@salesforce.com)
+ */
+public class ByteArrayInOutStream extends ByteArrayOutputStream {
+
+ /**
+ * Creates a new input stream from this object.
+ *
+ * @return A new input stream from this object.
+ */
+ public ByteArrayInputStream getInputStream() {
+ return new ByteArrayInputStream(this.buf, 0, this.count);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e6bf97a8/juneau-core/src/main/java/org/apache/juneau/internal/CharSequenceReader.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/internal/CharSequenceReader.java b/juneau-core/src/main/java/org/apache/juneau/internal/CharSequenceReader.java
new file mode 100644
index 0000000..fe89635
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/internal/CharSequenceReader.java
@@ -0,0 +1,100 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.internal;
+
+import java.io.*;
+
+/**
+ * Similar to {@link StringReader} except reads from a generic {@link CharSequenceReader}.
+ *
+ * @author jbognar
+ */
+public final class CharSequenceReader extends BufferedReader {
+
+ private final CharSequence cs;
+ private String s;
+ private StringBuffer sb;
+ private StringBuilder sb2;
+ private int length;
+ private int next = 0;
+
+ /**
+ * Constructor.
+ *
+ * @param cs The char sequence to read from. Can be <jk>null</jk>.
+ */
+ public CharSequenceReader(CharSequence cs) {
+ super(new StringReader(""), 1); // Does not actually use a reader.
+ if (cs == null)
+ cs = "";
+ this.cs = cs;
+ if (cs instanceof String)
+ s = (String)cs;
+ else if (cs instanceof StringBuffer)
+ sb = (StringBuffer)cs;
+ else if (cs instanceof StringBuilder)
+ sb2 = (StringBuilder)cs;
+ this.length = cs.length();
+ }
+
+ @Override /* Reader */
+ public int read() {
+ if (next >= length)
+ return -1;
+ return cs.charAt(next++);
+ }
+
+ @Override /* Reader */
+ public boolean markSupported() {
+ return false;
+ }
+
+ @Override /* Reader */
+ public int read(final char[] cbuf, final int off, final int len) {
+ if (next >= length)
+ return -1;
+ int n = Math.min(length - next, len);
+ if (s != null)
+ s.getChars(next, next + n, cbuf, off);
+ else if (sb != null)
+ sb.getChars(next, next + n, cbuf, off);
+ else if (sb2 != null)
+ sb2.getChars(next, next + n, cbuf, off);
+ else {
+ for (int i = 0; i < n; i++)
+ cbuf[off+i] = cs.charAt(next+i);
+ }
+ next += n;
+ return n;
+ }
+
+ @Override /* Reader */
+ public long skip(long ns) {
+ if (next >= length)
+ return 0;
+ long n = Math.min(length - next, ns);
+ n = Math.max(-next, n);
+ next += n;
+ return n;
+ }
+
+ @Override /* Reader */
+ public void close() {
+ // no-op
+ }
+
+ @Override /* Object */
+ public String toString() {
+ return cs.toString();
+ }
+}
\ No newline at end of file