CQ5中的Sling Servlet即服务(OSGi Bundle)

时间:2014-04-18 20:11:45

标签: servlets osgi cq5 sling

我们尝试为CQ5创建osgi后端服务。我们使用eq作为开发环境的cqblueprints原型。我们可以使用控制台将其部署到CQ。在CQ Web控制台中,我们可以查看捆绑包的详细信息

com.acme.wcm.cq - acme-corporate-servicescom.acme.wcm.cq.acme-corporate-services
Symbolic Name   com.acme.wcm.cq.acme-corporate-services
Version 1.0.0.SNAPSHOT
Bundle Location inputstream:acme-corporate-services-1.0.0-SNAPSHOT.jar
Last Modification   Fri Apr 18 12:30:34 PDT 2014
Description Bundle containing the backend services for acme CQ
Start Level 20
Bundle Classpath    .,OSGI-INF/lib/json-simple-1.1.jar,OSGI-INF/lib/commons-lang-2.3.jar,OSGI-INF/lib/gson-2.2.4.jar,OSGI-INF/lib/org.apache.servicemix.bundles.rome-1.0_3.jar,OSGI-INF/lib/org.apache.servicemix.bundles.jdom-2.0.2_1.jar,OSGI-INF/lib/org.apache.servicemix.bundles.jaxen-1.1.6_1.jar,OSGI-INF/lib/org.apache.servicemix.bundles.dom4j-1.6.1_5.jar,OSGI-INF/lib/xml-apis-1.0.b2.jar
Exported Packages   com.acme.wcm.cq.exception,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.feed,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.feed.util,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.filter,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.osgi,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.search,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.servlet,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.servlet.model,version=1.0.0.SNAPSHOT
com.acme.wcm.cq.util,version=1.0.0.SNAPSHOT
Imported Packages   com.day.cq.search,version=0.0.0 from com.day.cq.cq-search (209)
com.day.cq.search.result,version=0.0.0 from com.day.cq.cq-search (209)
com.day.cq.wcm.api,version=1.0.0 from com.day.cq.wcm.cq-wcm-api (288)
javax.jcr,version=2.0.0 from org.apache.sling.jcr.jcr-wrapper (76)
javax.jcr,version=1.1.0 from org.apache.sling.jcr.jcr-wrapper (76)
javax.servlet,    ble,version=0.0.0.1_007_JavaSE from org.apache.felix.framework (0)
javax.swing.tree,version=0.0.0.1_007_JavaSE from org.apache.felix.framework (0)
javax.xml.bind,version=2.1.0 from org.apache.felix.framework (0)
javax.xml.namespace,version=0.0.0.fragment_xml from org.apache.felix.framework (0)
javax.xml.stream,version=1.0.0 from org.apache.felix.framework (0)
javax.xml.stream.events,version=1.0.0 from org.apache.felix.framework (0)
javax.xml.stream.util,version=1.0.0 from org.apache.felix.framework (0)
javax.xml.validation,version=0.0.0.fragment_xml from org.apache.felix.framework (0)
org.apache.sling.api,version=2.2.0 from org.apache.sling.api (123)
org.apache.sling.api.request,version=2.3.0 from org.apache.sling.api (123)
org.apache.sling.api.resource,version=2.3.2 from org.apache.sling.api (123)
org.apache.sling.api.servlets,version=2.1.0 from org.apache.sling.api (123)
org.osgi.framework,version=1.6.0 from org.apache.felix.framework (0)
org.slf4j,version=1.6.4 from slf4j.api (14)
Manifest Headers    Bnd-LastModified: 1397849403046
Build-Jdk: 1.7.0_45
Built-By: jsunny
Bundle-Activator: com.acme.wcm.cq.osgi.Activator
Bundle-ClassPath: ., OSGI-INF/lib/json-simple-1.1.jar, OSGI-INF/lib/commons-lang-2.3.jar, OSGI-INF/lib/gson-2.2.4.jar, OSGI-INF/lib/org.apache.servicemix.bundles.rome-1.0_3.jar, OSGI-INF/lib/org.apache.servicemix.bundles.jdom-2.0.2_1.jar, OSGI-INF/lib/org.apache.servicemix.bundles.jaxen-1.1.6_1.jar, OSGI-INF/lib/org.apache.servicemix.bundles.dom4j-1.6.1_5.jar, OSGI-INF/lib/xml-apis-1.0.b2.jar
Bundle-Description: Bundle containing the backend services for acme CQ
Bundle-ManifestVersion: 2
Bundle-Name: com.acme.wcm.cq - acme-corporate-services
Bundle-SymbolicName: com.acme.wcm.cq.acme-corporate-services
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Embed-Dependency: *; scope=compile|runtime
Embed-Directory: OSGI-INF/lib
Embed-Transitive: true
Embedded-Artifacts: OSGI-INF/lib/json-simple-1.1.jar; g="com.googlecode.json-simple"; a="json-simple"; v="1.1", OSGI-INF/lib/commons-lang-2.3.jar; g="commons-lang"; a="commons-lang"; v="2.3", OSGI-INF/lib/gson-2.2.4.jar; g="com.google.code.gson"; a="gson"; v="2.2.4", OSGI-INF/lib/org.apache.servicemix.bundles.rome-1.0_3.jar; g="org.apache.servicemix.bundles"; a="org.apache.servicemix.bundles.rome"; v="1.0_3", OSGI-INF/lib/org.apache.servicemix.bundles.jdom-2.0.2_1.jar; g="org.apache.servicemix.bundles"; a="org.apache.servicemix.bundles.jdom"; v="2.0.2_1", OSGI-INF/lib/org.apache.servicemix.bundles.jaxen-1.1.6_1.jar; g="org.apache.servicemix.bundles"; a="org.apache.servicemix.bundles.jaxen"; v="1.1.6_1", OSGI-INF/lib/org.apache.servicemix.bundles.dom4j-1.6.1_5.jar; g="org.apache.servicemix.bundles"; a="org.apache.servicemix.bundles.dom4j"; v="1.6.1_5", OSGI-INF/lib/xml-apis-1.0.b2.jar; g="xml-apis"; a="xml-apis"; v="1.0.b2"
Export-Package: com.acme.wcm.cq.exception; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.feed; uses:="javax.jcr, com.day.cq.search.result, org.apache.sling.api.resource"; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.feed.util; uses:="javax.jcr, com.day.cq.search.result, org.apache.sling.api.resource"; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.filter; uses:="javax.servlet, org.apache.sling.api.request, org.apache.sling.api, org.slf4j"; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.osgi; uses:="org.osgi.framework"; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.search; uses:="javax.jcr, com.day.cq.search.result, org.apache.sling.api"; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.servlet; uses:="org.apache.sling.api.servlets, javax.servlet, org.apache.sling.api, org.slf4j, javax.jcr, com.acme.wcm.cq.search, com.day.cq.search.result, com.acme.wcm.cq.exception, com.acme.wcm.cq.servlet.model"; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.servlet.model; version="1.0.0.SNAPSHOT", com.acme.wcm.cq.util; uses:="javax.jcr, com.day.cq.search.result, org.apache.sling.api, com.day.cq.search, org.apache.sling.api.resource, com.day.cq.wcm.api, org.apache.sling.api.servlets, javax.servlet"; version="1.0.0.SNAPSHOT"
Import-Package: com.acme.wcm.cq.search, com.day.cq.search, com.day.cq.search.result, com.day.cq.wcm.api, com.sun.msv.datatype; resolution:=optional, com.sun.msv.datatype.xsd; resolution:=optional, javax.jcr; version="[2.0, 3)", javax.servlet, javax.swing.table, javax.swing.tree, javax.xml.bind, javax.xml.namespace, javax.xml.stream, javax.xml.stream.events, javax.xml.stream.util, javax.xml.validation, nu.xom; resolution:=optional, org.apache.sling.api; version="[2.2, 3)", org.apache.sling.api.request; version="[2.2, 3)", org.apache.sling.api.resource; version="[2.1, 3)", org.apache.sling.api.servlets; version="[2.1, 3)", org.gjt.xpp; resolution:=optional, org.jdom; resolution:=optional, org.jdom.input; resolution:=optional, org.jdom.output; resolution:=optional, org.osgi.framework; version="[1.4, 2)", org.relaxng.datatype; resolution:=optional, org.slf4j; version="[1.6, 2)", org.xmlpull.v1; resolution:=optional
Manifest-Version: 1.0
Tool: Bnd-1.50.0

