~kirkland/eucalyptus/label-metadata

« back to all changes in this revision

Viewing changes to clc/modules/msgs/src/main/java/com/eucalyptus/images/ImageInfo.java

  • Committer: Neil
  • Date: 2010-06-18 23:43:02 UTC
  • mfrom: (1195.1.47 main)
  • Revision ID: neil@pall-20100618234302-ylt6pznuzk7j09pw
latest merge from main.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*******************************************************************************
2
 
*Copyright (c) 2009  Eucalyptus Systems, Inc.
3
 
4
 
*  This program is free software: you can redistribute it and/or modify
5
 
*  it under the terms of the GNU General Public License as published by
6
 
*  the Free Software Foundation, only version 3 of the License.
7
 
8
 
9
 
*  This file is distributed in the hope that it will be useful, but WITHOUT
10
 
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 
*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 
*  for more details.
13
 
14
 
*  You should have received a copy of the GNU General Public License along
15
 
*  with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
 
*  Please contact Eucalyptus Systems, Inc., 130 Castilian
18
 
*  Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
19
 
*  if you need additional information or have any questions.
20
 
21
 
*  This file may incorporate work covered under the following copyright and
22
 
*  permission notice:
23
 
24
 
*    Software License Agreement (BSD License)
25
 
26
 
*    Copyright (c) 2008, Regents of the University of California
27
 
*    All rights reserved.
28
 
29
 
*    Redistribution and use of this software in source and binary forms, with
30
 
*    or without modification, are permitted provided that the following
31
 
*    conditions are met:
32
 
33
 
*      Redistributions of source code must retain the above copyright notice,
34
 
*      this list of conditions and the following disclaimer.
35
 
36
 
*      Redistributions in binary form must reproduce the above copyright
37
 
*      notice, this list of conditions and the following disclaimer in the
38
 
*      documentation and/or other materials provided with the distribution.
39
 
40
 
*    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
41
 
*    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42
 
*    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43
 
*    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
44
 
*    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45
 
*    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46
 
*    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47
 
*    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48
 
*    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
 
*    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
 
*    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
51
 
*    THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
52
 
*    LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
53
 
*    SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
54
 
*    IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
55
 
*    BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
56
 
*    THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
57
 
*    OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
58
 
*    WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
59
 
*    ANY SUCH LICENSES OR RIGHTS.
60
 
*******************************************************************************/
 
2
 *Copyright (c) 2009  Eucalyptus Systems, Inc.
 
3
 * 
 
4
 *  This program is free software: you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation, only version 3 of the License.
 
7
 * 
 
8
 * 
 
9
 *  This file is distributed in the hope that it will be useful, but WITHOUT
 
10
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
12
 *  for more details.
 
13
 * 
 
14
 *  You should have received a copy of the GNU General Public License along
 
15
 *  with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 * 
 
17
 *  Please contact Eucalyptus Systems, Inc., 130 Castilian
 
18
 *  Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
 
19
 *  if you need additional information or have any questions.
 
20
 * 
 
21
 *  This file may incorporate work covered under the following copyright and
 
22
 *  permission notice:
 
23
 * 
 
24
 *    Software License Agreement (BSD License)
 
25
 * 
 
26
 *    Copyright (c) 2008, Regents of the University of California
 
27
 *    All rights reserved.
 
28
 * 
 
29
 *    Redistribution and use of this software in source and binary forms, with
 
30
 *    or without modification, are permitted provided that the following
 
31
 *    conditions are met:
 
32
 * 
 
33
 *      Redistributions of source code must retain the above copyright notice,
 
34
 *      this list of conditions and the following disclaimer.
 
35
 * 
 
36
 *      Redistributions in binary form must reproduce the above copyright
 
37
 *      notice, this list of conditions and the following disclaimer in the
 
38
 *      documentation and/or other materials provided with the distribution.
 
39
 * 
 
40
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
41
 *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 
42
 *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
43
 *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 
44
 *    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
45
 *    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
46
 *    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
47
 *    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
48
 *    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
49
 *    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
50
 *    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
 
51
 *    THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
 
52
 *    LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
 
53
 *    SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
 
54
 *    IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
 
55
 *    BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
 
56
 *    THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
 
57
 *    OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
 
58
 *    WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
 
59
 *    ANY SUCH LICENSES OR RIGHTS.
 
60
 *******************************************************************************/
61
61
/*
62
62
 * Author: chris grzegorczyk <grze@eucalyptus.com>
63
63
 */
64
64
 
65
65
package com.eucalyptus.images;
66
66
 
 
67
import java.security.Principal;
67
68
import java.util.ArrayList;
 
69
import java.util.HashSet;
68
70
import java.util.List;
 
71
import java.util.Set;
69
72
import javax.persistence.CascadeType;
70
73
import javax.persistence.Column;
71
74
import javax.persistence.Entity;
 
75
import javax.persistence.FetchType;
72
76
import javax.persistence.GeneratedValue;
73
77
import javax.persistence.Id;
74
78
import javax.persistence.JoinColumn;
75
79
import javax.persistence.JoinTable;
76
80
import javax.persistence.Lob;
77
81
import javax.persistence.ManyToMany;
 
82
import javax.persistence.OneToMany;
78
83
import javax.persistence.PersistenceContext;
79
84
import javax.persistence.Table;
80
85
import javax.persistence.Transient;
 
86
import org.apache.log4j.Logger;
81
87
import org.hibernate.annotations.Cache;
82
88
import org.hibernate.annotations.CacheConcurrencyStrategy;
 
89
import com.eucalyptus.auth.Groups;
83
90
import com.eucalyptus.auth.NoSuchUserException;
84
91
import com.eucalyptus.auth.UserInfo;
85
92
import com.eucalyptus.auth.Users;
 
93
import com.eucalyptus.auth.principal.Group;
 
94
import com.eucalyptus.auth.principal.User;
86
95
import com.eucalyptus.entities.EntityWrapper;
87
96
import com.eucalyptus.util.EucalyptusCloudException;
 
97
import com.eucalyptus.util.JoinTx;
 
98
import com.eucalyptus.util.TransactionException;
 
99
import com.eucalyptus.util.Transactions;
 
100
import com.eucalyptus.util.Tx;
88
101
import com.google.common.base.Function;
89
102
import edu.ucsb.eucalyptus.msgs.ImageDetails;
90
103
 
91
104
@Entity
92
 
@PersistenceContext(name="eucalyptus_general")
 
105
@PersistenceContext( name = "eucalyptus_general" )
93
106
@Table( name = "Images" )
94
 
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
 
107
@Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
95
108
public class ImageInfo implements Image {
96
109
  @Transient
97
 
  public static ImageInfo ALL = new ImageInfo();
 
110
  private static Logger   LOG = Logger.getLogger( ImageInfo.class );
 
111
  @Transient
 
112
  public static ImageInfo ALL = new ImageInfo( );
98
113
  @Id
99
114
  @GeneratedValue
100
115
  @Column( name = "image_id" )
101
 
  private Long id = -1l;
 
116
  private Long            id  = -1l;
102
117
  @Column( name = "image_name" )
103
 
  private String imageId;
 
118
  private String          imageId;
104
119
  @Column( name = "image_path" )
105
 
  private String imageLocation;
 
120
  private String          imageLocation;
106
121
  @Column( name = "image_availability" )
107
 
  private String imageState;
 
122
  private String          imageState;
108
123
  @Column( name = "image_owner_id" )
109
 
  private String imageOwnerId;
 
124
  private String          imageOwnerId;
110
125
  @Column( name = "image_arch" )
111
 
  private String architecture;
 
126
  private String          architecture;
112
127
  @Column( name = "image_type" )
113
 
  private String imageType;
 
128
  private String          imageType;
114
129
  @Column( name = "image_kernel_id" )
115
 
  private String kernelId;
 
130
  private String          kernelId;
116
131
  @Column( name = "image_ramdisk_id" )
117
 
  private String ramdiskId;
 
132
  private String          ramdiskId;
118
133
  @Column( name = "image_is_public" )
119
 
  private Boolean imagePublic;
 
134
  private Boolean         imagePublic;
120
135
  @Lob
121
136
  @Column( name = "image_signature" )
122
 
  private String signature;
123
 
//  @ManyToMany( cascade = CascadeType.PERSIST )
124
 
//  @JoinTable(
125
 
//      name = "image_has_groups",
126
 
//      joinColumns = { @JoinColumn( name = "image_id" ) },
127
 
//      inverseJoinColumns = @JoinColumn( name = "user_group_id" )
128
 
//  )
129
 
//  @Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
130
 
//  private List<Group> userGroups = new ArrayList<Group>();
131
 
