首页

基于apache的fulcrum源码包定义JettyService服务实现源码示例分析

标签:jetty容器,web服务器,apache,fulcrum,源码示例     发布时间:2018-05-08   

一、前言

关于apachefulcrum的源码包定义org.apache.fulcrum.jetty.JettyService、org.apache.fulcrum.jetty.JettyServiceImpl接口实现类,通过该源码示例可以对自定义jetty应用服务器实现由更进一步的认识。

二、源码示例

1.JettyService接口

package org.apache.fulcrum.jetty;@b@@b@import org.mortbay.jetty.Server;@b@@b@public abstract interface JettyService@b@{@b@  public abstract Server getServer();@b@}

2.JettyServiceImpl实现类

package org.apache.fulcrum.jetty.impl;@b@@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import org.apache.avalon.framework.activity.Initializable;@b@import org.apache.avalon.framework.activity.Startable;@b@import org.apache.avalon.framework.configuration.Configuration;@b@import org.apache.avalon.framework.configuration.ConfigurationException;@b@import org.apache.avalon.framework.configuration.Reconfigurable;@b@import org.apache.avalon.framework.context.Context;@b@import org.apache.avalon.framework.context.ContextException;@b@import org.apache.avalon.framework.context.Contextualizable;@b@import org.apache.avalon.framework.logger.AbstractLogEnabled;@b@import org.apache.avalon.framework.logger.LogEnabled;@b@import org.apache.avalon.framework.logger.Logger;@b@import org.apache.fulcrum.jetty.JettyService;@b@import org.mortbay.jetty.Server;@b@import org.mortbay.xml.XmlConfiguration;@b@@b@public class JettyServiceImpl extends AbstractLogEnabled@b@  implements LogEnabled, Startable, Contextualizable, Initializable, Reconfigurable, JettyService@b@{@b@  private Server server;@b@  private String[] configurationLocations;@b@  private File serviceApplicationDir;@b@@b@  public void contextualize(Context context)@b@    throws ContextException@b@  {@b@    this.serviceApplicationDir = ((File)context.get("context-root"));@b@  }@b@@b@  public void configure(Configuration configuration)@b@    throws ConfigurationException@b@  {@b@    Configuration[] configurationsList = configuration.getChild("configurations").getChildren("configuration");@b@    this.configurationLocations = new String[configurationsList.length];@b@@b@    for (int i = 0; i < this.configurationLocations.length; ++i)@b@    {@b@      this.configurationLocations[i] = configurationsList[i].getValue();@b@    }@b@@b@    if (this.configurationLocations.length == 0)@b@    {@b@      String msg = "No configuration files for the Jetty are defined";@b@      throw new ConfigurationException(msg);@b@    }@b@@b@    Configuration[] propertiesConfiguration = configuration.getChild("properties", true).getChildren("property");@b@@b@    for (int i = 0; i < propertiesConfiguration.length; ++i)@b@    {@b@      String key = propertiesConfiguration[i].getAttribute("name");@b@      String value = propertiesConfiguration[i].getValue();@b@      getLogger().info("Setting the system property '" + key + "'==>'" + value + "'");@b@      System.setProperty(key, value);@b@    }@b@  }@b@@b@  public void initialize()@b@    throws Exception@b@  {@b@    Server currServer = new Server();@b@@b@    for (int i = 0; i < this.configurationLocations.length; ++i)@b@    {@b@      String currConfigurationLocation = this.configurationLocations[i];@b@      getLogger().info("Loading the Jetty serviceConfiguration file : " + currConfigurationLocation);@b@      InputStream is = locate(this.serviceApplicationDir, currConfigurationLocation);@b@      XmlConfiguration configuration = new XmlConfiguration(is);@b@      configuration.configure(currServer);@b@      is.close();@b@    }@b@@b@    this.server = currServer;@b@  }@b@@b@  public void start()@b@    throws Exception@b@  {@b@    getServer().start();@b@  }@b@@b@  public void stop()@b@    throws Exception@b@  {@b@    getServer().stop();@b@  }@b@@b@  public void reconfigure(Configuration configuration)@b@    throws ConfigurationException@b@  {@b@    if (configuration != null)@b@    {@b@      configure(configuration);@b@    }@b@@b@    try@b@    {@b@      initialize();@b@    }@b@    catch (Exception e)@b@    {@b@      String msg = "Initializing the new server failed";@b@      throw new ConfigurationException(msg, e);@b@    }@b@  }@b@@b@  public Server getServer()@b@  {@b@    return this.server;@b@  }@b@@b@  private InputStream locate(File applicationDir, String location)@b@    throws IOException@b@  {@b@    if ((location == null) || (location.length() == 0))@b@    {@b@      throw new IllegalArgumentException("location is null or empty");@b@    }@b@@b@    File file = null;@b@    InputStream is = null;@b@@b@    if (!(location.startsWith("/")))@b@    {@b@      file = new File(applicationDir, location);@b@@b@      getLogger().debug("Looking for " + location + " in the application directory");@b@@b@      if (file.exists())@b@      {@b@        is = new FileInputStream(file);@b@        getLogger().debug("Found " + location + " as " + file.getAbsolutePath());@b@      }@b@@b@    }@b@@b@    if ((is == null) && (location.startsWith("/")))@b@    {@b@      file = new File(location);@b@@b@      getLogger().debug("Looking for " + location + " as absolute file location");@b@@b@      if ((file.isAbsolute()) && (file.exists()))@b@      {@b@        is = new FileInputStream(file);@b@        getLogger().debug("Found " + location + " as " + file.getAbsolutePath());@b@      }@b@@b@    }@b@@b@    if ((is == null) && (location.startsWith("/")))@b@    {@b@      getLogger().debug("Looking for " + location + " using the class loader");@b@      is = getClass().getResourceAsStream(location);@b@@b@      if (is != null)@b@      {@b@        getLogger().debug("Successfully located " + location);@b@      }@b@    }@b@@b@    if (is == null)@b@    {@b@      getLogger().warn("Unable to find any resource with the name '" + location + "'");@b@    }@b@@b@    return is;@b@  }@b@}