diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5601d3bbb232b817dddb815531d4697e507721ab --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target/ +.project +.classpath +/.settings diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..5714caca9f9eceea30b7c5b28d67bfd3ca166193 --- /dev/null +++ b/pom.xml @@ -0,0 +1,73 @@ + + 4.0.0 + + + + org.etsi.osl + org.etsi.osl.main + 1.2.0-SNAPSHOT + ../org.etsi.osl.main + + + org.etsi.osl.centrallog.client + org.etsi.osl.centrallog.client + + + + + + org.apache.camel.springboot + camel-spring-boot-dependencies + ${camel.version} + pom + import + + + + + + + + + + + org.apache.camel.springboot + camel-spring-boot-starter + + + + + org.apache.camel.springboot + camel-http-starter + + + org.apache.camel + camel-activemq + + + org.apache.activemq + activemq-broker + + + + + org.apache.camel.springboot + camel-service-starter + + + org.apache.camel + camel-jackson + + + com.fasterxml.jackson.core + jackson-databind + + + org.apache.camel + camel-stream + + + + + + \ No newline at end of file diff --git a/src/main/java/org/etsi/osl/centrallog/client/CLevel.java b/src/main/java/org/etsi/osl/centrallog/client/CLevel.java new file mode 100644 index 0000000000000000000000000000000000000000..67afd6e4b8d21c97b73ca389ba9954aee5bfcc08 --- /dev/null +++ b/src/main/java/org/etsi/osl/centrallog/client/CLevel.java @@ -0,0 +1,34 @@ +/*- + * ========================LICENSE_START================================= + * CentralLoggerClient + * %% + * Copyright (C) 2019 - 2020 openslice.io + * %% + * 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. + * =========================LICENSE_END================================== + */ +package org.etsi.osl.centrallog.client; + + + + + +/** + * @author ctranoris + * + */ +public enum CLevel { + INFO, + WARN, + ERROR +} diff --git a/src/main/java/org/etsi/osl/centrallog/client/CentralLogMessage.java b/src/main/java/org/etsi/osl/centrallog/client/CentralLogMessage.java new file mode 100644 index 0000000000000000000000000000000000000000..aba9cd288346cbf099ca8e04905fe9a8da9653a4 --- /dev/null +++ b/src/main/java/org/etsi/osl/centrallog/client/CentralLogMessage.java @@ -0,0 +1,104 @@ +/*- + * ========================LICENSE_START================================= + * CentralLoggerClient + * %% + * Copyright (C) 2019 - 2020 openslice.io + * %% + * 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. + * =========================LICENSE_END================================== + */ +package org.etsi.osl.centrallog.client; + +import java.time.Instant; + +/** + * @author ctranoris + * + */ +public class CentralLogMessage { + + private CLevel clevel; + + private String message; + + private String time; + + private String component; + + /** + * + */ + public CentralLogMessage() { + this.clevel=CLevel.WARN; + this.message="This is the default CentralLogger message"; + this.time = Instant.now().toString(); + } + + /** + * @param cLevel + * @param message + */ + public CentralLogMessage(CLevel cLevel, String message) { + super(); + this.clevel = cLevel; + this.message = message; + this.time = Instant.now().toString(); + } + + + /** + * @return the cLevel + */ + public CLevel getclevel() { + return clevel; + } + + /** + * @param cLevel the cLevel to set + */ + public void setclevel(CLevel cLevel) { + this.clevel = cLevel; + } + + /** + * @return the message + */ + public String getMessage() { + return message; + } + + /** + * @param message the message to set + */ + public void setMessage(String message) { + this.message = message; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getComponent() { + return component; + } + + public void setComponent(String component) { + this.component = component; + } + + +} diff --git a/src/main/java/org/etsi/osl/centrallog/client/CentralLogPackage.java b/src/main/java/org/etsi/osl/centrallog/client/CentralLogPackage.java new file mode 100644 index 0000000000000000000000000000000000000000..5ef24feecea17e90ee229f85398ea3f2ce6ab823 --- /dev/null +++ b/src/main/java/org/etsi/osl/centrallog/client/CentralLogPackage.java @@ -0,0 +1,51 @@ +/*- + * ========================LICENSE_START================================= + * CentralLoggerClient + * %% + * Copyright (C) 2019 - 2020 openslice.io + * %% + * 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. + * =========================LICENSE_END================================== + */ +package org.etsi.osl.centrallog.client; + + +public class CentralLogPackage { + String endpoint; + CentralLogMessage centrallogmsg; + + /** + * + */ + public CentralLogPackage() { + this.endpoint = ""; + this.centrallogmsg = new CentralLogMessage(); + } + + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + public CentralLogMessage getCentralLogMessage() + { + return this.centrallogmsg; + } + public void setCentralLogMessage(CentralLogMessage clm) + { + this.centrallogmsg=clm; + } +} diff --git a/src/main/java/org/etsi/osl/centrallog/client/CentralLogger.java b/src/main/java/org/etsi/osl/centrallog/client/CentralLogger.java new file mode 100644 index 0000000000000000000000000000000000000000..4f2a281761de14691db840bf44871d9ec4ae6b25 --- /dev/null +++ b/src/main/java/org/etsi/osl/centrallog/client/CentralLogger.java @@ -0,0 +1,166 @@ +package org.etsi.osl.centrallog.client; +/*- + * ========================LICENSE_START================================= + * org.etsi.osl.portal.api + * %% + * Copyright (C) 2019 openslice.io + * %% + * 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. + * =========================LICENSE_END================================== + */ + +import java.util.concurrent.Future; + +import org.apache.camel.Exchange; +import org.apache.camel.FluentProducerTemplate; +import org.apache.camel.ProducerTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * @author ctranoris + * + * + */ +@Configuration +@Component +public class CentralLogger { + + /** the Camel Context configure via Spring. See bean.xml*/ +// private static CamelContext actx; + + + @Autowired + private ProducerTemplate template; + +// private static String centralloggerurl = null; +// +// public static String getCentralLoggerUrl() +// { +// return centralloggerurl; +// } +// +// public CentralLogger(String centralloggerurl_tmp) { +// super(); +// centralloggerurl = centralloggerurl_tmp; +// } +// +// public static void setCentralLoggerUrl(String centralLoggerUrl) +// { +// centralloggerurl=centralLoggerUrl; +// } + +// public void setActx(CamelContext actx) { +// CentralLogger.actx = actx; +// } + + + /** + * @param cl + * @param amessage + * @param componentName + */ +// public void log(CLevel cl, String amessage) { +// Map map = new HashMap(); +// map.put("endpoint", this.centralloggerurl ); +// map.put("clevel", cl.toString() ); +// map.put("message", amessage ); +// log(map); +// } + +// public static void log(Map map) +// { +// String json; +// try { +// json = new ObjectMapper().writeValueAsString(map); +// //System.out.println(json); +// FluentProducerTemplate template = actx.createFluentProducerTemplate().to("seda:centralLog?multipleConsumers=true"); +// Future result = template.withBody( json ).asyncSend(); +// waitAndStopForTemplate( result, template); +// +// } catch (JsonProcessingException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } + + public void log(CLevel cl, String amessage, String component) { + +// CentralLogPackage clp = new CentralLogPackage(); +// clp.setEndpoint(centralloggerurl); + CentralLogMessage clm = new CentralLogMessage(); + clm.setclevel(cl); + clm.setMessage(amessage); + clm.setComponent(component); +// clp.setCentralLogMessage(clm); + try { + log(clm); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void log(CentralLogMessage clm) throws Exception + { +// if ( actx==null) { +// throw new Exception("actx is null"); +// } + if ( template==null) { + throw new Exception("template is null"); + } + String json; + json = new ObjectMapper().writeValueAsString(clm); +// FluentProducerTemplate template = actx.createFluentProducerTemplate() +// .to("activemq:queue:centrallogger.log"); + //Future result = template.withBody( json ).asyncSend(); + //waitAndStopForTemplate( result, template); + template.sendBody("activemq:queue:centrallogger.log", json); + + } + + /** + * + * utility function to stop ProducerTemplate + * @param result + * @param template + */ + private void waitAndStopForTemplate(Future result, FluentProducerTemplate template) { + while (true) { + if (result.isDone()) { + //logger.info( "waitAndStopForTemplate: " + template.toString() + " [STOPPED]"); + try { + template.stop(); + //template.clearAll(); + template.cleanUp(); + break; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + try { + //logger.info( "waitAndStopForTemplate: " + template.toString() + " [WAITING...]"); + Thread.sleep( 5000 ); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } +}