Skip to content
Snippets Groups Projects
Commit fcb8fb05 authored by Constantin Jucovschi's avatar Constantin Jucovschi
Browse files

updated

parent 2ed2dcbc
No related branches found
No related tags found
No related merge requests found
...@@ -17,14 +17,12 @@ ...@@ -17,14 +17,12 @@
<packaging>bundle</packaging> <packaging>bundle</packaging>
<groupId>info.kwarc.sally4</groupId> <artifactId>client-java-camel</artifactId>
<artifactId>sally4-client</artifactId>
<version>0.0.4-SNAPSHOT</version>
<parent> <parent>
<groupId>info.kwarc.sally4</groupId> <groupId>info.kwarc.sally4</groupId>
<artifactId>sally4</artifactId> <artifactId>sally4</artifactId>
<version>0.0.4-SNAPSHOT</version> <version>0.0.4</version>
<relativePath>..</relativePath> <relativePath>..</relativePath>
</parent> </parent>
...@@ -34,18 +32,6 @@ ...@@ -34,18 +32,6 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>info.kwarc.sally4</groupId>
<artifactId>sally4-activemq</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>info.kwarc.sally4.comm</groupId> <groupId>info.kwarc.sally4.comm</groupId>
<artifactId>comm-core</artifactId> <artifactId>comm-core</artifactId>
...@@ -53,16 +39,23 @@ ...@@ -53,16 +39,23 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>org.apache.activemq</groupId>
<artifactId>servlet-api</artifactId> <artifactId>activemq-camel</artifactId>
<version>2.5</version> <version>5.9.0</version>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.camel</groupId> <groupId>org.apache.camel</groupId>
<artifactId>camel-stomp</artifactId> <artifactId>camel-jaxb</artifactId>
<version>${camel.version}</version> <version>${camel.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
package info.kwarc.sally4.client.impl; package info.kwarc.sally4.client.impl;
import info.kwarc.sally.comm.core.Registerdocument; import info.kwarc.sally.comm.core.RegisterClientRequest;
import info.kwarc.sally.comm.core.Registerdocumentresponse; import info.kwarc.sally.comm.core.RegisterClientResponse;
import info.kwarc.sally4.activemq.ActiveMQService;
import info.kwarc.sally4.client.SallyClient; import info.kwarc.sally4.client.SallyClient;
import info.kwarc.sally4.components.ProducerConsumerSplitterComponent; import info.kwarc.sally4.components.ProducerConsumerSplitterComponent;
import info.kwarc.sally4.core.CamelContextProvider;
import info.kwarc.sally4.marshalling.CommUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import javax.xml.bind.JAXBContext;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext; import org.apache.camel.CamelContext;
import org.apache.camel.Exchange; import org.apache.camel.Exchange;
import org.apache.camel.RoutesBuilder; import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.DataFormat;
import org.apache.felix.ipojo.annotations.Component; import org.apache.felix.ipojo.annotations.Component;
...@@ -22,61 +24,57 @@ import org.apache.felix.ipojo.annotations.Instantiate; ...@@ -22,61 +24,57 @@ import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate; import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Property; import org.apache.felix.ipojo.annotations.Property;
import org.apache.felix.ipojo.annotations.Provides; import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate; import org.apache.felix.ipojo.annotations.Validate;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@Component(managedservice="sally.client") @Component(managedservice="sally.client")
@Provides(specifications=SallyClient.class) @Provides(specifications=SallyClient.class)
@Instantiate @Instantiate
public class SallyClientImpl extends RouteBuilder implements SallyClient { public class SallyClientImpl extends RouteBuilder implements SallyClient {
@Requires
ActiveMQService activeMQ;
@Requires
CamelContextProvider camelContextProvider;
org.apache.camel.Component sallyConn; org.apache.camel.Component sallyConn;
CamelContext context; CamelContext context;
public SallyClientImpl(String host, String user, String password) {
this.host = host;
this.user = user;
this.password = password;
}
@Property(name="SallyHost") @Property(name="SallyHost")
String sallyHost; String host;
@Property(name="SallyUser") @Property(name="SallyUser")
String sallyUser; String user;
@Property(name="SallyPassword") @Property(name="SallyPassword")
String sallyPassword; String password;
public String getSallyHost() { public String getSallyHost() {
return sallyHost; return host;
} }
public String getSallyUser() { public String getSallyUser() {
return sallyUser; return user;
} }
public String getSallyPassword() { public String getSallyPassword() {
return sallyPassword; return password;
} }
public void setSallyHost(String sallyHost) { public void setSallyHost(String sallyHost) {
this.sallyHost = sallyHost; this.host = sallyHost;
} }
public void setSallyPassword(String sallyPassword) { public void setSallyPassword(String sallyPassword) {
this.sallyPassword = sallyPassword; this.password = sallyPassword;
} }
public void setSallyUser(String sallyUser) { public void setSallyUser(String sallyUser) {
this.sallyUser = sallyUser; this.user = sallyUser;
}
public void setActiveMQ(ActiveMQService activeMQ) {
this.activeMQ = activeMQ;
} }
boolean started = false; boolean started = false;
...@@ -86,11 +84,11 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { ...@@ -86,11 +84,11 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient {
@Validate @Validate
public void start() throws Exception { public void start() throws Exception {
context = new DefaultCamelContext(); context = new DefaultCamelContext();
if (sallyHost == null) {
sallyConn = camelContextProvider.getComponent("activemq"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(host);
} else { connectionFactory.setUserName(user);
sallyConn = activeMQ.createActiveMQConnection(sallyHost, sallyUser, sallyPassword); connectionFactory.setPassword(password);
} sallyConn = JmsComponent.jmsComponent(connectionFactory);
context.addComponent("activemq", sallyConn); context.addComponent("activemq", sallyConn);
context.addRoutes(this); context.addRoutes(this);
} }
...@@ -102,7 +100,9 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { ...@@ -102,7 +100,9 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient {
@Override @Override
public void configure() throws Exception { public void configure() throws Exception {
DataFormat core = CommUtils.getDataFormat("core", getClass().getClassLoader()); JAXBContext context = JAXBContext.newInstance("info.kwarc.sally.comm.core");
DataFormat core = new JaxbDataFormat(context);
from("direct:sally_register") from("direct:sally_register")
.setProperty("origRequest", body()) .setProperty("origRequest", body())
...@@ -115,13 +115,14 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { ...@@ -115,13 +115,14 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient {
} }
public void registerResponse(Exchange e) throws Exception { public void registerResponse(Exchange e) throws Exception {
Registerdocumentresponse response = e.getIn().getBody(Registerdocumentresponse.class); RegisterClientResponse response = e.getIn().getBody(RegisterClientResponse.class);
Registerdocument req = e.getProperty("origRequest", Registerdocument.class); RegisterClientRequest req = e.getProperty("origRequest", RegisterClientRequest.class);
RoutesBuilder builder = e.getProperty("builder", RoutesBuilder.class); RoutesBuilder builder = e.getProperty("builder", RoutesBuilder.class);
CamelContext docContext = new DefaultCamelContext(); CamelContext docContext = new DefaultCamelContext();
docContext.addComponent("sallyclient", sallyConn); docContext.addComponent("sallyclient", sallyConn);
ProducerConsumerSplitterComponent comp = new ProducerConsumerSplitterComponent("sallyclient:queue:"+req.getDocumentqueue(), "sallyclient:queue:"+response.getSallyqueue());
ProducerConsumerSplitterComponent comp = new ProducerConsumerSplitterComponent("sallyclient:queue:"+req.getListenQueue(), "sallyclient:queue:"+response.getSendQueue());
comp.setCamelContext(docContext); comp.setCamelContext(docContext);
docContext.addComponent("sally", comp); docContext.addComponent("sally", comp);
docContext.addRoutes(builder); docContext.addRoutes(builder);
...@@ -138,7 +139,7 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { ...@@ -138,7 +139,7 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient {
} }
@Override @Override
public void registerDocument(String docName, String[] interfaces, RoutesBuilder builder) { public void registerDocument(String docName, String[] schemas, RoutesBuilder builder) {
if (!started) { if (!started) {
try { try {
context.start(); context.start();
...@@ -148,11 +149,10 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { ...@@ -148,11 +149,10 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient {
return; return;
} }
} }
Registerdocument regDoc = new Registerdocument(); RegisterClientRequest regDoc = new RegisterClientRequest();
regDoc.setDocumentqueue(genUUID(docName)); regDoc.setListenQueue(genUUID(docName));
regDoc.setEnvironmentid(System.getenv("SALLYENVID")); regDoc.setEnvironmentID(System.getenv("SALLYENVID"));
regDoc.setUserid(""); regDoc.getSchemas().addAll(Arrays.asList(schemas));
regDoc.getInterfaces().addAll(Arrays.asList(interfaces));
getContext().createProducerTemplate().sendBodyAndProperty("direct:sally_register", regDoc, "builder", builder); getContext().createProducerTemplate().sendBodyAndProperty("direct:sally_register", regDoc, "builder", builder);
} }
......
package info.kwarc.sally4.components;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.ComponentConfiguration;
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointConfiguration;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.impl.ParameterConfiguration;
/**
*
* @author Constantin Jucovschi
*
* This component allows to write Camel routes like from("alex") or to("alex")
* and these will be resolved to the necessary
*/
public class ProducerConsumerSplitterComponent implements Component{
String consumerEndpoint, producerEndpoint;
CamelContext context;
public ProducerConsumerSplitterComponent(String consumerEndpoint, String producerEndpoint) {
this.consumerEndpoint = consumerEndpoint;
this.producerEndpoint = producerEndpoint;
}
@Override
public void setCamelContext(CamelContext camelContext) {
this.context = camelContext;
}
@Override
public CamelContext getCamelContext() {
return context;
}
@Override
public Endpoint createEndpoint(String uri) throws Exception {
return new ProducerConsumerSplitterEndpoint(uri, this, producerEndpoint, consumerEndpoint);
}
@Override
public boolean useRawUri() {
return true;
}
@Override
public EndpointConfiguration createConfiguration(String uri)
throws Exception {
return new ProducerConsumerSplitterEndpointConfig(uri);
}
@Override
public ComponentConfiguration createComponentConfiguration() {
return new ProducerConsumerSplitterComponentConfig();
}
class ProducerConsumerSplitterComponentConfig implements ComponentConfiguration {
String baseURI;
Map<String, Object> propertyValues = new HashMap<String, Object>();
@Override
public String getBaseUri() {
return baseURI;
}
@Override
public void setBaseUri(String baseUri) {
this.baseURI = baseUri;
}
@Override
public Map<String, Object> getParameters() {
return propertyValues;
}
@Override
public void setParameters(Map<String, Object> propertyValues) {
this.propertyValues = propertyValues;
}
@Override
public Object getParameter(String name) {
return propertyValues.get("name");
}
@Override
public void setParameter(String name, Object value) {
propertyValues.put(name, value);
}
@Override
public String getUriString() {
return "";
}
@Override
public void setUriString(String newValue) throws URISyntaxException {
}
@Override
public ParameterConfiguration getParameterConfiguration(String name) {
return new ParameterConfiguration(name, String.class);
}
@Override
public SortedMap<String, ParameterConfiguration> getParameterConfigurationMap() {
return new TreeMap<String, ParameterConfiguration> ();
}
@Override
public Endpoint createEndpoint() throws Exception {
return null;
}
@Override
public void configureEndpoint(Endpoint endpoint) {
}
@Override
public Object getEndpointParameter(Endpoint endpoint, String name)
throws RuntimeCamelException {
return null;
}
@Override
public void setEndpointParameter(Endpoint endpoint, String name,
Object value) throws RuntimeCamelException {
// TODO Auto-generated method stub
}
@Override
public List<String> completeEndpointPath(String completionText) {
// TODO Auto-generated method stub
return null;
}
@Override
public String createParameterJsonSchema() {
// TODO Auto-generated method stub
return null;
}
}
class ProducerConsumerSplitterEndpointConfig implements EndpointConfiguration{
HashMap<String, Object> params = new HashMap<String, Object>();
String uri;
public ProducerConsumerSplitterEndpointConfig(String uri) {
this.uri = uri;
}
@Override
public URI getURI() {
return URI.create(uri);
}
@Override
public <T> T getParameter(String name) throws RuntimeCamelException {
return (T) params.get(name);
}
@Override
public <T> void setParameter(String name, T value)
throws RuntimeCamelException {
params.put(name, value);
}
@Override
public String toUriString(UriFormat format) {
return null;
}
}
}
package info.kwarc.sally4.components;
public class ProducerConsumerSplitterComponentConfiguration {
}
package info.kwarc.sally4.components;
import java.net.URI;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.Consumer;
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointConfiguration;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.PollingConsumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.util.URISupport;
public class ProducerConsumerSplitterEndpoint implements Endpoint {
String endPoint;
Component comp;
CamelContext context;
Endpoint alexProducer;
Endpoint alexConsumer;
String producerPrefix;
String consumerPrefix;
ProducerConsumerSplitterEndpoint(String endPoint, Component comp, String producerPrefix, String consumerPrefix) {
this.endPoint = endPoint;
this.comp = comp;
this.context = comp.getCamelContext();
this.producerPrefix = producerPrefix;
this.consumerPrefix = consumerPrefix;
}
public boolean isSingleton() {
return false;
}
public void start() throws Exception {
Map<String, Object> defaultConsumer = URISupport.parseParameters(URI.create(consumerPrefix));
Map<String, Object> defaultProducer = URISupport.parseParameters(URI.create(producerPrefix));
Map<String, Object> params = URISupport.parseParameters(URI.create(endPoint));
defaultConsumer.putAll(params);
defaultProducer.putAll(params);
URI consumerURI = URISupport.createRemainingURI(URI.create(consumerPrefix), defaultConsumer);
URI producerURI = URISupport.createRemainingURI(URI.create(producerPrefix), defaultProducer);
alexConsumer = getCamelContext().getEndpoint(consumerURI.toString());
alexProducer = getCamelContext().getEndpoint(producerURI.toString());
alexConsumer.start();
alexProducer.start();
}
public void stop() throws Exception {
alexConsumer.stop();
alexProducer.stop();
}
public String getEndpointUri() {
return endPoint;
}
public EndpointConfiguration getEndpointConfiguration() {
return null;
}
public String getEndpointKey() {
return "alex";
}
public Exchange createExchange() {
return alexConsumer.createExchange();
}
public Exchange createExchange(ExchangePattern pattern) {
return alexConsumer.createExchange(pattern);
}
@Deprecated
public Exchange createExchange(Exchange exchange) {
return alexConsumer.createExchange(exchange);
}
public CamelContext getCamelContext() {
return comp.getCamelContext();
}
public Producer createProducer() throws Exception {
return alexProducer.createProducer();
}
public Consumer createConsumer(Processor processor) throws Exception {
return alexConsumer.createConsumer(processor);
}
public PollingConsumer createPollingConsumer() throws Exception {
return alexConsumer.createPollingConsumer();
}
public void configureProperties(Map<String, Object> options) {
}
public void setCamelContext(CamelContext context) {
this.context = context;
}
public boolean isLenientProperties() {
return false;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment