Jetty Study Notes-entry

Source: Internet
Author: User

Jetty will not write the download.

First, Jetty starts the script Jetty. Sh.

1. Set jetty_home

################################################### Try to determine JETTY_HOME if not set##################################################if [ -z "$JETTY_HOME" ]then  JETTY_SH=$0  case "$JETTY_SH" in    /*)   ;;    ./*)  ;;    *)    JETTY_SH=./$JETTY_SH ;;  esac  JETTY_HOME=${JETTY_SH%/*/*}  if [ ! -f "${JETTY_SH%/*/*}/$JETTY_INSTALL_TRACE_FILE" ]  then    JETTY_HOME=  fifi...

2. Search for configuration files

################################################### Try to find this script's configuration file,# but only if no configurations were given on the# command line.##################################################if [ -z "$JETTY_CONF" ]then  if [ -f /etc/jetty.conf ]  then    JETTY_CONF=/etc/jetty.conf  elif [ -f "$JETTY_HOME/etc/jetty.conf" ]  then    JETTY_CONF=$JETTY_HOME/etc/jetty.conf  fifi

3. Search for and set Java and jetty-related information

...################################################### Setup JAVA if unset##################################################if [ -z "$JAVA" ]then  JAVA=$(which java)fiif [ -z "$JAVA" ]then  echo "Cannot find a Java JDK. Please set either set JAVA or put java (>=1.5) in your PATH." 2>&2  exit 1fi...

4. Assemble and execute command lines

JETTY_START=$JETTY_HOME/start.jar[ ! -f "$JETTY_START" ] && JETTY_START=$JETTY_HOME/lib/start.jarSTART_INI=$(dirname $JETTY_START)/start.ini[ -r "$START_INI" ] || START_INI=""RUN_ARGS=(${JAVA_OPTIONS[@]} -jar "$JETTY_START" $JETTY_ARGS "${CONFIGS[@]}")RUN_CMD=("$JAVA" ${RUN_ARGS[@]})

5. Execute

case "$ACTION" in  start)...if [ "$JETTY_USER" ]      then        touch "$JETTY_PID"        chown "$JETTY_USER" "$JETTY_PID"        # FIXME: Broken solution: wordsplitting, pathname expansion, arbitrary command execution, etc.        su - "$JETTY_USER" -c "          ${RUN_CMD[*]} --daemon &          disown \$!          echo \$! > '$JETTY_PID'"      else        "${RUN_CMD[@]}" &        disown $!        echo $! > "$JETTY_PID"      fi      echo "STARTED Jetty `date`" ...

Start. Jar

1. Search for org. Eclipse. Jetty. Start. Main Based on the configuration.

Manifest-Version: 1.0Archiver-Version: Plexus ArchiverCreated-By: Apache MavenBuilt-By: jesseBuild-Jdk: 1.6.0_19Main-Class: org.eclipse.jetty.start.MainImplementation-Vendor: Eclipse.org - JettyImplementation-Version: 7.2.0.v20101020url: http://www.eclipse.org/jetty

2. Execute the main method

(1). Resolution parameter: parsecommandline

(2). Read the configuration file. Start. config

 // Load potential Config (start.config) List<String> configuredXmls = loadConfig(xmls);

