org.apache.openejb.config.sys.SaxAppCtxConfig Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openejb.config.sys;
import org.apache.openejb.config.AppModule;
import org.apache.openejb.config.BeanProperties;
import org.apache.openejb.config.DeploymentModule;
import org.apache.openejb.config.EjbModule;
import org.apache.openejb.config.EnvEntriesPropertiesDeployer;
import org.apache.openejb.config.PojoConfiguration;
import org.apache.openejb.config.WebModule;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.Logger;
import org.apache.openejb.util.Saxs;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class SaxAppCtxConfig {
private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB_STARTUP_CONFIG, SaxAppContextConfig.class);
public static void parse(final AppModule appModule, final InputSource source,
final EnvEntriesPropertiesDeployer envEntriesDeployer, final BeanProperties beanProperties)
throws SAXException, ParserConfigurationException, IOException {
Saxs.factory()
.newSAXParser()
.parse(source, new SaxAppContextConfig(appModule, envEntriesDeployer, beanProperties));
}
private static class SaxAppContextConfig extends StackHandler {
private static final Collection IMPORT_ALIASES = Arrays.asList("import", "include");
private static final Collection APPLICATION_ALIASES = Arrays.asList("appcontext", "app-context", "application");
private static final Collection POJOS_ALIASES = Arrays.asList("pojocontexts", "pojo-contexts", "pojos");
private static final Collection POJO_ALIASES = Arrays.asList("pojo");
private static final Collection BEAN_CONTEXTS_ALIASES = Arrays.asList("beancontexts", "bean-contexts", "ejbs");
private static final Collection WEBAPP_ALIASES = Arrays.asList("webapps", "webcontexts", "web-contexts", "wars");
private static final Collection MODULE_ALIASES = Arrays.asList("modulecontext", "module");
private static final Collection BEAN_CONTEXT_ALIASES = Arrays.asList("ejb", "beancontext", "bean-context");
private static final Collection CONFIGURATION_ALIASES = Arrays.asList("configuration", "properties", "settings");
private static final Collection RESOURCES_ALIASES = Arrays.asList("resources");
private static final Collection SERVICE_ALIASES = Arrays.asList("service");
private static final Collection RESOURCE_ALIASES = Arrays.asList("resource");
private static final Collection ENV_ENTRIES_ALIASES = Arrays.asList("enventries", "env-entries");
private static final Collection ENV_ENTRY_ALIASES = Arrays.asList("enventry", "env-entry");
private final AppModule module;
private final EnvEntriesPropertiesDeployer envEntriesDeployer;
private final BeanProperties beanPropertiesDeployer;
public SaxAppContextConfig(final AppModule appModule, final EnvEntriesPropertiesDeployer envEntriesDeployer, final BeanProperties beanProperties) {
this.module = appModule;
this.envEntriesDeployer = envEntriesDeployer;
this.beanPropertiesDeployer = beanProperties;
}
@Override
public void startDocument() throws SAXException {
push(new Document());
}
private class Document extends DefaultHandler {
@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
// by default don't care about root tag
if (!APPLICATION_ALIASES.contains(localName.toLowerCase())
&& SystemInstance.get().getOptions().get("openejb.configuration.strict-tags", false)) {
throw new IllegalStateException("Unsupported Element: " + localName);
}
push(new Root());
}
}
private class Root extends DefaultHandler {
@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
final String name = localName.toLowerCase();
if (CONFIGURATION_ALIASES.contains(name)) {
push(new Configuration("", module.getProperties()));
} else if (ENV_ENTRIES_ALIASES.contains(name)) {
push(new EnvEntries());
} else if (BEAN_CONTEXTS_ALIASES.contains(name)) {
push(new BeanContexts(null));
} else if (MODULE_ALIASES.contains(name)) {
push(new ModuleContext(attributes.getValue("id")));
} else if (WEBAPP_ALIASES.contains(name)) {
push(new WebAppContext(attributes.getValue("id")));
} else if (POJOS_ALIASES.contains(name)) {
push(new Pojos());
} else if (RESOURCES_ALIASES.contains(name)) {
push(new ResourcesConfig());
} else if (IMPORT_ALIASES.contains(name)) {
importFile(attributes.getValue("path"));
push(new DefaultHandler()); // just to keep the stack consistent
} else {
throw new IllegalStateException("Unsupported Element: " + localName);
}
}
private void importFile(final String path) throws SAXException {
final File file = new File(path);
if (file.exists()) {
try {
parse(module, new InputSource(new FileInputStream(file)), envEntriesDeployer, beanPropertiesDeployer);
} catch (final ParserConfigurationException e) {
throw new SAXException(e);
} catch (final IOException e) {
throw new SAXException(e);
}
} else { // try in the classpath
final ClassLoader cl = module.getClassLoader();
if (cl != null) {
final InputStream is = cl.getResourceAsStream(path);
if (is != null) {
try {
parse(module, new InputSource(is), envEntriesDeployer, beanPropertiesDeployer);
} catch (final ParserConfigurationException e) {
throw new SAXException(e);
} catch (final IOException e) {
throw new SAXException(e);
}
} else {
LOGGER.warning("Can't find " + path);
}
} else {
LOGGER.warning("Can't find " + path + ", no classloader for the module " + module);
}
}
}
}
private final class Configuration extends Content {
private final Properties properties;
private final String prefix;
private Configuration(final String prefix, final Properties properties) {
this.properties = properties;
this.prefix = prefix;
}
@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) {
push(new Configuration(prefix + localName + ".", properties));
}
@Override
public void setValue(final String text) {
if (properties == null) {
return;
}
try {
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy