How to configure log4j for an xtext gradle build? - gradle

When I start the gradle build in one of my modules, it prints an error-message to std-error:
:m28_presentation_api:generateXtext
Error initializing JvmElement
That's not very helpful and I hope, that I can configure log4j to print more details about the exception.
I think this message is logged by JvmTypesBuilder.initializeSafely()
LOG.error("Error initializing JvmElement", e);
Versions:
I am using xtext 2.13: in the MANIFEST.MF file, I see the
log4j version: 1.2.15
gradle version: 4.6
xtext-gradle-plugin version: 1.0.21
According to the log4j V1 docs, it should be enough when I add a log4j.properties file to the classpath: so I just save this file in src/main/java.
But it seems that this is not used/found - or maybe I did something wrong in the configuration file:
log4j.rootLogger=stderr
log4j.appender.stderr=org.apache.log4j.ConsoleAppender
log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stderr.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
When I now start the build, I'd expect a different log-output for the error-message, but it prints the same message as before. So obviously my log-config is not used for some reasons.
What am I missing?
Or can someone maybe point me to an example project?

it looks like LOG.error() does not print a stacktrace by default. maybe you can actively change your code e.g.
class MyDslJvmModelInferrer extends AbstractModelInferrer {
#Inject extension JvmTypesBuilder
private static Logger LOG = Logger.getLogger(MyDslJvmModelInferrer);
def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass(element.name, [
try {
throw new IllegalArgumentException("mimimi")
} catch (Exception e) {
e.printStackTrace
throw e
}
]))
}
}

This is not really an answer, but a workaround (maybe helpful for others).
I gave up on the log4j configuration after wasting some hours and applied this workaround which took only some minutes and revealed the real problem.
What I've done, is to create my own JvmTypesBuilder, override the initializeSafely method and directly print the stack-trace to stderr:
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1
import org.apache.log4j.Logger
class JvmTypesBuilderTm extends JvmTypesBuilder {
private static Logger LOG = Logger.getLogger(JvmTypesBuilder)
// TODO: nasty workaround because I cannot figure out how to configure the logging correctly
override <T extends EObject> initializeSafely(T targetElement, Procedure1<? super T> initializer) {
if(targetElement !== null && initializer !== null) {
try {
initializer.apply(targetElement);
} catch (Exception e) {
LOG.error("Error initializing JvmElement: "+targetElement.toString, e);
e.printStackTrace
}
}
return targetElement;
}
}
Then I just replaced all occurrences of the JvmTypesBuilder in my code with the new one. With the stacktrace, it was easy to find the real issue in my code.

Related

JOOQ execution listener does not catch exception

I'm trying to implement a generic solution for optimized locking. What I want to achieve is to have a specific piece of code run when record's version changes. I have it implemented as an ExecuteListener instance that looks for DataChangedException. It's registered as a Spring bean.
class LockingListener : DefaultExecuteListener() {
override fun exception(ctx: ExecuteContext) {
val exception = ctx.exception()
if (exception is DataChangedException) {
ctx.exception(IllegalStateException("Accessed data has been altered mid-operation."))
}
}
}
#Configuration
class JooqConfig {
#Bean
fun lockingListenerProvider() = DefaultExecuteListenerProvider(LockingListener())
}
I had a breakpoint set in org.jooq.impl.ExecuteListeners#get and it does look like it gets picked up alongside LoggerListener and JooqExceptionTranslator.
When I try to run a test case though, DataChangedException does not get picked up on UpdateableRecord#update and I get the following stacktrace instead, no IllegalStateException in sight.
org.jooq.exception.DataChangedException: Database record has been changed or doesn't exist any longer
at org.jooq.impl.UpdatableRecordImpl.checkIfChanged(UpdatableRecordImpl.java:540)
at org.jooq.impl.UpdatableRecordImpl.storeMergeOrUpdate0(UpdatableRecordImpl.java:349)
at org.jooq.impl.UpdatableRecordImpl.storeUpdate0(UpdatableRecordImpl.java:241)
at org.jooq.impl.UpdatableRecordImpl.access$100(UpdatableRecordImpl.java:89)
at org.jooq.impl.UpdatableRecordImpl$2.operate(UpdatableRecordImpl.java:232)
at org.jooq.impl.RecordDelegate.operate(RecordDelegate.java:149)
at org.jooq.impl.UpdatableRecordImpl.storeUpdate(UpdatableRecordImpl.java:228)
at org.jooq.impl.UpdatableRecordImpl.update(UpdatableRecordImpl.java:165)
Debugging shows that LockingListener#exception does not even get entered into.
That exception is not part of the ExecuteListener lifecycle, i.e. the lifecycle that deals with interactions with the JDBC API. In other words, it's not a SQLException, it happens higher up the stack. Use the RecordListener.exception() callback, instead.

How to configure the ObjectMapper for Unirest in a spring boot project

I am using Unirest in a project which is working fine for me. However, I want to post some data and do not want to escape all the JSON as it looks ugly and is just a pain in the neck.
I found a few links on how to configure the ObjectMapper for Unirest and it gives this code.
Unirest.setObjectMapper(new ObjectMapper() {
com.fasterxml.jackson.databind.ObjectMapper mapper =
new com.fasterxml.jackson.databind.ObjectMapper();
public String writeValue(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public <T> T readValue(String value, Class<T> valueType) {
try {
return mapper.readValue(value, valueType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
But, no examples show where it is best to do this in a Spring Boot API project.
I tried to set it up in the main class method, but I am getting an error that 'setObjectMapper' cannot be resolved. I also tried to do this in the controller but I get the same error.
My Gradle deps for these two libraries are:
// https://mvnrepository.com/artifact/com.mashape.unirest/unirest-java
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.5'
compile 'com.fasterxml.jackson.core:jackson-databind:2.10.1'
Can anyone show me how to use the Jackson object mapper with Unirest in a Spring Boot API project as I have been googling and reading docs for two days now. Would appreciate some help.
Thank you in advance
You have several issues here:
The version of unirest you're using (1.4.5) does not contain the feature to configure object mapper. This feature was added later (github PR). So you should update to the latest version available at maven central - 1.4.9. This alone will fix your compilation problem.
You can keep your Unirest configuration code in the main method. However if you want to use not default jackson ObjectMapper(), but the one from the spring context, then it's better to create something like a fake spring bean to inject ObjectMapper:
#Configuration
public class UnirestConfig {
#Autowired
private com.fasterxml.jackson.databind.ObjectMapper mapper;
#PostConstruct
public void postConstruct() {
Unirest.setObjectMapper(new ObjectMapper() {
public String writeValue(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public <T> T readValue(String value, Class<T> valueType) {
try {
return mapper.readValue(value, valueType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
}
Other than that it looks this library changed the package name. Now it's com.konghq. You might want to consider updating, but library API might have changed.
Upd: for the latest version
compile group: 'com.konghq', name: 'unirest-java', version: '3.1.04'
the new API is Unirest.config().setObjectMapper(...)

What is the best alternative for #ConfigurationProperties locations?

#ConfigurationProperties locations is deprecated in Spring Boot 1.4.x and option is now removed in 1.5.x
I was using it like this: BucketTestConfig.java
For now with deprecation, I'm trying to set the system property spring.config.location for both production code and test code as an alternative.
./gradlew clean test is still failing although I set the system property.
What is the best alternative for deprecated #ConfigurationProperties locations in this case?
UPDATE:
Using SpringApplicationBuilder.properties() doesn't work in the test (BucketTestRepositoryTests).
Using SpringApplicationBuilder.listeners() doesn't work in the test (BucketTestRepositoryTests), either.
UPDATE (2nd):
There was no reason to depend on #ConfigurationProperties in my case, so I went with Yaml instead as follows: https://github.com/izeye/spring-boot-throwaway-branches/commit/a1290672dceea98706b1a258f8a17e2628ea01ee
So this question's title is invalid and this question can be deleted.
Follow this thread for more information.
Basically, this thread suggests two options
First option is to set spring.config.name to a list of the files you want to load:
new SpringApplicationBuilder(Application.class)
.properties("spring.config.name=application,mine")
.run(args);
Second options is to add listeners
new SpringApplicationBuilder(SanityCheckApplication.class)
.listeners(new LoadAdditionalProperties())
.run(args);
Content of LoadAdditionalProperties
#Component
public class LoadAdditionalProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private ResourceLoader loader = new DefaultResourceLoader();
#Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
try {
Resource resource = loader.getResource("classpath:mine.properties");
PropertySource<?> propertySource = new PropertySourcesLoader().load(resource);
event.getEnvironment().getPropertySources().addLast(propertySource);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}

File inside jar is not visible for spring

All
I created a jar file with the following MANIFEST.MF inside:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)
Main-Class: my.Main
Class-Path: . lib/spring-core-3.2.0.M2.jar lib/spring-beans-3.2.0.M2.jar
In its root there is a file called my.config which is referenced in my spring-context.xml like this:
<bean id="..." class="...">
<property name="resource" value="classpath:my.config" />
</bean>
If I run the jar, everything looks fine escept the loading of that specific file:
Caused by: java.io.FileNotFoundException: class path resource [my.config] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/my.jar!/my.config
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:32)
at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:1)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 22 more
classes are loaded the from inside the jar
spring and other dependencies are loaded from separated jars
spring context is loaded (new ClassPathXmlApplicationContext("spring-context/applicationContext.xml"))
my.properties is loaded into PropertyPlaceholderConfigurer ("classpath:my.properties")
if I put my .config file outside the file system, and change the resource url to 'file:', everything seems to be fine...
Any tips?
If your spring-context.xml and my.config files are in different jars then you will need to use classpath*:my.config?
More info here
Also, make sure you are using resource.getInputStream() not resource.getFile() when loading from inside a jar file.
In the spring jar package, I use new ClassPathResource(filename).getFile(), which throws the exception:
cannot be resolved to absolute file path because it does not reside in the file system: jar
But using new ClassPathResource(filename).getInputStream() will solve this problem. The reason is that the configuration file in the jar does not exist in the operating system's file tree,so must use getInputStream().
I know this question has already been answered. However, for those using spring boot, this link helped me - https://smarterco.de/java-load-file-classpath-spring-boot/
However, the resourceLoader.getResource("classpath:file.txt").getFile(); was causing this problem and sbk's comment:
That's it. A java.io.File represents a file on the file system, in a
directory structure. The Jar is a java.io.File. But anything within
that file is beyond the reach of java.io.File. As far as java is
concerned, until it is uncompressed, a class in jar file is no
different than a word in a word document.
helped me understand why to use getInputStream() instead. It works for me now!
Thanks!
The error message is correct (if not very helpful): the file we're trying to load is not a file on the filesystem, but a chunk of bytes in a ZIP inside a ZIP.
Through experimentation (Java 11, Spring Boot 2.3.x), I found this to work without changing any config or even a wildcard:
var resource = ResourceUtils.getURL("classpath:some/resource/in/a/dependency");
new BufferedReader(
new InputStreamReader(resource.openStream())
).lines().forEach(System.out::println);
I had similar problem when using Tomcat6.x and none of the advices I found was helping.
At the end I deleted work folder (of Tomcat) and the problem gone.
I know it is illogical but for documentation purpose...
I was having an issue recursively loading resources in my Spring app, and found that the issue was I should be using resource.getInputStream. Here's an example showing how to recursively read in all files in config/myfiles that are json files.
Example.java
private String myFilesResourceUrl = "config/myfiles/**/";
private String myFilesResourceExtension = "json";
ResourceLoader rl = new ResourceLoader();
// Recursively get resources that match.
// Big note: If you decide to iterate over these,
// use resource.GetResourceAsStream to load the contents
// or use the `readFileResource` of the ResourceLoader class.
Resource[] resources = rl.getResourcesInResourceFolder(myFilesResourceUrl, myFilesResourceExtension);
// Recursively get resource and their contents that match.
// This loads all the files into memory, so maybe use the same approach
// as this method, if need be.
Map<Resource,String> contents = rl.getResourceContentsInResourceFolder(myFilesResourceUrl, myFilesResourceExtension);
ResourceLoader.java
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StreamUtils;
public class ResourceLoader {
public Resource[] getResourcesInResourceFolder(String folder, String extension) {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
String resourceUrl = folder + "/*." + extension;
Resource[] resources = resolver.getResources(resourceUrl);
return resources;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String readResource(Resource resource) throws IOException {
try (InputStream stream = resource.getInputStream()) {
return StreamUtils.copyToString(stream, Charset.defaultCharset());
}
}
public Map<Resource, String> getResourceContentsInResourceFolder(
String folder, String extension) {
Resource[] resources = getResourcesInResourceFolder(folder, extension);
HashMap<Resource, String> result = new HashMap<>();
for (var resource : resources) {
try {
String contents = readResource(resource);
result.put(resource, contents);
} catch (IOException e) {
throw new RuntimeException("Could not load resource=" + resource + ", e=" + e);
}
}
return result;
}
}
For kotlin users, I solved it like this:
val url = ResourceUtils.getURL("classpath:$fileName")
val response = url.openStream().bufferedReader().readText()
The answer by #sbk is the way we should do it in spring-boot environment (apart from #Value("${classpath*:})), in my opinion. But in my scenario it was not working if the execute from standalone jar..may be I did something wrong.
But this can be another way of doing this,
InputStream is = this.getClass().getClassLoader().getResourceAsStream(<relative path of the resource from resource directory>);
I was having an issue more complex because I have more than one file with same name, one is in the main Spring Boot jar and others are in jars inside main fat jar.
My solution was getting all the resources with same name and after that get the one I needed filtering by package name.
To get all the files:
ResourceLoader resourceLoader = new FileSystemResourceLoader();
final Enumeration<URL> systemResources = resourceLoader.getClassLoader().getResources(fileNameWithoutExt + FILE_EXT);
In Spring boot 1.5.22.RELEASE Jar packaging this worked for me:
InputStream resource = new ClassPathResource("example.pdf").getInputStream();
"example.pdf" is in src/main/resources.
And then to read it as byte[]
FileCopyUtils.copyToByteArray(resource);
I had the same issue, ended up using the much more convenient Guava Resources:
Resources.getResource("my.file")
While this is a very old thread, but I also faced the same issue while adding FCM in a Spring Boot Application.
In development, the file was getting opened and no errors but when I deployed the application to AWS Elastic beanstalk , the error of FileNotFoundException was getting thrown and FCM was not working.
So here's my solution to get it working on both development env and jar deployment production.
I have a Component class FCMService which has a method as follows:
#PostConstruct
public void initialize() {
log.info("Starting FCM Service");
InputStream inputStream;
try {
ClassPathResource resource = new ClassPathResource("classpath:fcm/my_project_firebase_config.json");
URL url = null;
try {
url = resource.getURL();
} catch (IOException e) {
}
if (url != null) {
inputStream = url.openStream();
} else {
File file = ResourceUtils.getFile("classpath:fcm/my_project_firebase_config.json");
inputStream = new FileInputStream(file);
}
FirebaseOptions options = FirebaseOptions.builder().setCredentials(GoogleCredentials.fromStream(inputStream))
.build();
FirebaseApp.initializeApp(options);
log.info("FCM Service started");
} catch (IOException e) {
log.error("Error starting FCM Service");
e.printStackTrace();
}
}
Hope this helps someone looking for a quick fix with implementing FCM.
Can be handled like:
var serviceAccount = ClassLoader.getSystemResourceAsStream(FB_CONFIG_FILE_NAME);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
Where FB_CONFIG_FILE_NAME is name of file in your 'resources' folder.

JUL to SLF4j with oracle jdbc driver

I am trying to redirect java.util.logging messages logged by the oracle jdbc driver and the oracle ucp (universal connection pool) library, but not able to do so.
The messages logged by my application using JUL get logged but the ones logged by the oracle libraries are not getting logged.
My intent here is redirect JUL messages to Logback to have more fine grained logging through configuration. i.e. enabling logging at class level instead of package level which I assume is not possible in JUL configuration (java.util.config file).
Below is the sample test code. Do you have any suggestions on the above two points?
import oracle.ucp.jdbc.PoolDataSourceImpl;
import org.slf4j.bridge.SLF4JBridgeHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;
public class JavaUtilToSlf4jApp {
private static Logger logger = Logger.getLogger(JavaUtilToSlf4jApp.class.getName());
public static void main(String[] args) {
SLF4JBridgeHandler.install();
startConnectionPool();
logger.info("Info Message");
}
private static void startConnectionPool() {
PoolDataSourceImpl pds = new PoolDataSourceImpl();
try {
pds.setConnectionPoolName("Pool Name");
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setConnectionFactoryProperties(getOracleDataSourceProperties());
pds.setDataSourceName("Datasource Name");
pds.setServerName("machine-name");
pds.setPortNumber(1521);
pds.setMinPoolSize(1);
pds.setMaxPoolSize(1);
pds.setMaxIdleTime(1800);
pds.setValidateConnectionOnBorrow(true);
pds.setUser("user");
pds.setPassword("password");
pds.startPool();
} catch (SQLException e) {
throw new RuntimeException("Cannot create project datasource ", e);
}
try {
Connection connection = pds.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
logger.info("Connection established");
}
private static Properties getOracleDataSourceProperties() {
Properties p = new Properties();
p.put("driverType", "oci");
p.put("networkProtocol", "tcp");
p.put("serviceName", "servicename");
return p;
}
}
If it helps. I took this approach in a Spring boot application, using the ojdbc_g version of the driver jar file:
Set the system property: System.setProperty("oracle.jdbc.Trace", "true").
Initialize the Oracle driver/datasource with whatever mechanism you have (manual, Spring, conn pool, etc.).
Once the datasource has been initialized, programmatically set the level for the "oracle.jdbc" JUL logger:
Logger ol = Logger.getLogger("oracle.jdbc");
ol.setLevel(Level.FINE);
The remaining part would be to figure out how to map the JUL logging levels to SLF4J logging levels.
Worked inside my Spring boot application. Cheers!
This was a complete mess, and after I got some logging, it still didn't work that well.
First off, you definitely need to be using the "_g" version of the jar, which is the jar that was compiled with the debug option, and logging turned on. If you don't use this driver, it'll be like getting blood from a stone.
Second, you need to add the java parameter -Doracle.jdbc.Trace=true.
Third, you need to define the package in your logging file (like logback.xml):
<logger name="oracle" level="INFO" additivity="false">
<appender-ref ref="SERVER_FILE" />
</logger>
This got me the following results:
11:17:45.393 [UCP-worker-thread-3] INFO oracle.jdbc.driver - SQL: select count(*) from mytable
11:17:45.956 [main] INFO oracle.jdbc.driver - SQL:
select myfield
from mytable
where myotherfield='myvalue'
11:17:46.159 [main] INFO oracle.jdbc.driver - SQL: begin :1 := dbms_pickler.get_type_shape(:2,:3,:4,:5,:6,:7); end;
Please let me know if you resolved your issues, how, and if any of this makes sense. There's poor and conflicting comments all of the web on this specific issue.

Resources