但是,如果我在“服务”菜单中搜索(在Felix控制台中),我找不到任何我创建的内容。

这是正常的吗?为了验证这一点,我创建了一个Simple servelet

import java.io.IOException;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.slf4j.Logger;
 org.slf4j.LoggerFactory;

@Component(immediate = true, metatype = false)
@Service(value = javax.servlet.Servlet.class)
@Properties(value = { @Property(name = "sling.servlet.methods", value = { "GET" }),
        @Property(name = "sling.servlet.paths", value = { "/path/to/my/servlet", "/apps/path/to/my/servlet" }) })
public class AbsoluteUrlServlet extends SlingSafeMethodsServlet {
   private static final long serialVersionUID = -1920460619265757059L;
   private static final Logger logger = LoggerFactory.getLogger(AbsoluteUrlServlet.class);

   @Override
   protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
      logger.debug("AbsoluteUrlServlet::doGet()");
      response.setContentType("text/plain");
      response.getOutputStream().print("Hello AbsoluteUrlServlet World!");
   }
}

但是,当我从浏览器请求servlet时,

    http://localhost:4502/apps/path/to/my/servlet

我收到以下错误消息。     找不到资源

Cannot serve request to /apps/path/to/my/servlet in /libs/sling/servlet/errorhandler/404.jsp
Request Progress:

      0 (2014-04-18 12:33:58) TIMER_START{Request Processing}
      0 (2014-04-18 12:33:58) COMMENT timer_end format is {<elapsed msec>,<timer name>} <optional message>
      0 (2014-04-18 12:33:58) LOG Method=GET, PathInfo=/apps/path/to/my/servlet
      0 (2014-04-18 12:33:58) TIMER_START{ResourceResolution}
      0 (2014-04-18 12:33:58) TIMER_END{0,ResourceResolution} URI=/apps/path/to/my/servlet resolves to Resource=NonExistingResource, path=/apps/path/to/my/servlet
      1 (2014-04-18 12:33:58) LOG Resource Path Info: SlingRequestPathInfo: path='/apps/path/to/my/servlet', selectorString='null', extension='null', suffix='null'
      1 (2014-04-18 12:33:58) TIMER_START{ServletResolution}
      1 (2014-04-18 12:33:58) TIMER_START{resolveServlet(NonExistingResource, path=/apps/path/to/my/servlet)}
      1 (2014-04-18 12:33:58) LOG {0}: no servlet found
      1 (2014-04-18 12:33:58) TIMER_END{0,resolveServlet(NonExistingResource, path=/apps/path/to/my/servlet)} Using servlet org.apache.sling.servlets.get.DefaultGetServlet
      1 (2014-04-18 12:33:58) TIMER_END{0,ServletResolution} URI=/apps/path/to/my/servlet handled by Servlet=org.apache.sling.servlets.get.DefaultGetServlet
      1 (2014-04-18 12:33:58) LOG Applying Requestfilters
      1 (2014-04-18 12:33:58) LOG Calling filter: org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter
      1 (2014-04-18 12:33:58) LOG Calling filter: org.apache.sling.i18n.impl.I18NFilter

请提供解决问题的一些指示。

1 个答案:

答案 0 :(得分:2)

终于能找到原因了。

我错过了我的POM.xml中的maven-scr-plugin,因为我的捆绑包中缺少serviceComponents.xml。(请参阅My previous thread