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 org.opencastproject.util.ConfigurationException;
26  
27  /**
28   * Factory to retrieve instances of a media package builder. Use the static method {@link #newInstance()} to obtain a
29   * reference to a concrete implementation of a <code>MediaPackageBuilderFactory</code>. This instance can then be used
30   * to create or load media packages.
31   * <p>
32   * The factory can be configured by specifying the concrete implementation class through the system property
33   * <code>org.opencastproject.mediapackage.builder</code>.
34   * </p>
35   */
36  public final class MediaPackageBuilderFactory {
37  
38    /** Class name for the default media package builder */
39    private static final String BUILDER_CLASS = "org.opencastproject.mediapackage.MediaPackageBuilderImpl";
40  
41    /** Name of the system property */
42    public static final String PROPERTY_NAME = "org.opencastproject.mediapackage.builder";
43  
44    /** The implementation class name */
45    private static String builderClassName = BUILDER_CLASS;
46  
47    /** The singleton instance of this factory */
48    private static final MediaPackageBuilderFactory factory = new MediaPackageBuilderFactory();
49  
50    /**
51     * Private method to create a new media package builder factory.
52     */
53    private MediaPackageBuilderFactory() {
54      String className = System.getProperty(PROPERTY_NAME);
55      if (className != null) {
56        builderClassName = className;
57      }
58    }
59  
60    /**
61     * Returns an instance of a MediaPackageBuilderFactory.
62     *
63     * @return the media package builder factory
64     * @throws ConfigurationException
65     *           if the factory cannot be instantiated
66     */
67    public static MediaPackageBuilderFactory newInstance() throws ConfigurationException {
68      return factory;
69    }
70  
71    /**
72     * Factory method that returns an instance of a media package builder.
73     * <p>
74     * It uses the following ordered lookup procedure to determine which implementation of the {@link MediaPackageBuilder}
75     * interface to use:
76     * <ul>
77     * <li>Implementation specified using the <code>org.opencastproject.mediapackage.builder</code> system property</li>
78     * <li>Platform default implementation</li>
79     * </ul>
80     *
81     * @return the media package builder
82     * @throws ConfigurationException
83     *           If the builder cannot be instantiated
84     */
85    public MediaPackageBuilder newMediaPackageBuilder() throws ConfigurationException {
86      MediaPackageBuilder builder = null;
87      try {
88        Class<?> builderClass = Class.forName(builderClassName);
89        builder = (MediaPackageBuilder) builderClass.newInstance();
90      } catch (ClassNotFoundException e) {
91        throw new ConfigurationException("Class not found while creating media package builder: " + e.getMessage(), e);
92      } catch (InstantiationException e) {
93        throw new ConfigurationException("Instantiation exception while creating media package builder: "
94                + e.getMessage(), e);
95      } catch (IllegalAccessException e) {
96        throw new ConfigurationException("Access exception while creating media package builder: " + e.getMessage(), e);
97      }
98      return builder;
99    }
100 
101 }