Search code examples
jspjaradobecrx

Adding external Java Libraries to Adobe/Day CRX (2.2) for use in JSP files


my company is looking into using Adobe CQ5 as a new content management system and I've been given the task of figuring out how to do certain things with it.

One thing we would like to do is use some JARs we created for our old web-apps in scriptlets within our JSP pages, possibly to call a service or something.

I looked into doing this with OSGi bundles but I don't think it's what we want to do. Right now I'm having trouble figuring out where to put external libraries to be used in our JSP files.

I created a JAR that just has a generic class

package org.company.test;

import java.lang.String;

public class TestService{
    private String myString;
    public TestService(String input){myString = input;}
    public String getMyString(){return myString;}
}

That is the only thing in the jar.

I tried putting it in /crx-quickstart/server/lib/common

which the README for that folder says

"Put libraries in this folder that should be shared among all web applications and the server."

but my JSP file has a few issues when I try to access it. This is the JSP file in its entirety:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ TR/html4/loose.dtd">
<%%>
<%@page session="false"%>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
<sling:defineObjects/>

<%@ page import="javax.jcr.Repository, javax.jcr.Session, javax.jcr.SimpleCredentials, javax.jcr.Node, javax.jcr.NodeIterator,
                 java.net.URLEncoder, java.util.List, java.util.Iterator, javax.jcr.Value, javax.jcr.RepositoryException,org.company.test.TestService;"%>
<html>
    <head> 
        <style type="text/css"><jsp:include page="/content/myBlog/style.css"/></style>
        <!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <% 
            TestService jServ = new TestService("TigerBlood");
            String returnValue = "Failed";
            if(jServ!=null){
                returnValue = jServ.getMyString();
            }            
        %>
        <title><%= returnValue %></title> 
    </head> 
    <body> 
        <a href="/content/myBlog.html" class="imgcontainer"><img src="/apps/myBlog/myBlog.png" width="80px" height="60px" border="0" alt="myBlog" /></a>
        <h1><%= returnValue %></h1>
        <div class="body">
            <br>
            <a href="/apps/myBlog/comment.html">Comment</a>
        </div>
    </body> 
</html>

When I navigate to it in a browser I get these errors:

Unable to compile class for JSP: An error occurred at line: 16 in the generated java file Only a type can be imported. org.company.test.TestService resolves to a package 

An error occurred at line: 17 in the jsp file: /apps/myBlog/test.jsp TestService cannot be resolved to a type  
14: <!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->  
15: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
16: <%  
17: TestService jServ = new TestService("TigerBlood");  
18: String returnValue = "Failed";  
19: if(jServ!=null){  20: returnValue = jServ.getMyString(); 

An error occurred at line: 17 in the jsp file: /apps/myBlog/test.jsp TestService cannot be resolved to a type  
14: <!--<link rel="stylesheet" href="/content/myBlog/style.css" type="text/css">-->  
15: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
16: <%  
17: TestService jServ = new TestService("TigerBlood");  
18: String returnValue = "Failed";  
19: if(jServ!=null){  
20: returnValue = jServ.getMyString();

I can fix the first error about the package by changing the import to com.company.test.* but it does not fix the other two errors.

I know this is not the best 'practice' for using services but I was hoping to just get a Proof of Concept going that shows we could use those libraries if we wanted. Any help would be appreciated.


Solution

  • I've found the answer, you have to bundle external libraries as an OSGi bundle.

    The process can be found here

    It was surprisingly hard to find that answer and although it's more time consuming than just adding a JAR, it works.