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