  @ManyToMany()
132
 
  @JoinTable(
133
 
      name = "image_has_perms",
134
 
      joinColumns = { @JoinColumn( name = "image_id" ) },
135
 
      inverseJoinColumns = @JoinColumn( name = "user_id" )
136
 
  )
137
 
  @Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
138
 
  private List<UserInfo> permissions = new ArrayList<UserInfo>();
139
 
  @ManyToMany( cascade = CascadeType.PERSIST )
140
 
  @JoinTable(
141
 
      name = "image_has_product_codes",
142
 
      joinColumns = { @JoinColumn( name = "image_id" ) },
143
 
      inverseJoinColumns = @JoinColumn( name = "image_product_code_id" )
144
 
  )
145
 
  @Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
146
 
  private List<ProductCode> productCodes = new ArrayList<ProductCode>();
147
 
 
148
 
  public static ImageInfo deregistered() {
149
 
    ImageInfo img = new ImageInfo();
 
137
  private String          signature;
 
138
  @Column( name = "image_platform" )
 
139
  private String          platform;
 
140
  @OneToMany( cascade = CascadeType.ALL )
 
141
  @JoinTable( name = "image_has_group_auth", joinColumns = { @JoinColumn( name = "image_id" ) }, inverseJoinColumns = @JoinColumn( name = "image_auth_id" ) )
 
142
  @Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
 
143
  private Set<ImageAuthorization> userGroups   = new HashSet<ImageAuthorization>( );
 
144
  @OneToMany( cascade = CascadeType.ALL )
 
145
  @JoinTable( name = "image_has_user_auth", joinColumns = { @JoinColumn( name = "image_id" ) }, inverseJoinColumns = @JoinColumn( name = "image_auth_id" ) )
 
146
  @Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
 
147
  private Set<ImageAuthorization> permissions  = new HashSet<ImageAuthorization>( );
 
148
  @OneToMany( cascade = CascadeType.ALL )
 
149
  @JoinTable( name = "image_has_product_codes", joinColumns = { @JoinColumn( name = "image_id" ) }, inverseJoinColumns = @JoinColumn( name = "image_product_code_id" ) )
 
150
  @Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
 
151
  private Set<ProductCode>        productCodes = new HashSet<ProductCode>( );
 
152
  
 
153
  public static ImageInfo deregistered( ) {
 
154
    ImageInfo img = new ImageInfo( );
150
155
    img.setImageState( "deregistered" );
151
156
    return img;
152
157
  }
153
 
 
 
158
  
154
159
  public static ImageInfo byOwnerId( String ownerId ) {
155
 
    ImageInfo img = new ImageInfo();
 
160
    ImageInfo img = new ImageInfo( );
156
161
    img.setImageOwnerId( ownerId );
157
162
    return img;
158
163
  }
159
 
 
160
 
  public ImageInfo() {}
161
 
 
 
164
  
 
165
  public ImageInfo( ) {}
 
166
  
162
167
  public ImageInfo( final String imageId ) {
163
 
    this.imageId = imageId.substring( 0, 4 ).toLowerCase( ) + imageId.substring( 4 ).toUpperCase();
 
168
    this.imageId = imageId.substring( 0, 4 ).toLowerCase( ) + imageId.substring( 4 ).toUpperCase( );
164
169
  }
165
 
 
 
170
  
166
171
  public ImageInfo( final String imageLocation, final String imageOwnerId, final String imageState, final Boolean aPublic ) {
167
172
    this.imageLocation = imageLocation;
168
173
    this.imageOwnerId = imageOwnerId;
169
174
    this.imageState = imageState;
170
175
    this.imagePublic = aPublic;
171
176
  }
172
 
 
173
 
  public ImageInfo( String architecture, String imageId, String imageLocation, String imageOwnerId, String imageState, String imageType, Boolean aPublic, String kernelId, String ramdiskId ) {
 
177
  
 
178
  public ImageInfo( String architecture, String imageId, String imageLocation, String imageOwnerId, String imageState, String imageType, Boolean aPublic,
 
179
                    String kernelId, String ramdiskId ) {
174
180
    this.architecture = architecture;
175
181
    this.imageId = imageId;
176
182
    this.imageLocation = imageLocation;
181
187
    this.kernelId = kernelId;
182
188
    this.ramdiskId = ramdiskId;
183
189
  }
184
 
 
185
 
  public Long getId() {
 
190
  
 
191
  public Long getId( ) {
186
192
    return this.id;
187
193
  }
188
 
 
 
194
  
189
195
  /**
190
196
   * @see com.eucalyptus.images.Image#getArchitecture()
191
197
   * @return
192
198
   */
193
 
  public String getArchitecture() {
 
199
  public String getArchitecture( ) {
194
200
    return architecture;
195
201
  }
196
 
 
 
202
  
197
203
  /**
198
204
   * @see com.eucalyptus.images.Image#setArchitecture(java.lang.String)
199
205
   * @param architecture
201
207
  public void setArchitecture( String architecture ) {
202
208
    this.architecture = architecture;
203
209
  }
204
 
 
 
210
  
205
211
  /**
206
212
   * @see com.eucalyptus.images.Image#getImageId()
207
213
   * @return
208
214
   */
209
 
  public String getImageId() {
 
215
  public String getImageId( ) {
210
216
    return imageId;
211
217
  }
212
 
 
 
218
  
213
219
  /**
214
220
   * @see com.eucalyptus.images.Image#setImageId(java.lang.String)
215
221
   * @param imageId
217
223
  public void setImageId( String imageId ) {
218
224
    this.imageId = imageId;
219
225
  }
220
 
 
 
226
  
221
227
  /**
222
228
   * @see com.eucalyptus.images.Image#getImageLocation()
223
229
   * @return
224
230
   */
225
 
  public String getImageLocation() {
 
231
  public String getImageLocation( ) {
226
232
    return imageLocation;
227
233
  }
228
 
 
 
234
  
229
235
  /**
230
236
   * @see com.eucalyptus.images.Image#setImageLocation(java.lang.String)
231
237
   * @param imageLocation
233
239
  public void setImageLocation( String imageLocation ) {
234
240
    this.imageLocation = imageLocation;
235
241
  }
236
 
 
 
242
  
237
243
  /**
238
244
   * @see com.eucalyptus.images.Image#getImageOwnerId()
239
245
   * @return
240
246
   */
241
 
  public String getImageOwnerId() {
 
247
  public String getImageOwnerId( ) {
242
248
    return imageOwnerId;
243
249
  }
244
 
 
 
250
  
245
251
  /**
246
252
   * @see com.eucalyptus.images.Image#setImageOwnerId(java.lang.String)
247
253
   * @param imageOwnerId
249
255
  public void setImageOwnerId( String imageOwnerId ) {
250
256
    this.imageOwnerId = imageOwnerId;
251
257
  }
252
 
 
 
258
  
253
259
  /**
254
260
   * @see com.eucalyptus.images.Image#getImageState()
255
261
   * @return
256
262
   */
257
 
  public String getImageState() {
 
263
  public String getImageState( ) {
258
264
    return imageState;
259
265
  }
260
 
 
 
266
  
261
267
  /**
262
268
   * @see com.eucalyptus.images.Image#setImageState(java.lang.String)
263
269
   * @param imageState
265
271
  public void setImageState( String imageState ) {
266
272
    this.imageState = imageState;
267
273
  }
268
 
 
 
274
  
269
275
  /**
270
276
   * @see com.eucalyptus.images.Image#getImageType()
271
277
   * @return
272
278
   */
273
 
  public String getImageType() {
 
279
  public String getImageType( ) {
274
280
    return imageType;
275
281
  }
276
 
 
 
282
  
277
283
  /**
278
284
   * @see com.eucalyptus.images.Image#setImageType(java.lang.String)
279
285
   * @param imageType
281
287
  public void setImageType( String imageType ) {
282
288
    this.imageType = imageType;
283
289
  }
284
 
 
 
290
  
285
291
  /**
286
292
   * @see com.eucalyptus.images.Image#getPublic()
287
293
   * @return
288
294
   */
289
 
  public Boolean getImagePublic() {
 
295
  public Boolean getImagePublic( ) {
290
296
    return imagePublic;
291
297
  }
292
 
 
 
298
  
293
299
  /**
294
300
   * @see com.eucalyptus.images.Image#setPublic(java.lang.Boolean)
295
301
   * @param aPublic
297
303
  public void setImagePublic( Boolean aPublic ) {
298
304
    imagePublic = aPublic;
299
305
  }
300
 
 
 
306
  
301
307
  /**
302
308
   * @see com.eucalyptus.images.Image#getKernelId()
303
309
   * @return
304
310
   */
305
 
  public String getKernelId() {
 
311
  public String getKernelId( ) {
306
312
    return kernelId;
307
313
  }
308
 
 
 
314
  
309
315
  /**
310
316
   * @see com.eucalyptus.images.Image#setKernelId(java.lang.String)
311
317
   * @param kernelId
313
319
  public void setKernelId( String kernelId ) {
314
320
    this.kernelId = kernelId;
315
321
  }
316
 
 
 
322
  
317
323
  /**
318
324
   * @see com.eucalyptus.images.Image#getRamdiskId()
319
325
   * @return
320
326
   */
321
 
  public String getRamdiskId() {
 
327
  public String getRamdiskId( ) {
322
328
    return ramdiskId;
323
329
  }
324
 
 
 
330
  
325
331
  /**
326
332
   * @see com.eucalyptus.images.Image#setRamdiskId(java.lang.String)
327
333
   * @param ramdiskId
329
335
  public void setRamdiskId( String ramdiskId ) {
330
336
    this.ramdiskId = ramdiskId;
331
337
  }
332
 
 
 
338
  
333
339
  /**
334
340
   * @see com.eucalyptus.images.Image#getSignature()
335
341
   * @return
336
342
   */
337
 
  public String getSignature() {
 
343
  public String getSignature( ) {
338
344
    return signature;
339
345
  }
340
 
 
 
346
  
341
347
  /**
342
348
   * @see com.eucalyptus.images.Image#setSignature(java.lang.String)
343
349
   * @param signature
345
351
  public void setSignature( final String signature ) {
346
352
    this.signature = signature;
347
353
  }
348
 
 
349
 
//  public List<UserGroupEntity> getUserGroups() {
350
 
//    return userGroups;
351
 
//  }
352
 
//
353
 
//  public void setUserGroups( final List<UserGroupEntity> userGroups ) {
354
 
//    this.userGroups = userGroups;
355
 
//  }
356
 
 
357
 
  public List<UserInfo> getPermissions() {
 
354
  
 
355
  public Set<ImageAuthorization> getUserGroups( ) {
 
356
    return userGroups;
 
357
  }
 
358
  
 
359
  public void setUserGroups( final Set<ImageAuthorization> userGroups ) {
 
360
    this.userGroups = userGroups;
 
361
  }
 
362
  
 
363
  public Set<ImageAuthorization> getPermissions( ) {
358
364
    return permissions;
359
365
  }
360
 
 
361
 
  public void setPermissions( final List<UserInfo> permissions ) {
 
366
  
 
367
  public void setPermissions( final Set<ImageAuthorization> permissions ) {
362
368
    this.permissions = permissions;
363
369
  }
364
370
 
 
371
  public String getPlatform( ) {
 
372
    return this.platform;
 
373
  }
 
374
  
 
375
  public void setPlatform( String platform ) {
 
376
    this.platform = platform;
 
377
  }
 
378
  
 
379
 
 
380
  public ImageInfo grantPermission( final Principal prin ) {
 
381
    try {
 
382
      ImageInfo search = new ImageInfo( );
 
383
      search.setImageId( this.imageId );
 
384
      Transactions.one( search, new JoinTx<ImageInfo>( ) {
 
385
        @Override
 
386
        public void fire( EntityWrapper<ImageInfo> db, ImageInfo t ) throws Throwable {
 
387
          ImageAuthorization imgAuth = new ImageAuthorization( prin.getName( ) );
 
388
          if( prin instanceof Group ) {
 
389
            if ( !t.getUserGroups( ).contains( imgAuth ) ) {
 
390
              db.recast( ImageAuthorization.class ).add( imgAuth );
 
391
              t.getUserGroups( ).add( imgAuth );
 
392
            }
 
393
          } else if( prin instanceof User ) {
 
394
            if ( !t.getPermissions( ).contains( imgAuth ) ) {
 
395
              db.recast( ImageAuthorization.class ).add( imgAuth );
 
396
              t.getPermissions( ).add( imgAuth );
 
397
            }
 
398
          }
 
399
          if ( t.getUserGroups( ).contains( new ImageAuthorization( "all" ) ) ) {
 
400
            t.setImagePublic( true );
 
401
          }
 
402
        }
 
403
      } );
 
404
    } catch ( TransactionException e ) {
 
405
      LOG.debug( e, e );
 
406
    }
 
407
    return this;
 
408
  }
 
409
  
 
410
  public boolean checkPermission( final Principal prin ) throws EucalyptusCloudException {
 
411
    final boolean[] result = { false };
 
412
    try {
 
413
      ImageInfo search = new ImageInfo( );
 
414
      search.setImageId( this.imageId );
 
415
      Transactions.one( search, new Tx<ImageInfo>( ) {
 
416
        @Override
 
417
        public void fire( ImageInfo t ) throws Throwable {
 
418
          if( prin instanceof Group ) {
 
419
            result[0] = t.getUserGroups( ).contains( new ImageAuthorization( prin.getName( ) ) );
 
420
          } else if ( prin instanceof User ) {
 
421
            result[0] = t.getPermissions( ).contains( new ImageAuthorization( prin.getName( ) ) );
 
422
          }
 
423
        }
 
424
      } );
 
425
    } catch ( TransactionException e ) {
 
426
      return false;
 
427
    }
 
428
    return result[0];
 
429
  }
 
430
  
 
431
  public ImageInfo revokePermission( final Principal prin ) {
 
432
    try {
 
433
      ImageInfo search = new ImageInfo( );
 
434
      search.setImageId( this.imageId );
 
435
      Transactions.one( search, new Tx<ImageInfo>( ) {
 
436
        @Override
 
437
        public void fire( ImageInfo t ) throws Throwable {
 
438
          ImageAuthorization imgAuth = new ImageAuthorization( prin.getName( ) );
 
439
          if( prin instanceof Group ) {
 
440
            t.getUserGroups( ).remove( imgAuth );
 
441
          } else if( prin instanceof User ) {
 
442
            t.getPermissions( ).remove( imgAuth );
 
443
          }
 
444
          if ( !t.getPermissions( ).contains( new ImageAuthorization( "all" ) ) ) {
 
445
            t.setImagePublic( false );
 
446
          }
 
447
        }
 
448
      } );
 
449
    } catch ( TransactionException e ) {
 
450
      LOG.debug( e, e );
 
451
    }
 
452
    return this;
 
453
  }
 
454
  
365
455
  /**
366
456
   * @see com.eucalyptus.images.Image#getAsImageDetails()
367
457
   * @return
368
458
   */
369
 
  public ImageDetails getAsImageDetails() {
370
 
    ImageDetails i = new ImageDetails();
371
 
    i.setArchitecture( this.getArchitecture() );
372
 
    i.setImageId( this.getImageId() );
373
 
    i.setImageLocation( this.getImageLocation() );
374
 
    i.setImageOwnerId( this.getImageOwnerId() );
375
 
    i.setImageState( this.getImageState() );
376
 
    i.setImageType( this.getImageType() );
377
 
    i.setIsPublic( this.getImagePublic() );
378
 
    i.setKernelId( this.getKernelId() );
379
 
    i.setRamdiskId( this.getRamdiskId() );
 
459
  public ImageDetails getAsImageDetails( ) {
 
460
    ImageDetails i = new ImageDetails( );
 
461
    i.setArchitecture( this.getArchitecture( ) );
 
462
    i.setImageId( this.getImageId( ) );
 
463
    i.setImageLocation( this.getImageLocation( ) );
 
464
    i.setImageOwnerId( this.getImageOwnerId( ) );
 
465
    i.setImageState( this.getImageState( ) );
 
466
    i.setImageType( this.getImageType( ) );
 
467
    i.setIsPublic( this.getImagePublic( ) );
 
468
    i.setKernelId( this.getKernelId( ) );
 
469
    i.setRamdiskId( this.getRamdiskId( ) );
 
470
    i.setPlatform( this.getPlatform( ) );
380
471
    return i;
381
472
  }
382
 
 
 
473
  
383
474
  /**
384
475
   * @see com.eucalyptus.images.Image#getProductCodes()
385
476
   * @return
386
477
   */
387
 
  public List<ProductCode> getProductCodes() {
 
478
  public Set<ProductCode> getProductCodes( ) {
388
479
    return productCodes;
389
480
  }
390
 
 
 
481
  
391
482
  /**
392
483
   * @see com.eucalyptus.images.Image#setProductCodes(java.util.List)
393
484
   * @param productCodes
394
485
   */
395
 
  public void setProductCodes( final List<ProductCode> productCodes ) {
 
486
  public void setProductCodes( final Set<ProductCode> productCodes ) {
396
487
    this.productCodes = productCodes;
397
488
  }
398
 
 
 
489
  
399
490
  /**
400
491
   * @see com.eucalyptus.images.Image#equals(java.lang.Object)
401
492
   * @param o
404
495
  @Override
405
496
  public boolean equals( final Object o ) {
406
497
    if ( this == o ) return true;
407
 
    if ( o == null || getClass() != o.getClass() ) return false;
408
 
 
 
498
    if ( o == null || getClass( ) != o.getClass( ) ) return false;
 
499
    
409
500
    ImageInfo imageInfo = ( ImageInfo ) o;
410
 
 
 
501
    
411
502
    if ( !imageId.equals( imageInfo.imageId ) ) return false;
412
 
 
 
503
    
413
504
    return true;
414
505
  }
415
 
 
 
506
  
416
507
  /**
417
508
   * @see com.eucalyptus.images.Image#hashCode()
418
509
   * @return
419
510
   */
420
511
  @Override
421
 
  public int hashCode() {
422
 
    return imageId.hashCode();
 
512
  public int hashCode( ) {
 
513
    return imageId.hashCode( );
423
514
  }
424
 
 
 
515
  
425
516
  /**
426
517
   * @see com.eucalyptus.images.Image#isAllowed(com.eucalyptus.auth.UserInfo)
427
518
   * @param user
429
520
   */
430
521
  public boolean isAllowed( UserInfo user ) {
431
522
    try {
432
 
      if ( Users.lookupUser( user.getUserName( ) ).isAdministrator() || user.getUserName().equals( this.getImageOwnerId() ) )
433
 
        return true;
 
523
      if ( Users.lookupUser( user.getUserName( ) ).isAdministrator( ) || user.getUserName( ).equals( this.getImageOwnerId( ) ) ) return true;
434
524
    } catch ( NoSuchUserException e ) {
435
525
      return false;
436
526
    }
437
 
//    for ( UserGroupEntity g : this.getUserGroups() )
438
 
//      if ( "all".equals( g.getName() ) )
439
 
        return true;
440
 
//    return this.getPermissions().contains( user );
 
527
    //    for ( UserGroupEntity g : this.getUserGroups() )
 
528
    //      if ( "all".equals( g.getName() ) )
 
529
    return true;
 
530
    //    return this.getPermissions().contains( user );
441
531
  }
442
 
 
 
532
  
443
533
  public static ImageInfo named( String imageId ) throws EucalyptusCloudException {
444
 
    EntityWrapper<ImageInfo> db = new EntityWrapper<ImageInfo>();
 
534
    EntityWrapper<ImageInfo> db = new EntityWrapper<ImageInfo>( );
445
535
    ImageInfo image = null;
446
536
    try {
447
537
      image = db.getUnique( new ImageInfo( imageId ) );
448
538
      db.commit( );
449
 
    } catch (Throwable t) {
450
 
      db.commit();
 
539
    } catch ( Throwable t ) {
 
540
      db.commit( );
451
541
    }
452
542
    return image;
453
543
  }
454
 
 
 
544
  
455
545
  /**
456
546
   * @see com.eucalyptus.images.Image#toString()
457
547
   * @return
458
548
   */
459
549
  @Override
460
 
  public String toString() {
 
550
  public String toString( ) {
461
551
    return this.imageId;
462
552
  }
 
553
  
463
554
  public static ImageInfoToDetails TO_IMAGE_DETAILS = new ImageInfoToDetails( );
464
 
  static class ImageInfoToDetails implements Function<ImageInfo,ImageDetails> {
465
 
    @Override public ImageDetails apply( ImageInfo arg0 ) {
 
555
  
 
556
  static class ImageInfoToDetails implements Function<ImageInfo, ImageDetails> {
 
557
    @Override
 
558
    public ImageDetails apply( ImageInfo arg0 ) {
466
559
      return arg0.getAsImageDetails( );
467
 
    }    
 
560
    }
468
561
  }
469
562
}