Skip to content

Commit

Permalink
Starting to test and debug the component.
Browse files Browse the repository at this point in the history
  • Loading branch information
InfoSec812 committed Oct 31, 2014
1 parent 81a62af commit 435dd0a
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 40 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ ignored. The *channel* parameter, however, is always required.

## URI Format

pgevent:[datasource] || [//dbHost:dbPort]/<database>/<channel>[?parameters]
pgevent:[datasource] || [//host:port]/<database>/<channel>[?parameters]

pgevent:myDataSource/proddb/userupdates

pgevent://192.168.1.12:5432/proddb/groupupdates?pgUser=username&pgPass=secret
pgevent://192.168.1.12:5432/proddb/groupupdates?user=username&pass=secret

pgevent://192.168.1.12/proddb/groupupdates?pgUser=username&pgPass=secret
pgevent://192.168.1.12/proddb/groupupdates?user=username&pass=secret

pgevent:///proddb/customerupdates ## Uses all defaults

Expand Down
44 changes: 42 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.camel</groupId>
Expand All @@ -10,6 +25,11 @@

<name>Camel PgEvent Component</name>
<url>http://www.zanclus.com/</url>
<inceptionYear>2014</inceptionYear>
<organization>
<name>Apache Software Foundation</name>
<url>http://www.apache.org/</url>
</organization>
<description>
A Camel component capable of sending/receiving notifications
in PostgreSQL version 8.3 and higher via the use of the pgjdbc-ng
Expand Down Expand Up @@ -73,8 +93,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

Expand Down Expand Up @@ -104,4 +124,24 @@
</plugins>
</build>

<scm>
<url>https://github.com/InfoSec812/pgevent.git</url>
<connection>scm:git:[email protected]:InfoSec812/pgevent.git</connection>
<developerConnection>scm:git:[email protected]:InfoSec812/pgevent.git</developerConnection>
</scm>

<developers>
<developer>
<id>dphillips</id>
<name>Deven Phillips</name>
</developer>
</developers>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
</project>
23 changes: 17 additions & 6 deletions src/main/java/org/apache/camel/pgevent/PgEventEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.impossibl.postgres.api.jdbc.PGConnection;
import com.impossibl.postgres.jdbc.PGDataSource;
import java.io.InvalidClassException;
import java.net.URISyntaxException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.directory.InvalidAttributesException;
import javax.sql.DataSource;
import org.apache.camel.Consumer;
Expand All @@ -13,18 +15,22 @@
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.util.URISupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Represents a PgEvent endpoint.
*/
@UriEndpoint(scheme = "pgevent")
public class PgEventEndpoint extends DefaultEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(PgEventEndpoint.class);

private static final String FORMAT1 =
"^pgevent://([^:]*):(\\d+?)/(\\w+?)/(\\w+?).*$";

private static final String FORMAT2 =
"^pgevent://([^:]*)/(\\w+?)/(\\w+?).*$";
"^pgevent://([^:]+?)/(\\w+?)/(\\w+?).*$";

private static final String FORMAT3 =
"^pgevent:///(\\w+?)/(\\w+?).*$";
Expand Down Expand Up @@ -57,18 +63,17 @@ public class PgEventEndpoint extends DefaultEndpoint {

private PGConnection dbConnection;

public final PGConnection initJdbc() throws ClassNotFoundException, SQLException {
public final PGConnection initJdbc() throws Exception {
PGConnection conn;
Properties props = new Properties();
props.putAll(URISupport.parseQuery(uri));
if (this.getDatasource()!=null) {
conn = (PGConnection)this.getDatasource().getConnection();
} else {
System.setProperty("pgjdbc.test.user", this.getUser());
System.setProperty("pgjdbc.test.password", this.getPass());

Class.forName("com.impossibl.postgres.jdbc.PGDriver");
conn = (PGConnection)DriverManager.getConnection(
"jdbc:pgsql://"+this.getHost()+":"+this.getPort()+"/"+
this.getDatabase());
this.getDatabase(), this.getUser(), this.getPass());
}
return conn;
}
Expand Down Expand Up @@ -103,7 +108,9 @@ public PgEventEndpoint(String uri) throws InvalidAttributesException {
* @throws InvalidAttributesException if there is an error in the parameters
*/
protected final void parseUri() throws InvalidAttributesException {
LOG.info("URI: "+uri);
if (uri.matches(FORMAT1)) {
LOG.info("FORMAT1");
String[] parts = uri
.replaceFirst(FORMAT1, "$1:$2:$3:$4")
.split(":");
Expand All @@ -112,6 +119,7 @@ protected final void parseUri() throws InvalidAttributesException {
database = parts[2];
channel = parts[3];
} else if (uri.matches(FORMAT2)) {
LOG.info("FORMAT2");
String[] parts = uri
.replaceFirst(FORMAT2, "$1:$2:$3")
.split(":");
Expand All @@ -120,6 +128,7 @@ protected final void parseUri() throws InvalidAttributesException {
database = parts[1];
channel = parts[2];
} else if (uri.matches(FORMAT3)) {
LOG.info("FORMAT3");
String[] parts = uri
.replaceFirst(FORMAT3, "$1:$2")
.split(":");
Expand All @@ -128,6 +137,7 @@ protected final void parseUri() throws InvalidAttributesException {
database = parts[0];
channel = parts[1];
} else if (uri.matches(FORMAT4)) {
LOG.info("FORMAT4");
String[] parts = uri
.replaceFirst(FORMAT4, "$1:$2")
.split(":");
Expand All @@ -151,6 +161,7 @@ private void validateInputs() throws InvalidClassException, InvalidAttributesExc
"not set when creating this Endpoint (channel)");
}
if (datasource!=null) {
LOG.info("******Datasource detected*****");
if (!PGDataSource.class.isInstance(datasource)) {
throw new InvalidClassException("The datasource passed to the "+
"pgevent component is NOT a PGDataSource class from the"+
Expand Down
89 changes: 89 additions & 0 deletions src/test/java/org/apache/camel/pgevent/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2014 Apache Software Foundation.
*
* 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.apache.camel.pgevent;

import com.impossibl.postgres.jdbc.PGDataSource;
import javax.sql.DataSource;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.log.LogComponent;
import org.apache.camel.component.timer.TimerComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.main.Main;
import org.junit.Before;
import org.junit.Test;

/**
*
* @author dphillips
*/
public class IntegrationTest {

private Main main;

private PGDataSource ds;

@Before
public void setUp() throws Exception {
ds = new PGDataSource();
ds.setHost(System.getProperty("pgjdbc.test.server", "localhost"));
ds.setPort(Integer.parseInt(System.getProperty("pgjdbc.test.port", "5432")));
ds.setDatabase(System.getProperty("pgjdbc.test.db", "event_tests"));
ds.setUser(System.getProperty("pgjdbc.test.user", "dphillips"));

main = new Main();
main.bind("pgevent", new PgEventComponent(PgEventEndpoint.class));
main.bind("log", new LogComponent());
main.bind("timer", new TimerComponent());
main.enableHangupSupport();
main.bind("test", ds);
main.addRouteBuilder(buildConsumer());
main.addRouteBuilder(buildProducer());
}

RouteBuilder buildConsumer() {
RouteBuilder builder = new RouteBuilder() {

@Override
public void configure() throws Exception {
from("pgevent://127.0.0.1:5432/event_tests/testchannel?user=dphillips")
.to("log:org.apache.camel.pgevent.PgEventConsumer?level=DEBUG");
}
};

return builder;
}

RouteBuilder buildProducer() {
RouteBuilder builder = new RouteBuilder() {

@Override
public void configure() throws Exception {
from("timer://test?fixedRate=true&period=5000")
.to("pgevent://127.0.0.1:5432/event_tests/testchannel?user=dphillips");
}
};

return builder;
}

@Test
public void waitHere() throws Exception {
main.run();
}
}
28 changes: 0 additions & 28 deletions src/test/java/org/apache/camel/pgevent/PgEventComponentTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# The logging properties used
#
log4j.rootLogger=INFO, out
log4j.rootLogger=DEBUG, out

# uncomment the following line to turn on Camel debugging
#log4j.logger.org.apache.camel=DEBUG
Expand Down

0 comments on commit 435dd0a

Please sign in to comment.