WSO2 just released a newer version of the popular web services framework for php (WSF/PHP). It has lot of bug fixes + performance improvement. And it now support PHP 5.3. So you can use wsf/php with the latest php version.
You can download the new release from http://wso2.org/downloads/wsf/php.
Wednesday, July 07, 2010
Thursday, June 03, 2010
WSO2 Stratos: WSO2 Brings The Whole SOA Stack to The Cloud
WSO2 announced that the SOA stack that they provided as downloadable packages are now available in the cloud as hosted instances with the code name WSO2 Stratos. You can try them out for free from https://cloud.wso2.com. You can register your organization for an account in the WSO2 Stratos by clicking the 'Register' button in the home page. You can find a detail guide on 'How to register for WSO2 Stratos' from Charitha's blog, http://charithaka.blogspot.com/2010/06/wso2-stratos-introducing-wso2.html.
At the registration, you will be asked to provide a username and password for the admin account. Use this credential to login as admin for the Stratos services and surf through the products. Here is a brief introduction on all the products currently available.
With this release WSO2 bring complete SOA stack to the cloud, Now your enterprise can enjoy the power of SOA without the hassle of maintaining your own SOA infrastructure.
At the registration, you will be asked to provide a username and password for the admin account. Use this credential to login as admin for the Stratos services and surf through the products. Here is a brief introduction on all the products currently available.
- Stratos Governance: Store and govern your services, wsdls, schemas, policies and other SOA artifacts
- Stratos Identity: Manage user bases, authentication mechanisms, permissions and all the identity aspects of your enterprise.
- Stratos Application Server: Host your web apps, web services and manage their QoS aspects like security, reliability.
- Stratos Gadgets Server: Write and host gadgets complaint with Google gadget standards.
- Stratos Mashup Server: Write mashup using scripting languages like javascript.
- Stratos Business Activity Monitor: Monitor activities of your services.
- Stratos Enterprise Service Bus: Coming soon with message routing, intermediate message transformations, task scheduling and many more features.
With this release WSO2 bring complete SOA stack to the cloud, Now your enterprise can enjoy the power of SOA without the hassle of maintaining your own SOA infrastructure.
Labels:
app server,
Cloud,
gadget server,
Governance Registry,
identity,
mashup server,
SOA,
stratos,
WSO2
Wednesday, May 19, 2010
PHP Web Services: Webinar From WSO2 And Zend
WSO2 and Zend jointly present a webinar titling "PHP Web Services: Why You Should Care" on 26th of May, 2010. The webinar will mainly focus on the following aspects,
• Understand Web services development best practices
• Discuss Web service myths and pitfalls
• Learn about prominent PHP Web services extensions
• Watch a demo of building Web Services with both the Zend Framework and WSO2 Web Services Framework for PHP.
You can find more information about the webinar and the link to registration here, http://wso2.org/library/webinars/2010/05/webinar-php-web-services-you-should-care.
• Understand Web services development best practices
• Discuss Web service myths and pitfalls
• Learn about prominent PHP Web services extensions
• Watch a demo of building Web Services with both the Zend Framework and WSO2 Web Services Framework for PHP.
You can find more information about the webinar and the link to registration here, http://wso2.org/library/webinars/2010/05/webinar-php-web-services-you-should-care.
Wednesday, May 05, 2010
Metering Java Reader and Writer Objects
Last few days I was working on measuring the bandwidth consumed by different java objects passed to our remote interfaces. It was simple task to do it with 'String' objects, byte arrays as we can directly get the sizes of them using String.length and byte[].size() methods.
But there were objects of type Reader and Writer, which is supposed to transfer big chunk of data. There also we could load all the data to memory and measure the sizes.
But that will consume lot of memory (even possible to exceed available heap size). So it is wrong to use the above method to measure the size of reader or writer.
Anyway there is an easy solution for the problem. We can use the design of Reader API itself to measure it size. The Reader interface has a method that read the data by small chunks. We just need to intercept that call and measure the size of each small chunk and add them all. For that we need to implement the Reader interface in to a custom class (say 'MonitoredReader'). Here is how it is implemented.
So our code to measure the size will simple reduce to the following piece of code.
Similarly we can use this method to get the data size of the writer. (amount of data written to the writer).
Here is how it is used in measuring the writer size.
Anyway like every good methods, there are drawbacks of using these methods to measure the data size on Reader and Writer objects.
If we take measuring the bandwidth consumed by a reader in a remote interface, this gives a slightly low value because this particular code only provide the size of the data read by the end user application and not by the network hardware layers. But actually these low layers read more data and keep it in a buffer which is not measured here. But if we assume that most of the time the end user application read all the data from the reader (and very rarely read portion of data and give up), this give nearly accurate value.
The other drawback could be the performance degradation by wrapping the reader/writer with our custom implementation. But mostly reader and writers are used in IO bound operations (like to read through network or files), so going through an another layer does really little effect to the overall performance. And after all the 'Observer effect theory' says we can't measure anything without causing any effect to the actual cause...
But there were objects of type Reader and Writer, which is supposed to transfer big chunk of data. There also we could load all the data to memory and measure the sizes.
// measuring reader size - memory inefficient method
// read all data to buffer and measure the length
StringReader stringReader = new StringReader(reader);
int size = stringReader.toString().length;
doRealWork(stringReader);
But that will consume lot of memory (even possible to exceed available heap size). So it is wrong to use the above method to measure the size of reader or writer.
Anyway there is an easy solution for the problem. We can use the design of Reader API itself to measure it size. The Reader interface has a method that read the data by small chunks. We just need to intercept that call and measure the size of each small chunk and add them all. For that we need to implement the Reader interface in to a custom class (say 'MonitoredReader'). Here is how it is implemented.
import java.io.IOException;
import java.io.Reader;
/**
* The class to intercept the read method and calculate
* the number of reads
*/
public class MonitoredReader extends Reader {
Reader reader;
int totalReadSize;
/**
* constructor that wraps the original reader object
*/
public MonitoredReader(Reader reader) {
this.reader = reader;
totalReadSize = 0;
}
/**
* The method to call by the user to read the data. We will just calculate the amount
* of data read here.
*
* @param cBuf destination buffer
* @param off offset at which to start storing characters
* @param len maximum number of characters to read
*
* @return the number of characters read, or -1 if the end of the stream has been reached
* @throws IOException if an I/O error occurs
*/
public int read(char cbuf[], int off, int len) throws IOException {
int read = reader.read(cbuf, off, len);
totalReadSize += read;
return read;
}
/**
* Method to call after finishing reading the data. We will just pass the call to the
* original reader
*/
public void close() throws IOException {
reader.close();
}
/**
* Custom method that will return the total size of read data
*
* @return the size of the data read
*/
public int getTotalReadSize() {
return totalReadSize;
}
}
So our code to measure the size will simple reduce to the following piece of code.
// measuring reader size - memory efficient method
// just wrap the original reader with our custom reader
MonitoredReader monitoredReader = new MonitoredReader(reader);
// pass our custom reader to the real work
doRealWork(monitoredReader);
// get the size read in the real work
int size = monitoredReader.getTotalReadSize();
Similarly we can use this method to get the data size of the writer. (amount of data written to the writer).
import java.io.IOException;
import java.io.Writer;
/**
* The class to intercept the write method and calculate
* the number of writes
*/
public class MonitoredWriter extends Writer {
Writer writer;
int totalWrittenSize;
/**
* constructor that wraps the original writer object
*/
public MonitoredWriter(Writer writer) {
this.writer = writer;
totalWrittenSize = 0;
}
/**
* The method to call by the user to write the data. We will just calculate the amount
* of data written here.
*
* @param cBuf Array of characters
* @param off Offset from which to start writing characters
* @param len Number of characters to write
*
* @throws java.io.IOException If an I/O error occurs
*/
public void write(char cbuf[], int off, int len) throws IOException {
totalWrittenSize += (len - off);
writer.write(cbuf, off, len);
}
/**
* Method to call after finishing writing the data. We will just pass the call to the
* original writer
*/
public void close() throws IOException {
writer.close();
}
/**
* flush already written data. Here also we just pass the call to the original writer
*/
public void flush() throws IOException {
writer.flush();
}
/**
* Custom method that will return the total size of written data
*
* @return the size of the data written
*/
public int getTotalWrittenSize() {
return totalWrittenSize;
}
}
Here is how it is used in measuring the writer size.
// measuring writer size - memory efficient method
// just wrap the original writer with our custom writer
MonitoredWriter monitoredWriter = new MonitoredWriter(writer);
// pass our custom wrter to the real work
doRealWork(monitoredWriter);
// get the size written in the real work
int size = monitoredWriter.getTotalWrittenSize();
Anyway like every good methods, there are drawbacks of using these methods to measure the data size on Reader and Writer objects.
If we take measuring the bandwidth consumed by a reader in a remote interface, this gives a slightly low value because this particular code only provide the size of the data read by the end user application and not by the network hardware layers. But actually these low layers read more data and keep it in a buffer which is not measured here. But if we assume that most of the time the end user application read all the data from the reader (and very rarely read portion of data and give up), this give nearly accurate value.
The other drawback could be the performance degradation by wrapping the reader/writer with our custom implementation. But mostly reader and writers are used in IO bound operations (like to read through network or files), so going through an another layer does really little effect to the overall performance. And after all the 'Observer effect theory' says we can't measure anything without causing any effect to the actual cause...
Monday, May 03, 2010
WSO2 Governance Registry, WSO2 Identity Server, WSO2 ESB, WSO2 Web Service Application Server Released
WSO2 has released new versions of their SOA platform products including WSO2 Governance Registry, WSO2 Identity Server, WSO2 ESB, WSO2 Web Service Application Server. This is a major version upgrade of these products introducing many features.
ESB:
Governance Registry
Web Service Application Server (WSO2 WSAS):
Identity Server:
ESB:
- Priority based mediation through priority executors
- WS-Discovery support and dynamic endpoint discovery
- Message Relay for efficient pass through of messages
- Component manager to install and uninstall features (provisioning support)
- Common Internet File System (CIFS) support through the VFS transport
- File locking functionality in the VFS transport to support concurrent polling
- Smooks mediator for efficient message transformation
- Enrich mediator for smart message manipulation
- OAuth mediator for 2-legged OAuth support
- Default endpoint UI
- Hot deploy and hot update configuration elements (sequences, endpoints, proxy services etc)
- Transport level statistics collection and monitoring
- POX security support
- Dependency detection and alerting for mediation configuration elements
- Mediation statistics API and custom mediation statistics consumers
- Multiple certificate/identity support in the NHTTP transport sender
- Improved logging capabilities for the NHTTP transport
- Templates based proxy service development in the UI
- Dashboard to monitor server environment and runtime
- Easy creation and management capabilities for dynamic sequences and endpoints
- Pagination to service management, endpoint management and sequence management UIs
- Obtaining resources like WSDL's through web proxy servers
Governance Registry
- Gadgets for impact analysis on services and registry resources
- WSDL custom view
- Dynamic Handler configuration
- Handler simulator
- Tree-based Resource view
- API to govern SOA artifacts
- Complete Web Services API for Registry
- Improved options for Service discovery
- WS-Discovery support
- Scripting support for lifecycle management
- Improved P2 based provisioning and feature management support
- Support for adding remote mounts
- Platform, and Group/Cluster based deployment model
- Support for multiple server instances
- E-mail verification for subscriptions
- Support for deleting tags and comments
- Support for PostgreSQL and DB2 DBMS
- Paged activity and resource search
- Hierarchical permission model with granular and extensible permissions
- Ability to upload metadata (in addition to importing)
- Governance Archive for uploading WSDLs and Schemas with imports
- Ability to update resource content by uploading
- Rich text editor for editing text resources
- XML editor for editing handler, lifecycle and service UI configurations
Web Service Application Server (WSO2 WSAS):
- Component Manager - The UI tool to install/uninstall Carbon featrues.
- Various bug fixes & enhancements including architectural improvements to Apache Axis2, Apache Rampart, Apache Sandesha2 , WSO2 Carbon & other projects.
- Equinox P2 based provisioning support - extend your WSAS instance by installin new P2 features. See P2 based provisioning in WSO2 Carbon
- Hierarchical service support for Axis2 services, JAX-WS services, Spring services & Jar services
- Report generation for deployed services
Identity Server:
- SAML 2.0 based Single Sign-on support
- OAuth Support
- Support for bulk-user import
- Various bug fixes and enhancements including architectural improvements to Apache Axis2, Apache Rampart, Apache Sandesha2 , WSO2 Carbon and other projects.
Labels:
carbon,
esb,
governance registry,
identity,
SOA,
web service application server,
wsas,
WSO2
Wednesday, February 03, 2010
Webinar: WSO2 Business Activity Monitor for Agile Enterprises
Samisa Abeysinghe, the directory of engineering at WSO2 will present a webinar on Building an Agile Enterprise With Business Activity Monitoring today (3rd February 2010).
There he will provide an overview of WSO2 Business Activity Monitor (WSO2 BAM), the latest product from the WSO2 Carbon platform, including its built-in dashboard to view analytics , reports of past and present activities of the enterprise SOA infrastructure and how these information can be used in tactical and strategic decision making.
There he will provide an overview of WSO2 Business Activity Monitor (WSO2 BAM), the latest product from the WSO2 Carbon platform, including its built-in dashboard to view analytics , reports of past and present activities of the enterprise SOA infrastructure and how these information can be used in tactical and strategic decision making.
Labels:
agile enterprsie,
BAM,
buisness activity monitor,
Governance,
report,
SOA,
webinar,
WSO2
Monday, January 11, 2010
Access WSO2 Governance as a Service From Remote Registry
WSO2 Governance as a Service is a hosted instance of WSO2 Governance Registry with multi-tenant support. WSO2 Governance as a Service provide you almost all the functionalities provided with the Governance Registry targeting the enterprise SOA governance, same time it provides all the advantages inherent with the Software as a Service model.
Here I'm talking about how to use a popular feature available in Governance Registry, inside WSO2 Governance as a Service. i.e. Remote Registry Client. With Remote Registry Client, you can access the resources in registry programatically. It uses atom/pub protocol to communicate with the registry server.
Here is an example of using Remote Registry Client. I assumed I have an account with domain name 'example.com' with a user name 'example_user' ('example_password'). You have to change this to valid values before running this code, You can create an account in Governance as a Service freely for a limited use.
Here I'm talking about how to use a popular feature available in Governance Registry, inside WSO2 Governance as a Service. i.e. Remote Registry Client. With Remote Registry Client, you can access the resources in registry programatically. It uses atom/pub protocol to communicate with the registry server.
Here is an example of using Remote Registry Client. I assumed I have an account with domain name 'example.com' with a user name 'example_user' ('example_password'). You have to change this to valid values before running this code, You can create an account in Governance as a Service freely for a limited use.
import java.net.URL;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.app.RemoteRegistry;
class RegistryDemo {
public static void main(String[] args) throws Exception {
// calls the registry with the authentication information
callRemoteRegistry("http://governance.cloud.wso2.com/registry",
"example_username@example.com", "example_password");
}
public static void callRemoteRegistry(String url, String username,
String password) throws Exception {
Registry myRegistry = new RemoteRegistry(new URL(url), username, password);
if (!myRegistry.resourceExists("/demoResource")) {
Resource r = myRegistry.newResource();
r.setContent("demo content");
myRegistry.put("/demoResource", r);
}
Resource r = myRegistry.get("/demoResource");
byte[] contentBytes = (byte[])r.getContent();
String content = new String(contentBytes);
System.out.println("Content: " + content);
}
}
Labels:
Cloud,
GaaS,
Governance,
governance as a service,
governance registry,
remote registry,
SOA,
WSO2
Sunday, January 10, 2010
Make vs Ant
Ant was developed mainly to run java programs, so it is good at building and running java programs. But you can use the good all Make program to build and even run java programs.
Say I have an ant file that will
For this, I will only compile one java file name src/test/HelloWorld.java
If you want to learn what each line of this means, just follow the excellent ant tutorial at http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html.
Here is the Makefile that will do the above tasks,
You may notice I had to provide the package name to the compile command, as it doesn't have a proper wildcards to represent the jar. Similar to ant all these make tasks will execute only if it is required. i.e. for an example if all the .class files are up to date with .java it will not try to recompile it.
Say I have an ant file that will
- Clean the build - ant clean
- Compile - ant compile
- Make a Jar - ant jar
- Run - ant run or simply ant
For this, I will only compile one java file name src/test/HelloWorld.java
<project name="HelloWorld" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="test.HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
If you want to learn what each line of this means, just follow the excellent ant tutorial at http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html.
Here is the Makefile that will do the above tasks,
SRC_DIR=src
BUILD_DIR=build
CLASSES_DIR=$(BUILD_DIR)/classes
JAR_DIR=$(BUILD_DIR)/jar
PROJECT=HelloWorld
MAIN_CLASS=test.HelloWorld
PACKAGE=test
run: clean jar
java -jar $(JAR_DIR)/$(PROJECT).jar
jar: $(JAR_DIR)/$(PROJECT).jar
$(JAR_DIR)/$(PROJECT).jar: $(CLASSES_DIR)/$(PACKAGE)/*.class
echo Main-Class: $(MAIN_CLASS)> mf
test -d $(JAR_DIR) | mkdir -p $(JAR_DIR)
jar cfm $@ mf -C $(CLASSES_DIR) .
compile: $(CLASSES_DIR)/$(PACKAGE)/*.class
$(CLASSES_DIR)/$(PACKAGE)/%.class: ${SRC_DIR}/$(PACKAGE)/*.java
test -d $(CLASSES_DIR) | mkdir -p $(CLASSES_DIR)
javac -sourcepath src -d ${CLASSES_DIR} $<
clean:
rm -rf build mf
You may notice I had to provide the package name to the compile command, as it doesn't have a proper wildcards to represent the jar. Similar to ant all these make tasks will execute only if it is required. i.e. for an example if all the .class files are up to date with .java it will not try to recompile it.
Friday, January 08, 2010
WSF/PHP Code First Approach: Returning an Array of String
Here is a problem that many people have asked me how to do it. "Returning an array of string" with the code first approach. The API or WSDL generation annotation guide, http://wso2.org/project/wsf/php/2.0.0/docs/wsdl_generation_api.html contain all the things required in details. Here is an example of a service that return an array of string.
Note that the annotation corresponding to the return value.
This will generate an schema with an element of maxOccurs='unbounded'. Note that you can get the wsdl from the 'serviceurl?wsdl'.
Now just generate a client for this service using wsdl2php and try invoke it. You will get an array of string as the response.
<?php
/** splitMe function
* @param string $string_to_split string to split
* (maps to the xs:string XML schema type )
* @return array of string $result split to array
*(maps to the xs:double XML schema type )
*/
function splitMe($string_to_split)
{
return array("result" => split(":", $string_to_split));
}
$operations = array("splitMe"=>"splitMe");
$opParams = array("splitMe"=>"MIXED");
$svr = new WSService(array("operations"=>$operations,
"bindingStyle"=>"doclit",
"opParams"=>$opParams));
$svr->reply();
?>
Note that the annotation corresponding to the return value.
* @return array of spring $result split to array
This will generate an schema with an element of maxOccurs='unbounded'. Note that you can get the wsdl from the 'serviceurl?wsdl'.
<xsd:element name="splitMeResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="result" maxOccurs="unbounded" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Now just generate a client for this service using wsdl2php and try invoke it. You will get an array of string as the response.
$input = new splitMe();
$input->string_to_split = "a:b:c:d";
// call the operation
$response = $proxy->splitMe($input);
print_r($response);
Labels:
code first,
PHP,
SOA,
Tutorial/Guide,
WSDL,
wsdl generation,
WSF/PHP,
WSO2,
xml,
xml schema
Thursday, January 07, 2010
Writing Business Rules in WSO2 Carbon Platform
If you want to write rules in a Java program you have lot of choices. You can use a third party library like Drools or use the JAVA built-in JSR-94 reference implementation. In WSO2 Carbon, there is a component that abstract the behaviour of different rule engine and give you a unified API. Currently it has plugged into Drools and JAVA built-in JSR-94 implementation.
The rule component in WSO2 Carbon platform mainly used by WSO2 ESB product to mediate messages according to the given business rules. But the component is written to facilitate any requirement of using business rules in WSO2 Carbon platform. I had such a requirement in past few days and manage to use the rule component easily with the help of the component author, indika@wso2.com. So I thought it is worth sharing my experience in here.
Here You will be preparing the following stuff.
1. Rule configuration -
We can use this to provide the information about the rule implementation we are going to use, the rules (You can write rules inline or provide a reference to an external file) and the input and output adapter information.
2. The Rules -
You can write rules inline in the above configuration or put it in a file and refer it from a key which can be refered from the ResourceHelper (described below).
3. Data Context -
The context object that can be used to feed and retrieve data from and to rule engine. Here is the data context for my application.
And the Item I'm going to manipulate using rule is a simple bean like this,
4. Data Adapter
You have to adapt the input and output with the rule engine. Mostly here you only have to wrap the data context. The advantage of having the data adapter is, a data adapter always associated with a input/output type. So in the rule configuration I can provide the type for the input and output. If you see my rule configuration above, you see the input/output type is marked as "ItemData". Here is my custom data adapter that is associated with the "itemData" type.
5. Resource Helper
Resource Helper will map the keys refered from the configuration to JAVA objects. This is mostly used in mediation rule configurations which can extract the message data using a key or an xpath. In this example, we don't have much keys refering from the configuration only the rule file and the data context.
That is all the accessories. Now you will be able to write the rule engine execution code.
The rule component in WSO2 Carbon platform mainly used by WSO2 ESB product to mediate messages according to the given business rules. But the component is written to facilitate any requirement of using business rules in WSO2 Carbon platform. I had such a requirement in past few days and manage to use the rule component easily with the help of the component author, indika@wso2.com. So I thought it is worth sharing my experience in here.
Here You will be preparing the following stuff.
1. Rule configuration -
We can use this to provide the information about the rule implementation we are going to use, the rules (You can write rules inline or provide a reference to an external file) and the input and output adapter information.
<configuration xmlns="http://www.wso2.org/products/rule/drools">
<executionSet uri="simpleItemRuleXML">
<source key="file:src/test/resources/rules/simple-rules.drl"/>
<!-- <source>
<x><![CDATA[
rule InvokeABC
// rules inbuilt to the rule conf
end
]]>
</x>
</source> -->
<creation>
<property name="source" value="drl"/>
</creation>
</executionSet>
<session type="stateless"/>
<input name="facts" type="itemData" key="dataContext"></input>
<output name="results" type="itemData" key="dataContext"></output>
</configuration>
2. The Rules -
You can write rules inline in the above configuration or put it in a file and refer it from a key which can be refered from the ResourceHelper (described below).
import java.util.Calendar;
rule YearEndDiscount
when
$item : org.test.pojo.SimpleItem(price > 100 )
then
Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.MONTH) == Calendar.JANUARY) {
$item.setPrice($item.getPrice() * 80/100);
}
end
3. Data Context -
The context object that can be used to feed and retrieve data from and to rule engine. Here is the data context for my application.
public class SimpleDataContext {
public List<NameValuePair> getInput() {
// in reality the data will be retrieve from a database or some datasource
List<NameValuePair> itemPairList = new ArrayList<NameValuePair>();
SimpleItem item1 = new SimpleItem();
item1.setName("item1");
item1.setPrice(50);
itemPairList.add(new NameValuePair(item1.getName(), item1));
SimpleItem item2 = new SimpleItem();
item2.setName("item2");
item2.setPrice(120);
itemPairList.add(new NameValuePair(item2.getName(), item2));
SimpleItem item3 = new SimpleItem();
item3.setName("item3");
item3.setPrice(130);
itemPairList.add(new NameValuePair(item3.getName(), item3));
return itemPairList;
}
public void setResult(Object result) {
if (!(result instanceof SimpleItem)) {
System.out.println("it is not a SimpleItem");
}
SimpleItem item = (SimpleItem)result;
System.out.println("Item: " + item.getName() + ", Price: " + item.getPrice());
}
}
And the Item I'm going to manipulate using rule is a simple bean like this,
public class SimpleItem {
String name;
int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
4. Data Adapter
You have to adapt the input and output with the rule engine. Mostly here you only have to wrap the data context. The advantage of having the data adapter is, a data adapter always associated with a input/output type. So in the rule configuration I can provide the type for the input and output. If you see my rule configuration above, you see the input/output type is marked as "ItemData". Here is my custom data adapter that is associated with the "itemData" type.
public class SimpleDataAdapter implements
ResourceAdapter, InputAdaptable, OutputAdaptable {
// the type associated with the adapter
private final static String TYPE = "itemData";
public String getType() {
return TYPE;
}
public Object adaptInput(ResourceDescription resourceDescription, Object tobeadapted) {
if (!(tobeadapted instanceof SimpleDataContext)) {
return null;
}
SimpleDataContext dataContext = (SimpleDataContext)tobeadapted;
return dataContext.getInput();
}
public boolean adaptOutput(ResourceDescription description,
Object value,
Object context,
ResourceHelper resourceHelper) {
if (!(context instanceof SimpleDataContext)) {
return false;
}
((SimpleDataContext)context).setResult(value);
return true;
}
public boolean canAdapt(ResourceDescription description, Object ouptput) {
String key = description.getKey();
return key != null && !"".equals(key);
}
}
5. Resource Helper
Resource Helper will map the keys refered from the configuration to JAVA objects. This is mostly used in mediation rule configurations which can extract the message data using a key or an xpath. In this example, we don't have much keys refering from the configuration only the rule file and the data context.
public class SimpleResourceHelper extends ResourceHelper {
public ReturnValue findByKey(String key, Object source, Object defaultValue) {
if (!(source instanceof SimpleDataContext)) {
return new ReturnValue(defaultValue);
}
SimpleDataContext dataContext = (SimpleDataContext)source;
if (key.startsWith("file:")) {
String filename = key.substring("file:".length());
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
return new ReturnValue(in);
} catch (Exception e) {
return new ReturnValue(defaultValue);
}
}
if (key.startsWith("dataContext")) {
return new ReturnValue(dataContext);
}
return new ReturnValue(defaultValue);
}
// there are few more methods to be implemented, which can just leave not implemented for this example
}
}
That is all the accessories. Now you will be able to write the rule engine execution code.
File ruleConfigFile = new File(ruleConfigFilename);
XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(ruleConfigFile));
//create the builder
StAXOMBuilder builder = new StAXOMBuilder(parser);
//get the root element (in this case the envelope)
OMElement ruleConfig = builder.getDocumentElement();
EngineConfiguration configuration =
new EngineConfigurationFactory().create(ruleConfig, new AXIOMXPathFactory());
EngineController
engineController = new EngineController(configuration, new SimpleResourceHelper());
final ResourceAdapterFactory factory = ResourceAdapterFactory.getInstance();
ResourceAdapter adapter = new SimpleDataAdapter();
String adapterType = adapter.getType();
if (!factory.containsResourceAdapter(adapterType)) {
factory.addResourceAdapter(adapter);
}
SimpleDataContext simpleContext = new SimpleDataContext();
if (!engineController.isInitialized()) {
engineController.init(simpleContext);
}
if (engineController.isInitialized()) {
engineController.execute(simpleContext, simpleContext);
}
Labels:
business rules,
carbon,
esb,
java,
SOA,
Tutorial/Guide,
WSO2
Wednesday, January 06, 2010
Getting the size of BLOB in MySql
If you want to store binary in database, you can use BLOB as the data type of that column. In Mysql you can use TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB depending on your space requirement. Here is an example of database table using BLOB as a column type.
Storing Data
PHP:
Java:
Retrieving Data
PHP
Java:
Retrieving the Size of the Blob
After you store your data as a blob, you can manipulate or query the data with some of the in-built String functions in mysql. For an example if you want to query the size of the blob you just stored, you can use OCTET_LENGTH function. Here is an example, (this will give you the size in bytes.)
CREATE TABLE BloBTest (
id INT NOT NULL AUTO_INCREMENT,
filename VARCHAR( 32 ) NOT NULL,
content BLOB NOT NULL,
PRIMARY KEY ( id )
)
Storing Data
PHP:
$filename = "myimage.png";
$filecontent = file_get_contents($filename);
$filecontent_escaped = mysql_real_escape_string($filecontent);
$sql = "INSERT INTO BloBTest(filename, content) " +
"VALUES('$filename','$filecontent_escaped')";
mysql_query($sql, $link);
Java:
String filename = "myimage.png";
InputStream filecontent = new FileInputStream(filename);
String sql = "INSERT INTO BloBTest(filename, content) VALUES(?, ?)";
int size = filecontent.available();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, filename);
ps.setBinaryStream(2, filecontent, size);
ps.executeUpdate();
Retrieving Data
PHP
$sql = "SELECT filename, content FROM BloBTest";
$result = mysql_query($sql, $link);
while ($row = mysql_fetch_assoc($result)) {
$filename = $row["filename"];
$content = $row["content"];
$new_filename = "new_" . $filename;
file_put_contents($new_filename, $content);
}
Java:
String sql = "SELECT filename, content FROM BloBTest";
PrepareStatement ps = conn.prepareStatement(resourceContentSQL);
ResultSet result = ps.executeQuery();
if (result.next()){
String filename = result.getString("filename");
InputStream contentStream = result.getBinaryStream("content");
String newFilename = "new_" + filename;
// storing the input stream in the file
OutputStream out=new FileOutputStream(newFilename);
byte buf[]=new byte[1024];
int len;
while((len=contentStream.read(buf))>0)
out.write(buf,0,len);
out.close();
}
Retrieving the Size of the Blob
After you store your data as a blob, you can manipulate or query the data with some of the in-built String functions in mysql. For an example if you want to query the size of the blob you just stored, you can use OCTET_LENGTH function. Here is an example, (this will give you the size in bytes.)
SELECT OCTET_LENGTH(content) FROM BloBTest WHERE filename='myimage.png'
.
Labels:
blob,
data,
database,
mysql,
OCTET_LENGTH,
PHP,
Tutorial/Guide
Tuesday, January 05, 2010
Register Today for WSO2 Governance as a Service
WSO2 Governance as a Service is an online multi-tenant supported instance of WSO2 Governance Registry which is the solution for SOA Governance from the WSO2 SOA stack. You can start trying out WSO2 Governance as a Service by accessing the http://governance.cloud.wso2.com and creating an account for your organization (free for limited use).
In order to identify your account, you have to provide the domain name of your organization. I will demonstrate how to create an account using the "ws.dimuthu.org" as my domain name.
1. First go to http://governance.cloud.wso2.com from a web browser and click the 'Register' button. You will be asked to enter the domain name as the first step.
[caption id="attachment_1036" align="aligncenter" width="773" caption="Enter the domain"][/caption]
After that, you have the option of validating the ownership of the domain right at the registration process, or you can skip the validation and continue to the next step in which case your domain will be appended '-trial' suffix. You can validate the ownership of the domain later at any stage.
Here I want to validate the domain right now, so I click 'Take me to the domain ownership confirmation page straight-away' and click the 'Submit' button.
2. This will redirect you to the domain ownership validation page. You can validate the ownership of your domain in one of two ways.
Method i). Just create a text file named 'wso2gaas.txt' in the web root of your domain and enter the given text. This is the most simplest method of two.
[caption id="attachment_1039" align="aligncenter" width="722" caption="Validate domain name using Textfile"][/caption]
Method ii). You can put a DNS entry according to the given instructions. This is a little tedious approch to validate the domain. In fact it may take a while to propagate the new DNS information, so you may have to wait hours without refreshing the page until you finally validate the domain ownership.
Click the continue button after the domain validation done. Then you will be redirected to a page requesting more information.
3. Tenant Registration Page
[caption id="attachment_1041" align="aligncenter" width="787" caption="Tenant Registration"][/caption]
4) After this step, you will be notified to check for your email which will contain a mail with a link to proceed with the registration. There you will be able to select a theme for your organization and finalize creating your account. Login to the admin account for your tenant with the credential you provided a the time of the registration.
The domain ownership validation was introduced to WSO2 Governance as a Service account registration only now. So for organizations who have already have account will have a message similar to this when they are trying to login to their account.
[caption id="attachment_1046" align="aligncenter" width="460" caption="Info box at login"][/caption]
So the account I have registered using the domain name 'example.com' has been renamed to 'example.com-trial'. As the instruction of the message says you can go to the account management page after the login and validate the domain ownership.
[caption id="attachment_1043" align="aligncenter" width="808" caption="Account Management Page"][/caption]
In order to identify your account, you have to provide the domain name of your organization. I will demonstrate how to create an account using the "ws.dimuthu.org" as my domain name.
1. First go to http://governance.cloud.wso2.com from a web browser and click the 'Register' button. You will be asked to enter the domain name as the first step.
[caption id="attachment_1036" align="aligncenter" width="773" caption="Enter the domain"][/caption]
After that, you have the option of validating the ownership of the domain right at the registration process, or you can skip the validation and continue to the next step in which case your domain will be appended '-trial' suffix. You can validate the ownership of the domain later at any stage.
Here I want to validate the domain right now, so I click 'Take me to the domain ownership confirmation page straight-away' and click the 'Submit' button.
2. This will redirect you to the domain ownership validation page. You can validate the ownership of your domain in one of two ways.
Method i). Just create a text file named 'wso2gaas.txt' in the web root of your domain and enter the given text. This is the most simplest method of two.
[caption id="attachment_1039" align="aligncenter" width="722" caption="Validate domain name using Textfile"][/caption]
Method ii). You can put a DNS entry according to the given instructions. This is a little tedious approch to validate the domain. In fact it may take a while to propagate the new DNS information, so you may have to wait hours without refreshing the page until you finally validate the domain ownership.
Click the continue button after the domain validation done. Then you will be redirected to a page requesting more information.
3. Tenant Registration Page
[caption id="attachment_1041" align="aligncenter" width="787" caption="Tenant Registration"][/caption]
4) After this step, you will be notified to check for your email which will contain a mail with a link to proceed with the registration. There you will be able to select a theme for your organization and finalize creating your account. Login to the admin account for your tenant with the credential you provided a the time of the registration.
The domain ownership validation was introduced to WSO2 Governance as a Service account registration only now. So for organizations who have already have account will have a message similar to this when they are trying to login to their account.
[caption id="attachment_1046" align="aligncenter" width="460" caption="Info box at login"][/caption]
So the account I have registered using the domain name 'example.com' has been renamed to 'example.com-trial'. As the instruction of the message says you can go to the account management page after the login and validate the domain ownership.
[caption id="attachment_1043" align="aligncenter" width="808" caption="Account Management Page"][/caption]
Labels:
Cloud,
gaas,
Governance,
Governance Registry,
SaaS,
services,
SOA,
Tutorial/Guide,
web services,
WSO2
Subscribe to:
Posts (Atom)