~ccheney/ubuntu/lucid/eucalyptus/lucid-sru

« back to all changes in this revision

Viewing changes to clc/modules/core/src/edu/ucsb/eucalyptus/cloud/entities/ImageInfo.java

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2009-02-11 02:45:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090211024539-0jhzbpg3hk6nu1yg
Tags: upstream-1.5~bzr139
ImportĀ upstreamĀ versionĀ 1.5~bzr139

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Software License Agreement (BSD License)
 
3
 *
 
4
 * Copyright (c) 2008, Regents of the University of California
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use of this software in source and binary forms, with or
 
8
 * without modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * * Redistributions of source code must retain the above
 
12
 *   copyright notice, this list of conditions and the
 
13
 *   following disclaimer.
 
14
 *
 
15
 * * Redistributions in binary form must reproduce the above
 
16
 *   copyright notice, this list of conditions and the
 
17
 *   following disclaimer in the documentation and/or other
 
18
 *   materials provided with the distribution.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
21
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
23
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
24
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
30
 * POSSIBILITY OF SUCH DAMAGE.
 
31
 *
 
32
 * Author: Chris Grzegorczyk grze@cs.ucsb.edu
 
33
 */
 
34
 
 
35
package edu.ucsb.eucalyptus.cloud.entities;
 
36
 
 
37
import edu.ucsb.eucalyptus.cloud.EucalyptusCloudException;
 
38
import edu.ucsb.eucalyptus.msgs.CacheImageType;
 
39
import edu.ucsb.eucalyptus.msgs.CheckImageType;
 
40
import edu.ucsb.eucalyptus.msgs.FlushCachedImageType;
 
41
import edu.ucsb.eucalyptus.msgs.ImageDetails;
 
42
import edu.ucsb.eucalyptus.util.Messaging;
 
43
import edu.ucsb.eucalyptus.util.WalrusProperties;
 
44
import org.hibernate.annotations.Cache;
 
45
import org.hibernate.annotations.CacheConcurrencyStrategy;
 
46
 
 
47
import javax.persistence.*;
 
48
import java.util.ArrayList;
 
49
import java.util.List;
 
50
 
 
51
@Entity
 
52
@Table( name = "Images" )
 
53
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
 
