View Javadoc

1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.jetty.example;
16  
17  import org.mortbay.jetty.Server;
18  import org.mortbay.jetty.handler.ContextHandler;
19  import org.mortbay.xml.XmlConfiguration;
20  
21  public class FromXmlConfiguration
22  {
23      public static void main(String[] args)
24          throws Exception
25      {
26          String server_config=
27              "<Configure id=\"Server\" class=\"org.mortbay.jetty.Server\">\n"+
28              "  <Call name=\"addConnector\">\n" +
29              "    <Arg>\n" +
30              "      <New class=\"org.mortbay.jetty.nio.SelectChannelConnector\">\n" +
31              "        <Set name=\"port\"><SystemProperty name=\"jetty.port\" default=\"8080\"/></Set>\n" +
32              "      </New>\n" +
33              "    </Arg>\n"+
34              "  </Call>\n"+
35              "</Configure>\n";
36     
37          String context_config=
38              "<Configure id=\"Server\" class=\"org.mortbay.jetty.servlet.Context\">\n"+
39              "  <Set name=\"contextPath\">/</Set>\n"+
40              "  <Set name=\"resourceBase\"><SystemProperty name=\"jetty.docroot\" default=\".\"/></Set>\n"+
41              "  <Call name=\"addServlet\"><Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg><Arg>/</Arg></Call>\n"+
42              "</Configure>\n";
43          
44          // Apply configuration to an existing object
45          Server server = new Server();
46          XmlConfiguration configuration = new XmlConfiguration(server_config); 
47          configuration.configure(server);
48          
49          // configuration creates new object
50          configuration = new XmlConfiguration(context_config); 
51          ContextHandler context = (ContextHandler)configuration.configure();
52          
53          server.setHandler(context);
54          server.start();
55      }
56  }