Rob
Rob

Reputation: 187

Grails Service injection fails when run on Tomcat

I am trying to deploy a small app that I developed using IntelliJ (Windows) on Tomcat 7 (CentOS 6). Everything runs on my development machine. I have run it using run-app and prod run-app.

My app has 1 controller that uses a Service (def logService). I have my service defined in the Service directory (class LogService). When run on my development system, logService in my controller is NOT null, but when I deploy it (using war file) logService is null.

Here is my controller:

package LogViewer

import grails.transaction.Transactional

import java.text.SimpleDateFormat

@Transactional(readOnly = true)
class OlrBrowserLogController
{
    def olrBrowserLogService

    def index() {
        def startDate = params.startDate

        Calendar now = Calendar.getInstance()
        if (startDate == null)
            now.add(Calendar.MONTH, -2)
        else {
            SimpleDateFormat sdf = new SimpleDateFormat('M/d/yyyy');
            now = sdf.parse(startDate).toCalendar()
        }

        def c = OlrBrowserLog.createCriteria()
        def rawLogEntries = c.list {
            gt("createdDate", now.getTime())
            order("createdDate", "desc")
        }

        def parsedLogEntries = []

        for (logEntry in rawLogEntries) {
            def parsedLogEntry = olrBrowserLogService.parseAgentString(logEntry)

            parsedLogEntries.add(parsedLogEntry)
        }

        return [parsedLogEntries: parsedLogEntries, startDate: now.getTime().format("M/d/y")]
    }
}

Here is my service:

package logviewer

import LogViewer.OlrBrowserLog
import LogViewer.bLog
import grails.transaction.Transactional
import net.sf.uadetector.ReadableUserAgent
import net.sf.uadetector.UserAgentStringParser
import net.sf.uadetector.service.UADetectorServiceFactory

@Transactional
class OlrBrowserLogService {
    def parseAgentString(OlrBrowserLog olrBrowserLogInstance)
    {
        // Get an UserAgentStringParser and analyze the requesting client
        UserAgentStringParser parser = UADetectorServiceFactory.getResourceModuleParser();
        ReadableUserAgent agent = parser.parse(olrBrowserLogInstance.browserUserAgent);

        def bLogInstance = new bLog()
        bLogInstance.userEmailAddress = olrBrowserLogInstance.wcUserEmailAddress
        bLogInstance.ipAddress = olrBrowserLogInstance.ipAddress
        bLogInstance.createdDate = olrBrowserLogInstance.createdDate.format("M/d/y h:mm a")
        bLogInstance.browserUserAgent = olrBrowserLogInstance.browserUserAgent
        bLogInstance.screenWidth = olrBrowserLogInstance.screenWidth
        bLogInstance.screenHeight = olrBrowserLogInstance.screenHeight
        bLogInstance.browserWidth = olrBrowserLogInstance.browserWidth
        bLogInstance.browserHeight = olrBrowserLogInstance.browserHeight
        bLogInstance.device = agent.deviceCategory.name
        bLogInstance.operatingSystem = agent.operatingSystem.name
        bLogInstance.browser = agent.name
        bLogInstance.version = agent.versionNumber.toVersionString()

        return bLogInstance
    }
}

I turned on some logging and I am getting this message:

2014-06-09 07:52:23,904 [http-bio-8080-exec-2] WARN commons.GrailsApplicationFactoryBean - Class with name [logviewer.OlrBrowserLogService] was not found, and hence not loaded. Possible empty class or script definition?

Upvotes: 0

Views: 240

Answers (2)

mez79
mez79

Reputation: 103

Probably much too late, but may be helpful for others:

I ran into the same problem after I did a renaming of a controller class - but I changed only one letter from upper- to lowercase. For some reason the compiler did not tidy up reliable and therefore the old class file remained in place. Because my development machine runs on Windows, this wasn't a problem there - but as soon as I deployed to a Linux machine I saw the same warning message:

"Class with name ["+className+"] was not found, and hence not loaded. Possible empty class or script definition?"

The solution is trivial:

run grails clean to make sure everything gets re-compiled. Sometimes you have to delete the target directory too.

Probably you have a similar relict hanging around. And even more suspect is, that your service is in package logviewer but your controller is in LogViewer (and has no import of logviewer.OlrBrowserLogService). Correct this naming clutter, do a clean deployment and you will be fine.

Upvotes: 1

Graeme Rocher
Graeme Rocher

Reputation: 7985

Most likely cause of this issue is that you are either missing a package declaration on OlrBrowserLogService or that the package definition does not match the directory structure. Verify your package is correct and try deploy again.

Upvotes: 0

Related Questions