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      type = TYPE_MEDIAPACKAGE;
67      if (mediaPackage.getIdentifier() != null)
68        identifier = mediaPackage.getIdentifier().toString();
69      else
70        identifier = SELF;
71      properties = new HashMap<String, String>();
72    }
73  
74    /**
75     * Creates a reference to the specified media package element.
76     * <p>
77     * Note that the referenced element must already be part of the media package, otherwise a
78     * <code>MediaPackageException</code> will be thrown as the object holding this reference is added to the media
79     * package.
80     *
81     * @param mediaPackageElement
82     *          the media package element to refer to
83     */
84    public MediaPackageReferenceImpl(MediaPackageElement mediaPackageElement) {
85      if (mediaPackageElement == null)
86        throw new IllegalArgumentException("Parameter media package element must not be null");
87      this.type = mediaPackageElement.getElementType().toString().toLowerCase();
88      this.identifier = mediaPackageElement.getIdentifier();
89      if (identifier == null)
90        throw new IllegalArgumentException("Media package element must have an identifier");
91      this.properties = new HashMap<String, String>();
92    }
93  
94    /**
95     * Creates a reference to the entity identified by <code>type</code> and <code>identifier</code>.
96     *
97     * @param type
98     *          the reference type
99     * @param identifier
100    *          the reference identifier
101    */
102   public MediaPackageReferenceImpl(String type, String identifier) {
103     if (type == null)
104       throw new IllegalArgumentException("Parameter type must not be null");
105     if (identifier == null)
106       throw new IllegalArgumentException("Parameter identifier must not be null");
107     this.type = type;
108     this.identifier = identifier;
109     this.properties = new HashMap<String, String>();
110   }
111 
112   /**
113    * Returns a media package reference from the given string.
114    *
115    * @return the media package reference
116    * @throws IllegalArgumentException
117    *           if the string is malformed
118    */
119   public static MediaPackageReference fromString(String reference) throws IllegalArgumentException {
120     if (reference == null)
121       throw new IllegalArgumentException("Reference is null");
122 
123     MediaPackageReference ref = null;
124 
125     String[] parts = reference.split(";");
126     String elementReference = parts[0];
127 
128     // Check for special reference
129     if ("self".equals(elementReference))
130       ref = new MediaPackageReferenceImpl(MediaPackageReference.TYPE_MEDIAPACKAGE, "self");
131     else {
132       String[] elementReferenceParts = elementReference.split(":");
133       if (elementReferenceParts.length != 2)
134         throw new IllegalArgumentException("Reference " + reference + " is malformed");
135       ref = new MediaPackageReferenceImpl(elementReferenceParts[0], elementReferenceParts[1]);
136     }
137 
138     // Process the reference properties
139     for (int i = 1; i < parts.length; i++) {
140       String[] propertyParts = parts[i].split("=");
141       if (propertyParts.length != 2)
142         throw new IllegalStateException("malformatted reference properties");
143       String key = propertyParts[0];
144       String value = propertyParts[1];
145       ref.setProperty(key, value);
146     }
147 
148     return ref;
149   }
150 
151   /**
152    * @see org.opencastproject.mediapackage.MediaPackageReference#getIdentifier()
153    */
154   public String getIdentifier() {
155     return identifier;
156   }
157 
158   /**
159    * @see org.opencastproject.mediapackage.MediaPackageReference#getType()
160    */
161   public String getType() {
162     return type;
163   }
164 
165   /**
166    * @return the properties
167    */
168   public Map<String, String> getProperties() {
169     return properties;
170   }
171 
172   /**
173    * @param properties
174    *          the properties to set
175    */
176   public void setProperties(Map<String, String> properties) {
177     this.properties = properties;
178   }
179 
180   /**
181    * {@inheritDoc}
182    *
183    * @see org.opencastproject.mediapackage.MediaPackageReference#getProperty(java.lang.String)
184    */
185   @Override
186   public String getProperty(String key) {
187     return properties.get(key);
188   }
189 
190   /**
191    * {@inheritDoc}
192    *
193    * @see org.opencastproject.mediapackage.MediaPackageReference#setProperty(java.lang.String, java.lang.String)
194    */
195   @Override
196   public void setProperty(String key, String value) {
197     if (value == null)
198       this.properties.remove(key);
199     this.properties.put(key, value);
200   }
201 
202   /**
203    * @see org.opencastproject.mediapackage.MediaPackageReference#matches(org.opencastproject.mediapackage.MediaPackageReference)
204    */
205   public boolean matches(MediaPackageReference reference) {
206     if (reference == null)
207       return false;
208 
209     // type
210     if (!type.equals(reference.getType()))
211       return false;
212 
213     // properties
214     if (properties != null && !properties.equals(reference.getProperties()))
215       return false;
216     else if (reference.getProperties() != null && !reference.getProperties().equals(properties))
217       return false;
218 
219     // identifier
220     if (identifier.equals(reference.getIdentifier()))
221       return true;
222     else if (ANY.equals(identifier) || ANY.equals(reference.getIdentifier()))
223       return true;
224     else if (SELF.equals(identifier) || SELF.equals(reference.getIdentifier()))
225       return true;
226 
227     return false;
228   }
229 
230   /**
231    * {@inheritDoc}
232    *
233    * @see java.lang.Object#clone()
234    */
235   @Override
236   public Object clone() {
237     MediaPackageReferenceImpl clone = new MediaPackageReferenceImpl(type, identifier);
238     clone.getProperties().putAll(properties);
239     return clone;
240   }
241 
242   /**
243    * @see java.lang.Object#hashCode()
244    */
245   @Override
246   public int hashCode() {
247     return toString().hashCode();
248   }
249 
250   /**
251    * @see java.lang.Object#equals(java.lang.Object)
252    */
253   @Override
254   public boolean equals(Object obj) {
255     if (obj == null || !(obj instanceof MediaPackageReference))
256       return false;
257     MediaPackageReference ref = (MediaPackageReference) obj;
258     return type.equals(ref.getType()) && identifier.equals(ref.getIdentifier());
259   }
260 
261   /**
262    * @see java.lang.Object#toString()
263    */
264   @Override
265   public String toString() {
266     if (externalForm == null) {
267       StringBuffer buf = new StringBuffer();
268       if (TYPE_MEDIAPACKAGE.equals(type) && SELF.equals(identifier)) {
269         buf.append("self");
270       } else {
271         buf.append(type);
272         buf.append(":");
273         buf.append(identifier);
274       }
275       if (properties.size() > 0) {
276         for (Map.Entry<String, String> entry : properties.entrySet()) {
277           buf.append(";");
278           buf.append(entry.getKey());
279           buf.append("=");
280           buf.append(entry.getValue());
281         }
282       }
283       externalForm = buf.toString();
284     }
285     return externalForm;
286   }
287 
288   public static class Adapter extends XmlAdapter<String, MediaPackageReference> {
289     @Override
290     public String marshal(MediaPackageReference ref) throws Exception {
291       if (ref == null)
292         return null;
293       return ref.toString();
294     }
295 
296     @Override
297     public MediaPackageReference unmarshal(String ref) throws Exception {
298       if (ref == null)
299         return null;
300       return MediaPackageReferenceImpl.fromString(ref);
301     }
302   }
303 }