001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    
019    import java.util.Map;
020    
021    /**
022     * @author Shuyang Zhou
023     * @author Raymond Aug??
024     * @author Gregory Amerson
025     */
026    public class ArgumentsUtil {
027    
028            public static Map<String, String> parseArguments(String[] args) {
029                    Map<String, String> arguments = new ArgumentsMap();
030    
031                    for (String arg : args) {
032                            int pos = arg.indexOf('=');
033    
034                            if (pos <= 0) {
035                                    throw new IllegalArgumentException("Bad argument " + arg);
036                            }
037    
038                            String key = arg.substring(0, pos).trim();
039                            String value = arg.substring(pos + 1).trim();
040    
041                            if (key.startsWith("-D")) {
042                                    key = key.substring(2);
043    
044                                    System.setProperty(key, value);
045                            }
046                            else {
047                                    arguments.put(key, value);
048                            }
049                    }
050    
051                    return arguments;
052            }
053    
054            public static void processMainException(
055                            Map<String, String> arguments, Exception e)
056                    throws Exception {
057    
058                    String throwMainException = arguments.get("tools.throw.main.exception");
059    
060                    if (GetterUtil.getBoolean(throwMainException, true)) {
061                            throw e;
062                    }
063    
064                    e.printStackTrace();
065            }
066    
067    }