http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigFileWritable.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigFileWritable.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigFileWritable.java new file mode 100755 index 0000000..503f27b --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigFileWritable.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.ini; + +import java.io.*; + +import com.ibm.juno.core.*; + +/** + * Wraps a {@link ConfigFile} in a {@link Writable} to be rendered as plain text. + */ +class ConfigFileWritable implements Writable { + + private ConfigFileImpl cf; + + protected ConfigFileWritable(ConfigFileImpl cf) { + this.cf = cf; + } + + @Override /* Writable */ + public void writeTo(Writer out) throws IOException { + cf.readLock(); + try { + cf.serializeTo(out); + } finally { + cf.readUnlock(); + } + } + + @Override /* Writable */ + public String getMediaType() { + return "text/plain"; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.class new file mode 100755 index 0000000..d074cbd Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.java new file mode 100755 index 0000000..70a95d7 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigMgr.java @@ -0,0 +1,312 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.ini; + +import static com.ibm.juno.core.ini.ConfigFileFormat.*; + +import java.io.*; +import java.nio.charset.*; +import java.util.*; +import java.util.concurrent.*; + +import com.ibm.juno.core.json.*; +import com.ibm.juno.core.parser.*; +import com.ibm.juno.core.serializer.*; +import com.ibm.juno.core.utils.*; + +/** + * Manager for retrieving shared instances of {@link ConfigFile ConfigFiles}. + * <p> + * Example: + * <p class='bcode'> + * ConfigFile cf = ConfigMgr.<jsf>DEFAULT</jsf>.get(<js>"MyConfig.cfg"</js>); + * String setting = cf.get(<js>"MySection/mysetting"</js>); + * </p> + */ +public class ConfigMgr { + + /** + * Default reusable configuration manager. + * <ul> + * <li>Read-only: <jk>false</jk>. + * <li>Encoder: {@link XorEncoder}. + * <li>Serializer: {@link JsonSerializer#DEFAULT}. + * <li>Parser: {@link JsonParser#DEFAULT}. + * <li>Charset: {@link Charset#defaultCharset()}. + * <li>Search paths: [<js>"."</js>]. + * </ul> + */ + public static final ConfigMgr DEFAULT = new ConfigMgr(false, new XorEncoder(), JsonSerializer.DEFAULT, JsonParser.DEFAULT, Charset.defaultCharset(), new String[]{"."}); + + private ConcurrentHashMap<String,File> files = new ConcurrentHashMap<String,File>(); + private ConcurrentHashMap<File,ConfigFile> configs = new ConcurrentHashMap<File,ConfigFile>(); + private final WriterSerializer serializer; + private final ReaderParser parser; + private final Encoder encoder; + private final boolean readOnly; + private final Charset charset; + private final List<File> searchPaths = new LinkedList<File>(); + + /** + * Create a custom configuration manager. + * + * @param readOnly Make {@link ConfigFile ConfigFiles} read-only. + * @param encoder Optional. Specify the encoder to use for encoded config file entries (e.g. <js>"mySecret*={...}"</js>). + * @param serializer Optional. Specify the serializer to use for serializing POJOs when using {@link ConfigFile#put(String, Object)}. + * @param parser Optional. Specify the parser to use for parsing POJOs when using {@link ConfigFile#getObject(Class,String)}. + * @param charset Optional. Specify the config file character encoding. If <jk>null</jk>, uses {@link Charset#defaultCharset()}. + * @param searchPaths Specify the search paths for config files. Can contain relative or absolute paths. + */ + public ConfigMgr(boolean readOnly, Encoder encoder, WriterSerializer serializer, ReaderParser parser, Charset charset, String[] searchPaths) { + this.readOnly = readOnly; + this.encoder = encoder; + this.serializer = serializer; + this.parser = parser; + this.charset = charset; + if (searchPaths != null) + for (String p : searchPaths) + this.searchPaths.add(new File(p)); + } + + /** + * Returns the config file with the specified absolute or relative path. + * <p> + * Multiple calls to the same path return the same <code>ConfigFile</code> instance. + * + * @param path The absolute or relative path of the config file. + * @return The config file. + * @throws IOException If config file could not be parsed. + * @throws FileNotFoundException If config file could not be found. + */ + public ConfigFile get(String path) throws IOException { + return get(path, false); + } + + /** + * Returns the config file with the specified absolute or relative path. + * <p> + * Multiple calls to the same path return the same <code>ConfigFile</code> instance. + * <p> + * If file doesn't exist and <code>create</code> is <jk>true</jk>, the configuration file will be + * create in the location identified by the first entry in the search paths. + * + * @param path The absolute or relative path of the config file. + * @param create Create the config file if it doesn't exist. + * @return The config file. + * @throws IOException If config file could not be parsed. + * @throws FileNotFoundException If config file could not be found or could not be created. + */ + public ConfigFile get(String path, boolean create) throws IOException { + + File f = resolve(path, create); + + ConfigFile cf = configs.get(f); + if (cf != null) + return cf; + + cf = new ConfigFileImpl(f, readOnly, encoder, serializer, parser, charset); + configs.putIfAbsent(f, cf); + return configs.get(f); + } + + /** + * Create a new empty config file not backed by any file. + * + * @return A new config file. + * @throws IOException + */ + public ConfigFile create() throws IOException { + return new ConfigFileImpl(null, false, encoder, serializer, parser, charset); + } + + /** + * Create a new config file backed by the specified file. + * Note that {@link #get(String)} is the preferred method for getting access to config files + * since this method will create a new config file each time it is called. + * This method is provided primarily for testing purposes. + * + * @param f The file to create a config file from. + * @return A new config file. + * @throws IOException + */ + public ConfigFile create(File f) throws IOException { + return new ConfigFileImpl(f, false, encoder, serializer, parser, charset); + } + + /** + * Create a new config file not backed by a file. + * + * @param r The reader containing an INI-formatted file to initialize the config file from. + * @return A new config file. + * @throws IOException + */ + public ConfigFile create(Reader r) throws IOException { + return new ConfigFileImpl(null, false, encoder, serializer, parser, charset).load(r); + } + + /** + * Reloads any config files that were modified. + * @throws IOException + */ + public void loadIfModified() throws IOException { + for (ConfigFile cf : configs.values()) + cf.loadIfModified(); + } + + /** + * Delete all configuration files registered with this config manager. + */ + public void deleteAll() { + for (File f : configs.keySet()) + FileUtils.delete(f); + files.clear(); + configs.clear(); + } + + private File resolve(String path, boolean create) throws IOException { + + // See if it's cached. + File f = files.get(path); + if (f != null) + return f; + + // Handle absolute file. + f = new File(path); + if (f.isAbsolute()) { + if (create) + FileUtils.create(f); + if (f.exists()) + return addFile(path, f); + throw new FileNotFoundException("Could not find config file '"+path+"'"); + } + + if (searchPaths.isEmpty()) + throw new FileNotFoundException("No search paths specified on ConfigMgr."); + + // Handle paths relative to search paths. + for (File sf : searchPaths) { + f = new File(sf.getAbsolutePath() + "/" + path); + if (f.exists()) + return addFile(path, f); + } + + if (create) { + f = new File(searchPaths.get(0).getAbsolutePath() + "/" + path); + FileUtils.create(f); + return addFile(path, f); + } + + throw new FileNotFoundException("Could not find config file '"+path+"'"); + } + + private File addFile(String path, File f) { + files.putIfAbsent(path, f); + return files.get(path); + } + + /** + * Implements command-line features for working with INI configuration files. + * <p> + * Invoke as a normal Java program... + * <p> + * <p class='bcode'> + * java com.ibm.juno.core.ini.ConfigMgr [args] + * </p> + * <p> + * Arguments can be any of the following... + * <ul> + * <li>No arguments<br> + * Prints usage message.<br> + * <br> + * <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.<br> + * <br> + * <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.<br> + * <br> + * <li><code>setVals -configFile <configFile> -vals [var1=val1 [var2=val2...]] [-verbose]</code> + * Sets values in config files.<br> + * </ul> + * <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 class='bcode'> + * java com.ibm.juno.core.ini.ConfigMgr createBatchEnvFile -configfile C:\foo\MyConfig.cfg -batchfile C:\foo\MyConfig.bat + * </p> + * + * @param args Command-line arguments + */ + public static void main(String[] args) { + + Args a = new Args(args); + String command = a.getArg(0); + String configFile = a.getArg("configFile"); + String envFile = a.getArg("envFile"); + List<String> vals = a.getArgs("vals"); + + if (command == null || ! (command.equals("createBatchEnvFile") || command.equals("createShellEnvFile") || command.equals("setVals"))) + printUsageAndExit(); + else if (configFile.isEmpty()) + printUsageAndExit(); + else if (command.equals("setVals") && vals.isEmpty()) + printUsageAndExit(); + else if ((command.equals("createBatchEnvFile") || command.equals("createShellEnvFile")) && envFile.isEmpty()) + printUsageAndExit(); + else { + try { + ConfigFile cf = ConfigMgr.DEFAULT.get(configFile); + + if (command.equalsIgnoreCase("setVals")) { + for (String val : vals) { + String[] x = val.split("\\="); + if (x.length != 2) + throw new RuntimeException("Invalid format for value: '"+val+"'. Must be in the format 'key=value'"); + cf.put(x[0], x[1]); + } + cf.save(); + return; + + } else if (command.equalsIgnoreCase("createBatchEnvFile")) { + Writer fw = new OutputStreamWriter(new FileOutputStream(envFile), Charset.defaultCharset()); + try { + cf.serializeTo(fw, BATCH); + } finally { + fw.close(); + } + return; + + } else if (command.equalsIgnoreCase("createShellEnvFile")) { + Writer fw = new OutputStreamWriter(new FileOutputStream(envFile), Charset.defaultCharset()); + try { + cf.serializeTo(fw, SHELL); + } finally { + fw.close(); + } + return; + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private static void printUsageAndExit() { + System.err.println("---Usage---"); + System.err.println("java -cp juno.jar com.ibm.juno.core.ini.ConfigFile createBatchEnvFile -configFile <configFile> -envFile <envFile> [-verbose]"); + System.err.println("java -cp juno.jar com.ibm.juno.core.ini.ConfigFile createShellEnvFile -configFile <configFile> -envFile <envFile> [-verbose]"); + System.err.println("java -cp juno.jar com.ibm.juno.core.ini.ConfigFile setVals -configFile <configFile> -vals [var1 val1 [var2 val2...]] [-verbose]"); + int rc = Integer.getInteger("exit.2", 2); + if (rc != 0) + System.exit(rc); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.class new file mode 100755 index 0000000..94ff3b4 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.java new file mode 100755 index 0000000..68e6e53 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/ConfigUtils.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.ini; + +/** + * Internal utility methods. + * + * @author James Bognar ([email protected]) + */ +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; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.class new file mode 100755 index 0000000..203a1a0 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.java new file mode 100755 index 0000000..5785eef --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Encoder.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.ini; + +/** + * API for defining a string encoding/decoding mechanism for entries in {@link ConfigFile}. + * + * @author James Bognar ([email protected]) + */ +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/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.class new file mode 100755 index 0000000..9b88930 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.java new file mode 100755 index 0000000..f49de62 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/EntryListener.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.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/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1$1.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1$1.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1$1.class new file mode 100755 index 0000000..a34e642 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1$1.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1.class new file mode 100755 index 0000000..5bf46e2 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$1.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2$1.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2$1.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2$1.class new file mode 100755 index 0000000..c411238 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2$1.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2.class new file mode 100755 index 0000000..b6d0ecc Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section$2.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.class new file mode 100755 index 0000000..e1ce001 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.java new file mode 100755 index 0000000..7626602 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/Section.java @@ -0,0 +1,577 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.ini; + +import static com.ibm.juno.core.ini.ConfigFileFormat.*; +import static com.ibm.juno.core.ini.ConfigUtils.*; + +import java.io.*; +import java.util.*; +import java.util.concurrent.locks.*; + +import com.ibm.juno.core.annotation.*; +import com.ibm.juno.core.utils.*; + +/** + * 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); + if (s != null && s.indexOf('\u0000') != -1) + return s.replace("\u0000", ""); + 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) { + String NL = System.getProperty("line.separator"); + 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); + val = val.replace("\u0000", "\\"+NL+"\t"); + 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 + //-------------------------------------------------------------------------------- + + private String pendingMultiline; + + /* + * Add lines to this section. + */ + Section addLines(Set<String> changes, String...l) { + writeLock(); + try { + if (l == null) + l = new String[0]; + for (String line : l) { + if (line == null) + line = ""; + if (line.matches("\\s*\\#.*")) + this.lines.add(line); + else if (line.matches("\\s*\\S+\\s*\\=.*")) { + // Key/value pairs are stored as either ">key" or "*key"; + String key = line.substring(0, line.indexOf('=')).trim(); + String val = line.substring(line.indexOf('=')+1).trim(); + boolean encoded = key.length() > 1 && key.endsWith("*"); + pendingMultiline = val.endsWith("\\") ? key : null; + if (pendingMultiline != null) + val = val.replaceAll("\\\\$", "\u0000"); + 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 if (pendingMultiline != null) { + line = line.trim(); + String key = pendingMultiline; + String val = entries.get(key); + if (line.endsWith("\\")) { + pendingMultiline = key; + line = line.replaceAll("\\\\$", "\u0000"); + } else { + pendingMultiline = null; + } + val += line; + 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/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.class new file mode 100755 index 0000000..65a4787 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.java new file mode 100755 index 0000000..88f24da --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/SectionListener.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.ini; + +import java.util.*; + +import com.ibm.juno.core.utils.*; + + +/** + * 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/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.class new file mode 100755 index 0000000..fcc1ac5 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.java new file mode 100755 index 0000000..5157d20 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/XorEncoder.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.ini; + +import com.ibm.juno.core.utils.*; + +/** + * 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 ([email protected]) + */ +public final class XorEncoder implements Encoder { + + /** Reusable XOR-Encoder instance. */ + public static final XorEncoder INSTANCE = new XorEncoder(); + + private static final String key = System.getProperty("com.ibm.juno.core.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/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config1.png ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config1.png b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config1.png new file mode 100755 index 0000000..531f280 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config1.png differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config2.png ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config2.png b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config2.png new file mode 100755 index 0000000..7f5a4b3 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config2.png differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config3.png ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config3.png b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config3.png new file mode 100755 index 0000000..749da14 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/doc-files/config3.png differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/package.html ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/package.html b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/package.html new file mode 100755 index 0000000..cd94ced --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/ini/package.html @@ -0,0 +1,643 @@ +<!DOCTYPE HTML> +<!-- + Licensed Materials - Property of IBM + (c) Copyright IBM Corporation 2014. All Rights Reserved. + + Note to U.S. Government Users Restricted Rights: + Use, duplication or disclosure restricted by GSA ADP Schedule + Contract with IBM Corp. + --> +<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 com.ibm.juno.core.ini.ConfigMgr} and {@link com.ibm.juno.core.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 com.ibm.juno.core.ini.ConfigMgr} class is used to instantiate instances of + {@link com.ibm.juno.core.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 com.ibm.juno.core.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 com.ibm.juno.core.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 com.ibm.juno.core.utils.StringVarResolver} API.<br> + </p> + <p> + Resolving config files can be retrieved through the following methods: + <ul> + <li>{@link com.ibm.juno.core.ini.ConfigFile#getResolving()} - Returns a config file that resolves a default set of variables. + <li>{@link com.ibm.juno.core.ini.ConfigFile#getResolving(StringVarResolver)} - Returns a config file that resolves a custom set of variables. + </ul> + </p> + <p> + The default {@link com.ibm.juno.core.ini.ConfigFile#getResolving()} method returns a config file that resolves the following + variables: + </p> + <ul> + <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 com.ibm.juno.core.utils.StringVarResolver StringVarResolvers}.<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 com.ibm.juno.core.ini.XorEncoder} which is a simple XOR+Base64 encoder.<br> + If desired, custom encoder can be used by implementing the {@link com.ibm.juno.core.ini.Encoder} + interface and creating your own <code>ConfigMgr</code> using the {@link com.ibm.juno.core.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 com.ibm.juno.core.ini.ConfigFile#addListener(ConfigFileListener)}. + </p> + <p> + Subclasses are provided for listening for different kinds of events: + </p> + <ul> + <li>{@link com.ibm.juno.core.ini.ConfigFileListener} - Config file is saved, loaded, or modified. + <li>{@link com.ibm.juno.core.ini.SectionListener} - One or more entries in a section are modified. + <li>{@link com.ibm.juno.core.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 com.ibm.juno.core.ini.ConfigFile} instances for the same physical files are shared in {@link com.ibm.juno.core.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 com.ibm.juno.core.ini.ConfigMgr} class contains a {@link com.ibm.juno.core.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 juno.jar com.ibm.juno.core.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 com.ibm.juno.core.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 com.ibm.juno.core.ini.ConfigFile} are POJOs that can be serialized to and parsed from + all supported Juno languages. + </p> + <p> + The <code>com.ibm.juno.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.juno.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>juno-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/juno.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>{ com.ibm.juno:'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 com.ibm.juno.core.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 com.ibm.juno.core.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/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.class new file mode 100755 index 0000000..0e88fb4 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.java new file mode 100755 index 0000000..94f0480 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/Constants.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2014, 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import com.ibm.juno.core.serializer.*; + +/** + * Constants used by the {@link RdfSerializer} and {@link RdfParser} classes. + * + * @author James Bognar ([email protected]) + */ +public final class Constants { + + //-------------------------------------------------------------------------------- + // Built-in Jena languages. + //-------------------------------------------------------------------------------- + + /** Jena language support: <js>"RDF/XML"</js>.*/ + public static final String LANG_RDF_XML = "RDF/XML"; + + /** Jena language support: <js>"RDF/XML-ABBREV"</js>.*/ + public static final String LANG_RDF_XML_ABBREV = "RDF/XML-ABBREV"; + + /** Jena language support: <js>"N-TRIPLE"</js>.*/ + public static final String LANG_NTRIPLE = "N-TRIPLE"; + + /** Jena language support: <js>"TURTLE"</js>.*/ + public static final String LANG_TURTLE = "TURTLE"; + + /** Jena language support: <js>"N3"</js>.*/ + public static final String LANG_N3 = "N3"; + + + //-------------------------------------------------------------------------------- + // Built-in Juno properties. + //-------------------------------------------------------------------------------- + + /** + * RDF property identifier <js>"items"</js>. + * <p> + * For resources that are collections, this property identifies the RDF Sequence + * container for the items in the collection. + */ + public static final String RDF_junoNs_ITEMS = "items"; + + /** + * RDF property identifier <js>"root"</js>. + * <p> + * Property added to root nodes to help identify them as root elements during parsing. + * <p> + * Added if {@link RdfSerializerProperties#RDF_addRootProperty} setting is enabled. + */ + public static final String RDF_junoNs_ROOT = "root"; + + /** + * RDF property identifier <js>"class"</js>. + * <p> + * Property added to bean resources to identify the class type. + * <p> + * Added if {@link SerializerProperties#SERIALIZER_addClassAttrs} setting is enabled. + */ + public static final String RDF_junoNs_CLASS = "class"; + + /** + * RDF property identifier <js>"value"</js>. + * <p> + * Property added to nodes to identify a simple value. + */ + public static final String RDF_junoNs_VALUE = "value"; + + /** + * RDF resource that identifies a <jk>null</jk> value. + */ + public static final String RDF_NIL = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"; + + /** + * RDF resource that identifies a <code>Seq</code> value. + */ + public static final String RDF_SEQ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq"; + + /** + * RDF resource that identifies a <code>Bag</code> value. + */ + public static final String RDF_BAG = "http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag"; +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.class new file mode 100755 index 0000000..a2b5d07 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.java new file mode 100755 index 0000000..9a0cd4a --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfBeanPropertyMeta.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import static com.ibm.juno.core.jena.RdfCollectionFormat.*; + +import java.util.*; + +import com.ibm.juno.core.*; +import com.ibm.juno.core.jena.annotation.*; +import com.ibm.juno.core.xml.*; + +/** + * Metadata on bean properties specific to the RDF serializers and parsers pulled from the {@link Rdf @Rdf} annotation on the bean property. + * + * @author James Bognar ([email protected]) + * @param <T> The bean class. + */ +public class RdfBeanPropertyMeta<T> { + + private RdfCollectionFormat collectionFormat = DEFAULT; + private Namespace namespace = null; + + /** + * Constructor. + * + * @param bpMeta The metadata of the bean property of this additional metadata. + */ + public RdfBeanPropertyMeta(BeanPropertyMeta<T> bpMeta) { + + List<Rdf> rdfs = bpMeta.findAnnotations(Rdf.class); + List<RdfSchema> schemas = bpMeta.findAnnotations(RdfSchema.class); + + for (Rdf rdf : rdfs) + if (collectionFormat == DEFAULT) + collectionFormat = rdf.collectionFormat(); + + namespace = RdfUtils.findNamespace(rdfs, schemas); + } + + /** + * Returns the RDF collection format of this property from the {@link Rdf#collectionFormat} annotation on this bean property. + * + * @return The RDF collection format, or {@link RdfCollectionFormat#DEFAULT} if annotation not specified. + */ + protected RdfCollectionFormat getCollectionFormat() { + return collectionFormat; + } + + /** + * Returns the RDF namespace associated with this bean property. + * <p> + * Namespace is determined in the following order: + * <ol> + * <li>{@link Rdf#prefix()} annotation defined on bean property field. + * <li>{@link Rdf#prefix()} annotation defined on bean getter. + * <li>{@link Rdf#prefix()} annotation defined on bean setter. + * <li>{@link Rdf#prefix()} annotation defined on bean. + * <li>{@link Rdf#prefix()} annotation defined on bean package. + * <li>{@link Rdf#prefix()} annotation defined on bean superclasses. + * <li>{@link Rdf#prefix()} annotation defined on bean superclass packages. + * <li>{@link Rdf#prefix()} annotation defined on bean interfaces. + * <li>{@link Rdf#prefix()} annotation defined on bean interface packages. + * </ol> + * + * @return The namespace associated with this bean property, or <jk>null</jk> if no namespace is + * associated with it. + */ + public Namespace getNamespace() { + return namespace; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.class new file mode 100755 index 0000000..6269aad Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.class differ http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.java b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.java new file mode 100755 index 0000000..d23e2f0 --- /dev/null +++ b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfClassMeta.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Licensed Materials - Property of IBM + * (c) Copyright IBM Corporation 2015. All Rights Reserved. + * + * The source code for this program is not published or otherwise + * divested of its trade secrets, irrespective of what has been + * deposited with the U.S. Copyright Office. + *******************************************************************************/ +package com.ibm.juno.core.jena; + +import java.util.*; + +import com.ibm.juno.core.jena.annotation.*; +import com.ibm.juno.core.utils.*; +import com.ibm.juno.core.xml.*; + +/** + * Metadata on classes specific to the RDF serializers and parsers pulled from the {@link Rdf @Rdf} annotation on the class. + * + * @author James Bognar ([email protected]) + */ +public class RdfClassMeta { + + private final Rdf rdf; + private final RdfCollectionFormat collectionFormat; + private final Namespace namespace; + + /** + * Constructor. + * + * @param c The class that this annotation is defined on. + */ + public RdfClassMeta(Class<?> c) { + this.rdf = ReflectionUtils.getAnnotation(Rdf.class, c); + if (rdf != null) { + collectionFormat = rdf.collectionFormat(); + } else { + collectionFormat = RdfCollectionFormat.DEFAULT; + } + List<Rdf> rdfs = ReflectionUtils.findAnnotations(Rdf.class, c); + List<RdfSchema> schemas = ReflectionUtils.findAnnotations(RdfSchema.class, c); + this.namespace = RdfUtils.findNamespace(rdfs, schemas); + } + + /** + * Returns the {@link Rdf} annotation defined on the class. + * + * @return The value of the {@link Rdf} annotation, or <jk>null</jk> if annotation is not specified. + */ + protected Rdf getAnnotation() { + return rdf; + } + + /** + * Returns the {@link Rdf#collectionFormat()} annotation defined on the class. + * + * @return The value of the {@link Rdf#collectionFormat()} annotation, or <jk>null</jk> if annotation is not specified. + */ + protected RdfCollectionFormat getCollectionFormat() { + return collectionFormat; + } + + /** + * Returns the RDF namespace associated with this class. + * <p> + * Namespace is determined in the following order: + * <ol> + * <li>{@link Rdf#prefix()} annotation defined on class. + * <li>{@link Rdf#prefix()} annotation defined on package. + * <li>{@link Rdf#prefix()} annotation defined on superclasses. + * <li>{@link Rdf#prefix()} annotation defined on superclass packages. + * <li>{@link Rdf#prefix()} annotation defined on interfaces. + * <li>{@link Rdf#prefix()} annotation defined on interface packages. + * </ol> + * + * @return The namespace associated with this class, or <jk>null</jk> if no namespace is + * associated with it. + */ + protected Namespace getNamespace() { + return namespace; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7e4f63e6/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.class ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.class b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.class new file mode 100755 index 0000000..445b907 Binary files /dev/null and b/com.ibm.team.juno.releng/bin/core/com/ibm/juno/core/jena/RdfCollectionFormat.class differ