private InputStream  [More ...] getConfigStream() throws FileNotFoundException    {        String config=_startConfig;        if (config==null || config.length()==0)            config=System.getProperty("START","org/eclipse/jetty/start/start.config");        Config.debug("config=" + config);        // Look up config as resource first.        InputStream cfgstream = getClass().getClassLoader().getResourceAsStream(config);        // resource not found, try filesystem next        if (cfgstream == null)            cfgstream = new FileInputStream(config);        return cfgstream;    }

(3) initialize classloader

  // Get Desired Classpath based on user provided Active Options.  Classpath classpath = _config.getActiveClasspath();  System.setProperty("java.class.path",classpath.toString());  lassLoader cl = classpath.getClassLoader();...   // Set current context class loader to what is selected.   Thread.currentThread().setContextClassLoader(cl);

(4). initialize the security manager

// Initialize the SecurityinitSecurity(cl);

(5) execute the mainclass specified in the configuration file: org. Eclipse. Jetty. xml. xmlconfiguration. Class

// Get main class as defined in start.config            String classname = _config.getMainClassname();            // Check for override of start class (via "jetty.server" property)            String mainClass = System.getProperty("jetty.server");            if (mainClass != null)                classname = mainClass;            // Check for override of start class (via "main.class" property)            mainClass = System.getProperty("main.class");            if (mainClass != null)                classname = mainClass;            Config.debug("main.class=" + classname);            invokeMain(cl,classname,configuredXmls);

Parsing of start. config

The file starts with an explanation of the format in the content, which can be understood by comparing the content below

If the Lib file or directory exists, traverse the jar and ZIP files to a start Property

# add a property defined library directory${lib}/**                                        exists ${lib}

Set Jetty. Home to System Properties

# Try different settings of jetty.home until the start.jar is found.jetty.home=.                                     ! exists $(jetty.home)/start.jar jetty.home=..                                    ! exists $(jetty.home)/start.jar jetty.home=jetty-distribution/src/main/resources     ! exists $(jetty.home)/start.jar jetty.home=../jetty-distribution/src/main/resources  ! exists $(jetty.home)/start.jar jetty.home=.                                     ! exists $(jetty.home)/start.jarjetty.home/=$(jetty.home)                        exists $(jetty.home)/start.jar

Set main class

# The main class to runorg.eclipse.jetty.xml.XmlConfiguration.class${start.class}.class                             property start.class

Set the default configuration file

# The default configuration files$(jetty.home)/etc/jetty.xml                      nargs == 0./jetty-server/src/main/config/etc/jetty.xml     nargs == 0 AND ! exists $(jetty.home)/etc/jetty.xml

Set the unique features of the module. If any of all/resources/default in the options to be started, execute the relevant settings.

# Add a resources directory if it is there[All,resources,default]$(jetty.home)/resources/

Xmlconfiguration

1. Use accesscontroller. doprivileged to grant the code privilege inside the run to avoid checkpermission. For details, see: http://www.jar114.com/jdk5/zh_CN/api/java/security/AccessController.html

  public static void main(final String[] args) throws Exception    {        final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();        AccessController.doPrivileged(new PrivilegedAction()        {            public Object run()            {              ...             }        });        Throwable th = exception.get();        ... }

2. Read the configuration in start. jar.

 public Object run()            {                try                {                    Properties properties = null;                    // Look for properties from start.jar                    try                    {                        Class<?> config = XmlConfiguration.class.getClassLoader().loadClass("org.eclipse.jetty.start.Config");                        properties = (Properties)config.getMethod("getProperties").invoke(null);                        Log.debug("org.eclipse.jetty.start.Config properties = {}",properties);                    }

3. Load properties or Parse XML

 // For all arguments, load properties or parse XMLs                    XmlConfiguration last = null;                    Object[] obj = new Object[args.length];                    for (int i = 0; i < args.length; i++)                    {                        if (args[i].toLowerCase().endsWith(".properties"))                        {                            properties.load(Resource.newResource(args[i]).getInputStream());                        }                        else                        {                            XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(args[i]).getURL());                            ...                        }                    }

4. Execute lifecycle, go

/ For all objects created by XmlConfigurations, start them if they are lifecycles.                    for (int i = 0; i < args.length; i++)                    {                        if (obj[i] instanceof LifeCycle)                        {                            LifeCycle lc = (LifeCycle)obj[i];                            if (!lc.isRunning())                                lc.start();                        }                    }

Jetty. xml

In the third step above, parsing XML will process Jetty. XML (for details about the location of the jetty. xml file, refer to the above section)

Below is a simple configuration of Jetty. xml

<Configure id = "server" class = "org. Eclipse. Jetty. server. Server">: equivalent

<Set name = "threadpool">: sets the _ threadpool attribute in the server object.

<New class = "org. Eclipse. Jetty. util. thread. queuedthreadpool">: Creates a pool.

<Set name = "minthreads"> 10 </set>: Set the pool attribute.

<Call name = "addconnector">: Call the addconnector method of the service.

<Arg>: Specifies the method parameters.

<Configure id="Server" class="org.eclipse.jetty.server.Server">    <Set name="ThreadPool">      <!-- Default queued blocking threadpool -->      <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">        <Set name="minThreads">10</Set>        <Set name="maxThreads">200</Set>      </New>    </Set>    <Call name="addConnector">      <Arg>          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">            <Set name="host"><Property name="jetty.host" /></Set>            <Set name="port"><Property name="jetty.port" default="8080"/>7001</Set>            <Set name="maxIdleTime">300000</Set>            <Set name="Acceptors">2</Set>            <Set name="statsOn">false</Set>            <Set name="confidentialPort">8443</Set>    <Set name="lowResourcesConnections">20000</Set>    <Set name="lowResourcesMaxIdleTime">5000</Set>          </New>      </Arg>    </Call>    <Set name="handler">      <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">        <Set name="handlers">         <Array type="org.eclipse.jetty.server.Handler">           <Item>             <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>           </Item>           <Item>             <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>           </Item>         </Array>        </Set>      </New>    </Set>    <Set name="stopAtShutdown">true</Set>    <Set name="sendServerVersion">true</Set>    <Set name="sendDateHeader">true</Set>    <Set name="gracefulShutdown">1000</Set></Configure>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.