Thursday, December 15, 2011

Using WSO2 Registry Eventing for notification through an Executor

We will gonna do this through an Executor associated to a registry LC (Life Cycle) .

What we gonna do: We are going to create a simple registry LC associate a resource to it and write an Executor class which notify via email when that resource promote to its next state. To send email we use registry eventing functionality.

Executors are one of the facilitators which helps to extend the WSO2 GReg functionality. Here an Executor is associated to a registry life cycle. Life Cycle is basically a set of pre-defined states which can be associated to a registry Resource and allows that associated resource to evolve through that LC as defined. There users can basically do "Promote" and "Demote" a resource from one state to another.

You can write your own WSO2 GReg Executor class by implementing the org.wso2.carbon.governance.registry.extensions.interfaces.Execution interface. There you can override the execution method.

public class LCStateChangeNotificationExecutor implements Execution {
public void init(Map map) { }
public boolean execute(RequestContext requestContext, String s, String s1) {
// DO what you want to do when the resource change its LC state. Here we try to send an email notification to a subscriber.
sendEmail(requestContext);
return true;
}
}
Now lest see how to achieve the notification through registry eventing. Here we create a ResourceUpdateEvent and send that event to a subscriber resource which is a dummy resource where we manually subscribed to its update mode via my email. ( when state change happens to a resource, the executor gets HIT and it will send a Update Event to a dummy resource to which user is subscribed via email.)

UserRegistry registry registry = (UserRegistry) requestContext.getSystemRegistry();
ResourceUpdatedEvent resourceUpdatedEvent = new ResourceUpdatedEvent(CUSTOM_EMAIL_MESSAGE);

resourceUpdatedEvent.setResourcePath(SUBSCRIPTION_RESOURCE_PATH);
if (registry != null) {
resourceUpdatedEvent.setTenantId(registry.getTenantId());
}


Now Event instance is ready and only needs to sent it through registry NotificationService.

 org.wso2.carbon.registry.common.eventing.NotificationService.notify(resourceUpdatedEvent);

NOTE: Notification service is exposed as an OSGi service where that service included component class has getters and setters so that you can call the getRegistryNotificationService() method on that class which will return the NotificationService set by the eventing service.


Then you are done with the Executor class. Please add an entry inside to a LC state from which you want the Executor to be HIT when state change takes place.

So to define reference to Executor.
Now we have to define the Executor in our LC. Find the EventLifeCycle.xml.xml LC file under GREG_HOME/samples/sportmeet-scenario/src/resources.
(In the EventLifeCycle.xml.xml file we have added an entry for our executor inside the first state, because here, we want the executor to hit as soon as LC state promote from its initial state).
Here is the sample LC XML.
<aspect name="WSO2SportMeetEventHandler" class="org.wso2.carbon.governance.registry.extensions.aspects.DefaultLifeCycle">
<configuration type="literal">
<lifecycle>
<scxml xmlns="http://www.w3.org/2005/07/scxml"
version="1.0"
initialstate="Create">
<state id="Create">
<datamodel>
<data name="checkItems">
<item name="Created the Event" forEvent="Promote">
</item>
<item name="Announced the Event" forEvent="Promote">
</item>
</data>
<data name="transitionValidation">
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.PromoteValidator">
</validation>
</data>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
</datamodel>
<transition event="Promote" target="Edit"/>
</state>
<state id="Edit">
<datamodel>
<data name="checkItems">
<item name="Add participants" forEvent="Promote">
</item>
<item name="Collect participant details" forEvent="Promote">
<validations>
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.ParticipantValidator">
</validation>
</validations>
</item>
</data>
<data name="transitionValidation">
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.PromoteValidator">
</validation>
</data>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
</datamodel>
<transition event="Promote" target="Review"/>
<transition event="Demote" targedt="Create"/>
</state>
<state id="Review">
<datamodel>
<data name="checkItems">
<item name="Not eligible participants removed" forEvent="Promote">
</item>
<item name="Extra participants removed(if maximum number exceeds)" forEvent="Promote">
</item>
<item name="All the participhants are not eligible for the Event" forEvent="Demote">
</item>
</data>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
<data name="transitionValidation">
<validation forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.validators.PromoteValidator">
</validation>
</data>
</datamodel>
<transition event="Promote" target="Accept"/>
<transition event="Demote" target="Edit"/>
</state>
<state id="Accept">
<transition event="Demote" target="Review"/>
<datamodel>
<data name="transitionExecution">
<execution forEvent="Promote"
class="org.wso2.carbon.registry.samples.sportmeet.executors.LCStateChangeNotificationExecutor">
</execution>
</data>
</datamodel>
</state>
</scxml>
</lifecycle>
</configuration>
</aspect>



For the notification part you must enable the axis2 TransportSender in GREG_HOME/reposiroty/conf/axis2.xml.


1. Now everything is ready and start the server.

2. Go to admin console.

3.Add a resource (which you provided as SUBSCRIPTION_PATH) and subscribe to it via email under update resource mode.

4 . Now add another resource and associate the service LC to it and do a promote.

5 When promoted, an email notification will be sent to your previously
subscribed email address.


References:





Monday, June 27, 2011

Configuring Eclipse to use OpenCV



