KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > setup > StandardTVSConfigurationSetupManagerFactory


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.setup;
5
6 import org.apache.commons.cli.CommandLine;
7 import org.apache.commons.cli.Option;
8 import org.apache.commons.cli.Options;
9 import org.apache.commons.cli.ParseException;
10 import org.apache.commons.cli.PosixParser;
11 import org.apache.commons.lang.StringUtils;
12
13 import com.tc.config.schema.IllegalConfigurationChangeHandler;
14 import com.tc.logging.TCLogger;
15
16 import java.io.File JavaDoc;
17 import java.net.InetAddress JavaDoc;
18 import java.net.UnknownHostException JavaDoc;
19
20 /**
21  * The standard implementation of {@link com.tc.config.schema.setup.TVSConfigurationSetupManagerFactory} — uses
22  * system properties and command-line arguments to create the relevant factories.
23  */

24 public class StandardTVSConfigurationSetupManagerFactory extends BaseTVSConfigurationSetupManagerFactory {
25
26   private static final String JavaDoc CONFIG_SPEC_ARGUMENT_NAME = "config";
27   /**
28    * <code>public</code> for <strong>TESTS AND DOCUMENTATION ONLY</strong>.
29    */

30   public static final String JavaDoc CONFIG_SPEC_ARGUMENT_WORD = "--" + CONFIG_SPEC_ARGUMENT_NAME;
31
32   private static final String JavaDoc L2_NAME_PROPERTY_NAME = "tc.server.name";
33   public static final String JavaDoc DEFAULT_CONFIG_SPEC = "tc-config.xml";
34   public static final String JavaDoc DEFAULT_CONFIG_PATH = "default-config.xml";
35
36   private final String JavaDoc defaultL2Identifier;
37   private final String JavaDoc configSpec;
38   private final File JavaDoc cwd;
39
40   public StandardTVSConfigurationSetupManagerFactory(boolean isForL2,
41                                                      IllegalConfigurationChangeHandler illegalChangeHandler)
42       throws ConfigurationSetupException {
43     this((String JavaDoc[]) null, isForL2, illegalChangeHandler);
44   }
45
46   public StandardTVSConfigurationSetupManagerFactory(String JavaDoc[] args, boolean isForL2,
47                                                      IllegalConfigurationChangeHandler illegalChangeHandler)
48       throws ConfigurationSetupException {
49     this(parseDefaultCommandLine(args, isForL2), isForL2, illegalChangeHandler);
50   }
51
52   private static CommandLine parseDefaultCommandLine(String JavaDoc[] args, boolean isForL2) throws ConfigurationSetupException {
53     try {
54       if (args == null || args.length == 0) {
55         return new PosixParser().parse(new Options(), new String JavaDoc[0]);
56       } else {
57         Options options = createOptions(isForL2);
58
59         return new PosixParser().parse(options, args);
60       }
61     } catch (ParseException pe) {
62       throw new ConfigurationSetupException(pe.getLocalizedMessage(), pe);
63     }
64   }
65
66   public StandardTVSConfigurationSetupManagerFactory(CommandLine commandLine, boolean isForL2,
67                                                      IllegalConfigurationChangeHandler illegalChangeHandler)
68       throws ConfigurationSetupException {
69     super(illegalChangeHandler);
70
71     String JavaDoc configFileOnCommandLine = null;
72     String JavaDoc l2NameOnCommandLine = null;
73
74     configFileOnCommandLine = StringUtils.trimToNull(commandLine.getOptionValue('f'));
75     l2NameOnCommandLine = StringUtils.trimToNull(commandLine.getOptionValue('n'));
76
77     String JavaDoc effectiveConfigSpec;
78     effectiveConfigSpec = StringUtils.trimToNull(configFileOnCommandLine != null ? configFileOnCommandLine : System
79         .getProperty(TVSConfigurationSetupManagerFactory.CONFIG_FILE_PROPERTY_NAME));
80     String JavaDoc specifiedL2Identifier = StringUtils.trimToNull(l2NameOnCommandLine != null ? l2NameOnCommandLine : System
81         .getProperty(L2_NAME_PROPERTY_NAME));
82
83     if (StringUtils.isBlank(effectiveConfigSpec)) {
84       File JavaDoc localConfig = new File JavaDoc(System.getProperty("user.dir"), DEFAULT_CONFIG_SPEC);
85       
86       if(localConfig.exists()) {
87         effectiveConfigSpec = localConfig.getAbsolutePath();
88       } else if(isForL2) {
89         String JavaDoc packageName = getClass().getPackage().getName();
90         effectiveConfigSpec = "resource:///" + packageName.replace('.', '/') + "/" + DEFAULT_CONFIG_PATH;
91       }
92     }
93     this.configSpec = effectiveConfigSpec;
94     
95     if (StringUtils.isBlank(this.configSpec)) {
96       // formatting
97
throw new ConfigurationSetupException("You must specify the location of the Terracotta "
98                                             + "configuration file for this process, using the " + "'"
99                                             + CONFIG_FILE_PROPERTY_NAME + "' system property.");
100     }
101
102     String JavaDoc cwdAsString = System.getProperty("user.dir");
103     if (StringUtils.isBlank(cwdAsString)) {
104       // formatting
105
throw new ConfigurationSetupException(
106                                             "We can't find the working directory of the process; we need this to continue. "
107                                             + "(The system property 'user.dir' was "
108                                             + (cwdAsString == null ? "null" : "'" + cwdAsString + "'") + ".)");
109     }
110
111     this.cwd = new File JavaDoc(cwdAsString);
112
113     if (specifiedL2Identifier != null) {
114       this.defaultL2Identifier = specifiedL2Identifier;
115     } else {
116       String JavaDoc hostName = null;
117
118       try {
119         hostName = InetAddress.getLocalHost().getHostName();
120       } catch (UnknownHostException JavaDoc uhe) {
121         /**/
122       }
123
124       String JavaDoc potentialName = hostName;
125
126       if (potentialName != null && potentialName.indexOf(".") >= 0) potentialName = potentialName
127           .substring(0, potentialName.indexOf("."));
128       if (potentialName != null) potentialName = potentialName.trim();
129
130       if (!StringUtils.isBlank(potentialName) && (!potentialName.equalsIgnoreCase("localhost"))) {
131         this.defaultL2Identifier = potentialName;
132       } else {
133         this.defaultL2Identifier = null;
134       }
135     }
136   }
137
138   public static Options createOptions(boolean isForL2) {
139     Options options = new Options();
140
141     Option configFileOption = new Option("f", CONFIG_SPEC_ARGUMENT_NAME, true,
142                                          "the configuration file to use, specified as a file path or URL");
143     configFileOption.setArgName("file-or-URL");
144     configFileOption.setType(String JavaDoc.class);
145
146     Option l2NameOption = new Option("n", "name", true, "the name of this L2; defaults to the host name");
147     l2NameOption.setRequired(false);
148     l2NameOption.setArgName("l2-name");
149
150     if (isForL2) {
151       configFileOption.setRequired(false);
152       options.addOption(configFileOption);
153       options.addOption(l2NameOption);
154     } else {
155       configFileOption.setRequired(true);
156       options.addOption(configFileOption);
157     }
158
159     return options;
160   }
161
162
163   public L1TVSConfigurationSetupManager createL1TVSConfigurationSetupManager(TCLogger logger)
164     throws ConfigurationSetupException
165   {
166     ConfigurationCreator configurationCreator = new StandardXMLFileConfigurationCreator(logger,
167                                                                                         this.configSpec,
168                                                                                         this.cwd,
169                                                                                         this.beanFactory);
170   
171     L1TVSConfigurationSetupManager setupManager = new StandardL1TVSConfigurationSetupManager(configurationCreator,
172                                                                                              this.defaultValueProvider,
173                                                                                              this.xmlObjectComparator,
174                                                                                              this.illegalChangeHandler);
175     
176     return setupManager;
177   }
178
179   public L1TVSConfigurationSetupManager createL1TVSConfigurationSetupManager()
180     throws ConfigurationSetupException
181   {
182     ConfigurationCreator configurationCreator = new StandardXMLFileConfigurationCreator(this.configSpec,
183                                                                                         this.cwd,
184                                                                                         this.beanFactory);
185
186     L1TVSConfigurationSetupManager setupManager = new StandardL1TVSConfigurationSetupManager(configurationCreator,
187                                                                                              this.defaultValueProvider,
188                                                                                              this.xmlObjectComparator,
189                                                                                              this.illegalChangeHandler);
190     
191     return setupManager;
192   }
193
194   public L2TVSConfigurationSetupManager createL2TVSConfigurationSetupManager(String JavaDoc l2Name)
195     throws ConfigurationSetupException
196   {
197     if (l2Name == null) l2Name = this.defaultL2Identifier;
198
199     ConfigurationCreator configurationCreator;
200     configurationCreator = new StandardXMLFileConfigurationCreator(this.configSpec,
201                                                                    this.cwd,
202                                                                    this.beanFactory);
203
204     return new StandardL2TVSConfigurationSetupManager(configurationCreator,
205                                                       l2Name,
206                                                       this.defaultValueProvider,
207                                                       this.xmlObjectComparator,
208                                                       this.illegalChangeHandler);
209   }
210
211 }
212
Popular Tags