~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/www/src/main/java/com/eucalyptus/bootstrap/HttpServerBootstrapper.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
Import upstream version 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
 *    SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
54
54
 *    IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
55
55
 *    BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
56
 
 *    THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
 
56
 *    THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
57
57
 *    OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
58
58
 *    WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
59
59
 *    ANY SUCH LICENSES OR RIGHTS.
60
60
 *******************************************************************************/
61
61
package com.eucalyptus.bootstrap;
62
62
 
 
63
import java.io.File;
63
64
import java.net.URL;
64
65
import java.util.concurrent.ExecutorService;
65
66
import java.util.concurrent.Executors;
67
68
import org.mortbay.jetty.Server;
68
69
import org.mortbay.xml.XmlConfiguration;
69
70
import com.eucalyptus.bootstrap.Bootstrap.Stage;
 
71
import com.eucalyptus.component.id.Eucalyptus;
 
72
import com.eucalyptus.component.id.HttpService;
70
73
import com.eucalyptus.configurable.ConfigurableClass;
71
74
import com.eucalyptus.configurable.ConfigurableField;
72
75
import com.eucalyptus.configurable.ConfigurableProperty;
73
 
import com.eucalyptus.event.PassiveEventListener;
 
76
import com.eucalyptus.configurable.ConfigurablePropertyException;
 
77
import com.eucalyptus.configurable.PropertyChangeListener;
 
78
import com.eucalyptus.empyrean.Empyrean;
 
79
import com.eucalyptus.system.BaseDirectory;
74
80
import com.eucalyptus.system.Threads;
75
 
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
 
81
import java.util.concurrent.TimeUnit;
 
82
import com.google.common.io.Files;
76
83
 
77
 
@Provides( Component.jetty )
78
 
@RunDuring( Bootstrap.Stage.CloudServiceInit )
79
 
@DependsLocal( Component.eucalyptus )
80
 
@ConfigurableClass( root = "www", description = "Parameters controlling the web UI's http server." )
81
 
public class HttpServerBootstrapper extends Bootstrapper {  
 
84
@Provides( Empyrean.class )
 
85
@RunDuring( Bootstrap.Stage.UserCredentialsInit )
 
86
@DependsLocal( Eucalyptus.class )
 
87
@ConfigurableClass( root = "www",
 
88
                    description = "Parameters controlling the web UI's http server." )
 
89
public class HttpServerBootstrapper extends Bootstrapper {
82
90
  private static Logger LOG        = Logger.getLogger( HttpServerBootstrapper.class );
83
 
  @ConfigurableField( description = "Listen to HTTPs on this port.", initial = "" + 8443, changeListener = PortChangeListener.class, displayName="euca.https.port" )
 
91
  @ConfigurableField( description = "Listen to HTTPs on this port.",
 
92
                      initial = "" + 8443,
 
93
                      changeListener = PortChangeListener.class,
 
94
                      displayName = "euca.https.port" )
84
95
  public static Integer HTTPS_PORT = 8443;
85
 
  @ConfigurableField( description = "Listen to HTTP on this port.", initial = "" + 8080, changeListener = PortChangeListener.class, displayName="euca.http.port" )
 
96
  @ConfigurableField( description = "Listen to HTTP on this port.",
 
97
                      initial = "" + 8080,
 
98
                      changeListener = PortChangeListener.class,
 
99
                      displayName = "euca.http.port" )
86
100
  public static Integer HTTP_PORT  = 8080;
87
101
  private static Server jettyServer;
88
 
  private static Thread serverThread;
89
 
  @ConfigurableField( initial = "", description = "Http Proxy Host" )
90
 
  public static String httpProxyHost;
91
 
  @ConfigurableField( initial = "", description = "Http Proxy Port" )
92
 
  public static String httpProxyPort;
93
 
 
 
102
  @ConfigurableField( initial = "",
 
103
                      description = "Http Proxy Host" )
 
104
  public static String  httpProxyHost;
 
105
  @ConfigurableField( initial = "",
 
106
                      description = "Http Proxy Port" )
 
107
  public static String  httpProxyPort;
 
108
  
94
109
  private static void setupJettyServer( ) throws Exception {
95
110
    //http proxy setup
96
 
        if(System.getProperty("http.proxyHost") != null) {
97
 
                httpProxyHost = System.getProperty("http.proxyHost");
98
 
        }
99
 
        if(System.getProperty("http.proxyPort") != null) {
100
 
                httpProxyPort = System.getProperty("http.proxyPort");
101
 
        } 
102
 
        jettyServer = new org.mortbay.jetty.Server( );
 
111
    if ( System.getProperty( "http.proxyHost" ) != null ) {
 
112
      httpProxyHost = System.getProperty( "http.proxyHost" );
 
113
    }
 
114
    if ( System.getProperty( "http.proxyPort" ) != null ) {
 
115
      httpProxyPort = System.getProperty( "http.proxyPort" );
 
116
    }
 
117
    jettyServer = new org.mortbay.jetty.Server( );
103
118
    System.setProperty( "euca.http.port", "" + HTTP_PORT );
104
119
    System.setProperty( "euca.https.port", "" + HTTPS_PORT );
105
120
    URL defaultConfig = ClassLoader.getSystemResource( "eucalyptus-jetty.xml" );
108
123
  }
109
124
  
110
125
  private static void startJettyServer( ) {
111
 
    serverThread = Threads.newThread( new Runnable( ) {
 
126
    Threads.lookup( HttpService.class, HttpServerBootstrapper.class ).submit( new Runnable( ) {
112
127
      @Override
113
128
      public void run( ) {
114
129
        try {
 
130
          
 
131
          String path = "/var/run/eucalyptus/webapp";
 
132
          File dir = new File( BaseDirectory.HOME + path );
 
133
          if ( dir.exists( ) ) {
 
134
            Files.deleteDirectoryContents( dir );
 
135
            dir.delete( );
 
136
          }
 
137
          
115
138
          jettyServer.start( );
116
139
        } catch ( Exception e ) {
117
140
          LOG.debug( e, e );
118
141
        }
119
142
      }
120
 
    }, "jetty" );
121
 
    serverThread.start( );
 
143
    } );
122
144
  }
123
145
  
124
146
  @Override
125
 
