-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加<setBuffering enabled="true|false" /> valve,可以在pipeline中开关buffering…
…,以简化screen代码。
- Loading branch information
Michael Zhou
committed
Nov 1, 2012
1 parent
1e38748
commit f19d8a4
Showing
6 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
webx/turbine/src/main/java/com/alibaba/citrus/turbine/pipeline/valve/SetBufferingValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2002-2012 Alibaba Group Holding Limited. | ||
* All rights reserved. | ||
* | ||
* Licensed 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.alibaba.citrus.turbine.pipeline.valve; | ||
|
||
import static com.alibaba.citrus.springext.util.SpringExtUtil.*; | ||
|
||
import com.alibaba.citrus.service.pipeline.PipelineContext; | ||
import com.alibaba.citrus.service.pipeline.support.AbstractValve; | ||
import com.alibaba.citrus.service.pipeline.support.AbstractValveDefinitionParser; | ||
import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.xml.ParserContext; | ||
import org.w3c.dom.Element; | ||
|
||
/** | ||
* 开关buffering。 | ||
* | ||
* @author Michael Zhou | ||
*/ | ||
public class SetBufferingValve extends AbstractValve { | ||
private Logger log = LoggerFactory.getLogger(getClass()); | ||
private boolean enabled; | ||
|
||
@Autowired | ||
private BufferedRequestContext brc; | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
@Override | ||
public void invoke(PipelineContext pipelineContext) throws Exception { | ||
try { | ||
brc.setBuffering(enabled); | ||
} catch (IllegalStateException e) { | ||
log.warn("Unable to setBuffering({}): {}", enabled, e.getMessage()); | ||
} | ||
} | ||
|
||
public static class DefinitionParser extends AbstractValveDefinitionParser<SetBufferingValve> { | ||
@Override | ||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { | ||
attributesToProperties(element, builder, "enabled"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
webx/turbine/src/main/resources/META-INF/services/pipeline/valves/setBuffering.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" | ||
xmlns:springext="http://www.alibaba.com/schema/springext/base" elementFormDefault="qualified"> | ||
|
||
<xsd:import namespace="http://www.springframework.org/schema/beans" | ||
schemaLocation="http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd" /> | ||
|
||
<xsd:import namespace="http://www.alibaba.com/schema/springext/base" | ||
schemaLocation="http://localhost:8080/schema/www.alibaba.com/schema/springext/springext-base.xsd" /> | ||
|
||
<xsd:element name="setBuffering" type="SetBufferingValveType"> | ||
<xsd:annotation> | ||
<xsd:documentation><![CDATA[ | ||
开关buffering。 | ||
]]></xsd:documentation> | ||
</xsd:annotation> | ||
</xsd:element> | ||
|
||
<xsd:complexType name="SetBufferingValveType"> | ||
<xsd:attribute name="enabled" type="springext:booleanOrPlaceholder" default="true" /> | ||
</xsd:complexType> | ||
|
||
</xsd:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...rbine/src/test/java/com/alibaba/citrus/turbine/pipeline/valve/SetBufferingValveTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2002-2012 Alibaba Group Holding Limited. | ||
* All rights reserved. | ||
* | ||
* Licensed 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.alibaba.citrus.turbine.pipeline.valve; | ||
|
||
import static com.alibaba.citrus.service.requestcontext.util.RequestContextUtil.*; | ||
import static org.junit.Assert.*; | ||
|
||
import com.alibaba.citrus.service.pipeline.impl.PipelineImpl; | ||
import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext; | ||
import org.junit.Test; | ||
|
||
public class SetBufferingValveTests extends AbstractValveTests { | ||
private BufferedRequestContext brc; | ||
|
||
private void initPipeline(String id) { | ||
pipeline = (PipelineImpl) factory.getBean(id); | ||
assertNotNull(pipeline); | ||
} | ||
|
||
@Test | ||
public void setBuffering_default() throws Exception { | ||
assertBufferingEnabled("setBuffering_default"); | ||
} | ||
|
||
@Test | ||
public void setBuffering_enabled() throws Exception { | ||
assertBufferingEnabled("setBuffering_enabled"); | ||
} | ||
|
||
private void assertBufferingEnabled(String id) throws Exception { | ||
initPipeline(id); | ||
|
||
getInvocationContext("http://localhost/app1/myscreen"); | ||
initRequestContext(); | ||
|
||
brc = findRequestContext(newRequest, BufferedRequestContext.class); | ||
brc.setBuffering(false); | ||
assertEquals(false, brc.isBuffering()); | ||
|
||
pipeline.newInvocation().invoke(); | ||
assertEquals(true, brc.isBuffering()); | ||
} | ||
|
||
@Test | ||
public void setBuffering_disabled() throws Exception { | ||
initPipeline("setBuffering_disabled"); | ||
|
||
getInvocationContext("http://localhost/app1/myscreen"); | ||
initRequestContext(); | ||
|
||
brc = findRequestContext(newRequest, BufferedRequestContext.class); | ||
assertEquals(true, brc.isBuffering()); | ||
|
||
pipeline.newInvocation().invoke(); | ||
assertEquals(false, brc.isBuffering()); | ||
} | ||
|
||
@Test | ||
public void setBuffering_illegalState() throws Exception { | ||
initPipeline("setBuffering_disabled"); | ||
|
||
getInvocationContext("http://localhost/app1/myscreen"); | ||
initRequestContext(); | ||
|
||
brc = findRequestContext(newRequest, BufferedRequestContext.class); | ||
assertEquals(true, brc.isBuffering()); | ||
|
||
newResponse.getWriter(); | ||
|
||
pipeline.newInvocation().invoke(); | ||
assertEquals(true, brc.isBuffering()); // unchanged | ||
} | ||
} |