提交 51f3a125 编写于 作者: F Frankie Wu

configure model refactory

上级 8d61e6c4
......@@ -32,10 +32,6 @@ import com.dianping.cat.storage.BucketManager;
import com.dianping.cat.storage.internal.AbstractFileBucket;
import com.site.lookup.annotation.Inject;
/**
* @author sean.wang
* @since Jan 5, 2012
*/
public class TransactionAnalyzer extends AbstractMessageAnalyzer<TransactionReport> implements LogEnabled {
private static final long MINUTE = 60 * 1000;
......
......@@ -39,7 +39,7 @@
<plugin>
<groupId>com.site.maven.plugins</groupId>
<artifactId>maven-codegen-plugin</artifactId>
<version>1.0.10</version>
<version>1.0.11</version>
<executions>
<execution>
<id>default-cli</id>
......@@ -48,7 +48,7 @@
<goal>dal-model</goal>
</goals>
<configuration>
<manifest>${basedir}/src/main/resources/META-INF/dal/model/manifest.xml
<manifest>${basedir}/src/main/resources/META-INF/dal/model/config-manifest.xml
</manifest>
</configuration>
</execution>
......
......@@ -8,6 +8,7 @@ import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import com.dianping.cat.configuration.ClientConfigMerger;
import com.dianping.cat.configuration.ClientConfigValidator;
import com.dianping.cat.configuration.model.entity.Config;
import com.dianping.cat.configuration.model.transform.DefaultXmlParser;
......@@ -68,17 +69,42 @@ public class Cat {
}
public static void initialize(PlexusContainer container, File configFile) {
Config config = null;
if (container != null) {
if (!s_instance.m_initialized) {
s_instance.setContainer(container);
s_instance.m_initialized = true;
} else {
throw new RuntimeException("Cat has already been initialized before!");
}
}
Config config = loadClientConfig(configFile);
if (config != null) {
getInstance().m_manager.initializeClient(config);
} else {
getInstance().m_manager.initializeClient(null);
System.out.println("[WARN] Cat client is disabled due to no config file found!");
}
}
static Config loadClientConfig(File configFile) {
Config globalConfig = null;
Config clientConfig = null;
// read config from local file system
try {
// read the global configure from local file system
// so that OPS can:
// - configure the cat servers to connect
// - enable/disable Cat for specific domain(s)
if (configFile != null) {
String xml = Files.forIO().readFrom(configFile.getCanonicalFile(), "utf-8");
config = new DefaultXmlParser().parse(xml);
globalConfig = new DefaultXmlParser().parse(xml);
}
if (config == null) {
// load the client configure from Java class-path
if (clientConfig == null) {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(CAT_CLIENT_XML);
if (in == null) {
......@@ -88,31 +114,24 @@ public class Cat {
if (in != null) {
String xml = Files.forIO().readFrom(in, "utf-8");
config = new DefaultXmlParser().parse(xml);
clientConfig = new DefaultXmlParser().parse(xml);
}
}
} catch (Exception e) {
throw new RuntimeException(String.format("Error when loading configuration file: %s!", configFile), e);
throw new RuntimeException(String.format("Error when loading configuration file(%s)!", configFile), e);
}
if (container != null) {
if (!s_instance.m_initialized) {
s_instance.setContainer(container);
s_instance.m_initialized = true;
} else {
throw new RuntimeException("Cat has already been initialized before!");
}
// merge the two configures together to make it effected
if (globalConfig != null && clientConfig != null) {
globalConfig.accept(new ClientConfigMerger(clientConfig));
}
if (config != null) {
ClientConfigValidator validator = new ClientConfigValidator();
config.accept(validator);
getInstance().m_manager.initializeClient(config);
} else {
getInstance().m_manager.initializeClient(null);
System.out.println("[WARN] Cat client is disabled due to no config file found!");
// do validation
if (clientConfig != null) {
clientConfig.accept(new ClientConfigValidator());
}
return clientConfig;
}
public static boolean isInitialized() {
......
package com.dianping.cat.configuration;
import com.dianping.cat.configuration.model.entity.Config;
import com.dianping.cat.configuration.model.entity.Domain;
import com.dianping.cat.configuration.model.entity.Property;
import com.dianping.cat.configuration.model.transform.DefaultMerger;
public class ClientConfigMerger extends DefaultMerger {
public ClientConfigMerger(Config config) {
super(config);
}
@Override
protected void visitConfigChildren(Config old, Config config) {
if (old != null) {
getStack().push(old);
// if servers is configured, then never merge it
if (old.getServers().isEmpty()) {
old.getServers().addAll(config.getServers());
}
// only configured domain in client configure will be merged
for (Domain domain : config.getDomains().values()) {
if (old.getDomains().containsKey(domain.getId())) {
visitDomain(domain);
}
}
for (Property property : config.getProperties().values()) {
visitProperty(property);
}
getStack().pop();
}
}
@Override
protected void mergeDomain(Domain old, Domain domain) {
if (domain.getIp() != null) {
old.setIp(domain.getIp());
}
if (domain.getEnabled() != null) {
old.setEnabled(domain.getEnabled());
}
}
}
package com.dianping.cat.configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;
import com.dianping.cat.configuration.model.entity.Config;
import com.dianping.cat.configuration.model.entity.Domain;
import com.dianping.cat.configuration.model.entity.Server;
import com.dianping.cat.configuration.model.transform.DefaultValidator;
public class ClientConfigValidator extends DefaultValidator {
private String getLocalAddress() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// ignore it
}
return null;
}
@Override
public void visitConfig(Config config) {
if (!"client".equals(config.getMode())) {
throw new RuntimeException(String.format("Attribute(%s) at path(%s) is required!", "mode", "/config"));
} else if (config.getApp() == null) {
throw new RuntimeException(String.format("Element(%s) at path(%s) is required!", "app", "/config"));
throw new RuntimeException(String.format("Attribute(%s) of /config is required in config: %s", "mode", config));
} else if (config.getServers().size() == 0) {
throw new RuntimeException(String.format("Element(%s) at path(%s) is required!", "servers", "/config"));
throw new RuntimeException(
String.format("Element(%s) of /config is required in config: %s", "servers", config));
}
super.visitConfig(config);
}
@Override
public void visitDomain(Domain domain) {
super.visitDomain(domain);
// set default values
if (domain.getEnabled() == null) {
domain.setEnabled(true);
}
if (domain.getIp() == null) {
domain.setIp(getLocalAddress());
}
}
@Override
public void visitServer(Server server) {
super.visitServer(server);
// set default values
if (server.getPort() == null) {
server.setPort(2280);
}
if (server.getEnabled() == null) {
server.setEnabled(true);
}
}
}
......@@ -16,10 +16,10 @@ public class CatAppender extends AppenderSkeleton {
if (throwableInformation != null) {
if (Cat.getManager().getThreadLocalMessageTree() != null) {
Cat.getProducer().logError(throwableInformation.getThrowable());
} else {
Cat.setup(null);
Cat.getProducer().logError(throwableInformation.getThrowable());
Cat.reset();
// } else {
// Cat.setup(null);
// Cat.getProducer().logError(throwableInformation.getThrowable());
// Cat.reset();
}
}
}
......
......@@ -3,6 +3,7 @@ package com.dianping.cat.message.internal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import org.codehaus.plexus.logging.LogEnabled;
......@@ -10,6 +11,7 @@ import org.codehaus.plexus.logging.Logger;
import com.dianping.cat.Cat;
import com.dianping.cat.configuration.model.entity.Config;
import com.dianping.cat.configuration.model.entity.Domain;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.io.MessageSender;
......@@ -121,9 +123,9 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
m_clientConfig.setMode("client");
}
if (m_clientConfig.getApp() != null) {
m_domain = m_clientConfig.getApp().getDomain();
}
Map<String, Domain> domains = clientConfig.getDomains();
m_domain = domains.isEmpty() ? "unknown" : domains.keySet().iterator().next();
try {
InetAddress localHost = InetAddress.getLocalHost();
......@@ -138,9 +140,9 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
m_factory = lookup(MessageIdFactory.class);
// initialize domain and ip address
m_factory.initialize(m_clientConfig);
m_factory.initialize(m_domain);
// initialize milli second resolution level timer
// initialize milli-second resolution level timer
MilliSecondTimer.initialize();
}
......
......@@ -3,8 +3,6 @@ package com.dianping.cat.message.internal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import com.dianping.cat.configuration.model.entity.Config;
public class MessageIdFactory {
private long m_lastTimestamp = getTimestamp();
......@@ -44,12 +42,8 @@ public class MessageIdFactory {
return MilliSecondTimer.currentTimeMillis();
}
public void initialize(Config clientConfig) {
try {
m_domain = clientConfig.getApp().getDomain();
} catch (Exception e) {
// ignore it
}
public void initialize(String domain) {
m_domain = domain;
if (m_ipAddress == null) {
try {
......
package com.dianping.cat.message.io;
import java.net.InetSocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
......@@ -39,11 +42,13 @@ public class TcpSocketReceiver implements MessageReceiver, LogEnabled {
@Inject
private MessageCodec m_codec;
private BlockingQueue<ChannelBuffer> m_queue;
private ChannelFactory m_factory;
private ChannelGroup m_channelGroup = new DefaultChannelGroup();
private MessageHandler m_messageHandler;
private boolean m_active = true;
private Logger m_logger;
......@@ -62,6 +67,8 @@ public class TcpSocketReceiver implements MessageReceiver, LogEnabled {
address = new InetSocketAddress(m_host, m_port);
}
m_queue = new LinkedBlockingQueue<ChannelBuffer>();
ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ServerBootstrap bootstrap = new ServerBootstrap(factory);
......@@ -80,9 +87,35 @@ public class TcpSocketReceiver implements MessageReceiver, LogEnabled {
m_factory = factory;
}
public boolean isActive() {
synchronized (this) {
return m_active;
}
}
@Override
public void onMessage(MessageHandler handler) {
m_messageHandler = handler;
try {
while (true) {
ChannelBuffer buf = m_queue.poll(1, TimeUnit.MILLISECONDS);
if (buf != null) {
MessageTree tree = new DefaultMessageTree();
m_codec.decode(buf, tree);
handler.handle(tree);
} else if (!isActive()) {
break;
}
}
} catch (InterruptedException e) {
// ignore it
}
ChannelGroupFuture future = m_channelGroup.close();
future.awaitUninterruptibly();
m_factory.releaseExternalResources();
}
public void setCodec(MessageCodec codec) {
......@@ -99,10 +132,7 @@ public class TcpSocketReceiver implements MessageReceiver, LogEnabled {
@Override
public void shutdown() {
ChannelGroupFuture future = m_channelGroup.close();
future.awaitUninterruptibly();
m_factory.releaseExternalResources();
m_active = false;
}
public static class MyDecoder extends FrameDecoder {
......@@ -146,10 +176,8 @@ public class TcpSocketReceiver implements MessageReceiver, LogEnabled {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) {
ChannelBuffer buf = (ChannelBuffer) event.getMessage();
MessageTree tree = new DefaultMessageTree();
m_codec.decode(buf, tree);
m_messageHandler.handle(tree);
m_queue.offer(buf);
}
}
}
package com.dianping.cat.message.spi;
import java.io.File;
import java.net.URL;
import java.util.Date;
public interface MessagePathBuilder {
......@@ -9,11 +8,9 @@ public interface MessagePathBuilder {
public File getLogViewBaseDir();
public URL getLogViewBaseUrl();
public String getLogViewPath(String messageId);
public String getMessagePath(String domain, Date timestamp);
public String getReportPath(Date timestamp);
}
......@@ -2,6 +2,8 @@ package com.dianping.cat.message.spi.internal;
import java.util.List;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
......@@ -17,7 +19,8 @@ import com.dianping.cat.message.spi.MessageTree;
import com.site.lookup.ContainerHolder;
import com.site.lookup.annotation.Inject;
public class DefaultMessageHandler extends ContainerHolder implements MessageHandler, Initializable, Runnable {
public class DefaultMessageHandler extends ContainerHolder implements MessageHandler, Initializable, LogEnabled,
Runnable {
@Inject
private MessageManager m_manager;
......@@ -26,6 +29,13 @@ public class DefaultMessageHandler extends ContainerHolder implements MessageHan
private MessageReceiver m_receiver;
private Logger m_logger;
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
@Override
public void handle(MessageTree tree) {
List<MessageConsumer> consumers = m_registry.getConsumers();
......@@ -37,7 +47,7 @@ public class DefaultMessageHandler extends ContainerHolder implements MessageHan
try {
consumer.consume(tree);
} catch (Exception e) {
e.printStackTrace();
m_logger.error("Error when consuming message in " + consumer + "!", e);
}
}
}
......
......@@ -2,8 +2,6 @@ package com.dianping.cat.message.spi.internal;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Date;
......@@ -24,8 +22,6 @@ public class DefaultMessagePathBuilder implements MessagePathBuilder, Initializa
private File m_baseLogDir;
private URL m_baseLogUrl;
private Logger m_logger;
@Override
......@@ -55,11 +51,6 @@ public class DefaultMessagePathBuilder implements MessagePathBuilder, Initializa
return m_baseLogDir;
}
@Override
public URL getLogViewBaseUrl() {
return m_baseLogUrl;
}
@Override
public String getLogViewPath(String messageId) {
return messageId + "/logview.html";
......@@ -88,7 +79,6 @@ public class DefaultMessagePathBuilder implements MessagePathBuilder, Initializa
}
String baseLogDir = config.getBaseLogDir();
String baseLogUrl = config.getBaseLogUrl();
try {
m_baseLogDir = new File(baseLogDir).getCanonicalFile();
......@@ -96,23 +86,9 @@ public class DefaultMessagePathBuilder implements MessagePathBuilder, Initializa
} catch (IOException e) {
throw new InitializationException(String.format("Unable to create log directory(%s)!", m_baseLogDir), e);
}
try {
if (baseLogUrl == null) {
m_baseLogUrl = m_baseLogDir.toURI().toURL();
} else {
m_baseLogUrl = new URL(baseLogUrl);
}
} catch (MalformedURLException e) {
throw new InitializationException("Unable to build base log URL!", e);
}
}
public void setBaseLogDir(File baseLogDir) {
m_baseLogDir = baseLogDir;
}
public void setBaseLogUrl(URL baseLogUrl) {
m_baseLogUrl = baseLogUrl;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<model>
<entity name="config" root="true">
<attribute name="mode" value-type="String" />
<attribute name="base-log-dir" value-type="String" />
<attribute name="base-log-url" value-type="String" />
<entity-ref name="app" />
<entity-ref name="server" type="list" names="servers" xml-indent="true" />
<entity-ref name="bind" />
<entity-ref name="filter" />
<entity-ref name="property" type="list" names="properties" xml-indent="true" />
</entity>
<entity name="app">
<attribute name="domain" value-type="String" />
<attribute name="ip" value-type="String" />
</entity>
<entity name="server">
<attribute name="ip" value-type="String" />
<attribute name="port" value-type="int" />
<attribute name="enabled" value-type="boolean" />
</entity>
<entity name="bind">
<attribute name="ip" value-type="String" />
<attribute name="port" value-type="int" />
</entity>
<entity name="filter">
<element name="domain" value-type="String" type="list" names="domains" />
</entity>
<entity name="property">
<attribute name="name" value-type="String" key="true"/>
<attribute name="value" value-type="String" text="true"/>
</entity>
</model>
<?xml version="1.0" encoding="UTF-8"?>
<model>
<entity name="config" root="true">
<attribute name="mode" value-type="String" />
<entity-ref name="server" type="list" names="servers" xml-indent="true" />
<entity-ref name="domain" type="list" names="domains" />
<entity-ref name="bind" />
<entity-ref name="property" type="list" names="properties" xml-indent="true" />
</entity>
<entity name="server">
<attribute name="ip" value-type="String" />
<attribute name="port" value-type="int" />
<attribute name="enabled" value-type="boolean" />
</entity>
<entity name="domain">
<attribute name="id" value-type="String" />
<attribute name="ip" value-type="String" />
<attribute name="enabled" value-type="boolean" />
</entity>
<entity name="bind">
<attribute name="ip" value-type="String" />
<attribute name="port" value-type="int" />
</entity>
<entity name="property">
<attribute name="name" value-type="String" />
<element name="text" value-type="String" text="true" />
</entity>
</model>
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<file path="codegen.xml" />
<file path="model.xml" />
<file path="config-codegen.xml" />
<file path="config-model.xml" />
</manifest>
<?xml version="1.0" encoding="UTF-8"?>
<model model-package="com.dianping.cat.configuration.model" enable-xml-parser="true" enable-validator="true"
enable-xml-schema="true" enable-xml-sample="true">
<entity name="config" root="true">
enable-xml-schema="true" enable-merger="true">
<entity name="config" root="true" dynamic-attributes="true">
<attribute name="mode" required="true" />
<attribute name="base-log-dir" default-value="target/catlog" />
<entity-ref name="property" type="map" />
</entity>
<entity name="app">
<attribute name="domain" required="true" />
<element name="base-log-dir" value-type="String" default-value="target/catlog" />
<entity-ref name="domain" type="map" names="domains" />
<entity-ref name="property" type="map" names="properties" />
</entity>
<entity name="server">
<attribute name="port" default-value="2280" />
<attribute name="enabled" default-value="true" />
<attribute name="ip" value-type="String" key="true" />
</entity>
<entity name="domain">
<attribute name="id" value-type="String" key="true" />
</entity>
<entity name="bind">
<attribute name="port" default-value="2280" />
<attribute name="port" primitive="true" default-value="2280" />
</entity>
<entity name="property">
<attribute name="name" value-type="String" key="true" />
</entity>
</model>
......@@ -3,7 +3,7 @@
<xs:element name="config" type="ConfigType"/>
<xs:complexType name="ConfigType">
<xs:sequence>
<xs:element name="app" type="AppType" minOccurs="0" maxOccurs="1"/>
<xs:element name="base-log-dir" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="servers">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
......@@ -11,8 +11,8 @@
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="domain" type="DomainType"/>
<xs:element name="bind" type="BindType" minOccurs="0" maxOccurs="1"/>
<xs:element name="filter" type="FilterType" minOccurs="0" maxOccurs="1"/>
<xs:element name="properties">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
......@@ -22,22 +22,21 @@
</xs:element>
</xs:sequence>
<xs:attribute name="mode" type="xs:string" use="required"/>
<xs:attribute name="base-log-dir" type="xs:string" default="target/catlog"/>
<xs:attribute name="base-log-url" type="xs:string"/>
</xs:complexType>
<xs:complexType name="AppType">
<xs:complexType name="ServerType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="domain" type="xs:string" use="required"/>
<xs:attribute name="ip" type="xs:string"/>
<xs:attribute name="port" type="xs:int" default="2280"/>
<xs:attribute name="enabled" type="xs:boolean" default="true"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ServerType">
<xs:complexType name="DomainType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="ip" type="xs:string"/>
<xs:attribute name="port" type="xs:int" default="2280"/>
<xs:attribute name="enabled" type="xs:boolean" default="true"/>
</xs:extension>
</xs:simpleContent>
......@@ -50,11 +49,6 @@
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="FilterType">
<xs:sequence>
<xs:element name="domain" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PropertyType">
<xs:simpleContent>
<xs:extension base="xs:string">
......
......@@ -9,8 +9,8 @@ import org.junit.After;
import org.junit.Before;
import com.dianping.cat.Cat;
import com.dianping.cat.configuration.model.entity.App;
import com.dianping.cat.configuration.model.entity.Config;
import com.dianping.cat.configuration.model.entity.Domain;
import com.dianping.cat.configuration.model.entity.Server;
import com.site.helper.Files;
import com.site.lookup.ComponentTestCase;
......@@ -28,8 +28,8 @@ public abstract class CatTestCase extends ComponentTestCase {
Config config = new Config();
config.setMode("client");
config.setApp(new App().setDomain("Test"));
config.addServer(new Server().setIp("localhost").setPort(2280));
config.addDomain(new Domain("Cat"));
config.addServer(new Server("localhost").setPort(2280));
File file = new File("target/cat-config.xml");
......
package com.dianping.cat.message.configuration;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import junit.framework.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.xml.sax.SAXException;
import com.dianping.cat.configuration.ClientConfigMerger;
import com.dianping.cat.configuration.ClientConfigValidator;
import com.dianping.cat.configuration.model.IEntity;
import com.dianping.cat.configuration.model.entity.Config;
import com.dianping.cat.configuration.model.entity.Server;
import com.dianping.cat.configuration.model.transform.DefaultXmlBuilder;
import com.dianping.cat.configuration.model.transform.DefaultXmlParser;
import com.site.helper.Files;
public class ConfigTest {
@Test
public void testClient() throws Exception {
InputStream in = getClass().getResourceAsStream("client.xml");
String xml = Files.forIO().readFrom(in, "utf-8");
Config config = new DefaultXmlParser().parse(xml);
Config clientConfig = loadConfig("client-config.xml");
Config globalConfig = loadConfig("global-config.xml");
Assert.assertEquals("client", clientConfig.getMode());
globalConfig.accept(new ClientConfigMerger(clientConfig));
clientConfig.accept(new ClientConfigValidator());
Assert.assertEquals("client", config.getMode());
Assert.assertEquals("Review", config.getApp().getDomain());
Assert.assertEquals("192.168.8.1", config.getApp().getIp());
// System.out.println(clientConfig);
List<Server> servers = clientConfig.getServers();
List<Server> servers = config.getServers();
Assert.assertEquals(3, servers.size());
Assert.assertEquals(2280, servers.get(0).getPort().intValue());
Assert.assertEquals(true, servers.get(0).isEnabled());
......@@ -34,6 +48,44 @@ public class ConfigTest {
Assert.assertEquals(true, servers.get(2).isEnabled());
}
private Config loadConfig(String configXml) throws IOException, SAXException {
InputStream in = getClass().getResourceAsStream(configXml);
String xml = Files.forIO().readFrom(in, "utf-8");
Config clientConfig = new DefaultXmlParser().parse(xml);
return clientConfig;
}
@Test
public void testConfig() throws Exception {
DefaultXmlParser parser = new DefaultXmlParser();
String source = Files.forIO().readFrom(getClass().getResourceAsStream("config.xml"), "utf-8");
Config root = parser.parse(source);
String xml = new DefaultXmlBuilder().buildXml(root);
String expected = source;
Assert.assertEquals("XML is not well parsed!", expected.replace("\r", ""), xml.replace("\r", ""));
}
@Test
@Ignore
public void testSchema() throws Exception {
// define the type of schema - we use W3C:
String schemaLang = "http://www.w3.org/2001/XMLSchema";
// get validation driver:
SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
// create schema by reading it from an XSD file:
String path = "/" + IEntity.class.getPackage().getName().replace('.', '/');
Schema schema = factory.newSchema(new StreamSource(getClass().getResourceAsStream(path + "/config.xsd")));
Validator validator = schema.newValidator();
// at last perform validation:
validator.validate(new StreamSource(getClass().getResourceAsStream("client.xml")));
validator.validate(new StreamSource(getClass().getResourceAsStream("server.xml")));
validator.validate(new StreamSource(getClass().getResourceAsStream("config.xml")));
}
@Test
public void testServer() throws Exception {
InputStream in = getClass().getResourceAsStream("server.xml");
......@@ -42,8 +94,6 @@ public class ConfigTest {
Assert.assertEquals("server", config.getMode());
Assert.assertEquals("192.168.8.21", config.getBind().getIp());
Assert.assertEquals(2280, config.getBind().getPort().intValue());
Assert.assertEquals("[Review, Group]", config.getFilter().getDomains().toString());
Assert.assertEquals(2280, config.getBind().getPort());
}
}
......@@ -4,8 +4,6 @@ import junit.framework.Assert;
import org.junit.Test;
import com.dianping.cat.configuration.model.entity.Config;
public class MessageIdFactoryTest {
private long m_timestamp = 1330327814748L;
......@@ -35,7 +33,7 @@ public class MessageIdFactoryTest {
@Test
public void testNextId() throws Exception {
m_factory.initialize(new Config());
m_factory.initialize("test");
check("domain1", "domain1-c0a83f99-1330327814748-0");
check("domain1", "domain1-c0a83f99-1330327814748-1");
......
<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="config.xsd" mode="">
<app domain=""/>
<servers>
<server/>
<!-- more server -->
</servers>
<bind/>
<filter>
<domain>{domain}</domain>
<!-- more domain -->
</filter>
<properties>
<property name=""/>
<!-- more property -->
</properties>
</config>
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<domain id="Group" />
</config>
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<servers>
<server ip="192.168.8.21" port="2280" enabled="true"/>
<server ip="192.168.8.22" port="2281" enabled="false"/>
<server ip="192.168.8.23" port="2280" enabled="true"/>
</servers>
<domain id="Group" enabled="true"/>
<domain id="Review" ip="192.168.8.1" enabled="false"/>
<bind ip="192.168.8.21" port="2280"/>
<properties>
<property name="failure-base-dir">/data/appdatas/cat/report/failure/</property>
<property name="transaction-base-dir">/data/appdatas/cat/report/transaction/</property>
</properties>
<base-log-dir>target/catlog</base-log-dir>
</config>
......@@ -3,7 +3,7 @@
<xs:element name="config" type="ConfigType"/>
<xs:complexType name="ConfigType">
<xs:sequence>
<xs:element name="app" type="AppType" minOccurs="0" maxOccurs="1"/>
<xs:element name="base-log-dir" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="servers">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
......@@ -11,27 +11,49 @@
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="domain" type="DomainType"/>
<xs:element name="bind" type="BindType" minOccurs="0" maxOccurs="1"/>
<xs:element name="filter" type="FilterType" minOccurs="0" maxOccurs="1"/>
<xs:element name="properties">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="property" type="PropertyType"/>
</xs:sequence>
<xs:attribute name="mode" type="xs:string"/>
</xs:complexType>
<xs:complexType name="AppType">
<xs:attribute name="domain" type="xs:string"/>
<xs:attribute name="ip" type="xs:string"/>
</xs:element>
</xs:sequence>
<xs:attribute name="mode" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="ServerType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ip" type="xs:string"/>
<xs:attribute name="port" type="xs:string"/>
<xs:attribute name="enabled" type="xs:boolean" default="false"/>
<xs:attribute name="port" type="xs:int" default="2280"/>
<xs:attribute name="enabled" type="xs:boolean" default="true"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="DomainType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:string"/>
<xs:attribute name="ip" type="xs:string"/>
<xs:attribute name="enabled" type="xs:boolean"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="BindType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ip" type="xs:string"/>
<xs:attribute name="port" type="xs:string"/>
<xs:attribute name="port" type="xs:string" default="2280"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="FilterType">
<xs:sequence>
<xs:element name="domain" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:complexType name="PropertyType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<app domain="Review" ip="192.168.8.1" />
<servers>
<server ip="192.168.8.21" port="2280" />
<server ip="192.168.8.22" port="2281" enabled="false" />
<server ip="192.168.8.23" />
</servers>
</config>
\ No newline at end of file
<domain id="Group" enabled="false"/>
<domain id="Review" ip="192.168.8.1"/>
</config>
<config mode="server" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<bind ip="192.168.8.21" />
<filter>
<domain>Review</domain>
<domain>Group</domain>
</filter>
<properties>
<property name="failure-base-dir">/data/appdatas/cat/report/failure/</property>
<property name="transaction-base-dir">/data/appdatas/cat/report/transaction/</property>
</properties>
</config>
\ No newline at end of file
</config>
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<app domain="Cat" />
<servers>
<server ip="127.0.0.1" port="2280" />
</servers>
</config>
\ No newline at end of file
<domain id="Cat"/>
</config>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="config" type="ConfigType"/>
<xs:complexType name="ConfigType">
<xs:sequence>
<xs:element name="app" type="AppType" minOccurs="0" maxOccurs="1"/>
<xs:element name="servers">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="server" type="ServerType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="bind" type="BindType" minOccurs="0" maxOccurs="1"/>
<xs:element name="filter" type="FilterType" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="mode" type="xs:string"/>
</xs:complexType>
<xs:complexType name="AppType">
<xs:attribute name="domain" type="xs:string"/>
<xs:attribute name="ip" type="xs:string"/>
</xs:complexType>
<xs:complexType name="ServerType">
<xs:attribute name="ip" type="xs:string"/>
<xs:attribute name="port" type="xs:string"/>
<xs:attribute name="enabled" type="xs:boolean" default="false"/>
</xs:complexType>
<xs:complexType name="BindType">
<xs:attribute name="ip" type="xs:string"/>
<xs:attribute name="port" type="xs:string"/>
</xs:complexType>
<xs:complexType name="FilterType">
<xs:sequence>
<xs:element name="domain" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="config" type="ConfigType" />
<xs:complexType name="ConfigType">
<xs:sequence>
<xs:element name="base-log-dir" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="servers">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="server" type="ServerType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="domain" type="DomainType" />
<xs:element name="bind" type="BindType" minOccurs="0" maxOccurs="1" />
<xs:element name="properties">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="property" type="PropertyType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="mode" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="ServerType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ip" type="xs:string" />
<xs:attribute name="port" type="xs:int" default="2280" />
<xs:attribute name="enabled" type="xs:boolean" default="true" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="DomainType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="ip" type="xs:string" />
<xs:attribute name="enabled" type="xs:boolean" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="BindType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ip" type="xs:string" />
<xs:attribute name="port" type="xs:string" default="2280" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="PropertyType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
......@@ -40,7 +40,6 @@ public class BucketTest extends ComponentTestCase {
for (int i = 0; i < 100; i++) {
String id = "id" + i;
String t1 = "value" + i;
String tag = "tag" + (i % 10);
String t2 = new String(bucket.findById(id));
Assert.assertEquals("Unable to find data after stored it.", t1, t2);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册