View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.component.image.utils;
8   
9   import java.awt.Toolkit;
10  
11  import com.octo.captcha.CaptchaException;
12  
13  /***
14   * <p>Description: This Factory is used in order to switch from the java.awt.Toolkit component to other implementation
15   * like <a href="http://www.eteks.com/pja/en/">PJA Toolkit</a>. By default this factory return the java.awt.Toolkit
16   * object. But if the the parameter toolkit.implementation is fixed as a parameter of the virtual machine with the value
17   * of the class name of another implementation of Toolkit, this factory return an implementation of this class. For
18   * exemple if you set to your virtual machine -Dtoolkit.implementation=com.eteks.awt.PJAToolkit, the factory returns an
19   * implementation of com.eteks.awt.PJAToolkit </p>
20   * <p/>
21   * see http://www.geocities.com/marcoschmidt.geo/java-image-faq.html#x for more info.
22   *
23   * @author <a href="mailto:mga@octo.com">Mathieu Gandin</a>
24   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
25   * @author <a href="antoine.veret@gmail.com">Antoine Veret</a>
26   * @version 1.0
27   */
28  public class ToolkitFactory {
29  
30      public static String TOOLKIT_IMPL = "toolkit.implementation";
31  
32      public static Toolkit getToolkit() {
33          Toolkit defaultToolkit = null;
34  
35          try {
36              String tempToolkitClass = System.getProperty(TOOLKIT_IMPL);
37              if (tempToolkitClass != null) {
38                  defaultToolkit = (Toolkit)
39                          Class.forName(tempToolkitClass).newInstance();
40              } else {
41                  defaultToolkit = getDefaultToolkit();
42              }
43  
44          } catch (Throwable e) {
45              throw new CaptchaException("toolkit has not been abble to be "
46                      + "initialized", e);
47  
48          }
49  
50          return defaultToolkit;
51  
52      }
53  
54      private static Toolkit getDefaultToolkit() {
55          Toolkit defaultToolkit;
56          defaultToolkit = Toolkit.getDefaultToolkit();
57          return defaultToolkit;
58      }
59  
60  }