8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: MergeProperties.java,v 1.6 2008/06/25 05:44:03 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.setup;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.File;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.FileWriter;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Enumeration;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.ResourceBundle;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Merge two properties file into one.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class MergeProperties {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private MergeProperties() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private MergeProperties(String origProp, String prependProp, String outfile)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws IOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuffer buff = new StringBuffer();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map p1 = getProperties(origProp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map p2 = getProperties(prependProp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set p1Keys = (Set)p1.keySet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set p2Keys = (Set)p2.keySet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator i = p1Keys.iterator(); i.hasNext(); ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String)i.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String val = ((String)p1.get(key)).trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (p2Keys.contains(key)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (val.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster val = ((String)p2.get(key)).trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster val += " " + ((String)p2.get(key)).trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buff.append(key)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append("=")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(val)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append("\n");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator i = p2Keys.iterator(); i.hasNext(); ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String)i.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String val = (String)p2.get(key);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!p1Keys.contains(key)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buff.append(key)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append("=")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(val)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append("\n");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writeToFile(outfile, buff);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map getProperties(String propertyName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map results = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle res = ResourceBundle.getBundle(propertyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Enumeration e = res.getKeys(); e.hasMoreElements(); ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String)e.nextElement();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster results.put(key, res.getString(key));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return results;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void writeToFile(String filename, StringBuffer content)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws IOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int idx = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (idx != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster idx = content.indexOf("\\", idx);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (idx != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster content.insert(idx, '\\');
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster idx += 2;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filename != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File fileHandle = new File(filename);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FileWriter out = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out = new FileWriter(filename);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.write(content.toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (out != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.close();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Merges two properties files into one. The first argument is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the original properties file; the second is the one to prepend;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and the third is the output filename.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <p>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * E.g.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>key1=x (from original properties file)</code> and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>key1=y (from the other properties file)</code> will result in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>key1=x y</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void main(String[] args) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new MergeProperties(args[0], args[1], args[2]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster e.printStackTrace();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}