Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.xml;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jetty.util.ArrayQueue;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlParser.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
*
Configures objects from XML.
*
This class reads an XML file conforming to the configure.dtd DTD
* and uses it to configure and object by calling set, put or other methods on the object.
*
The actual XML file format may be changed (eg to spring XML) by implementing the
* {@link ConfigurationProcessorFactory} interface to be found by the
* {@link ServiceLoader} by using the DTD and first tag element in the file.
* Note that DTD will be null if validation is off.
*
* The configuration can be parameterised with properties that are looked up via the
* Property XML element and set on the configuration via the map returned from
* {@link #getProperties()}
*
* The configuration can create and lookup beans by ID. If multiple configurations are used, then it
* is good practise to copy the entries from the {@link #getIdMap()} of a configuration to the next
* configuration so that they can share an ID space for beans.
*/
public class XmlConfiguration
{
private static final Logger LOG = Log.getLogger(XmlConfiguration.class);
private static final Class>[] __primitives =
{Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE};
private static final Class>[] __boxedPrimitives =
{Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Void.class};
private static final Class>[] __supportedCollections =
{ArrayList.class, ArrayQueue.class, HashSet.class, Queue.class, List.class, Set.class, Collection.class,};
private static final Iterable __factoryLoader = ServiceLoader.load(ConfigurationProcessorFactory.class);
private static final XmlParser __parser = initParser();
private static XmlParser initParser()
{
XmlParser parser = new XmlParser();
URL config60 = Loader.getResource(XmlConfiguration.class, "org/eclipse/jetty/xml/configure_6_0.dtd");
URL config76 = Loader.getResource(XmlConfiguration.class,"org/eclipse/jetty/xml/configure_7_6.dtd");
URL config90 = Loader.getResource(XmlConfiguration.class,"org/eclipse/jetty/xml/configure_9_0.dtd");
parser.redirectEntity("configure.dtd",config90);
parser.redirectEntity("configure_1_0.dtd",config60);
parser.redirectEntity("configure_1_1.dtd",config60);
parser.redirectEntity("configure_1_2.dtd",config60);
parser.redirectEntity("configure_1_3.dtd",config60);
parser.redirectEntity("configure_6_0.dtd",config60);
parser.redirectEntity("configure_7_6.dtd",config76);
parser.redirectEntity("configure_9_0.dtd",config90);
parser.redirectEntity("http://jetty.mortbay.org/configure.dtd",config90);
parser.redirectEntity("http://jetty.eclipse.org/configure.dtd",config90);
parser.redirectEntity("http://www.eclipse.org/jetty/configure.dtd",config90);
parser.redirectEntity("-//Mort Bay Consulting//DTD Configure//EN",config90);
parser.redirectEntity("-//Jetty//Configure//EN",config90);
return parser;
}
private final Map _idMap = new HashMap<>();
private final Map _propertyMap = new HashMap<>();
private final URL _url;
private final String _dtd;
private ConfigurationProcessor _processor;
/**
* Reads and parses the XML configuration file.
*
* @param configuration the URL of the XML configuration
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
*/
public XmlConfiguration(URL configuration) throws SAXException, IOException
{
synchronized (__parser)
{
_url=configuration;
setConfig(__parser.parse(configuration.toString()));
_dtd=__parser.getDTD();
}
}
/**
* Reads and parses the XML configuration string.
*
* @param configuration String of XML configuration commands excluding the normal XML preamble.
* The String should start with a "<Configure ....>" element.
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
*/
public XmlConfiguration(String configuration) throws SAXException, IOException
{
configuration = "\n"
+ configuration;
InputSource source = new InputSource(new StringReader(configuration));
synchronized (__parser)
{
_url=null;
setConfig( __parser.parse(source));
_dtd=__parser.getDTD();
}
}
/**
* Reads and parses the XML configuration stream.
*
* @param configuration An input stream containing a complete configuration file
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
*/
public XmlConfiguration(InputStream configuration) throws SAXException, IOException
{
InputSource source = new InputSource(configuration);
synchronized (__parser)
{
_url=null;
setConfig(__parser.parse(source));
_dtd=__parser.getDTD();
}
}
private void setConfig(XmlParser.Node config)
{
if ("Configure".equals(config.getTag()))
{
_processor=new JettyXmlConfiguration();
}
else if (__factoryLoader!=null)
{
for (ConfigurationProcessorFactory factory : __factoryLoader)
{
_processor = factory.getConfigurationProcessor(_dtd, config.getTag());
if (_processor!=null)
break;
}
if (_processor==null)
throw new IllegalStateException("Unknown configuration type: "+config.getTag()+" in "+this);
}
else
{
throw new IllegalArgumentException("Unknown XML tag:"+config.getTag());
}
_processor.init(_url,config,this);
}
/* ------------------------------------------------------------ */
/** Get the map of ID String to Objects that is used to hold
* and lookup any objects by ID.
*
* A New, Get or Call XML element may have an
* id attribute which will cause the resulting object to be placed into
* this map. A Ref XML element will lookup an object from this map.
*
* When chaining configuration files, it is good practise to copy the
* ID entries from the ID map to the map of the next configuration, so
* that they may share an ID space
*
*
* @return A modifiable map of ID strings to Objects
*/
public Map getIdMap()
{
return _idMap;
}
/* ------------------------------------------------------------ */
/**
* Get the map of properties used by the Property XML element
* to parameterise configuration.
* @return A modifiable map of properties.
*/
public Map getProperties()
{
return _propertyMap;
}
/**
* Applies the XML configuration script to the given object.
*
* @param obj The object to be configured, which must be of a type or super type
* of the class attribute of the <Configure> element.
* @throws Exception if the configuration fails
* @return the configured object
*/
public Object configure(Object obj) throws Exception
{
return _processor.configure(obj);
}
/**
* Applies the XML configuration script.
* If the root element of the configuration has an ID, an object is looked up by ID and its type checked
* against the root element's type.
* Otherwise a new object of the type specified by the root element is created.
*
* @return The newly created configured object.
* @throws Exception if the configuration fails
*/
public Object configure() throws Exception
{
return _processor.configure();
}
/* ------------------------------------------------------------ */
/** Initialize a new Object defaults.
*
This method must be called by any {@link ConfigurationProcessor} when it
* creates a new instance of an object before configuring it, so that a derived
* XmlConfiguration class may inject default values.
* @param object the object to initialize defaults on
*/
public void initializeDefaults(Object object)
{
}
private static class JettyXmlConfiguration implements ConfigurationProcessor
{
private String _url;
XmlParser.Node _root;
XmlConfiguration _configuration;
public void init(URL url, XmlParser.Node root, XmlConfiguration configuration)
{
_url=url==null?null:url.toString();
_root=root;
_configuration=configuration;
}
public Object configure(Object obj) throws Exception
{
// Check the class of the object
Class> oClass = nodeClass(_root);
if (oClass != null && !oClass.isInstance(obj))
{
String loaders = (oClass.getClassLoader()==obj.getClass().getClassLoader())?"":"Object Class and type Class are from different loaders.";
throw new IllegalArgumentException("Object of class '"+obj.getClass().getCanonicalName()+"' is not of type '" + oClass.getCanonicalName()+"'. "+loaders+" in "+_url);
}
configure(obj,_root,0);
return obj;
}
public Object configure() throws Exception
{
Class> oClass = nodeClass(_root);
String id = _root.getAttribute("id");
Object obj = id == null?null:_configuration.getIdMap().get(id);
int index = 0;
if (obj == null && oClass != null)
{
index = _root.size();
Map namedArgMap = new HashMap<>();
List