Connect to FileMaker Database from Grails app - grails

I can't achieve to connect with a FileMaker database when I use the fmjdbc driver. I put the jar library in the /lib folder and I try connect with the next connection params:
dataSource {
pooled = true
driverClassName = "com.ddtek.jdbc.sequelink.SequeLinkDriver"
username = "myUser"
password = "myPass"
}
...
test {
dataSource {
dbCreate = "update"
url ="jdbc:sequelink://myIP:2399;serverDataSource=mydataBase;user=myUser;password=myPass"
}
}
Unfortunately, It return a ClassNotFoundException error:
"...
Caused by SQLNestedException: Cannot load JDBC driver class 'com.ddtek.jdbc.sequelink.SequeLinkDriver'
...
"
I think It could be a unregister-dependency lack, as well as the runtime dependency of mysql driver placed at buildConfig.groovy file:
dependencies {
runtime 'mysql:mysql-connector-java:5.1.16'
}
I have seen the manifest.mf jar file but This don't have any relevant data. Here the content:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_26-156 (Apple Inc.)
but I don't know how build the dependency string for com.ddtek.jdbc.sequelink.SequeLinkDriver driver..
Anybody says how could be it? Will It be the problem, or maybe coud be another?
Thanks a lot.
Luis.

Related

What classspath is used for executing Grails' application.groovy

