From fcb8fb05c9dcc21a7915c161dba219c226e8ada3 Mon Sep 17 00:00:00 2001 From: Constantin Jucovschi <jucovschi@gmail.com> Date: Sun, 17 Aug 2014 22:05:08 +0200 Subject: [PATCH] updated --- pom.xml | 35 ++-- .../sally4/client/impl/SallyClientImpl.java | 98 ++++----- .../ProducerConsumerSplitterComponent.java | 190 ++++++++++++++++++ ...onsumerSplitterComponentConfiguration.java | 5 + .../ProducerConsumerSplitterEndpoint.java | 118 +++++++++++ 5 files changed, 376 insertions(+), 70 deletions(-) create mode 100644 src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponent.java create mode 100644 src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponentConfiguration.java create mode 100644 src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterEndpoint.java diff --git a/pom.xml b/pom.xml index b089746..204a062 100644 --- a/pom.xml +++ b/pom.xml @@ -17,14 +17,12 @@ <packaging>bundle</packaging> - <groupId>info.kwarc.sally4</groupId> - <artifactId>sally4-client</artifactId> - <version>0.0.4-SNAPSHOT</version> + <artifactId>client-java-camel</artifactId> <parent> <groupId>info.kwarc.sally4</groupId> <artifactId>sally4</artifactId> - <version>0.0.4-SNAPSHOT</version> + <version>0.0.4</version> <relativePath>..</relativePath> </parent> @@ -34,18 +32,6 @@ </properties> <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> <groupId>info.kwarc.sally4.comm</groupId> <artifactId>comm-core</artifactId> @@ -53,16 +39,23 @@ </dependency> <dependency> - <groupId>javax.servlet</groupId> - <artifactId>servlet-api</artifactId> - <version>2.5</version> - <scope>compile</scope> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-camel</artifactId> + <version>5.9.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-stomp</artifactId> + <artifactId>camel-jaxb</artifactId> <version>${camel.version}</version> </dependency> + + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <version>1.7.7</version> + </dependency> + </dependencies> </project> diff --git a/src/main/java/info/kwarc/sally4/client/impl/SallyClientImpl.java b/src/main/java/info/kwarc/sally4/client/impl/SallyClientImpl.java index 8f65376..e625cf0 100644 --- a/src/main/java/info/kwarc/sally4/client/impl/SallyClientImpl.java +++ b/src/main/java/info/kwarc/sally4/client/impl/SallyClientImpl.java @@ -1,20 +1,22 @@ package info.kwarc.sally4.client.impl; -import info.kwarc.sally.comm.core.Registerdocument; -import info.kwarc.sally.comm.core.Registerdocumentresponse; -import info.kwarc.sally4.activemq.ActiveMQService; +import info.kwarc.sally.comm.core.RegisterClientRequest; +import info.kwarc.sally.comm.core.RegisterClientResponse; import info.kwarc.sally4.client.SallyClient; 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.UUID; +import javax.xml.bind.JAXBContext; + +import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.RoutesBuilder; 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.spi.DataFormat; import org.apache.felix.ipojo.annotations.Component; @@ -22,75 +24,71 @@ import org.apache.felix.ipojo.annotations.Instantiate; import org.apache.felix.ipojo.annotations.Invalidate; import org.apache.felix.ipojo.annotations.Property; import org.apache.felix.ipojo.annotations.Provides; -import org.apache.felix.ipojo.annotations.Requires; import org.apache.felix.ipojo.annotations.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + @Component(managedservice="sally.client") @Provides(specifications=SallyClient.class) @Instantiate public class SallyClientImpl extends RouteBuilder implements SallyClient { - @Requires - ActiveMQService activeMQ; - - @Requires - CamelContextProvider camelContextProvider; - org.apache.camel.Component sallyConn; - + CamelContext context; + public SallyClientImpl(String host, String user, String password) { + this.host = host; + this.user = user; + this.password = password; + } + @Property(name="SallyHost") - String sallyHost; + String host; @Property(name="SallyUser") - String sallyUser; + String user; @Property(name="SallyPassword") - String sallyPassword; - + String password; + public String getSallyHost() { - return sallyHost; + return host; } - + public String getSallyUser() { - return sallyUser; + return user; } - + public String getSallyPassword() { - return sallyPassword; + return password; } - + public void setSallyHost(String sallyHost) { - this.sallyHost = sallyHost; + this.host = sallyHost; } - + public void setSallyPassword(String sallyPassword) { - this.sallyPassword = sallyPassword; + this.password = sallyPassword; } - + public void setSallyUser(String sallyUser) { - this.sallyUser = sallyUser; - } - - public void setActiveMQ(ActiveMQService activeMQ) { - this.activeMQ = activeMQ; + this.user = sallyUser; } - + boolean started = false; - + Logger log = LoggerFactory.getLogger(getClass()); @Validate public void start() throws Exception { context = new DefaultCamelContext(); - if (sallyHost == null) { - sallyConn = camelContextProvider.getComponent("activemq"); - } else { - sallyConn = activeMQ.createActiveMQConnection(sallyHost, sallyUser, sallyPassword); - } + + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(host); + connectionFactory.setUserName(user); + connectionFactory.setPassword(password); + sallyConn = JmsComponent.jmsComponent(connectionFactory); context.addComponent("activemq", sallyConn); context.addRoutes(this); } @@ -102,7 +100,9 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { @Override 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") .setProperty("origRequest", body()) @@ -115,13 +115,14 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { } public void registerResponse(Exchange e) throws Exception { - Registerdocumentresponse response = e.getIn().getBody(Registerdocumentresponse.class); - Registerdocument req = e.getProperty("origRequest", Registerdocument.class); + RegisterClientResponse response = e.getIn().getBody(RegisterClientResponse.class); + RegisterClientRequest req = e.getProperty("origRequest", RegisterClientRequest.class); RoutesBuilder builder = e.getProperty("builder", RoutesBuilder.class); CamelContext docContext = new DefaultCamelContext(); 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); docContext.addComponent("sally", comp); docContext.addRoutes(builder); @@ -138,7 +139,7 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { } @Override - public void registerDocument(String docName, String[] interfaces, RoutesBuilder builder) { + public void registerDocument(String docName, String[] schemas, RoutesBuilder builder) { if (!started) { try { context.start(); @@ -148,11 +149,10 @@ public class SallyClientImpl extends RouteBuilder implements SallyClient { return; } } - Registerdocument regDoc = new Registerdocument(); - regDoc.setDocumentqueue(genUUID(docName)); - regDoc.setEnvironmentid(System.getenv("SALLYENVID")); - regDoc.setUserid(""); - regDoc.getInterfaces().addAll(Arrays.asList(interfaces)); + RegisterClientRequest regDoc = new RegisterClientRequest(); + regDoc.setListenQueue(genUUID(docName)); + regDoc.setEnvironmentID(System.getenv("SALLYENVID")); + regDoc.getSchemas().addAll(Arrays.asList(schemas)); getContext().createProducerTemplate().sendBodyAndProperty("direct:sally_register", regDoc, "builder", builder); } diff --git a/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponent.java b/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponent.java new file mode 100644 index 0000000..448fb2c --- /dev/null +++ b/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponent.java @@ -0,0 +1,190 @@ +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; + } + + } + +} diff --git a/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponentConfiguration.java b/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponentConfiguration.java new file mode 100644 index 0000000..0cf299d --- /dev/null +++ b/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterComponentConfiguration.java @@ -0,0 +1,5 @@ +package info.kwarc.sally4.components; + +public class ProducerConsumerSplitterComponentConfiguration { + +} diff --git a/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterEndpoint.java b/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterEndpoint.java new file mode 100644 index 0000000..96922d7 --- /dev/null +++ b/src/main/java/info/kwarc/sally4/components/ProducerConsumerSplitterEndpoint.java @@ -0,0 +1,118 @@ +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; + } + + +} -- GitLab