Pages

Subscribe:

Ads 468x60px

Featured Posts

Saturday, June 14, 2014

Mount WSO2 products to a remote WSO2 Governance Registry instance(MsSQL 2012)

In this post we are going to mount wso2 products into a remote governance registry. In the default scenario wso2 governance registry is pointed to a local h2 database.

First lets configure the governance registry instance to point to the MsSQL data source.
  • Download WSO2 Governance Registry product and extract the product.
  • Go to /repository/conf/datasources  and open master-datasources.xml
  • Then change the following entries in the WSO2CARBON data source.
<datasource>
            <name>WSO2_CARBON_DB</name>
            <description>The datasource used for registry and user manager</description>
            <jndiConfig>
                <name>jdbc/WSO2CarbonDB</name>
            </jndiConfig>
            <definition type="RDBMS">
                <configuration>
                    <url>jdbc:sqlserver://127.0.0.1:1433;databaseName=REMOTEDB</url>
                    <username>sa</username>
                    <password>pass#word2</password>
                    <driverClassName>com.microsoft.sqlserver.jdbc.SQLServerDriver</driverClassName>
                    <maxActive>50</maxActive>
                    <maxWait>60000</maxWait>
                    <testOnBorrow>true</testOnBorrow>
                    <validationQuery>SELECT 1</validationQuery>
                    <validationInterval>30000</validationInterval>
                </configuration>
            </definition>
</datasource>

  • Fill the url, username, password according to your MsSQL Instance.
  • Log into the MSSQL 2012 Management studio and create a database called REMOTEDB
  • Then move to the /repository/components/lib folder in the extracted greg product folder and copy the downloaded MsSQL JDBC Connector. You can download the sqljdbc driver from this link. Download
  • Then Start the WSO2 governance product with the -Dsetup option. (-Dsetup option will populate the new schemas in the newly pointed MsSQL REMOTEDB)
D:\blog\wso2greg-4.6.0\bin>wso2server.bat -Dsetup

 Now lets mount a WSO2 product to this remote registry. 

  • Let's use WSO2 API Manager for this example post. First Download the product and extract it to your hard drive.
  • Go to /repository/conf/datasources folder and open the master-datasources.xml file
  • Then add a new data source as follows. For the demonstration purpose lets name the new datasource as WSO2_CARBON_DB2.
<datasource>
            <name>WSO2_CARBON_DB2</name>
            <description>The datasource used for registry and user manager</description>
            <jndiConfig>
                <name>jdbc/WSO2CarbonDB2</name>
            </jndiConfig>
            <definition type="RDBMS">
                <configuration>
                    <url>jdbc:sqlserver://127.0.0.1:1433;databaseName=REMOTEDB</url>
                    <username>sa</username>
                    <password>pass#word2</password>
                    <driverClassName>com.microsoft.sqlserver.jdbc.SQLServerDriver</driverClassName>
                    <maxActive>50</maxActive>
                    <maxWait>60000</maxWait>
                    <testOnBorrow>true</testOnBorrow>
                    <validationQuery>SELECT 1</validationQuery>
                    <validationInterval>30000</validationInterval>
                </configuration>
            </definition>
</datasource>

  • Note that this datasource is a completely new entry and let the default config reamins as it is.
  • Now as we did in WSO2 governance product copy the saljdbc driver to the /repository/components/lib folder
  • No open up the /repository/conf registry.xml file in the WSO2 API Manager product and do the following changes.
  •  Add a new dbConfig as follows
<dbConfig name="remoteRegistry">
       <dataSource>jdbc/WSO2CarbonDB2</dataSource>
</dbConfig>
  • Note that the dataSource is set to the jndiConfig name which we used in the new datasource defined in the API Manager's master-datasources.xml
  • Now find the following configuration, uncomment it and change it to the following config values.
<remoteInstance url="https://localhost:9443/registry">
        <id>apimanager</id>
        <dbConfig>remoteRegistry</dbConfig>
        <readOnly>false</readOnly>
        <enableCache>true</enableCache>
        <registryRoot>/</registryRoot>
