1/*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.accessibility;
27
28/**
29 * Service Provider Interface (SPI) for Assistive Technology.
30 * <p>
31 * This service provider class provides mappings from the platform specific
32 * accessibility APIs to the Java Accessibility API.
33 * <p>
34 * Each service provider implementation is named and can be activated via the
35 * {@link #activate} method. Service providers can be loaded when the default
36 * {@link java.awt.Toolkit toolkit} is initialized.
37 *
38 * @apiNote There will typically be one provider per platform, such as Windows
39 *          or Linux, to support accessibility for screen readers and
40 *          magnifiers. However, more than one service provider can be
41 *          activated. For example, a test tool which provides visual results
42 *          obtained by interrogating the Java Accessibility API can be
43 *          activated along with the activation of the support for screen
44 *          readers and screen magnifiers.
45 * @see java.awt.Toolkit#getDefaultToolkit
46 * @see java.util.ServiceLoader
47 * @since 9
48 */
49public abstract class AccessibilityProvider {
50
51    /**
52     * Initializes a new accessibility provider.
53     *
54     * @throws SecurityException If a security manager has been installed and it
55     *         denies {@link RuntimePermission} {@code "accessibilityProvider"}
56     */
57    protected AccessibilityProvider() {
58        // Use a permission check when calling a private constructor to check
59        // that the proper security permission has been granted before the
60        // {@code Object} superclass is called. If an exception is thrown before
61        // the {@code Object} superclass is constructed a finalizer in a
62        // subclass of this class will not be run. This protects against a
63        // finalizer vulnerability.
64        this(checkPermission());
65    }
66
67    /**
68     * Allows to check a permission before the {@code Object} is called.
69     *
70     * @param  ignore unused stub to call a {@link #checkPermission()}}
71     */
72    private AccessibilityProvider(Void ignore) { }
73
74    /**
75     * If this code is running with a security manager and if the permission
76     * {@code "accessibilityProvider"} has not been granted
77     * {@code SecurityException} will be thrown.
78     *
79     * @return {@code null} if {@code SecurityException} was not thrown
80     * @throws SecurityException If a security manager has been installed and it
81     *         denies {@link RuntimePermission} {@code "accessibilityProvider"}
82     */
83    private static Void checkPermission() {
84        SecurityManager sm = System.getSecurityManager();
85        if (sm != null)
86            sm.checkPermission(new RuntimePermission("accessibilityProvider"));
87        return null;
88    }
89
90    /**
91     * Returns the name of this service provider. This name is used to locate a
92     * requested service provider.
93     *
94     * @return the name of this service provider
95     */
96    public abstract String getName();
97
98    /**
99     * Activates the support provided by this service provider.
100     */
101    public abstract void activate();
102}
103