What classspath is used for compiling/executing Grails' application.groovy?
In my application.groovy, I instantiate a custom class (contained in a dependency's jar) and assign it to one of the config properties, like so:
environments {
production {
configProperty = new com.example.CustomClass()
I recently upgraded my application from Grails 3.1.5 to 3.2.2, and now this no longer works.
I receive an error like the following when I try to run grails run-app:
Error occurred running Grails CLI: startup failed:
script14788250424471597489853.groovy: 43: unable to resolve class com.example.CustomClass
# line 43, column 33.
configProperty = new com.example.CustomClass()
(Notice that the code is in the production block, but I'm running in development (run-app). That makes me think it's the compilation of this script that is failing.)
So I'm guessing I just need to add my dependency (that contains the CustomClass) to the appropriate classpath, but I'm not sure which one.
I'm using gradle, and have the following in my build.gradle file, to pull in the dependency containing CustomClass:
buildscript {
dependencies {
classpath "com.example:custom-module:1.1"
// ...
dependencies {
compile group: 'com.example', name: 'custom-module', version:'1.1'
}
The grails-app/conf/application.groovy file shouldn't reference application classes because it is read before compilation. If you wish to reference application classes in configuration please use grails-app/conf/runtime.groovy

Grails log4j SMTPAppender NoClassDefFoundError

How do I configure SMTPAppender in a new Grails 2.4.5 project? I receive a NoClassDefFoundError when running in the development environment:
| Error log4j:ERROR Error initializing log4j: javax/mail/Message
| Error java.lang.NoClassDefFoundError: javax/mail/Message
Gist: Detailed stacktrace
I have configured a dependency for javax.mail and configured log4j as follows:
dependencies {
provided 'javax.mail:mail:1.4.7'
}
log4j = {
appenders {
appender new org.apache.log4j.net.SMTPAppender(
name: 'smtp',
layout: pattern(conversionPattern: '%d{MM-dd-yyyy HH:mm:ss.SSS} [%t] %c %M %x%n%p: %m%n')
to: 'example#example.com',
from: 'example#example.com',
subject: 'Grails Message',
SMTPHost: '127.0.0.1')
)
}
}
GitHub: Example Project
I know this post is old, but I struggled with the same till I found a solution that is at least working for me (not with SMTPAppender but with Sentry - same purpose).
The explanation I found with the errors you where receiving
Error log4j:ERROR Error initializing log4j: javax/mail/Message
Error java.lang.NoClassDefFoundError: javax/mail/Message
Come from this piece of code:
dependencies {
provided 'javax.mail:mail:1.4.7'
}
The thing is that when you compile things work, when you try to do grails run-app you receive this error.
The explanation I found is that the log4j initializes before the maven dependency is resolved.
I wrote a comment of how I've used Sentry as an appender.
https://github.com/getsentry/raven-java/issues/184#issuecomment-259432057
Basically, instead of adding the maven dependency I've downloaded the java file, and added it to the grails project in src/java
So for Sentry por example I added the SentryAppender.java to my.package.sentry so then in log4j I added:
appenders {
environments {
// This block is set up to use the stock raven SentryAppender in
// production. Sentry Appender runs into all kinds of
// class loading weirdness when used in a forked grails environment
production {
appender new my.package.sentry.SentryAppender(
name: 'sentry',
dsn: 'REDACTED',
threshold: org.apache.log4j.Level.WARN
)
}
// Uncomment this block if you need to test sentry
// in a dev environment
development {
appender new my.package.sentry.SentryAppender(
name: 'sentry',
dsn: 'REDACTED',
threshold: org.apache.log4j.Level.WARN
)
}
}
}
root {
warn 'stdout', 'sentry'
error 'stdout', 'sentry'
additivity = false
}
in that way, it does not depend on an external dependency.
I guess that you could do something similar to the mail dependency and add the java files to the src folder.
I hope it helps!
That looks weird - are you combining the dependencies block and the log4j block here unintenionally, or are they in the same file in your app? The dependency should be in BuildConfig.groovy and the log4j block should be in Config.groovy. Also, it shouldn't be log4j { but rather log4j = {.
This is likely a timing issue. If Config.groovy is parsed before the Javamail dependency is resolved, it will fail. Try commenting out the parts that reference the Javamail classes and run grails clean and grails compile. That will resolve dependencies and add that jar to the classpath. Then you can uncomment that code and run grails compile again.

External properties file in grails 3

I need read configuration from a external file properties in grails 3. In grails 2.x I link the file with:
grails.config.locations = ["classpath:config.properties"]
In the config.groovy, but this file do not exists in grails 3.
Have you any idea for solve?
Because Grails 3 is built on Spring Boot, you can use the Spring Boot mechanisms for externalized properties. Namely, using the spring.config.location command line parameter, or the SPRING_BOOT_LOCATION environment variable. Here's the Spring documentation page on it.
The example the documentation provides for the command line parameter is this:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
The way I have been using it is by setting an environment variable, like this:
export SPRING_CONFIG_LOCATION="/home/user/application-name/application.yml"
One of the nice features is that you can leave some properties in the properties file that is bundled in the app, but if there are some properties you do not want to include (such as passwords), those can be set specifically in the external config file.
Take a look at https://gist.github.com/reduardo7/d14ea1cd09108425e0f5ecc8d3d7fca0
External configuration in Grails 3
Working on Grails 3 I realized that I no longer can specify external configuration using the standard grails.config.locations property in Config.groovy file.
Reason is obvious! There is no Config.groovy now in Grails 3. Instead we now use application.yml to configure the properties. However, you can't specify the external configuration using this file too!
What the hack?
Now Grails 3 uses Spring's property source concept. To enable external config file to work we need to do something extra now.
Suppose I want to override some properties in application.yml file with my external configuration file.
E.g., from this:
dataSource:
username: sa
password:
driverClassName: "org.h2.Driver"
To this:
dataSource:
username: root
password: mysql
driverClassName: "com.mysql.jdbc.Driver"
First I need to place this file in application's root. E.g., I've following structure of my Grails 3 application with external configuration file app-config.yml in place:
[human#machine tmp]$ tree -L 1 myapp
myapp
├── app-config.yml // <---- external configuration file!
├── build.gradle
├── gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
├── grails-app
└── src
Now, to enable reading this file just add following:
To your build.gradle file
bootRun {
// local.config.location is just a random name. You can use yours.
jvmArgs = ['-Dlocal.config.location=app-config.yml']
}
To your Application.groovy file
package com.mycompany
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.context.EnvironmentAware
import org.springframework.core.env.Environment
import org.springframework.core.env.PropertiesPropertySource
import org.springframework.core.io.FileSystemResource
import org.springframework.core.io.Resource
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
public final static String LOCAL_CONFIG_LOCATION = "local.config.location"
static void main(String[] args) {
GrailsApp.run(Application, args)
}
#Override
void setEnvironment(Environment environment) {
String configPath = System.properties[LOCAL_CONFIG_LOCATION]
if (!configPath) {
throw new RuntimeException("Local configuration location variable is required: " + LOCAL_CONFIG_LOCATION)
}
File configFile = new File(configPath)
if (!configFile.exists()) {
throw new RuntimeException("Configuration file is required: " + configPath)
}
Resource resourceConfig = new FileSystemResource(configPath)
YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean()
ypfb.setResources([resourceConfig] as Resource[])
ypfb.afterPropertiesSet()
Properties properties = ypfb.getObject()
environment.propertySources.addFirst(new PropertiesPropertySource(LOCAL_CONFIG_LOCATION, properties))
}
}
Notice that Application class implements EnvironmentAware Interface and overrides its setEnvironment(Environment environment):void method.
Now this is all what you need to re-enable external config file in Grails 3 application.
Code and guidance is taken from following links with little modification:
http://grails.1312388.n4.nabble.com/Grails-3-External-config-td4658823.html
https://groups.google.com/forum/#!topic/grails-dev-discuss/_5VtFz4SpDY
Source: https://gist.github.com/ManvendraSK/8b166b47514ca817d36e
I am having the same problem to read the properties file from external location in Grails 3. I found this plugin which helpme to read the properties from external location. It has feature to read .yml, .groovy files as well.
Follow the steps mentioned in the documentation and it will work.
The steps are like:
Add dependency in build.gradle:
dependencies {compile 'org.grails.plugins:external-config:1.2.2'}
Add this in application.yml grails:
config:
locations:
- file:///opt/abc/application.yml
Create file at external location. In my case /opt/abc/application.yml.
Build the application and run.
You can use
def cfg = new ConfigSlurper.parse(getClass().classLoader.getResource('path/myExternalConfigfile.groovy'))
When running from a .jar file, I found that Spring Boot looks at the current directory for an application.yml file.
java -jar app-0.3.jar
In the current directory, I created an application.yml file with one line:
org.xyz.app.title: 'XYZZY'
I also used this file to specify the database and other such info.
Seems like there is no externalised config out of the box: http://grails.1312388.n4.nabble.com/Grails-3-External-config-td4658823.html

Grails 2.1 disables log4j config on JBoss

We recently upgraded our app from Grails 1.3.7 to 2.1, which builds under maven, and made a bunch of changes to the pom file and other config- and build-related files of grails to get things working.
Here's the issue: when our JBoss server deploys our grails war file, something appears to cause log4j to be reconfigured such that the appserver stops all logging from that point forward (for any class). The app server is still running fine, but there is no logging after this line:
INFO [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (main) deploy, ctxPath=/our-app
Removing our war file and restarting the server shows things logging correctly.
Our appserver has a server/default/conf/jboss-log4j.xml file in where we specify our cross-app log4j config.
I've tried removing log4j and slf4j jar files from our war, as that has caused issues in the past, but it hasn't solved the problem this time.
I also commented out the entire log4j section of grails-app/Config.groovy thinking that might be the culprit, but that had no effect.
I also tried removing the grails log4j plugin (by marking it as 'provided' scope in the pom), but that causes the following error during deployment:
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/out-app]] (main) Error configuring application listener of class org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener
java.lang.ClassNotFoundException: org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener
Any idea what I might have missed?
An abbreviated listing of my war file is below:
WEB-INF/applicationContext.xml
WEB-INF/classes/BootStrap.class
WEB-INF/classes/BuildConfig.class
WEB-INF/classes/Config.class
WEB-INF/classes/DataSource.class
WEB-INF/classes/HibernateGrailsPlugin.class
WEB-INF/classes/UrlMappings.class
WEB-INF/classes/application.properties
WEB-INF/classes/gsp/views.properties
WEB-INF/classes/resources.class
WEB-INF/grails.xml
WEB-INF/jboss-web.xml
WEB-INF/lib/antlr-2.7.7.jar
WEB-INF/lib/aopalliance-1.0.jar
WEB-INF/lib/aspectjrt-1.6.10.jar
WEB-INF/lib/aspectjweaver-1.6.10.jar
WEB-INF/lib/cglib-2.2.jar
WEB-INF/lib/commons-beanutils-1.8.3.jar
WEB-INF/lib/commons-codec-1.5.jar
WEB-INF/lib/commons-collections-3.2.1.jar
WEB-INF/lib/commons-dbcp-1.4.jar
WEB-INF/lib/commons-el-1.0.jar
WEB-INF/lib/commons-fileupload-1.2.2.jar
WEB-INF/lib/commons-io-2.0.1.jar
WEB-INF/lib/commons-lang-2.6.jar
WEB-INF/lib/commons-pool-1.5.6.jar
WEB-INF/lib/commons-validator-1.3.1.jar
WEB-INF/lib/concurrentlinkedhashmap-lru-1.2_jdk5.jar
WEB-INF/lib/dom4j-1.6.1.jar
WEB-INF/lib/ehcache-core-2.4.6.jar
WEB-INF/lib/encryption-1.0.jar
WEB-INF/lib/ezmorph-1.0.6.jar
WEB-INF/lib/grails-bootstrap-2.1.0.jar
WEB-INF/lib/grails-core-2.1.0.jar
WEB-INF/lib/grails-crud-2.1.0.jar
WEB-INF/lib/grails-datastore-core-1.0.9.RELEASE.jar
WEB-INF/lib/grails-datastore-gorm-1.0.9.RELEASE.jar
WEB-INF/lib/grails-datastore-simple-1.0.9.RELEASE.jar
WEB-INF/lib/grails-hibernate-2.1.0.jar
WEB-INF/lib/grails-logging-2.1.0.jar
WEB-INF/lib/grails-plugin-codecs-2.1.0.jar
WEB-INF/lib/grails-plugin-controllers-2.1.0.jar
WEB-INF/lib/grails-plugin-converters-2.1.0.jar
WEB-INF/lib/grails-plugin-datasource-2.1.0.jar
WEB-INF/lib/grails-plugin-domain-class-2.1.0.jar
WEB-INF/lib/grails-plugin-filters-2.1.0.jar
WEB-INF/lib/grails-plugin-gsp-2.1.0.jar
WEB-INF/lib/grails-plugin-i18n-2.1.0.jar
WEB-INF/lib/grails-plugin-log4j-2.1.0.jar
WEB-INF/lib/grails-plugin-mimetypes-2.1.0.jar
WEB-INF/lib/grails-plugin-scaffolding-2.1.0.jar
WEB-INF/lib/grails-plugin-services-2.1.0.jar
WEB-INF/lib/grails-plugin-servlets-2.1.0.jar
WEB-INF/lib/grails-plugin-url-mappings-2.1.0.jar
WEB-INF/lib/grails-plugin-validation-2.1.0.jar
WEB-INF/lib/grails-resources-2.1.0.jar
WEB-INF/lib/grails-spring-2.1.0.jar
WEB-INF/lib/grails-web-2.1.0.jar
WEB-INF/lib/grails-webflow-2.1.0.jar
WEB-INF/lib/groovy-all-1.8.6.jar
WEB-INF/lib/hibernate-ehcache-3.6.10.Final.jar
WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar
WEB-INF/lib/hibernate-validator-4.1.0.Final.jar
WEB-INF/lib/http-builder-0.5.0-RC3.jar
WEB-INF/lib/httpclient-4.0.jar
WEB-INF/lib/httpcore-4.0.1.jar
WEB-INF/lib/json-lib-2.3-jdk15.jar
WEB-INF/lib/jstl-1.1.2.jar
WEB-INF/lib/nekohtml-1.9.9.jar
WEB-INF/lib/oro-2.0.8.jar
WEB-INF/lib/sitemesh-2.4.jar
WEB-INF/lib/spring-aop-3.1.0.RELEASE.jar
WEB-INF/lib/spring-asm-3.1.0.RELEASE.jar
WEB-INF/lib/spring-aspects-3.1.0.RELEASE.jar
WEB-INF/lib/spring-beans-3.1.0.RELEASE.jar
WEB-INF/lib/spring-context-3.1.0.RELEASE.jar
WEB-INF/lib/spring-context-support-3.1.0.RELEASE.jar
WEB-INF/lib/spring-core-3.1.0.RELEASE.jar
WEB-INF/lib/spring-expression-3.1.0.RELEASE.jar
WEB-INF/lib/spring-jdbc-3.1.0.RELEASE.jar
WEB-INF/lib/spring-jms-3.1.0.RELEASE.jar
WEB-INF/lib/spring-orm-3.1.0.RELEASE.jar
WEB-INF/lib/spring-tx-3.1.0.RELEASE.jar
WEB-INF/lib/spring-web-3.1.0.RELEASE.jar
WEB-INF/lib/spring-webmvc-3.1.0.RELEASE.jar
WEB-INF/lib/util-io-1.2-SNAPSHOT.jar
WEB-INF/lib/utils-1.07.00.jar
WEB-INF/lib/validation-api-1.0.0.GA.jar
WEB-INF/lib/xml-resolver-1.2.jar
WEB-INF/lib/xpp3_min-1.1.4c.jar
WEB-INF/lib/zdecimal-3.2.jar
WEB-INF/plugins/hibernate-2.1.0/grails-app/i18n/messages.properties
WEB-INF/plugins/hibernate-2.1.0/plugin.xml
WEB-INF/sitemesh.xml
WEB-INF/web.xml
And Config.groovy (with app-specific stuff removed):
grails.project.groupId = appName // change this to alter the default package name and Maven publishing destination
grails.mime.file.extensions = true // enables the parsing of file extensions from URLs into the request format
grails.mime.use.accept.header = false
grails.mime.types = [
all: '*/*',
atom: 'application/atom+xml',
css: 'text/css',
csv: 'text/csv',
form: 'application/x-www-form-urlencoded',
html: ['text/html','application/xhtml+xml'],
js: 'text/javascript',
json: ['application/json', 'text/json'],
multipartForm: 'multipart/form-data',
rss: 'application/rss+xml',
text: 'text/plain',
xml: ['text/xml', 'application/xml']
]
grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*']
grails.views.default.codec = "none" // none, html, base64
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"
grails.views.gsp.sitemesh.preprocess = true
grails.scaffolding.templates.domainSuffix = 'Instance'
grails.json.legacy.builder = false
grails.enable.native2ascii = true
grails.logging.jul.usebridge = true
grails.spring.bean.packages = []
grails.hibernate.cache.queries = false
environments {
production {
grails.logging.jul.usebridge = false
}
development {
grails.logging.jul.usebridge = true
}
test {
// shouldn't be using this environment
}
}
Through some debugging breakpoints, I was able to determine that the Grails log4j plugin is the culprit. It appears to be resetting the global log4j config on startup.
I'll ask a separate question about how to disable this.
see How do disable log4j plugin in grails?

Struts2 example, not inserting records in mysql database because connection object is getting null

This Code is working fine with simple application so the drivers are fine.
so why the connection object is not able to initialise with drivers.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.opensymphony.xwork2.ActionSupport;
public class Insert extends ActionSupport {
public String execute() throws Exception, SQLException {
String sql = "";
String url = "jdbc:mysql://localhost:3306/test";
//String dbName = "test";
String driverName = "org.gjt.mm.mysql.Driver";
String userName = "root";
String password = "root";
Connection con=null;
Statement stmt=null;
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url, userName, password);
stmt = con.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
What does the exception say? A NullPointerException on createStatement?
In any case, has the class been loaded; is the class on the classpath (i.e. the MySQL jar on the classpath?)
Check the object returned from Class.forName() to check whether the class has been found.
After your comment it is clear that you have a classpath problem. The mysql jar is not on the classpath. I assume you are talking about a web app deliverable (war file), as changing the build path in eclipse is trivial.
In a web application deployed in, for example, tomcat you can look into <webapp-name>/WEB-INF/lib. The file WEB-INF/lib/mysql-connector-java-5.0.5.jar, or something similar, should be there.
If you have a war file (not yet deployed) you can extract it using the command line tool "jar", or get a file listing from it. If you do (on a command line) jar tf | grep mysql the jarfile should be visible. If you use windows; WinRAR (and probably WinZip) can also open warfiles. In WEB-INF/lib a MySQL jar should be visible.
If you use maven to build your web app; don't forget to add a dependency to the mysql jar. If you use ant to build; don't forget to copy the mysql jar file into WEB-INF/lib before creating the war file.
Please note that currently the recommended driver to ask for is 'com.mysql.driver.Driver', and not 'org.gjt.mm.mysql.Driver'. You can try to load that Driver class in stead of the older driver; further; check whether this driver is actually in the mysql jar (jar tf mysq.jar | grep Driver or so). If the Driver is in the mysql jar and the mysql jar is on the classpath of the webapp (in WEB-INF/lib) and there is only one mysql jar there (version conflicts are no fun), and it still does not work I really don't know what could be wrong. I think I'd download the driver jar again from MySQL and try again.

Resources