| Commit in servicemix/base/src on MAIN | |||
| main/java/org/servicemix/_expression_/ExpressionHelper.java | +50 | added 1.1 | |
| main/java/org/servicemix/components/rss/FeedWriter.java | +43 | -23 | 1.2 -> 1.3 |
| test/resources/org/servicemix/components/rss/example.xml | +25 | -7 | 1.1 -> 1.2 |
| +118 | -30 | ||
added expressions to the configuration to make it easier to make nicely formatted RSS feeds from any old messages using either script expressions or XPath
servicemix/base/src/main/java/org/servicemix/_expression_
ExpressionHelper.java added at 1.1
diff -N ExpressionHelper.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ExpressionHelper.java 23 Sep 2005 16:35:41 -0000 1.1 @@ -0,0 +1,50 @@
+/**
+ *
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ *
+ * 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 org.servicemix._expression_;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+
+/**
+ * A helper class for working with expressions.
+ *
+ * @version $Revision$
+ */
+public class ExpressionHelper {
+
+ /**
+ * Evaluates the given _expression_ as a string value.
+ *
+ * @param _expression_ the _expression_ to evaluate
+ * @param exchange the current exchange
+ * @param message the current message
+ * @param defaultValue the default value to use if the _expression_ is null or the value of the _expression_ is null
+ * @return the value of the _expression_ as a string if it is not null or the defaultValue
+ * @throws MessagingException if the _expression_ failed to be evaluated
+ */
+ public static String asString(_expression_ _expression_, MessageExchange exchange, NormalizedMessage message, String defaultValue) throws MessagingException {
+ if (_expression_ != null) {
+ Object answer = _expression_.evaluate(exchange, message);
+ if (answer != null) {
+ return answer.toString();
+ }
+ }
+ return defaultValue;
+ }
+}
servicemix/base/src/main/java/org/servicemix/components/rss
diff -u -r1.2 -r1.3 --- FeedWriter.java 23 Sep 2005 02:40:41 -0000 1.2 +++ FeedWriter.java 23 Sep 2005 16:35:41 -0000 1.3 @@ -28,6 +28,9 @@
import com.sun.syndication.io.SyndFeedOutput; import com.sun.syndication.io.XmlReader;
+import org.servicemix._expression_.ConstantExpression; +import org.servicemix._expression_._expression_; +import org.servicemix._expression_.ExpressionHelper;
import org.servicemix.jbi.binding.OutBinding; import org.servicemix.jbi.jaxp.SourceTransformer;
@@ -54,9 +57,11 @@
private String feedType = "atom_0.3";
private SyndFeed cachedFeed;
- private String title = "ServiceMix Message Feed"; - private String link; - private String feedDescription = "This feed is autogenerated by ServiceMix";
+ private String title; + private String link = "http://servicemix.org/RSS"; + private _expression_ entryTitle; + private _expression_ entryValue; + private String feedDescription = "This feed is auto-generated by ServiceMix";
private int maximumEntryCount = 20;
private File feedFile;
private String contentType = "text/plain";
@@ -90,6 +95,17 @@
this.feedDescription = feedDescription;
}
+ public String getTitle() {
+ if (title == null) {
+ title = "ServiceMix feed for service: " + getService();
+ }
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
public String getLink() {
return link;
}
@@ -98,12 +114,20 @@
this.link = link;
}
- public String getTitle() {
- return title;
+ public _expression_ getEntryTitle() {
+ return entryTitle;
}
- public void setTitle(String title) {
- this.title = title;
+ public void setEntryTitle(_expression_ title) {
+ this.entryTitle = title;
+ }
+
+ public _expression_ getEntryValue() {
+ return entryValue;
+ }
+
+ public void setEntryValue(_expression_ entryValue) {
+ this.entryValue = entryValue;
}
public int getMaximumEntryCount() {
@@ -158,8 +182,7 @@
// enforce validation
File file = getFeedFile();
if (file.exists() && file.isDirectory()) {
- throw new IllegalArgumentException("Cannot generate the cachedFeed as the cachedFeed file is a directory: "
- + file);
+ throw new IllegalArgumentException("Cannot generate the cachedFeed as the cachedFeed file is a directory: " + file);
}
}
@@ -181,8 +204,7 @@
}
}
- protected void addMessageToFeed(SyndFeed feed, MessageExchange exchange, NormalizedMessage message)
- throws TransformerException {
+ protected void addMessageToFeed(SyndFeed feed, MessageExchange exchange, NormalizedMessage message) throws TransformerException, MessagingException {
List entries = feed.getEntries();
SyndEntry entry = createEntry(exchange, message);
SyndContent description = createEntryContent(exchange, message);
@@ -190,28 +212,27 @@
entries.add(entry);
}
- protected SyndEntry createEntry(MessageExchange exchange, NormalizedMessage message) {
+ protected SyndEntry createEntry(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
SyndEntry entry = new SyndEntryImpl();
- // TODO use expressions here...
- entry.setTitle("ServiceMix");
- entry.setLink("http://servicemix.org/RSS");
+ entry.setTitle(ExpressionHelper.asString(getEntryTitle(), exchange, message, "ServiceMix Feed")); + entry.setLink(getLink());
entry.setPublishedDate(new Date());
return entry;
}
- protected SyndContent createEntryContent(MessageExchange exchange, NormalizedMessage message)
- throws TransformerException {
+ protected SyndContent createEntryContent(MessageExchange exchange, NormalizedMessage message) throws TransformerException, MessagingException {
SyndContent description = new SyndContentImpl();
description.setType(contentType);
Source content = message.getContent();
// TODO use an _expression_ for the value?
- if (content != null) {
- String value = getSourceTransformer().toString(content);
-
+ String value = ExpressionHelper.asString(getEntryValue(), exchange, message, null);
+ if (value == null && content != null) {
+ value = getSourceTransformer().toString(content);
value = encodeContent(value);
-
+ }
+ if (value != null) {
description.setValue(value);
}
return description;
@@ -224,8 +245,7 @@
return "<![CDATA[" + value + "]]>";
}
- protected void writeFeed(SyndFeed feed, MessageExchange messageExchange, NormalizedMessage message)
- throws IOException, FeedException {
+ protected void writeFeed(SyndFeed feed, MessageExchange messageExchange, NormalizedMessage message) throws IOException, FeedException {
Writer writer = new FileWriter(feedFile);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, writer);
servicemix/base/src/test/resources/org/servicemix/components/rss
diff -u -r1.1 -r1.2 --- example.xml 23 Sep 2005 02:40:41 -0000 1.1 +++ example.xml 23 Sep 2005 16:35:41 -0000 1.2 @@ -5,19 +5,37 @@
<container id="jbi">
<components>
- <component id="feedWriter" service="foo:feedWriter" class="org.servicemix.components.rss.FeedWriter"> - <property name="feedFile" value="target/serviceMixFeed.xml"/> -
+ <component id="feedWriter" service="foo:feedWriter"
+ class="org.servicemix.components.rss.FeedWriter">
+ <property name="feedFile" value="target/serviceMixFeed.xml" />
+ <property name="entryTitle">
+ <bean
+ class="org.servicemix._expression_.JaxenStringXPathExpression">
+ <constructor-arg
+ value="concat('Message ', /sample/@id)" />
+ </bean>
+ </property>
+ <property name="entryValue">
+ <bean
+ class="org.servicemix._expression_.JaxenStringXPathExpression">
+ <constructor-arg
+ value="concat('This is an entry with ID ', /sample/@id, ' sent: ', /sample/@sent, '. There is not much else in the message to tell :)')" />
+ </bean>
+ </property>
+
+
<!-- TODO temporarily disable loading due to bug when running the test case for the 2nd time -->
- <property name="loadOnStartup" value="false"/>
+ <property name="loadOnStartup" value="false" />
</component>
- <component id="receiver" service="foo:receiver" class="org.servicemix.examples.ReceiverComponent"/>
+ <component id="receiver" service="foo:receiver" + class="org.servicemix.examples.ReceiverComponent" />
</components> </container>
- <bean id="client" class="org.servicemix.client.DefaultServiceMixClient"> - <constructor-arg ref="jbi"/>
+ <bean id="client" + class="org.servicemix.client.DefaultServiceMixClient"> + <constructor-arg ref="jbi" />
</bean> </beans>
