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