Restlet学习:Restlet framework中API层与实现层的分层处理方式

The Restlet framework is composed of two main parts. First, there is the "Restlet API", a neutral API supporting the concepts of REST and facilitating the handling of calls for both client-side and server-side applications. This API must be supported by a Restlet implementation before it can effectively be used. Multiple implementations could be provided (open source projects or commercial products).
Restlet framework分为两个部分,API层和实现层。API层相当于规范,实现层可以有多种实现。要使用Restlet framework,必须在classpath中包含实现的jar包。Restlet framework提供了默认的实现。

层次结构图:


This separation between the API and the implementation is similar to the one between the Servlet API and Web containers like Jetty or Tomcat, or between the JDBC API and concrete JDBC drivers. Currently, the "Noelios Restlet Engine" (NRE) is available and acts as the reference implementation. When you download the Restlet distribution, the API and the NRE come bundled together, ready to be used. If you need to use a different implementation just add the implementation JAR file to the classpath and remove the NRE JAR file named com.noelios.restlet.jar.
API层和实现层的关系如Servlet API和Web Container,JDBC API和JDBC drivers。如果要使用自己的实现层,可以在classpath中去掉NRE的jar包com.noelios.restlet.jar。

The registration is done automatically. See the JAR specification for details. When an implementation is loaded, it automatically calls back the org.restlet.util.Engine.setInstance() method.
实现层是自动注册的,只需要把jar放到classpath即可。部分源代码如下:

//实现层jar的META-INF
com.noelios.restlet.jar\META-INF\services\org.restlet.util.Engine:
com.noelios.restlet.Engine    # The Noelios Restlet Engine

//实现层的Engine类,继承自API层的Engine类
public class Engine extends org.restlet.util.Engine {...}

//API层的Engine类
public abstract class Engine {
...

    private static final String providerResource = "META-INF/services/org.restlet.util.Engine";

    public static ClassLoader getClassLoader() {
        ClassLoader result = getUserClassLoader();

        if (result == null) {
            result = Thread.currentThread().getContextClassLoader();
        }

        if (result == null) {
            result = Class.class.getClassLoader();
        }

        if (result == null) {
            result = ClassLoader.getSystemClassLoader();
        }

        return result;
    }

    //从classpath中读取实现层的Engine类实例
    public static Engine getInstance() {
            ...

            // Try the default classloader
            ClassLoader cl = getClassLoader();
            URL configURL = cl.getResource(providerResource);

            if (configURL == null) {
                // Try the current thread's classloader
                cl = Thread.currentThread().getContextClassLoader();
                configURL = cl.getResource(providerResource);
            }

            if (configURL == null) {
                // Try the system classloader
                cl = ClassLoader.getSystemClassLoader();
                configURL = cl.getResource(providerResource);
            }

            ...
            engineClassName = providerName.substring(0,
                                providerName.indexOf('#')).trim();
            instance = (Engine) Engine.loadClass(engineClassName)
                            .newInstance();
            ...
}

参考:
http://www.restlet.org/documentation/1.1/tutorial

你可能感兴趣的:(thread,tomcat,servlet,jdbc,REST)