I posted this on another forum and wanted to see if I can reach more people.
I am working on an application that consists of different Spring web apps.
Say we have:
ComponentA.jar
ComponentB.jar
And WAR files:
Foo.war (contains ComponentA)
Baa.war (contains ComponentA &
ComponentB)
We are using Logback to log to our debug log. So say that the various classes of the application have the the following logger declaration:
private static final Log log = LoggerFactory.getLogger(NAME_OF_WAR_FILE + "." + NAME_OF_CONTAINING_COMPONENT + "." + PACKAGE.CLASS_NAME);
So example:
package a.b.c;
public class SomeClass {
private static final Log log = LoggerFactory.getLogger("Foo.war" + "." + "ComponentA" + "." + SomeClass.class);
}
package x.y.z;
public class SomeOtherClass {
private static final Log log = LoggerFactory.getLogger("Baa.war" + "." + "ComponentA" + "." + SomeOtherClass .class);
}
Assume that the name of the war file and component is set by a property and not hard-coded.
Is it possible to have an Aspect and Advice that can do something like the following (pseudo since I'm not sure it can be done):
#Aspect
public class TheAspect{
#Around("execution of a method")
public Object aroundSomething(ProceedingJoinPoint pjp){
Log log = get the log instance from the class that this advice is running on
if(log.isDebugEnabled())
// log something
Object o = pjp.proceed();
if(log.isDebugEnabled())
// log something else
return o;
}
}
The point here is to write to the log file using the log of the class instance which contains the method which is being intercepted by the Advice.
The application is presented as a single web app composed of Foo.war and Baa.war. Both Foo.war and Baa.war write to the same log file.
Example:
2011-09-22 14:35:35.159 MDT,DEBUG,Foo.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentB.x.y.z.SomeOtherClass,Hello World Debug message
Thanks in advance.
You can use thisJoinPoint inside your aroundSomething method.
To get the class name:
Signature sig = thisJoinPoint.getSignature();
String className = sig.getDeclaringTypeName();
You can also get the class object:
Class<?> type = sig.getDeclaringType();
And maybe you can use the package to identify your war file:
Package pack type.getPackage();
Related
I'm using Citrus 2.7.8 with Cucumber 2.4.0. I'm making a soap call and want to get the response and do some advanced parsing on it to validate a graphql response has matching values. (I understand how to do validations when it's something that just has one element, but I need something able to handle when there could be one or many elements returned (for example, 1 vehicle or 4 vehicles)). To make my validation very dynamic and able to handle many different 'quotes', I want to store the response to a Citrus variable and then make it available to java to read in the file and do the advanced parsing and validation.
The TestContext injection doesn't appear to currently work with cucumber (see https://github.com/citrusframework/citrus/issues/657) so I'm using the workaround here:
How to inject TestContext using TestRunner and cucumber to manually create the context. Without this I get a nullpointerexception on anything with the context.
I am able to use Citrus's message function to grab the soap response which is awesome. My echo statements in the console show that it successfully put the right value into the citrus variable. But I'm having problems making that available to java so that I can then open it up and parse through it.
I've scaled down my step definition file to just the pertinent code. My couple attempts are listed below along with the problems I encountered in their results.
Does anyone have any ideas on how I can successfully workaround the context issues and make my response available to java?
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import com.consol.citrus.Citrus;
import com.consol.citrus.annotations.CitrusFramework;
import com.consol.citrus.annotations.CitrusResource;
import com.consol.citrus.config.CitrusSpringConfig;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;
import com.consol.citrus.dsl.runner.TestRunner;
import com.consol.citrus.ws.client.WebServiceClient;
import cucumber.api.java.en.When;
#ContextConfiguration(classes = CitrusSpringConfig.class)
public class CitrusSteps extends JUnit4CitrusTestRunner {
#CitrusFramework
private Citrus citrus;
#CitrusResource
private TestRunner runner;
#CitrusResource
private TestContext context;
#Autowired
private WebServiceClient getQuote;
#When("^I call getQuote with id \"([^\"]*)\"$")
public void i_call_getquote_with_id(String quoteId) throws Throwable {
context = citrus.createTestContext();
String soappayload = "my payload (taken out for privacy purposes)";
runner.soap(action -> action.client(getQuote)
.send()
.soapAction("getQuote")
.payload(soappayload));
runner.soap(action -> action.client(getQuote)
.receive()
.name("getQuoteResponseStoredMessage"));
//this bombs out on the context line with this: "com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'messageStoreGetQuoteResponse1'"
runner.variable("messageStoreGetQuoteResponse1", "citrus:message(getQuoteResponseStoredMessage.payload())");
runner.echo("First try: ${messageStoreGetQuoteResponse1}");
String firstTry = context.getVariable("messageStoreGetQuoteResponse1");
log.info("First Try java variable: " + firstTry);
//this bombs out on the context line with this: "com.consol.citrus.exceptions.CitrusRuntimeException: Unknown variable 'messageStoreGetQuoteResponse2'"
runner.createVariable("messageStoreGetQuoteResponse2", "citrus:message(getQuoteResponseStoredMessage.payload())");
runner.echo("Second try: ${messageStoreGetQuoteResponse2}");
String secondTry = context.getVariable("messageStoreGetQuoteResponse2");
log.info("Second Try java variable: " + secondTry);
//This stores the literal as the value - it doesn't store the message so it appears I can't use citrus functions within the context
context.setVariable("messageStoreGetQuoteResponse3", "citrus:message(getQuoteResponseStoredMessage.payload())");
String thirdTry = context.getVariable("messageStoreGetQuoteResponse3");
log.info("Third Try java variable: " + thirdTry);
}
}
A smart co-worker figured out a workaround for the injection not working w/ cucumber.
I replaced these two lines:
#CitrusResource
private TestContext context;
with these lines instead:
TestContext testContext;
public TestContext getTestContext() {
if (testContext == null) {
runner.run(new AbstractTestAction() {
#Override
public void doExecute(TestContext context) {
testContext = context;
}
});
}
return testContext;
}
Then within my step where I want the context, I can use the above method. In my case I wanted my message response, so I was able to use this and confirm that the response is now in my java variable:
String responseXML = getTestContext().getMessageStore().getMessage("getQuoteResponseStoredMessage").getPayload(String.class);
log.info("Show response XML: " + responseXML);
I'm using spring-integration bundled with spring-batch and got stuck trying to write integration tests to test the whole flow, not just single config.
I've created Embedded Sftp Server for this tests and trying to send message to sftpInboundChannel - the message is sent, but nothing happens, but when i send this message to the next channel (after sftpInboundChannel) it goes ok. Also i'm not able to load test source properties, even though i'm using #TestPropertySource annotation.
This are my class annotations
#TestPropertySource(properties = {
//here goes all the properties
})
#EnableConfigurationProperties
#RunWith(SpringRunner.class)
#Import({TestConfig.class, SessionConfig.class})
#ActiveProfiles("it")
#SpringIntegrationTest
#EnableIntegration
#SpringBootTest
#DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
This is my class body
#Autowired
private PollableChannel sftpInboundChannel;
#Autowired
private SessionFactory<ChannelSftp.LsEntry> defaultSftpSessionFactory;
#Autowired
private EmbeddedSftpServer server;
#Test
public void shouldDoSmth() {
RemoteFileTemplate<ChannelSftp.LsEntry> template;
try {
template = new RemoteFileTemplate<>(defaultSftpSessionFactory);
SftpTestUtils.moveToRemoteFolder(template);
final List<ChannelSftp.LsEntry> movedFiles = SftpTestUtils.listFilesFromDirectory("folder/subfolder", template);
log.info("Moved file {}", movedFiles.size());
final MessageBuilder<String> messageBuilder = MessageBuilder.withPayload("Sample.txt") // path to file
.setHeader("file_Path", "Sample.txt")
boolean wasSent = this.sftpInboundChannel.send(messageBuilder.build());
log.info("Was sent to sftpInboundChannel channel {}", wasSent);
log.info("message {}", messageBuilder.build());
} finally {
SftpTestUtils.cleanUp();
}
}
To the case of not read the property file one solution is add in your Test class something like this:
#BeforeClass
public static void beforeClass() {
System.setProperty("propertyfile", "nameOfFile.properties");
}
A second way is to create a xml (or class) config where you add the tag:
<context:property-placeholder
location="nameOfFile.properties"
ignore-resource-not-found="true" system-properties-mode="OVERRIDE" />
and your file will be localized.
The property file should be inside of resources folder.
I am trying to do something very simple. I have a com.mypackage.Logger logger class whose instantiation statement I would like to "insert" into every single class like so: private static Logger LOG = new Logger(Class.class). Then, I would like to log every single entry and exit instance for every single function in my project. Here is my aspect:
public aspect LoggingAspect pertypewithin(*) {
private static Logger LOG;
pointcut classes(): within(com.mypackage..*) && !within(com.mypackage.Logger) && !within(com.mypackage.LoggingAspect);
pointcut functions(): classes() && (execution(* *(..)) || execution(new(..)));
before(): staticinitialization(*) && classes() {
LOG = new Logger(thisJoinPointStaticPart.getSignature().getDeclaringType());
}
before() : functions() {
LOG.trace("ENTER " + thisJoinPoint.getSignature().toLongString());
}
after() returning(#SuppressWarnings("unused") Object ret) : functions() {
LOG.trace("EXIT " + thisJoinPoint.getSignature().toLongString());
}
Almost everything works properly. I am getting correct enter and exist log statements exactly as expected. The problem is that the logging class that is associated with each log entry is incorrect. I am using log4j, and each log entry is formatted like so:
[TRACE] (date and time stamp) (logging class name) (thread name) (some logging statement)
The problem is that the logging class used in Logger instantiation does not match the correct one that is indicated by thisJoinPoint.getSignature().getDeclaringTypeName().
I know that I am not doing something right with respect to the static Logger variable, so please help me. thank you for your time!!!
It's simple
Your LOG attribute is defined as private static. Static means that's a class attribute, not instance attribute.
This is clearly contradicting the instanciation model of your aspect, which is pertypewithin (one instance of aspect created for each type).
Try to remove the static modifier.
By the way, defining pertypewithin()* is quite large, you can restrict down the matching with pertypewithin(classes())
For the logging stuff, I've done some experimentations with AspectJ using instanciation model & inter-type declarations. I would advise the implementation using inter-type declaration because it is more memory-saving:
Logger injection with perthis
Logger injection with inter-type declaration
I'm using GlassFish Tools Bundle for Eclipse.
I need to create a bean and a client that tests it. The bean (and its interface) are the following.
package mykPK;
import java.math.BigDecimal;
import javax.ejb.*;
#Stateless
public class ConverterBean implements Converter {
private BigDecimal yenRate = new BigDecimal("115.3100");
private BigDecimal euroRate = new BigDecimal("0.0071");
public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
}
The interface:
package mykPK;
import java.math.BigDecimal;
import javax.ejb.Remote;
#Remote
public interface Converter {
public BigDecimal dollarToYen(BigDecimal dollars);
public BigDecimal yenToEuro(BigDecimal yen);
}
I create them correctly in an EJB project and run them "as a server". All seems to start correctly.
Now I want to create a client.
I tried to put the client inside the same project, creating a different project ("Application Client Project") or even creating a more general "E application project" with two subproject. The result is the same.
Now, the client code is the following
import java.math.BigDecimal;
import javax.ejb.EJB;
import mykPK.Converter; /*of course to to that, i reference in the client project the
EJB project*/
public class ConverterClient {
#EJB private static Converter converter;
public ConverterClient(String[] args) {
}
public static void main(String[] args) {
ConverterClient client = new ConverterClient(args);
client.doConversion();
}
public void doConversion() {
try {
BigDecimal param = new BigDecimal("100.00");
BigDecimal yenAmount = converter.dollarToYen(param);
System.out.println("$" + param + " is " + yenAmount
+ " Yen.");
BigDecimal euroAmount = converter.yenToEuro(yenAmount);
System.out.println(yenAmount + " Yen is " + euroAmount
+ " Euro.");
System.exit(0);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
When I run this file, i always get the same:
Caught an unexpected exception!
java.lang.NullPointerException
at ConverterClient.doConversion(ConverterClient.java:17)
at ConverterClient.main(ConverterClient.java:12)
I suppose this is beacause my client is not in the same container of the bean, and it is not "deployed" (I simply run the file). But when I tried the more general "Enterprise Application Project" the results were the same)
So, where to put the client and give him the access (with #EJB) to the Bean??
You're trying to inject into a non-managed object. You need to grab the initial context and look it up.
Pretty much the same thing as here:
cannot find my bean using the InitialContext.lookup() method
The stack trace suggests that you've directly launched the main method. In order to use injection in the main class, you must use the application client container.
A good example of this working can be found here Packaging your client for use with glassfish's application client container (via the "appclient" command) is shown, as is packaging it as a standalone Java app.
I wrote a simple Hello World Servlet in Eclipse containing the following in the doGet method of my HelloWorldServlet.java
PrintWriter writer = response.getWriter();
String hello = PropertyLoader.bundle.getProperty("hello");
writer.append(hello);
writer.flush();
PropertyLoader is a simple class in the same package as the Servlet that does the following:
public class PropertyLoader {
public static final Properties bundle = new Properties();
static {
InputStream stream = null;
URL url = PropertyLoader.class.getResource("/helloSettings.properties");
stream = new FileInputStream(url.getFile());
bundle.load(stream);
}
}//End of class
I placed a file called helloSettings.properties in /WebContent/WEB-IND/classes that contains the following single line of content:
hello=Hello Settings World
When I add Tomcat 6.0 to my project and run it in eclipse it successfully prints
"Hello Settings World" to the web browser.
However when I export the project as a war file and manually place it in
.../Tomcat 6.0/webapps I then get "null" as my result.
Is it a problem with the classpath/classloader configuration? permissions? any of the other configuration files? I know for a fact that the helloSettings.properties file is in the WEB-INF/classes folder.
Any help?
Well, after much browsing I found what seems a "normal" why to do what I'm trying to do:
Instead of...(how I was doing it)
public class PropertyLoader {
public static final Properties bundle = new Properties();
static {
InputStream stream = null;
URL url = PropertyLoader.class.getResource("/helloSettings.properties");
stream = new FileInputStream(url.getFile());
bundle.load(stream);
}
}//End of class
THE FIX
public class PropertyLoader {
public static final Properties bundle = new Properties();
static {
InputStream stream = null;
stream = SBOConstants.class.getResourceAsStream("/sbonline.properties");
bundle.load(stream);
}
}//End of class
I'm modifiying someone else's code so I'm not sure why they did it the other way in the first place... but I guess url.getFile() was my problem and I don't know why.