54
public class ImageInfo {
 
55
 
 
56
  @Id
 
57
  @GeneratedValue
 
58
  @Column( name = "image_id" )
 
59
  private Long id = -1l;
 
60
  @Column( name = "image_name" )
 
61
  private String imageId;
 
62
  @Column( name = "image_path" )
 
63
  private String imageLocation;
 
64
  @Column( name = "image_availability" )
 
65
  private String imageState;
 
66
  @Column( name = "image_owner_id" )
 
67
  private String imageOwnerId;
 
68
  @Column( name = "image_arch" )
 
69
  private String architecture;
 
70
  @Column( name = "image_type" )
 
71
  private String imageType;
 
72
  @Column( name = "image_kernel_id" )
 
73
  private String kernelId;
 
74
  @Column( name = "image_ramdisk_id" )
 
75
  private String ramdiskId;
 
76
  @Column( name = "image_is_public" )
 
77
  private Boolean isPublic;
 
78
  @Lob
 
79
  @Column( name = "image_signature" )
 
80
  private String signature;
 
81
  @ManyToMany( cascade = CascadeType.PERSIST )
 
82
  @JoinTable(
 
83
      name = "image_has_groups",
 
84
      joinColumns = { @JoinColumn( name = "image_id" ) },
 
85
      inverseJoinColumns = @JoinColumn( name = "user_group_id" )
 
86
  )
 
87
  @Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
 
88
  private List<UserGroupInfo> userGroups = new ArrayList<UserGroupInfo>();
 
89
  @ManyToMany()
 
90
  @JoinTable(
 
91
      name = "image_has_perms",
 
92
      joinColumns = { @JoinColumn( name = "image_id" ) },
 
93
      inverseJoinColumns = @JoinColumn( name = "user_id" )
 
94
  )
 
95
  @Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
 
96
  private List<UserInfo> permissions = new ArrayList<UserInfo>();
 
97
  @ManyToMany()
 
98
  @JoinTable(
 
99
      name = "image_has_product_codes",
 
100
      joinColumns = { @JoinColumn( name = "image_id" ) },
 
101
      inverseJoinColumns = @JoinColumn( name = "image_product_code_id" )
 
102
  )
 
103
  @Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
 
104
  private List<ProductCode> productCodes = new ArrayList<ProductCode>();
 
105
 
 
106
  public static ImageInfo deregistered() {
 
107
    ImageInfo img = new ImageInfo();
 
108
    img.setImageState( "deregistered" );
 
109
    return img;
 
110
  }
 
111
 
 
112
  public static ImageInfo byOwnerId( String ownerId ) {
 
113
    ImageInfo img = new ImageInfo();
 
114
    img.setImageOwnerId( ownerId );
 
115
    return img;
 
116
  }
 
117
 
 
118
  public ImageInfo() {}
 
119
 
 
120
  public ImageInfo( final String imageId ) {
 
121
    this.imageId = imageId;
 
122
  }
 
123
 
 
124
  public ImageInfo( final String imageLocation, final String imageOwnerId, final String imageState, final Boolean aPublic ) {
 
125
    this.imageLocation = imageLocation;
 
126
    this.imageOwnerId = imageOwnerId;
 
127
    this.imageState = imageState;
 
128
    this.isPublic = aPublic;
 
129
  }
 
130
 
 
131
  public ImageInfo( String architecture, String imageId, String imageLocation, String imageOwnerId, String imageState, String imageType, Boolean aPublic, String kernelId, String ramdiskId ) {
 
132
    this.architecture = architecture;
 
133
    this.imageId = imageId;
 
134
    this.imageLocation = imageLocation;
 
135
    this.imageOwnerId = imageOwnerId;
 
136
    this.imageState = imageState;
 
137
    this.imageType = imageType;
 
138
    this.isPublic = aPublic;
 
139
    this.kernelId = kernelId;
 
140
    this.ramdiskId = ramdiskId;
 
141
  }
 
142
 
 
143
  public Long getId() {
 
144
    return this.id;
 
145
  }
 
146
 
 
147
  public String getArchitecture() {
 
148
    return architecture;
 
149
  }
 
150
 
 
151
  public void setArchitecture( String architecture ) {
 
152
    this.architecture = architecture;
 
153
  }
 
154
 
 
155
  public String getImageId() {
 
156
    return imageId;
 
157
  }
 
158
 
 
159
  public void setImageId( String imageId ) {
 
160
    this.imageId = imageId;
 
161
  }
 
162
 
 
163
  public String getImageLocation() {
 
164
    return imageLocation;
 
165
  }
 
166
 
 
167
  public void setImageLocation( String imageLocation ) {
 
168
    this.imageLocation = imageLocation;
 
169
  }
 
170
 
 
171
  public String getImageOwnerId() {
 
172
    return imageOwnerId;
 
173
  }
 
174
 
 
175
  public void setImageOwnerId( String imageOwnerId ) {
 
176
    this.imageOwnerId = imageOwnerId;
 
177
  }
 
178
 
 
179
  public String getImageState() {
 
180
    return imageState;
 
181
  }
 
182
 
 
183
  public void setImageState( String imageState ) {
 
184
    this.imageState = imageState;
 
185
  }
 
186
 
 
187
  public String getImageType() {
 
188
    return imageType;
 
189
  }
 
190
 
 
191
  public void setImageType( String imageType ) {
 
192
    this.imageType = imageType;
 
193
  }
 
194
 
 
195
  public Boolean getPublic() {
 
196
    return isPublic;
 
197
  }
 
198
 
 
199
  public void setPublic( Boolean aPublic ) {
 
200
    isPublic = aPublic;
 
201
  }
 
202
 
 
203
  public String getKernelId() {
 
204
    return kernelId;
 
205
  }
 
206
 
 
207
  public void setKernelId( String kernelId ) {
 
208
    this.kernelId = kernelId;
 
209
  }
 
210
 
 
211
  public String getRamdiskId() {
 
212
    return ramdiskId;
 
213
  }
 
214
 
 
215
  public void setRamdiskId( String ramdiskId ) {
 
216
    this.ramdiskId = ramdiskId;
 
217
  }
 
218
 
 
219
  public String getSignature() {
 
220
    return signature;
 
221
  }
 
222
 
 
223
  public void setSignature( final String signature ) {
 
224
    this.signature = signature;
 
225
  }
 
226
 
 
227
  public List<UserGroupInfo> getUserGroups() {
 
228
    return userGroups;
 
229
  }
 
230
 
 
231
  public void setUserGroups( final List<UserGroupInfo> userGroups ) {
 
232
    this.userGroups = userGroups;
 
233
  }
 
234
 
 
235
  public List<UserInfo> getPermissions() {
 
236
    return permissions;
 
237
  }
 
238
 
 
239
  public void setPermissions( final List<UserInfo> permissions ) {
 
240
    this.permissions = permissions;
 
241
  }
 
242
 
 
243
  public ImageDetails getAsImageDetails() {
 
244
    ImageDetails i = new ImageDetails();
 
245
    i.setArchitecture( this.getArchitecture() );
 
246
    i.setImageId( this.getImageId() );
 
247
    i.setImageLocation( this.getImageLocation() );
 
248
    i.setImageOwnerId( this.getImageOwnerId() );
 
249
    i.setImageState( this.getImageState() );
 
250
    i.setImageType( this.getImageType() );
 
251
    i.setIsPublic( this.getPublic() );
 
252
    i.setKernelId( this.getKernelId() );
 
253
    i.setRamdiskId( this.getRamdiskId() );
 
254
    return i;
 
255
  }
 
256
 
 
257
  public List<ProductCode> getProductCodes() {
 
258
    return productCodes;
 
259
  }
 
260
 
 
261
  public void setProductCodes( final List<ProductCode> productCodes ) {
 
262
    this.productCodes = productCodes;
 
263
  }
 
264
 
 
265
  public void checkValid() {
 
266
    String[] parts = this.getImageLocation().split( "/" );
 
267
    CheckImageType check = new CheckImageType();
 
268
    check.setUserId( imageOwnerId );
 
269
    check.setBucket( parts[ 0 ] );
 
270
    check.setKey( parts[ 1 ] );
 
271
    Messaging.dispatch( WalrusProperties.WALRUS_REF, check );
 
272
  }
 
273
 
 
274
  public void triggerCaching() {
 
275
    String[] parts = this.getImageLocation().split( "/" );
 
276
    CacheImageType cache = new CacheImageType();
 
277
    cache.setUserId( imageOwnerId );
 
278
    cache.setBucket( parts[ 0 ] );
 
279
    cache.setKey( parts[ 1 ] );
 
280
    Messaging.dispatch( WalrusProperties.WALRUS_REF, cache );
 
281
  }
 
282
 
 
283
  public void invalidate() {
 
284
    String[] parts = this.getImageLocation().split( "/" );
 
285
    this.setImageState( "deregistered" );
 
286
    try {
 
287
      Messaging.dispatch( WalrusProperties.WALRUS_REF, new FlushCachedImageType( parts[ 0 ], parts[ 1 ] ) );
 
288
    } catch ( Exception e ) {}
 
289
  }
 
290
 
 
291
  @Override
 
292
  public boolean equals( final Object o ) {
 
293
    if ( this == o ) return true;
 
294
    if ( o == null || getClass() != o.getClass() ) return false;
 
295
 
 
296
    ImageInfo imageInfo = ( ImageInfo ) o;
 
297
 
 
298
    if ( !imageId.equals( imageInfo.imageId ) ) return false;
 
299
 
 
300
    return true;
 
301
  }
 
302
 
 
303
  @Override
 
304
  public int hashCode() {
 
305
    return imageId.hashCode();
 
306
  }
 
307
 
 
308
  public boolean isAllowed( UserInfo user ) {
 
309
    if ( user.isAdministrator() || user.getUserName().equals( this.getImageOwnerId() ) )
 
310
      return true;
 
311
    for ( UserGroupInfo g : this.getUserGroups() )
 
312
      if ( "all".equals( g.getName() ) )
 
313
        return true;
 
314
    return this.getPermissions().contains( user );
 
315
  }
 
316
 
 
317
  public static ImageInfo named( String imageId ) throws EucalyptusCloudException {
 
318
    EntityWrapper<ImageInfo> db = new EntityWrapper<ImageInfo>();
 
319
    ImageInfo image = null;
 
320
    try {
 
321
      image = db.getUnique( new ImageInfo( imageId ) );
 
322
    } finally {
 
323
      db.commit();
 
324
    }
 
325
    return image;
 
326
  }
 
327
}