1 package org.apache.maven.continuum.wagon;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 import org.apache.maven.wagon.authentication.AuthenticationInfo;
28 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
29 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Startable;
31 import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException;
33 import org.mortbay.http.BasicAuthenticator;
34 import org.mortbay.http.HashUserRealm;
35 import org.mortbay.http.SecurityConstraint;
36 import org.mortbay.http.SocketListener;
37 import org.mortbay.http.handler.SecurityHandler;
38 import org.mortbay.jetty.Server;
39 import org.mortbay.jetty.servlet.ServletHolder;
40 import org.mortbay.jetty.servlet.ServletHttpContext;
41 import org.mortbay.util.MultiException;
42
43
44
45
46
47
48 public class ServletServer
49 implements Initializable, Startable
50 {
51 public static final String ROLE = ServletServer.class.getName();
52
53 private Server server;
54
55 private int port;
56
57 private List contexts;
58
59 public void initialize()
60 throws InitializationException
61 {
62 server = new Server();
63
64 SocketListener listener = new SocketListener();
65 listener.setPort( port );
66
67 server.addListener( listener );
68
69 if ( contexts != null )
70 {
71 try
72 {
73 Iterator itcontext = contexts.iterator();
74
75 while ( itcontext.hasNext() )
76 {
77 Context wdc = (Context) itcontext.next();
78
79 ServletHttpContext context = (ServletHttpContext) server.getContext( wdc.getId() );
80
81 initContext( wdc, context );
82 }
83 }
84 catch ( Exception e )
85 {
86 throw new InitializationException( "Unable to initialize.", e );
87 }
88 }
89 }
90
91 private void initContext( Context wdc, ServletHttpContext context )
92 throws ClassNotFoundException, InstantiationException, IllegalAccessException
93 {
94 AuthenticationInfo authenticationInfo = wdc.getAuthenticationInfo();
95 if ( authenticationInfo != null )
96 {
97 HashUserRealm userRealm = new HashUserRealm( "basic" );
98 userRealm.put( authenticationInfo.getUserName(), authenticationInfo.getPassword() );
99 context.getHttpServer().addRealm( userRealm );
100
101 context.setAuthenticator( new BasicAuthenticator() );
102 context.addSecurityConstraint( "/*", new SecurityConstraint( "any", SecurityConstraint.ANY_ROLE ) );
103 context.addHandler( new SecurityHandler() );
104 }
105
106 Iterator itpaths = wdc.getServlets().iterator();
107 while ( itpaths.hasNext() )
108 {
109 Servlet servlet = (Servlet) itpaths.next();
110 initServlet( context, servlet );
111 }
112 }
113
114 private void initServlet( ServletHttpContext context, Servlet path )
115 throws ClassNotFoundException, InstantiationException, IllegalAccessException
116 {
117 ServletHolder servlet = context.addServlet( path.getId(), path.getPath(), path.getServlet() );
118
119 Iterator itparams = path.getParameters().entrySet().iterator();
120 while ( itparams.hasNext() )
121 {
122 Map.Entry entry = (Entry) itparams.next();
123 servlet.setInitParameter( (String) entry.getKey(), (String) entry.getValue() );
124 }
125 }
126
127 public void start()
128 throws StartingException
129 {
130 try
131 {
132 server.start();
133 }
134 catch ( MultiException e )
135 {
136 throw new StartingException( "Error starting the jetty webdav server: ", e );
137 }
138 }
139
140 public void stop()
141 throws StoppingException
142 {
143 try
144 {
145 server.stop();
146 }
147 catch ( InterruptedException e )
148 {
149 throw new StoppingException( "Error stopping the jetty webdav server: ", e );
150 }
151 }
152 }