View Javadoc
1   package fr.ifremer.quadrige3.batch;
2   
3   /*-
4    * #%L
5    * Quadrige3 Batch :: Shape import/export
6    * %%
7    * Copyright (C) 2017 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
25  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
26  import fr.ifremer.quadrige3.core.service.ServiceLocator;
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.io.IOUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.nuiton.config.ApplicationConfig;
32  import org.nuiton.i18n.I18n;
33  import org.nuiton.i18n.init.DefaultI18nInitializer;
34  import org.nuiton.i18n.init.UserI18nInitializer;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.Arrays;
39  import java.util.Locale;
40  
41  /**
42   * <p>
43   * QuadrigeCoreMain class.
44   * </p>
45   * 
46   */
47  public class BatchesServerMain {
48  
49  	/* Logger */
50  	private static final Log log = LogFactory.getLog(BatchesServerMain.class);
51  
52  	/**
53  	 * <p>
54  	 * main.
55  	 * </p>
56  	 *
57  	 * @param args
58  	 *            an array of {@link String} objects.
59  	 */
60  	public static void main(String[] args) {
61  		if (log.isInfoEnabled()) {
62  			log.info("Starting Quadrige3 :: Batch for Server with arguments: " + Arrays.toString(args));
63  		}
64  
65  		// By default, display help
66  		if (args == null || args.length == 0) {
67  			args = new String[] { "-h" };
68  		}
69  
70  		// Could override config file name (useful for dev)
71  		String configFile = "quadrige3-core.config";
72  		if (System.getProperty(configFile) != null) {
73  			configFile = System.getProperty(configFile);
74  			configFile = configFile.replaceAll("\\\\", "/");
75  		}
76  
77  		// Create configuration
78  		QuadrigeConfiguration config = new QuadrigeConfiguration(configFile, args) {
79  			protected void addAlias(ApplicationConfig applicationConfig) {
80  				super.addAlias(applicationConfig);
81  				// Add custom alias
82  				// ...
83  			}
84  		};
85  		QuadrigeConfiguration.setInstance(config);
86  
87  		// Init i18n
88  		try {
89  			initI18n(config);
90  		} catch (IOException e) {
91  			throw new QuadrigeTechnicalException("i18n initialization failed", e);
92  		}
93  
94  		try {
95  			config.getApplicationConfig().doAllAction();
96  		} catch (Exception e) {
97  			log.error("Error in action", e);
98  		}
99  
100 		IOUtils.closeQuietly(ServiceLocator.instance());
101 	}
102 
103 	/**
104 	 * <p>
105 	 * initI18n.
106 	 * </p>
107 	 *
108 	 * @param config
109 	 *            a {@link QuadrigeConfiguration} object.
110 	 * @throws IOException
111 	 *             if any.
112 	 */
113 	protected static void initI18n(QuadrigeConfiguration config) throws IOException {
114 
115 		// --------------------------------------------------------------------//
116 		// init i18n
117 		// --------------------------------------------------------------------//
118 		File i18nDirectory = new File(config.getDataDirectory(), "i18n");
119 		if (i18nDirectory.exists()) {
120 			// clean i18n cache
121 			FileUtils.cleanDirectory(i18nDirectory);
122 		}
123 
124 		FileUtils.forceMkdir(i18nDirectory);
125 
126 		if (log.isDebugEnabled()) {
127 			log.debug("I18N directory: " + i18nDirectory);
128 		}
129 
130 		Locale i18nLocale = config.getI18nLocale();
131 
132 		if (log.isInfoEnabled()) {
133 			log.info(String.format("Starts i18n with locale [%s] at [%s]",
134 					i18nLocale, i18nDirectory));
135 		}
136 		I18n.init(new UserI18nInitializer(
137 				i18nDirectory, new DefaultI18nInitializer(getI18nBundleName())),
138 				i18nLocale);
139 	}
140 
141 	/**
142 	 * <p>
143 	 * getI18nBundleName.
144 	 * </p>
145 	 *
146 	 * @return a {@link String} object.
147 	 */
148 	protected static String getI18nBundleName() {
149 		return "quadrige3-batches-server-i18n";
150 	}
151 }