8000 add extension and extension point support by straybirdzls · Pull Request #340 · sofastack/sofa-boot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add extension and extension point support #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ SOFA 的第一个版本是阿玺创造的,感谢阿玺给 SOFA 打下了坚实
## 七、文档

请参考 [SOFABoot 官方文档](http://www.sofastack.tech/sofa-boot/docs/Home)。

## 八、开源许可

SOFABoot 基于 Apache License 2.0 协议,SOFABoot 依赖了一些三方组件,它们的开源协议参见 [依赖组件版权说明](https://www.sofastack.tech/sofa-boot/docs/NOTICE)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<xsd:schema xmlns="http://sofastack.io/schema/sofaboot"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://sofastack.io/schema/sofaboot"
elementFormDefault="qualified" attributeFormDefault="unqualified">

Expand All @@ -30,4 +31,52 @@
<!-- http://itdoc.hitachi.co.jp/manuals/3020/30203Y4340e/EY430034.HTM -->
<xsd:include schemaLocation="./rpc.xsd"/>

<!-- extension-point -->
<xsd:element name="extension-point" type="Textension-point"/>

<xsd:complexType name="Textension-point">
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="object" type="Tobject" minOccurs="0"/>
<!-- nested bean declaration -->
<xsd:any namespace="##other" minOccurs="0" maxOccurs="1" processContents="skip"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="ref" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="Tobject">
<xsd:attribute name="class" type="xsd:string" use="required"/>
</xsd:complexType>

<xsd:element name="extension" type="Textension"/>

<xsd:complexType name="Textension">
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="require" type="Trequire" minOccurs="0"/>
<xsd:element name="content" type="Tcontent" minOccurs="0"
maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="bean" type="xsd:string" use="required"/>
<xsd:attribute name="point" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="Trequire">
<xsd:attribute name="bean" type="xsd:string" use="required"/>
</xsd:complexType>

<xsd:complexType name="Tcontent">
<xsd:sequence>
<xsd:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
</xsd:sequence>
</xsd:complexType>


</xsd:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 com.alipay.sofa.common.xmap;

import java.net.URL;
import java.util.ArrayList;

/**
* @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
* @author xi.hux@alipay.com
* @since 2.6.0
*/
public class Context extends ArrayList<Object> {

private static final long serialVersionUID = 1L;

public Class loadClass(String className) throws ClassNotFoundException {
return Thread.currentThread().getContextClassLoader().loadClass(className);
}

public URL getResource(String name) {
return Thread.currentThread().getContextClassLoader().getResource(name);
}

public Object getObject() {
int size = size();
if (size > 0) {
return get(size - 1);
}
return null;
}

public Object getParent() {
int size = size();
if (size > 1) {
return get(size - 2);
}
return null;
}

public void push(Object object) {
add(object);
}

public Object pop() {
int size = size();
if (size > 0) {
return remove(size - 1);
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* 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 com.alipay.sofa.common.xmap;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.Collection;
import java.util.Map;

/**
* @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
* @author xi.hux@alipay.com
* @since 2.6.0
*/
public final class DOMHelper {

private DOMHelper() {
}

/**
* Gets the value of the node at the given path
* relative to the given base element.
* For element nodes the value is the text content and for
* the attributes node the attribute value.
*
* @param base base element
* @param path path
* @return the node value or null if no such node was found
*/
public static String getNodeValue(Element base, Path path) {
Node node = getElementNode(base, path);
if (node != null) {
if (path.attribute != null) {
Node at = node.getAttributes().getNamedItem(path.attribute);
return at != null ? at.getNodeValue() : null;
} else {
return node.getTextContent();
}
}
return null;
}

public static void visitNodes(Context ctx, XAnnotatedList xam, Element base, Path path,
NodeVisitor visitor, Collection<Object> result) {
Node el = base;
int len = path.segments.length - 1;
for (int i = 0; i < len; i++) {
el = getElementNode(el, path.segments[i]);
if (el == null) {
return;
}
}
String name = path.segments[len];

if (path.attribute != null) {
visitAttributes(ctx, xam, el, name, path.attribute, visitor, result);
} else {
visitElements(ctx, xam, el, name, visitor, result);
}
}

public static void visitAttributes(Context ctx, XAnnotatedList xam, Node base, String name,
String attrName, NodeVisitor visitor,
Collection<Object> result) {
Node p = base.getFirstChild();
while (p != null) {
if (p.getNodeType() == Node.ELEMENT_NODE) {
if (name.equals(p.getNodeName())) {
Node at = p.getAttributes().getNamedItem(attrName);
if (at != null) {
visitor.visitNode(ctx, xam, at, result);
}
}
}
p = p.getNextSibling();
}
}

public static void visitElements(Context ctx, XAnnotatedList xam, Node base, String name,
NodeVisitor visitor, Collection<Object> result) {
Node p = base.getFirstChild();
while (p != null) {
if (p.getNodeType() == Node.ELEMENT_NODE) {
if (name.equals(p.getNodeName())) {
visitor.visitNode(ctx, xam, p, result);
}
}
p = p.getNextSibling();
}
}

public static void visitMapNodes(Context ctx, XAnnotatedMap xam, Element base, Path path,
NodeMapVisitor visitor, Map<String, Object> result) {
Node el = base;
int len = path.segments.length - 1;
for (int i = 0; i < len; i++) {
el = getElementNode(el, path.segments[i]);
if (el == null) {
return;
}
}
String name = path.segments[len];

if (path.attribute != null) {
visitMapAttributes(ctx, xam, el, name, path.attribute, visitor, result);
} else {
visitMapElements(ctx, xam, el, name, visitor, result);
}
}

public static void visitMapAttributes(Context ctx, XAnnotatedMap xam, Node base, String name,
String attrName, NodeMapVisitor visitor,
Map<String, Object> result) {
Node p = base.getFirstChild();
while (p != null) {
if (p.getNodeType() == Node.ELEMENT_NODE) {
if (name.equals(p.getNodeName())) {
Node at = p.getAttributes().getNamedItem(attrName);
if (at != null) {
String key = getNodeValue((Element) p, xam.key);
if (key != null) {
visitor.visitNode(ctx, xam, at, key, result);
}
}
}
}
p = p.getNextSibling();
}
}

public static void visitMapElements(Context ctx, XAnnotatedMap xam, Node base, String name,
NodeMapVisitor visitor, Map<String, Object> result) {
Node p = base.getFirstChild();
while (p != null) {
if (p.getNodeType() == Node.ELEMENT_NODE) {
if (name.equals(p.getNodeName())) {
String key = getNodeValue((Element) p, xam.key);
if (key != null) {
visitor.visitNode(ctx, xam, p, key, result);
}
}
}
p = p.getNextSibling();
}
}

public static Node getElementNode(Node base, String name) {
Node node = base.getFirstChild();
while (node != null) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (name.equals(node.getNodeName())) {
return node;
}
}
node = node.getNextSibling();
}
return null;
}

public static Node getElementNode(Node base, Path path) {
Node el = base;
int len = path.segments.length;
for (int i = 0; i < len; i++) {
el = getElementNode(el, path.segments[i]);
if (el == null) {
return null;
}
}
return el;
}

public abstract static class NodeVisitor {

public abstract void visitNode(Context ctx, XAnnotatedMember xam, Node node,
Collection<Object> result);

}

public abstract static class NodeMapVisitor {

public abstract void visitNode(Context ctx, XAnnotatedMember xam, Node node, String key,
Map<String, Object> result);

}

}
Loading
0