1 /*
2 Copyright (C) 2000 - 2007 Grid Systems, S.A.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 /*
19 * Project: KernelConfigurator
20 * Created on 25-feb-2004
21 *
22 * Copyright (c)2003 Grid Systems
23 */
24 package com.gridsystems.config.app;
25
26 import java.text.MessageFormat;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import com.gridsystems.config.Configurator;
32 import com.gridsystems.config.ConfiguratorModel;
33 import com.gridsystems.config.modules.jvm.JVMConfigurator;
34
35 /**
36 * Special UI that gives no user interface. Its goal is to perform a silent "apply all"
37 * operation.
38 *
39 * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
40 * @version 1.0
41 */
42 class NullUI extends UI {
43 /**
44 * For this class logs.
45 */
46 private static Log log = LogFactory.getLog(NullUI.class);
47
48 /**
49 * true if an error has occurred.
50 */
51 private boolean errors = false;
52
53 /**
54 * {@inheritDoc}
55 */
56 public boolean execute() {
57 Configurator[] configs = UI.getConfigurators();
58
59 JVMConfigurator jvmc = JVMConfigurator.getInstance();
60
61 // Loads all configurators
62 load(jvmc);
63 for (int i = 0; i < configs.length; i++) {
64 if (configs[i] != jvmc) {
65 load(configs[i]);
66 }
67 }
68
69 // Applies all configurators
70 for (int i = 0; i < configs.length; i++) {
71 if (configs[i] != jvmc) {
72 apply(configs[i]);
73 }
74 }
75 apply(jvmc);
76
77 // If there were errors, exit with a "failure" exit code
78 if (errors) {
79 System.exit(1);
80 }
81 return true;
82 }
83
84 /**
85 * Loads the model of the specified configuration instance.
86 *
87 * @param config The configuration instance whose model to load
88 */
89 private void load(Configurator config) {
90 if (config != null) {
91 try {
92 ConfiguratorModel model = config.getModel();
93 if (model != null) {
94 model.load();
95 }
96 } catch (Exception e) {
97 String pattern = UI.getString("errors.load_error");
98 String msg = MessageFormat.format(pattern, new Object[] { config.getName() });
99 log.error(msg + ":" + e.getMessage());
100 }
101 }
102 }
103
104 /**
105 * Stores and applies the model of the specified configuration instance.
106 *
107 * @param config The configuration instance whose model to apply
108 */
109 private void apply(Configurator config) {
110 if (config != null) {
111 try {
112 config.apply();
113 } catch (Exception e) {
114 String pattern = UI.getString("errors.apply_error");
115 String msg = MessageFormat.format(pattern, new Object[] { config.getName() });
116 log.error(msg + ":" + e.getMessage());
117 errors = true;
118 }
119 }
120 }
121 }