</remoteInstance>
  • Note that the dbConfig name is set to the newly added dbConfig name, which is "remoteRegistry" in this example. Give a unique id value.
  • Then we set the remote mounted path using the following configuration.
<mount path="/_system/config" overwrite="true">
        <instanceId>apimanager</instanceId>
        <targetPath>/_system/amnode</targetPath>
</mount>
  • This configuration is comment by default and you have to uncomment and provide the relevant values. Note that the instanceID value is same as the id value in remoteInstance configuration.
  • The target path is set to /_system/amnode if you are using several API Manager nodes you can set all to the same path. If it is a different product it is recommend to use a different path for example ESB node path would be /_system/esbnode
  • /_system/config is the path which the product is going to be mounted.
  • Now finally if you have followed the above steps correctly start the API Manager with some port Offset if the product is running in the same machine.
D:\blog\wso2am-1.6.0\bin>wso2server.bat -DportOffset=100
  • Now log into the API Manager Web UI using the following URL https://192.168.1.2:9543/carbon/
  • If you browse the resource can see the remotely mounted /_system/config registry.
API Manager Resource view
That's it for now. Feel free to shore your thoughts about the post. :)

Saturday, April 19, 2014

How to publish data to WSO2 BAM 2.4.0 (Business Activity Monitor)

In this post I'm going to explain how to publish data to a WSO2 BAM 2.4.0 instance.

Following is the code which I used to publish the data.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package org.wso2.carbon.publisher;

import org.apache.log4j.Logger;
import org.wso2.carbon.databridge.agent.thrift.Agent;
import org.wso2.carbon.databridge.agent.thrift.DataPublisher;
import org.wso2.carbon.databridge.agent.thrift.conf.AgentConfiguration;
import org.wso2.carbon.databridge.agent.thrift.exception.AgentException;
import org.wso2.carbon.databridge.commons.Event;
import org.wso2.carbon.databridge.commons.exception.*;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.SocketException;


/**
 * BAMDataPublisherExample.java
 */
public class BAMDataPublisherExample {
    private Logger log = Logger.getLogger(BAMDataPublisherExample.class);

    private DataPublisher dataPublisher = null;

    public BAMDataPublisherExample() {
        initDataPublisher();
    }

    /**
     * Initialize the BAM data publisher
     */

    private void initDataPublisher() {

        try {
            log.info("Starting BAM  client\n");
            AgentConfiguration agentConfiguration = new AgentConfiguration();

            System.setProperty("javax.net.ssl.trustStore", "/home/aruna/wso2bam/repository/resources/security/client-truststore.jks");

            System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
            Agent agent = new Agent(agentConfiguration);
            String host;

            host = "localhost";

            String port = "7611";
            String username = "admin";
            String password = "admin";

            this.dataPublisher = new DataPublisher("tcp://" + host + ":" + port,
                    username, password, agent);

        } catch (AgentException e) {
            log.error("Exception", e);

        } catch (MalformedURLException e) {
            log.error("Exception", e);

        } catch (org.wso2.carbon.databridge.commons.exception.AuthenticationException e) {
            log.error("Exception", e);

        } catch (TransportException e) {
            log.error("Exception", e);

        }

    }

    /**
     * @param dataPublisher
     * @param streamId
     * @param dataArray
     * @throws org.wso2.carbon.databridge.agent.thrift.exception.AgentException
     */
    private void publishEvents(DataPublisher dataPublisher, String streamId,
                               String dataArray[]) throws AgentException {

        Object[] objectArray = new Object[dataArray.length];
        for (int i = 0; i < dataArray.length; i++) {
            objectArray[i] = dataArray[i];
        }

        Event event = new Event(streamId, System.currentTimeMillis(), null,
                null, objectArray);

        this.dataPublisher.publish(event);

    }

    /**
     * Stop the publisher
     */
    public void stopPublisher() {
        log.info("Stopping Data Publisher");
        this.dataPublisher.stop();
    }