This is how I figured out and which was succeeded in getting eclipse to work with Eclipse. This is a bit tedious, but can do this in 15 min :).





1. If you are not already installed Eclipse, first you have to install Eclipse. If you installing eclipse for the first time, better to install a version which supports C++. Instead, if you already have eclipse, only need to do is install the CDT C/C++ plugin for eclipse.

To install CDT plugin use the name as CDT and give an available source repository link. But I could only found "http://download.eclipse.org/releases/ganymede/" available as most of them are not available.

2. Now you have to install OpenCV if not yet installed. So there are few steps to follow to accomplish this.

Because Prebuilt binaries for Linux are not included with the Linux version of OpenCV owing to the large variety of versions of GCC and GLIBC in different distributions (SuSE,Debian, Ubuntu, etc.). If your distribution doesn’t offer OpenCV, you’ll have to build it from sources.

Follow these steps to download the source and build OpenCV.
There are some prerequisite packages to be installed

a) GCC 4.x or later.. you can install it from the following command
sudo apt-get install build-essential
b) And things like pkgconfig , subversion, cmake and etc.

2.1 First download the source. Go to http://sourceforge.net/projects/opencvlibrary and download the tar ball and unzip it to build. Else easiest way is to checkout the src from opencv svn repo .You can do it by simply following command, or choose a latest tag from the svn.

svn co https://code.ros.org/svn/opencv/trunk

2.2 Now have to build this by cmake. So go to your opencv source directory and make a directory to put generated make files.
i.e
mkdir binary
cd binary
cmake ../

2.3 Now go to that binary directory
make
sudo make install

3. Now everything is fine and you can create a C++ project and use opencv.
But before that there is one other thing to do. We have to tell our project where opencv headers and libraries are located in.

3.1 For that purpose, go to Project > Properties goto C/C++ Build and goto Settings > Tool Setting tab.
3.2 There in GCC C++ Compiler in "Includes" or "Directories" you will find an option "-l" to fill. 3.3 Add the path where you installed the opencv in above steps.
3.4 Now go to GCC C++ Linker and set the opencv distribution lib to "Library Search path -L".
3.5 Also in the Libraries -l , add the libraries you want to use. Following are useful set of libraries.
  • opencv_core
  • opencv_imgproc
  • opencv_highgui
  • opencv_ml
  • opencv_video
  • opencv_features2d
  • opencv_calib3d
  • opencv_objdetect
  • opencv_contrib
  • opencv_legacy
  • opencv_flann
4. And now ITS ALL FINE....:-) Happy coding with OpenCV..

For more information please visit

Wednesday, June 8, 2011

How to create a Apache Tuscany SCA component for Apache PhotArk



Apache PhotArk is an open source rich photo gallery application where you can do many things what a typical photo gallery app can do as well as some additional features such as it contains a Google app engine to function in cloud. Apache Tuscany is what PhotArk uses for message passing bridge between front and and back end under Service Component Architecture. This is how you do $Title.

  • Step-1
In Tuscany way first you have to put and SCA component entry to the Tuscany composite, which is in photark, named as web.composite.xml.

 













  • Step2
In Photark web app, there is a sca-contribution.xml file for each of your PhotArk module. So There you have to define the packages you use externally as well as you own package name. So suppose I dont have any dependent external packages to import my service component implementation class. Then your sca-contribution.xml file can be something similar to this.









NOTE : If you are using a brand new PhotArk module, make sure you add the dependency of it not only to the parent pom, but also to the pom inside photark-webapp as well, because in deploying it on tomcat, maven can't find your dependency if you don't add it to this pom.
  • Step 3
Now everything is ready. Make sure you use proper annotations for your service component interface and its implementation. For instance, @Scope("COMPOSITE") for the impl and @Remotable for the interface.
  • Step 4

Call your service through JSON, in PhotArk we make JSON calls through DOJO. So to use the service use the absolute binding url (in composite you define the relative path only..visit constant.js file in PhotArk and you will find the absolute path..)

i.e add PhotArk context root to your tuscany binding url.

In this example it should be /photark/FaceRecognitionService?smd . NOTE : make sure you add "smd" param as well.


I think now its done :) Enjoy Apache products.





Sunday, May 29, 2011

How to obtain a Facebook access token to use face.com functionality

This is simple and here are the steps to follow.

1. Go to http://www.facebook.com/developers/apps.php/ page and create a new app and obtain your FB appID, api key and api secret.

2. If not, you have to create a face.com account and obtain a face.com API key, secret pair.

3. And register this FB api key and secret with your face.com API key. You can go to edit account settings in face.com and apply this.

Use app ID to fill client_id(i.e client_id=appID) to follows with redirect url and get the auth token. AND SKIP THE expire time variable also.
NOTE: Redirect_url should be in the same domian you specified your app in FB.

https://graph.facebook.com/oauth/authorize?
type=user_agent&
client_id=your_FB_APPID&
redirect_uri=http://abc.com/&
scope=user_photos,email,user_birthday,user_online_presence,offline_access

Out put of redirect url can be something like this and obtain the access token and use this to access the API.

http://face.com/about.php/#access_token=2242657575991sadasdasf1faf9%7C2.%7CYwds6rrGtfpOi7c&expires_in=0