Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 277994
Collapse All | Expand All

(-)src/org/eclipse/rwt/internal/resources/ResourceManagerImpl.java (-72 / +98 lines)
Lines 46-51 Link Here
46
  private ClassLoader loader;
46
  private ClassLoader loader;
47
  private ThreadLocal contextLoader;
47
  private ThreadLocal contextLoader;
48
  private JsConcatenator jsConcatenator;
48
  private JsConcatenator jsConcatenator;
49
  private Object jsConcatenatorLock = new Object();
49
50
50
  private static final class Resource {
51
  private static final class Resource {
51
    /** the 'raw' content of the resource. In case of a text resource (charset 
52
    /** the 'raw' content of the resource. In case of a text resource (charset 
Lines 157-164 Link Here
157
    }
158
    }
158
    return result;
159
    return result;
159
  }
160
  }
160
161
  
162
  //////////////////////
161
  //////////////////////
163
  // interface Adaptable
162
  // interface Adaptable
164
  
163
  
Lines 166-197 Link Here
166
    Object result = null;
165
    Object result = null;
167
    if( adapter == JsConcatenator.class ){
166
    if( adapter == JsConcatenator.class ){
168
      if( jsConcatenator == null ) {
167
      if( jsConcatenator == null ) {
169
        jsConcatenator = new JsConcatenator() {
168
        synchronized( jsConcatenatorLock ) {
170
          private String content;
169
          if (jsConcatenator == null) {
171
          private boolean registered = false;
170
            jsConcatenator = new JsConcatenator() {
172
          public String getLocation() {
171
              private String content;
173
            String concatedName = "rap.js";
172
              private boolean registered = false;
174
            if( !registered ) {
173
              private Object registeredLock = new Object();
175
              byte[] content = getContent().getBytes();
174
              public String getLocation() {
176
              register( concatedName,
175
                String concatedName = "rap.js";
177
                        new ByteArrayInputStream( content ),
176
                if( !registered ) {
178
                        HTML.CHARSET_NAME_UTF_8,
177
                  synchronized( registeredLock ) {
179
                        RegisterOptions.VERSION );
178
                    if( !registered ) {
180
              registered = true;
179
                      byte[] content = getContent().getBytes();
181
            }
180
                      register( concatedName,
182
            return ResourceManagerImpl.this.getLocation( concatedName );
181
                                new ByteArrayInputStream( content ),
183
          }
182
                                HTML.CHARSET_NAME_UTF_8,
184
183
                                RegisterOptions.VERSION );
185
          public void startJsConcatenation() {
184
                      registered = true;
186
            ResourceUtil.startJsConcatenation();
185
                    }
187
          }
186
                  }
188
          public String getContent() {
187
                }
189
            if( content == null ) {
188
                return ResourceManagerImpl.this.getLocation( concatedName );
190
              content = ResourceUtil.getJsConcatenationContentAsString();
189
              }
191
            }
190
192
            return content;
191
              public void startJsConcatenation() {
192
                ResourceUtil.startJsConcatenation();
193
              }
194
              public String getContent() {
195
                if( content == null ) {
196
                  content = ResourceUtil.getJsConcatenationContentAsString();
197
                }
198
                return content;
199
              }
200
            };
193
          }
201
          }
194
        };
202
        }
195
      }
203
      }
196
      result = jsConcatenator;
204
      result = jsConcatenator;
197
    }
205
    }
Lines 228-242 Link Here
228
    ParamCheck.notNull( name, "name" );
236
    ParamCheck.notNull( name, "name" );
229
    ParamCheck.notNull( is, "is" );
237
    ParamCheck.notNull( is, "is" );
230
    String key = createKey( name );
238
    String key = createKey( name );
231
    try {
239
    synchronized( repository ) {
232
      int[] content = ResourceUtil.readBinary( is );
240
      try {
233
      doRegister( name, null, RegisterOptions.NONE, key, content );
241
        int[] content = ResourceUtil.readBinary( is );
234
    } catch ( IOException e ) {
242
        doRegister( name, null, RegisterOptions.NONE, key, content );
235
      String text = "Failed to register resource ''{0}''.";
243
      } catch ( IOException e ) {
236
      String msg = MessageFormat.format( text, new Object[] { name } );
244
        String text = "Failed to register resource ''{0}''.";
237
      throw new ResourceRegistrationException( msg, e ) ;
245
        String msg = MessageFormat.format( text, new Object[] { name } );
246
        throw new ResourceRegistrationException( msg, e ) ;
247
      }
248
      repository.put( key, name );
238
    }
249
    }
239
    repository.put( key, name );
240
  }
250
  }
241
251
242
  public void register( final String name,
252
  public void register( final String name,
Lines 250-264 Link Here
250
    ParamCheck.notNull( options, "options" );
260
    ParamCheck.notNull( options, "options" );
251
    boolean compress = shouldCompress( options );
261
    boolean compress = shouldCompress( options );
252
    String key = createKey( name );
262
    String key = createKey( name );
253
    try {
263
    synchronized( repository ) {
254
      int[] content = ResourceUtil.read( is, charset, compress );
264
      try {
255
      doRegister( name, charset, options, key, content );
265
        int[] content = ResourceUtil.read( is, charset, compress );
256
    } catch ( IOException e ) {
266
        doRegister( name, charset, options, key, content );
257
      String text = "Failed to register resource ''{0}''.";
267
      } catch ( IOException e ) {
258
      String msg = MessageFormat.format( text, new Object[] { name } );
268
        String text = "Failed to register resource ''{0}''.";
259
      throw new ResourceRegistrationException( msg, e ) ;
269
        String msg = MessageFormat.format( text, new Object[] { name } );
270
        throw new ResourceRegistrationException( msg, e ) ;
271
      }
272
      repository.put( key, name );
260
    }
273
    }
261
    repository.put( key, name );
262
  }
274
  }
263
  
275
  
264
  public String getCharset( final String name ) {
276
  public String getCharset( final String name ) {
Lines 270-285 Link Here
270
  public boolean isRegistered( final String name ) {
282
  public boolean isRegistered( final String name ) {
271
    ParamCheck.notNull( name, "name" );
283
    ParamCheck.notNull( name, "name" );
272
    String key = createKey( name );
284
    String key = createKey( name );
273
    String fileName = ( String )repository.get( key );
285
    synchronized( repository ) {
274
    return fileName != null;    
286
      String fileName = ( String )repository.get( key );
287
      return fileName != null;
288
    }
275
  }
289
  }
276
290
277
  public String getLocation( final String name ) {
291
  public String getLocation( final String name ) {
278
    ParamCheck.notNull( name, "name" );
292
    ParamCheck.notNull( name, "name" );
279
    String key = createKey( name );
293
    String key = createKey( name );
280
    String fileName = ( String )repository.get( key );
294
    synchronized( repository ) {
281
    Assert.isNotNull( fileName, "No resource registered for key " + name );
295
      String fileName = ( String )repository.get( key );
282
    return createRequestURL( fileName, findVersion( name ) );
296
      Assert.isNotNull( fileName, "No resource registered for key " + name );
297
      return createRequestURL( fileName, findVersion( name ) );
298
    }
283
  }
299
  }
284
  
300
  
285
  public URL getResource( final String name ) {
301
  public URL getResource( final String name ) {
Lines 316-333 Link Here
316
  public InputStream getRegisteredContent( final String name ) {
332
  public InputStream getRegisteredContent( final String name ) {
317
    InputStream result = null;
333
    InputStream result = null;
318
    String key = createKey( name );
334
    String key = createKey( name );
319
    String fileName = ( String )repository.get( key );
335
    synchronized( repository ) {
320
    if( fileName != null ) {
336
      String fileName = ( String )repository.get( key );
321
      // TODO [rst] Works only for non-versioned content for now
337
      if( fileName != null ) {
322
      File file = getDiskLocation( name, null );
338
        // TODO [rst] Works only for non-versioned content for now
323
      try {
339
        File file = getDiskLocation( name, null );
324
        result = new FileInputStream( file );
340
        try {
325
      } catch( FileNotFoundException e ) {
341
          result = new FileInputStream( file );
326
        // should not happen
342
        } catch( FileNotFoundException e ) {
327
        throw new RuntimeException( e );
343
          // should not happen
344
          throw new RuntimeException( e );
345
        }
328
      }
346
      }
347
      return result;
329
    }
348
    }
330
    return result;
331
  }
349
  }
332
  
350
  
333
  //////////////////
351
  //////////////////
Lines 383-393 Link Here
383
  private String doLoad( final String resource ) {
401
  private String doLoad( final String resource ) {
384
    String key = createKey( resource );
402
    String key = createKey( resource );
385
    if( !repository.containsKey( key ) ) {
403
    if( !repository.containsKey( key ) ) {
386
      try {
404
      synchronized( repository ) {
387
        register( resource );
405
        if ( !repository.containsKey( key ) ) {
388
      } catch( ResourceRegistrationException e ) {
406
          try {
389
        // application file which is not managed by the resource manager
407
            register( resource );
390
        repository.put( key, resource );
408
          } catch( ResourceRegistrationException e ) {
409
            // application file which is not managed by the resource manager
410
            repository.put( key, resource );
411
          }
412
        }
391
      }
413
      }
392
    }
414
    }
393
    return createRequestURL( resource, null );
415
    return createRequestURL( resource, null );
Lines 401-416 Link Here
401
    // TODO [rh] should throw exception if contains key but has different
423
    // TODO [rh] should throw exception if contains key but has different
402
    //      charset or options
424
    //      charset or options
403
    if( !repository.containsKey( key ) ) {
425
    if( !repository.containsKey( key ) ) {
404
      boolean compress = shouldCompress( options );
426
      synchronized( repository ) {
405
      try {
427
        if ( !repository.containsKey( key ) ) {
406
        int[] content = ResourceUtil.read( name, charset, compress );
428
          boolean compress = shouldCompress( options );
407
        doRegister( name, charset, options, key, content );
429
          try {
408
      } catch ( IOException e ) {
430
            int[] content = ResourceUtil.read( name, charset, compress );
409
        String text = "Failed to register resource ''{0}''.";
431
            doRegister( name, charset, options, key, content );
410
        String msg = MessageFormat.format( text, new Object[] { name } );
432
          } catch ( IOException e ) {
411
        throw new ResourceRegistrationException( msg, e ) ;
433
            String text = "Failed to register resource ''{0}''.";
434
            String msg = MessageFormat.format( text, new Object[] { name } );
435
            throw new ResourceRegistrationException( msg, e ) ;
436
          }
437
          repository.put( key, name );
438
        }
412
      }
439
      }
413
      repository.put( key, name );
414
    }
440
    }
415
  }
441
  }
416
442

Return to bug 277994