    /**
     * @param dataArray
     * @param streamName
     * @param version
     * @throws org.wso2.carbon.databridge.commons.exception.NoStreamDefinitionExistException
     * @throws java.net.SocketException
     * @throws java.io.UnsupportedEncodingException
     * @throws org.wso2.carbon.databridge.agent.thrift.exception.AgentException
     * @throws org.wso2.carbon.databridge.commons.exception.MalformedStreamDefinitionException
     * @throws org.wso2.carbon.databridge.commons.exception.StreamDefinitionException
     * @throws org.wso2.carbon.databridge.commons.exception.DifferentStreamDefinitionAlreadyDefinedException
     */
    public void repositoryDataPublish(String[] dataArray, String streamName, String version) throws
            NoStreamDefinitionExistException,
            SocketException,
            UnsupportedEncodingException,
            AgentException,
            MalformedStreamDefinitionException,
            StreamDefinitionException,
            DifferentStreamDefinitionAlreadyDefinedException {

        String streamId = null;

        try {

            streamId = this.dataPublisher.findStream(streamName, version);

        } catch (NoStreamDefinitionExistException e) {

            String[] fields = {"repositoryID", "ownerID", "totalNumberOfCommits", "topContributor",
                    "totalPullRequests", "lastCommitDateTime", "commitsThisMonth", "commitLastYear"};

            String[] types = {"STRING", "STRING", "STRING", "STRING",
                    "STRING", "STRING", "STRING", "STRING"};

            streamId = dataPublisher.defineStream(getTableString(fields, types, streamName,
                    version, "Repository Data", "Repository Statistics Data"));

        } catch (Exception ex) {
            log.error("***Exception****" + ex);
        }

        // Publish event for a valid stream
        if (!streamId.isEmpty()) {
            log.info("Stream ID: " + streamId + "\n");
            publishEvents(dataPublisher, streamId, dataArray);

        }

    }


    /**
     * Create the Bam Table creation String
     *
     * @param fields
     * @param types
     * @param streamName
     * @param version
     * @param nickname
     * @param descrption
     * @return
     */
    public String getTableString(String[] fields, String[] types, String streamName, String version
            , String nickname, String descrption) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{  'name':'");
        stringBuilder.append(streamName);
        stringBuilder.append("',  'version':'");
        stringBuilder.append(version);
        stringBuilder.append("',  'nickName': '");
        stringBuilder.append(nickname);
        stringBuilder.append("',  'description': '");
        stringBuilder.append(descrption);
        stringBuilder.append("',  'payloadData':[");
        for (int i = 0; i < fields.length; i++) {

            stringBuilder.append("{'name':'");
            stringBuilder.append(fields[i]);
            stringBuilder.append("','type':'");
            stringBuilder.append(types[i]);
            if (i == fields.length - 1) {
                stringBuilder.append("'}");
            } else {
                stringBuilder.append("'},");
            }

        }

        stringBuilder.append(" ]}");
        return stringBuilder.toString();
    }


    public static void main(String[] args) {
        BAMDataPublisherExample pub = new BAMDataPublisherExample();
        try {

            String data[] = {"carbon-kernel", "wso2", "302", "Kishanthan", "0", "2014-04-04", "14",
                    "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0," +
                            " 0, 6, 5, 20, 30, 2, 7, 3, 5, 14, 16, 20, 12, 54, 68, 6, 10, 19, 43]"};
            pub.repositoryDataPublish(data, "GitHub_Stream", "1.0.0");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pub.stopPublisher();
        }

    }

}


Remember the following when using the code.
  1.  Set your BAM home location where you extracted the BAM pointing to the client-truststore.jks file. for example  "/home/aruna/wso2bam/repository/resources/security/client-truststore.jks"
  2.  Set the correct port for publish the data. If you have set an offset change the port accordingly.
  3.  Set the correct username and password for publish data.

Loging to the BAM Admin Console and you'll see the published data as follows.


Admin Console View of the Published Data

Visit the following link to See how to explore the WSO2 BAM Cluster and other BAM related documentation.
https://docs.wso2.org/display/BAM240/WSO2+Business+Activity+Monitor+Documentation