[Minor] code cleanup

- extracted constants for configuration
- fixed redundant null check
- cleaned up compiler warnings on i18n
This commit is contained in:
Robert von Burg 2014-08-24 10:15:52 +02:00
parent e82b14f783
commit 77de155b17
6 changed files with 102 additions and 42 deletions

View File

@ -18,6 +18,7 @@ package li.strolch.runtime.configuration;
import java.io.File;
import java.text.MessageFormat;
import li.strolch.model.Tags;
import li.strolch.runtime.configuration.ConfigurationSaxParser.ConfigurationBuilder;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.XmlHelper;
@ -29,9 +30,9 @@ public class ConfigurationParser {
// private static final Logger logger = LoggerFactory.getLogger(ConfigurationParser.class);
public static StrolchConfiguration parseConfiguration(String environment, File rootPathF) {
DBC.PRE.assertNotEmpty("environment value must be set!", environment);
DBC.PRE.assertNotNull("roothPath must be set!", rootPathF);
DBC.PRE.assertNotEquals("environment must be a value other than 'global'!", "global", environment);
DBC.PRE.assertNotEmpty("environment value must be set!", environment); //$NON-NLS-1$
DBC.PRE.assertNotNull("roothPath must be set!", rootPathF); //$NON-NLS-1$
DBC.PRE.assertNotEquals("environment must be a value other than 'global'!", Tags.GLOBAL, environment); //$NON-NLS-1$
if (!rootPathF.isDirectory() || !rootPathF.canRead()) {
String msg = "Root path is not readable at {0}"; //$NON-NLS-1$

View File

@ -15,6 +15,19 @@
*/
package li.strolch.runtime.configuration;
import static li.strolch.runtime.configuration.ConfigurationTags.API;
import static li.strolch.runtime.configuration.ConfigurationTags.APPLICATION_NAME;
import static li.strolch.runtime.configuration.ConfigurationTags.DEPENDS;
import static li.strolch.runtime.configuration.ConfigurationTags.ENV_GLOBAL;
import static li.strolch.runtime.configuration.ConfigurationTags.ID;
import static li.strolch.runtime.configuration.ConfigurationTags.IMPL;
import static li.strolch.runtime.configuration.ConfigurationTags.NAME;
import static li.strolch.runtime.configuration.ConfigurationTags.STROLCH_CONFIGURATION_ENV;
import static li.strolch.runtime.configuration.ConfigurationTags.STROLCH_CONFIGURATION_ENV_COMPONENT;
import static li.strolch.runtime.configuration.ConfigurationTags.STROLCH_CONFIGURATION_ENV_COMPONENT_PROPERTIES;
import static li.strolch.runtime.configuration.ConfigurationTags.STROLCH_CONFIGURATION_ENV_RUNTIME;
import static li.strolch.runtime.configuration.ConfigurationTags.STROLCH_CONFIGURATION_ENV_RUNTIME_PROPERTIES;
import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayDeque;
@ -40,7 +53,6 @@ public class ConfigurationSaxParser extends DefaultHandler {
//private static final Logger logger = LoggerFactory.getLogger(ConfigurationSaxParser.class);
private static final String ENV_GLOBAL = "global";
private final String environment;
private String currentEnvironment;
@ -92,20 +104,20 @@ public class ConfigurationSaxParser extends DefaultHandler {
switch (locator.toString()) {
case "StrolchConfiguration/env": //$NON-NLS-1$
String env = attributes.getValue("id");
DBC.PRE.assertNotEmpty("attribute 'id' must be set on element 'env'", env);
case STROLCH_CONFIGURATION_ENV:
String env = attributes.getValue(ID);
DBC.PRE.assertNotEmpty("attribute 'id' must be set on element 'env'", env); //$NON-NLS-1$
if (this.envBuilders.containsKey(env)) {
String msg = "Environment {0} already exists!";
String msg = "Environment {0} already exists!"; //$NON-NLS-1$
throw new IllegalStateException(MessageFormat.format(msg, env));
}
this.currentEnvironment = env;
ConfigurationBuilder newEnvBuilder = new ConfigurationBuilder();
newEnvBuilder.runtimeBuilder().setEnvironment(currentEnvironment);
newEnvBuilder.runtimeBuilder().setEnvironment(this.currentEnvironment);
this.envBuilders.put(env, newEnvBuilder);
break;
case "StrolchConfiguration/env/Runtime": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_RUNTIME:
if (isRequiredEnv(this.currentEnvironment)) {
ConfigurationBuilder configurationBuilder = getEnvBuilder(this.currentEnvironment);
RuntimeHandler runtimeHandler = new RuntimeHandler(configurationBuilder, locator);
@ -113,7 +125,7 @@ public class ConfigurationSaxParser extends DefaultHandler {
}
break;
case "StrolchConfiguration/env/Runtime/Properties": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_RUNTIME_PROPERTIES:
if (isRequiredEnv(this.currentEnvironment)) {
ConfigurationBuilder configurationBuilder = getEnvBuilder(this.currentEnvironment);
PropertiesHandler runtimePropertiesHandler = new PropertiesHandler(configurationBuilder, locator);
@ -122,7 +134,7 @@ public class ConfigurationSaxParser extends DefaultHandler {
}
break;
case "StrolchConfiguration/env/Component": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_COMPONENT:
if (isRequiredEnv(this.currentEnvironment)) {
ConfigurationBuilder configurationBuilder = getEnvBuilder(this.currentEnvironment);
configurationBuilder.nextComponentBuilder();
@ -131,7 +143,7 @@ public class ConfigurationSaxParser extends DefaultHandler {
}
break;
case "StrolchConfiguration/env/Component/Properties": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_COMPONENT_PROPERTIES:
if (isRequiredEnv(this.currentEnvironment)) {
ConfigurationBuilder configurationBuilder = getEnvBuilder(this.currentEnvironment);
PropertiesHandler componentPropertiesHandler = new PropertiesHandler(configurationBuilder, locator);
@ -148,13 +160,14 @@ public class ConfigurationSaxParser extends DefaultHandler {
private ConfigurationBuilder getEnvBuilder(String environment) {
if (StringHelper.isEmpty(environment))
throw new IllegalStateException("environment must be set!");
throw new IllegalStateException("environment must be set!"); //$NON-NLS-1$
else if (environment.equals(ENV_GLOBAL))
return globalEnvBuilder;
return this.globalEnvBuilder;
ConfigurationBuilder envBuilder = this.envBuilders.get(environment);
if (envBuilder == null)
throw new IllegalStateException("No ConfigurationBuilder exists for env " + environment);
throw new IllegalStateException(MessageFormat.format(
"No ConfigurationBuilder exists for env {0}", environment)); //$NON-NLS-1$
return envBuilder;
}
@ -167,16 +180,16 @@ public class ConfigurationSaxParser extends DefaultHandler {
switch (locator.toString()) {
case "StrolchConfiguration/env": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV:
break;
case "StrolchConfiguration/env/Runtime": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_RUNTIME:
if (isRequiredEnv(this.currentEnvironment)) {
assertExpectedLocator(locator, this.delegateHandlers.pop().getLocator());
}
break;
case "StrolchConfiguration/env/Runtime/Properties": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_RUNTIME_PROPERTIES:
if (isRequiredEnv(this.currentEnvironment)) {
ConfigurationBuilder configurationBuilder = getEnvBuilder(this.currentEnvironment);
assertExpectedLocator(locator, this.delegateHandlers.pop().getLocator());
@ -184,13 +197,13 @@ public class ConfigurationSaxParser extends DefaultHandler {
}
break;
case "StrolchConfiguration/env/Component": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_COMPONENT:
if (isRequiredEnv(this.currentEnvironment)) {
assertExpectedLocator(locator, this.delegateHandlers.pop().getLocator());
}
break;
case "StrolchConfiguration/env/Component/Properties": //$NON-NLS-1$
case STROLCH_CONFIGURATION_ENV_COMPONENT_PROPERTIES:
if (isRequiredEnv(this.currentEnvironment)) {
ConfigurationBuilder configurationBuilder = getEnvBuilder(this.currentEnvironment);
assertExpectedLocator(locator, this.delegateHandlers.pop().getLocator());
@ -220,8 +233,8 @@ public class ConfigurationSaxParser extends DefaultHandler {
protected StringBuilder valueBuffer;
public ElementHandler(ConfigurationBuilder configurationBuilder, Locator locator) {
DBC.PRE.assertNotNull("configurationBuilder must be set!", configurationBuilder);
DBC.PRE.assertNotNull("locator must be set!", locator);
DBC.PRE.assertNotNull("configurationBuilder must be set!", configurationBuilder); //$NON-NLS-1$
DBC.PRE.assertNotNull("locator must be set!", locator); //$NON-NLS-1$
this.configurationBuilder = configurationBuilder;
this.locator = locator;
}
@ -246,7 +259,7 @@ public class ConfigurationSaxParser extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "applicationName": //$NON-NLS-1$
case APPLICATION_NAME:
this.valueBuffer = new StringBuilder();
break;
default:
@ -257,7 +270,7 @@ public class ConfigurationSaxParser extends DefaultHandler {
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "applicationName": //$NON-NLS-1$
case APPLICATION_NAME:
String applicationName = this.valueBuffer.toString();
this.configurationBuilder.runtimeBuilder().setApplicationName(applicationName);
this.valueBuffer = null;
@ -277,16 +290,16 @@ public class ConfigurationSaxParser extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "name": //$NON-NLS-1$
case NAME:
this.valueBuffer = new StringBuilder();
break;
case "api": //$NON-NLS-1$
case API:
this.valueBuffer = new StringBuilder();
break;
case "impl": //$NON-NLS-1$
case IMPL:
this.valueBuffer = new StringBuilder();
break;
case "depends": //$NON-NLS-1$
case DEPENDS:
this.valueBuffer = new StringBuilder();
break;
default:
@ -297,21 +310,21 @@ public class ConfigurationSaxParser extends DefaultHandler {
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "name": //$NON-NLS-1$
case NAME:
String name = this.valueBuffer.toString();
this.configurationBuilder.componentBuilder().setName(name);
this.valueBuffer = null;
break;
case "api": //$NON-NLS-1$
case API:
String api = this.valueBuffer.toString();
this.configurationBuilder.componentBuilder().setApi(api);
this.valueBuffer = null;
break;
case "impl": //$NON-NLS-1$
case IMPL:
String impl = this.valueBuffer.toString();
this.configurationBuilder.componentBuilder().setImpl(impl);
break;
case "depends": //$NON-NLS-1$
case DEPENDS:
String depends = this.valueBuffer.toString();
this.configurationBuilder.componentBuilder().addDependency(depends);
break;
@ -472,7 +485,7 @@ public class ConfigurationSaxParser extends DefaultHandler {
}
public RuntimeConfiguration build(File rootPathF) {
RuntimeConfiguration configuration = new RuntimeConfiguration(this.applicationName, environment,
RuntimeConfiguration configuration = new RuntimeConfiguration(this.applicationName, this.environment,
getProperties(), rootPathF);
return configuration;
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package li.strolch.runtime.configuration;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ConfigurationTags {
public static final String ENV_GLOBAL = "global"; //$NON-NLS-1$
public static final String STROLCH_CONFIGURATION_ENV_COMPONENT_PROPERTIES = "StrolchConfiguration/env/Component/Properties"; //$NON-NLS-1$
public static final String STROLCH_CONFIGURATION_ENV_COMPONENT = "StrolchConfiguration/env/Component"; //$NON-NLS-1$
public static final String STROLCH_CONFIGURATION_ENV_RUNTIME_PROPERTIES = "StrolchConfiguration/env/Runtime/Properties"; //$NON-NLS-1$
public static final String STROLCH_CONFIGURATION_ENV_RUNTIME = "StrolchConfiguration/env/Runtime"; //$NON-NLS-1$
public static final String STROLCH_CONFIGURATION_ENV = "StrolchConfiguration/env"; //$NON-NLS-1$
public static final String APPLICATION_NAME = "applicationName"; //$NON-NLS-1$
public static final String ID = "id"; //$NON-NLS-1$
public static final String DEPENDS = "depends"; //$NON-NLS-1$
public static final String IMPL = "impl"; //$NON-NLS-1$
public static final String API = "api"; //$NON-NLS-1$
public static final String NAME = "name"; //$NON-NLS-1$
}

View File

@ -29,12 +29,12 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public class StrolchEnvironment {
public static final String ENV_PROPERTIES_FILE = "ENV.properties";
public static final String ENV_PROPERTIES_FILE = "ENV.properties"; //$NON-NLS-1$
public static String getEnvironmentFromSystemProperties() {
String environment = System.getProperties().getProperty(StrolchConstants.ENV_STROLCH);
if (StringHelper.isEmpty(environment)) {
String msg = "The system property {0} is missing!";
String msg = "The system property {0} is missing!"; //$NON-NLS-1$
throw new StrolchConfigurationException(MessageFormat.format(msg, StrolchConstants.ENV_STROLCH));
}
@ -43,17 +43,20 @@ public class StrolchEnvironment {
public static String getEnvironmentFromEnvProperties(File rootPath) {
File envF = new File(rootPath, ENV_PROPERTIES_FILE);
DBC.PRE.assertExists(ENV_PROPERTIES_FILE + " does not exist in " + rootPath.getAbsolutePath(), envF);
DBC.PRE.assertExists(
MessageFormat.format("{0} does not exist in {1}", ENV_PROPERTIES_FILE, rootPath.getAbsolutePath()), //$NON-NLS-1$
envF);
Properties envP = new Properties();
try (FileInputStream fin = new FileInputStream(envF)) {
envP.load(fin);
} catch (Exception e) {
throw new StrolchConfigurationException("Failed to load " + ENV_PROPERTIES_FILE + " in " + rootPath, e);
throw new StrolchConfigurationException(MessageFormat.format(
"Failed to load {0} in {1}", ENV_PROPERTIES_FILE, rootPath), e); //$NON-NLS-1$
}
String environment = envP.getProperty(StrolchConstants.ENV_STROLCH);
if (StringHelper.isEmpty(environment)) {
String msg = "The property {0} does not exist in {1}";
String msg = "The property {0} does not exist in {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, StrolchConstants.ENV_STROLCH, envF.getAbsolutePath());
throw new StrolchConfigurationException(msg);
}

View File

@ -88,7 +88,7 @@ public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement
@Override
public void visitAny() {
DBC.PRE.assertEmpty("Only one selection allowed when using Any!", this.selectors);
DBC.PRE.assertEmpty("Only one selection allowed when using Any!", this.selectors); //$NON-NLS-1$
addSelector(new AnySelector<T>());
this.any = true;
}

View File

@ -78,8 +78,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa
if (service instanceof AbstractService) {
AbstractService<?, ?> abstractService = (AbstractService<?, ?>) service;
abstractService.setContainer(getContainer());
if (privilegeContext != null)
abstractService.setPrivilegeContext(privilegeContext);
abstractService.setPrivilegeContext(privilegeContext);
}
U serviceResult = service.doService(argument);