View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  
23  package org.opencastproject.mediapackage;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.xml.bind.annotation.adapters.XmlAdapter;
29  
30  /**
31   * Default implementation for a {@link MediaPackageReference}.
32   */
33  public class MediaPackageReferenceImpl implements MediaPackageReference {
34  
35    /** Convenience reference that matches any media package */
36    public static final MediaPackageReference ANY_MEDIAPACKAGE = new MediaPackageReferenceImpl(TYPE_MEDIAPACKAGE, ANY);
37  
38    /** The reference identifier */
39    protected String identifier = null;
40  
41    /** The reference type */
42    protected String type = null;
43  
44    /** External representation */
45    private String externalForm = null;
46  
47    /** The properties that describe this reference */
48    private Map<String, String> properties = null;
49  
50    /**
51     * Creates a reference to the containing media package (<code>self</code>).
52     */
53    public MediaPackageReferenceImpl() {
54      this(TYPE_MEDIAPACKAGE, SELF);
55    }
56  
57    /**
58     * Creates a reference to the specified media package.
59     *
60     * @param mediaPackage
61     *          the media package to refer to
62     */
63    public MediaPackageReferenceImpl(MediaPackage mediaPackage) {
64      if (mediaPackage == null) {
65        throw new IllegalArgumentException("Parameter media package must not be null");
66      }
67      type = TYPE_MEDIAPACKAGE;
68      if (mediaPackage.getIdentifier() != null) {
69        identifier = mediaPackage.getIdentifier().toString();
70      } else {
71        identifier = SELF;
72      }
73      properties = new HashMap<String, String>();
74    }
75  
76    /**
77     * Creates a reference to the specified media package element.
78     * <p>
79     * Note that the referenced element must already be part of the media package, otherwise a
80     * <code>MediaPackageException</code> will be thrown as the object holding this reference is added to the media
81     * package.
82     *
83     * @param mediaPackageElement
84     *          the media package element to refer to
85     */
86    public MediaPackageReferenceImpl(MediaPackageElement mediaPackageElement) {
87      if (mediaPackageElement == null) {
88        throw new IllegalArgumentException("Parameter media package element must not be null");
89      }
90      this.type = mediaPackageElement.getElementType().toString().toLowerCase();
91      this.identifier = mediaPackageElement.getIdentifier();
92      if (identifier == null) {
93        throw new IllegalArgumentException("Media package element must have an identifier");
94      }
95      this.properties = new HashMap<String, String>();
96    }
97  
98    /**
99     * Creates a reference to the entity identified by <code>type</code> and <code>identifier</code>.
100    *
101    * @param type
102    *          the reference type
103    * @param identifier
104    *          the reference identifier
105    */
106   public MediaPackageReferenceImpl(String type, String identifier) {
107     if (type == null) {
108       throw new IllegalArgumentException("Parameter type must not be null");
109     }
110     if (identifier == null) {
111       throw new IllegalArgumentException("Parameter identifier must not be null");
112     }
113     this.type = type;
114     this.identifier = identifier;
115     this.properties = new HashMap<String, String>();
116   }
117 
118   /**
119    * Returns a media package reference from the given string.
120    *
121    * @return the media package reference
122    * @throws IllegalArgumentException
123    *           if the string is malformed
124    */
125   public static MediaPackageReference fromString(String reference) throws IllegalArgumentException {
126     if (reference == null) {
127       throw new IllegalArgumentException("Reference is null");
128     }
129 
130     MediaPackageReference ref = null;
131 
132     String[] parts = reference.split(";");
133     String elementReference = parts[0];
134 
135     // Check for special reference
136     if ("self".equals(elementReference)) {
137       ref = new MediaPackageReferenceImpl(MediaPackageReference.TYPE_MEDIAPACKAGE, "self");
138     } else {
139       String[] elementReferenceParts = elementReference.split(":");
140       if (elementReferenceParts.length != 2) {
141         throw new IllegalArgumentException("Reference " + reference + " is malformed");
142       }
143       ref = new MediaPackageReferenceImpl(elementReferenceParts[0], elementReferenceParts[1]);
144     }
145 
146     // Process the reference properties
147     for (int i = 1; i < parts.length; i++) {
148       String[] propertyParts = parts[i].split("=");
149       if (propertyParts.length != 2) {
150         throw new IllegalStateException("malformatted reference properties");
151       }
152       String key = propertyParts[0];
153       String value = propertyParts[1];
154       ref.setProperty(key, value);
155     }
156 
157     return ref;
158   }
159 
160   /**
161    * @see org.opencastproject.mediapackage.MediaPackageReference#getIdentifier()
162    */
163   public String getIdentifier() {
164     return identifier;
165   }
166 
167   /**
168    * @see org.opencastproject.mediapackage.MediaPackageReference#getType()
169    */
170   public String getType() {
171     return type;
172   }
173 
174   /**
175    * @return the properties
176    */
177   public Map<String, String> getProperties() {
178     return properties;
179   }
180 
181   /**
182    * @param properties
183    *          the properties to set
184    */
185   public void setProperties(Map<String, String> properties) {
186     this.properties = properties;
187   }
188 
189   /**
190    * {@inheritDoc}
191    *
192    * @see org.opencastproject.mediapackage.MediaPackageReference#getProperty(java.lang.String)
193    */
194   @Override
195   public String getProperty(String key) {
196     return properties.get(key);
197   }
198 
199   /**
200    * {@inheritDoc}
201    *
202    * @see org.opencastproject.mediapackage.MediaPackageReference#setProperty(java.lang.String, java.lang.String)
203    */
204   @Override
205   public void setProperty(String key, String value) {
206     if (value == null) {
207       this.properties.remove(key);
208     }
209     this.properties.put(key, value);
210   }
211 
212   /**
213    * @see org.opencastproject.mediapackage.MediaPackageReference#matches(
214    *      org.opencastproject.mediapackage.MediaPackageReference)
215    */
216   public boolean matches(MediaPackageReference reference) {
217     if (reference == null) {
218       return false;
219     }
220 
221     // type
222     if (!type.equals(reference.getType())) {
223       return false;
224     }
225 
226     // properties
227     if (properties != null && !properties.equals(reference.getProperties())) {
228       return false;
229     } else if (reference.getProperties() != null && !reference.getProperties().equals(properties)) {
230       return false;
231     }
232 
233     // identifier
234     if (identifier.equals(reference.getIdentifier())) {
235       return true;
236     } else if (ANY.equals(identifier) || ANY.equals(reference.getIdentifier())) {
237       return true;
238     } else if (SELF.equals(identifier) || SELF.equals(reference.getIdentifier())) {
239       return true;
240     }
241 
242     return false;
243   }
244 
245   /**
246    * {@inheritDoc}
247    *
248    * @see java.lang.Object#clone()
249    */
250   @Override
251   public Object clone() {
252     MediaPackageReferenceImpl clone = new MediaPackageReferenceImpl(type, identifier);
253     clone.getProperties().putAll(properties);
254     return clone;
255   }
256 
257   /**
258    * @see java.lang.Object#hashCode()
259    */
260   @Override
261   public int hashCode() {
262     return toString().hashCode();
263   }
264 
265   /**
266    * @see java.lang.Object#equals(java.lang.Object)
267    */
268   @Override
269   public boolean equals(Object obj) {
270     if (obj == null || !(obj instanceof MediaPackageReference)) {
271       return false;
272     }
273     MediaPackageReference ref = (MediaPackageReference) obj;
274     return type.equals(ref.getType()) && identifier.equals(ref.getIdentifier());
275   }
276 
277   /**
278    * @see java.lang.Object#toString()
279    */
280   @Override
281   public String toString() {
282     if (externalForm == null) {
283       StringBuffer buf = new StringBuffer();
284       if (TYPE_MEDIAPACKAGE.equals(type) && SELF.equals(identifier)) {
285         buf.append("self");
286       } else {
287         buf.append(type);
288         buf.append(":");
289         buf.append(identifier);
290       }
291       if (properties.size() > 0) {
292         for (Map.Entry<String, String> entry : properties.entrySet()) {
293           buf.append(";");
294           buf.append(entry.getKey());
295           buf.append("=");
296           buf.append(entry.getValue());
297         }
298       }
299       externalForm = buf.toString();
300     }
301     return externalForm;
302   }
303 
304   public static class Adapter extends XmlAdapter<String, MediaPackageReference> {
305     @Override
306     public String marshal(MediaPackageReference ref) throws Exception {
307       if (ref == null) {
308         return null;
309       }
310       return ref.toString();
311     }
312 
313     @Override
314     public MediaPackageReference unmarshal(String ref) throws Exception {
315       if (ref == null) {
316         return null;
317       }
318       return MediaPackageReferenceImpl.fromString(ref);
319     }
320   }
321 }