  public boolean load( Stage current ) throws Exception {
 
147
  public boolean load( ) throws Exception {
126
148
    setupJettyServer( );
127
149
    return true;
128
150
  }
133
155
    startJettyServer( );
134
156
    return true;
135
157
  }
136
 
  public static class PortChangeListener extends PassiveEventListener<ConfigurableProperty> {
 
158
  
 
159
  /**
 
160
   * @see com.eucalyptus.bootstrap.Bootstrapper#enable()
 
161
   */
 
162
  @Override
 
163
  public boolean enable( ) throws Exception {
 
164
    return true;
 
165
  }
 
166
  
 
167
  /**
 
168
   * @see com.eucalyptus.bootstrap.Bootstrapper#stop()
 
169
   */
 
170
  @Override
 
171
  public boolean stop( ) throws Exception {
 
172
    return true;
 
173
  }
 
174
  
 
175
  /**
 
176
   * @see com.eucalyptus.bootstrap.Bootstrapper#destroy()
 
177
   */
 
178
  @Override
 
179
  public void destroy( ) throws Exception {}
 
180
  
 
181
  /**
 
182
   * @see com.eucalyptus.bootstrap.Bootstrapper#disable()
 
183
   */
 
184
  @Override
 
185
  public boolean disable( ) throws Exception {
 
186
    return true;
 
187
  }
 
188
  
 
189
  /**
 
190
   * @see com.eucalyptus.bootstrap.Bootstrapper#check()
 
191
   */
 
192
  @Override
 
193
  public boolean check( ) throws Exception {
 
194
    return true;
 
195
  }
 
196
  
 
197
  public static class PortChangeListener implements PropertyChangeListener {
 
198
    /**
 
199
     * @see com.eucalyptus.configurable.PropertyChangeListener#fireChange(com.eucalyptus.configurable.ConfigurableProperty,
 
200
     *      java.lang.Object)
 
201
     */
137
202
    @Override
138
 
    public void firingEvent( ConfigurableProperty t ) {
139
 
      LOG.info( "Change occurred to property " + t.getQualifiedName( ) + " which requires restarting the web server." );
140
 
      if ( jettyServer == null ) {
 
203
    public void fireChange( ConfigurableProperty t, Object newValue ) throws ConfigurablePropertyException {
 
204
      LOG.warn( "Change occurred to property " + t.getQualifiedName( ) + " which will restart the servlet container." );
 
205
      try {
 
206
        t.getField( ).set( null, t.getTypeParser( ).apply( newValue ) );
 
207
      } catch ( IllegalArgumentException e1 ) {
 
208
        throw new ConfigurablePropertyException( e1 );
 
209
      } catch ( IllegalAccessException e1 ) {
 
210
        throw new ConfigurablePropertyException( e1 );
 
211
      }
 
212
      if ( jettyServer == null || !Bootstrap.isFinished( ) ) {
141
213
        return;
142
214
      } else if ( jettyServer.isRunning( ) ) {
143
215
        try {
145
217
          for ( int i = 0; i < 10 && !jettyServer.isStopped( ) && jettyServer.isStopping( ); i++ ) {
146
218
            try {
147
219
              TimeUnit.MILLISECONDS.sleep( 500 );
148
 
            } catch ( InterruptedException e ) {}
 
220
            } catch ( InterruptedException e ) {
 
221
              Thread.currentThread( ).interrupt( );
 
222
            }
149
223
          }
150
224
          jettyServer.destroy( );
151
225
        } catch ( Exception e ) {
152
226
          LOG.debug( e, e );
153
227
        }
154
228
        try {
155
 
          System.setProperty( t.getDisplayName( ), t.getValue( ) );
 
229
          System.setProperty( t.getDisplayName( ), ( String ) newValue );
156
230
          setupJettyServer( );
157
231
          startJettyServer( );
158
232
        } catch ( Exception e ) {
161
235
      }
162
236
    }
163
237
  }
164
 
 
 
238
  
165
239
}